diff --git a/src/function.ts b/src/function.ts index bb06afe..4ad9e9a 100644 --- a/src/function.ts +++ b/src/function.ts @@ -2,9 +2,16 @@ import { truncate } from './helpers.js' import type { Options } from './types.js' export default function inspectFunction(func: Function, options: Options) { + let functionType = 'Function' + // @ts-ignore + const stringTag = func[Symbol.toStringTag]; + if (typeof stringTag === 'string') { + functionType = stringTag; + } + const name = func.name if (!name) { - return options.stylize('[Function]', 'special') + return options.stylize(`[${functionType}]`, 'special') } - return options.stylize(`[Function ${truncate(name, options.truncate - 11)}]`, 'special') + return options.stylize(`[${functionType} ${truncate(name, options.truncate - 11)}]`, 'special') } diff --git a/src/index.ts b/src/index.ts index 4b0c644..e5656cb 100644 --- a/src/index.ts +++ b/src/index.ts @@ -129,7 +129,7 @@ export function inspect(value: unknown, opts: Partial = {}): string { if (type === 'object') { type = toString.call(value).slice(8, -1) } - + // If it is a base value that we already support, then use Loupe's inspector if (type in baseTypesMap) { return (baseTypesMap[type as keyof typeof baseTypesMap] as Inspect)(value, options) diff --git a/test/functions.js b/test/functions.js index 9281ead..9108fdc 100644 --- a/test/functions.js +++ b/test/functions.js @@ -65,3 +65,33 @@ describe('functions', () => { }) }) }) + +describe('async functions', () => { + it('returns the functions name wrapped in `[AsyncFunction ]`', () => { + expect(inspect(async function foo() {})).to.equal('[AsyncFunction foo]') + }) + + it('returns the `[AsyncFunction]` if given anonymous function', () => { + expect(inspect(async function () {})).to.equal('[AsyncFunction]') + }) +}) + +describe('generator functions', () => { + it('returns the functions name wrapped in `[GeneratorFunction ]`', () => { + expect(inspect(function* foo() {})).to.equal('[GeneratorFunction foo]') + }) + + it('returns the `[GeneratorFunction]` if given a generator function', () => { + expect(inspect(function* () {})).to.equal('[GeneratorFunction]') + }) +}) + +describe('async generator functions', () => { + it('returns the functions name wrapped in `[AsyncGeneratorFunction ]`', () => { + expect(inspect(async function* foo() {})).to.equal('[AsyncGeneratorFunction foo]') + }) + + it('returns the `[AsyncGeneratorFunction]` if given a async generator function', () => { + expect(inspect(async function* () {})).to.equal('[AsyncGeneratorFunction]') + }) +})