Skip to content

Commit 9b8a6de

Browse files
authored
Add support for different types of functions (#77)
add support for different types of functions
1 parent e732123 commit 9b8a6de

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

src/function.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import { truncate } from './helpers.js'
22
import type { Options } from './types.js'
33

4-
export default function inspectFunction(func: Function, options: Options) {
4+
type ToStringable = Function & {[Symbol.toStringTag]: string};
5+
6+
export default function inspectFunction(func: ToStringable, options: Options) {
7+
const functionType = func[Symbol.toStringTag] || 'Function'
8+
59
const name = func.name
610
if (!name) {
7-
return options.stylize('[Function]', 'special')
11+
return options.stylize(`[${functionType}]`, 'special')
812
}
9-
return options.stylize(`[Function ${truncate(name, options.truncate - 11)}]`, 'special')
13+
return options.stylize(`[${functionType} ${truncate(name, options.truncate - 11)}]`, 'special')
1014
}

test/functions.js

+30
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,33 @@ describe('functions', () => {
6565
})
6666
})
6767
})
68+
69+
describe('async functions', () => {
70+
it('returns the functions name wrapped in `[AsyncFunction ]`', () => {
71+
expect(inspect(async function foo() {})).to.equal('[AsyncFunction foo]')
72+
})
73+
74+
it('returns the `[AsyncFunction]` if given anonymous function', () => {
75+
expect(inspect(async function () {})).to.equal('[AsyncFunction]')
76+
})
77+
})
78+
79+
describe('generator functions', () => {
80+
it('returns the functions name wrapped in `[GeneratorFunction ]`', () => {
81+
expect(inspect(function* foo() {})).to.equal('[GeneratorFunction foo]')
82+
})
83+
84+
it('returns the `[GeneratorFunction]` if given a generator function', () => {
85+
expect(inspect(function* () {})).to.equal('[GeneratorFunction]')
86+
})
87+
})
88+
89+
describe('async generator functions', () => {
90+
it('returns the functions name wrapped in `[AsyncGeneratorFunction ]`', () => {
91+
expect(inspect(async function* foo() {})).to.equal('[AsyncGeneratorFunction foo]')
92+
})
93+
94+
it('returns the `[AsyncGeneratorFunction]` if given a async generator function', () => {
95+
expect(inspect(async function* () {})).to.equal('[AsyncGeneratorFunction]')
96+
})
97+
})

0 commit comments

Comments
 (0)