Skip to content

Commit

Permalink
Add support for different types of functions (#77)
Browse files Browse the repository at this point in the history
add support for different types of functions
  • Loading branch information
koddsson authored Jan 6, 2024
1 parent e732123 commit 9b8a6de
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/function.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { truncate } from './helpers.js'
import type { Options } from './types.js'

export default function inspectFunction(func: Function, options: Options) {
type ToStringable = Function & {[Symbol.toStringTag]: string};

export default function inspectFunction(func: ToStringable, options: Options) {
const functionType = func[Symbol.toStringTag] || 'Function'

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')
}
30 changes: 30 additions & 0 deletions test/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]')
})
})

0 comments on commit 9b8a6de

Please sign in to comment.