Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for different types of functions #77

Merged
merged 2 commits into from
Jan 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]')
})
})
Loading