File tree 2 files changed +37
-3
lines changed
2 files changed +37
-3
lines changed Original file line number Diff line number Diff line change 1
1
import { truncate } from './helpers.js'
2
2
import type { Options } from './types.js'
3
3
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
+
5
9
const name = func . name
6
10
if ( ! name ) {
7
- return options . stylize ( '[Function]' , 'special' )
11
+ return options . stylize ( `[ ${ functionType } ]` , 'special' )
8
12
}
9
- return options . stylize ( `[Function ${ truncate ( name , options . truncate - 11 ) } ]` , 'special' )
13
+ return options . stylize ( `[${ functionType } ${ truncate ( name , options . truncate - 11 ) } ]` , 'special' )
10
14
}
Original file line number Diff line number Diff line change @@ -65,3 +65,33 @@ describe('functions', () => {
65
65
} )
66
66
} )
67
67
} )
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
+ } )
You can’t perform that action at this time.
0 commit comments