@@ -102,10 +102,7 @@ const builtinType = (value: unknown): BuiltinType => {
102102 }
103103}
104104
105- const INTERNAL_SLOT_STATIC_NAMES : [ BuiltinType , string ] [ ] = [
106- [ `Array` , `isArray` ] ,
107- [ `Buffer` , `isBuffer` ] ,
108- ]
105+ const INTERNAL_SLOT_PREDICATE_NAMES : BuiltinType [ ] = [ `Array` , `Buffer` ]
109106const INTERNAL_SLOT_PROTOTYPE_NAMES : [ BuiltinType , string , any [ ] ?] [ ] = [
110107 [ `Boolean` , `valueOf` ] ,
111108 [ `Number` , `valueOf` ] ,
@@ -135,7 +132,7 @@ const INTERNAL_SLOT_PROTOTYPE_NAMES: [BuiltinType, string, any[]?][] = [
135132 [ `Temporal.PlainMonthDay` , `monthCode` ] ,
136133 [ `Temporal.Duration` , `sign` ] ,
137134]
138- const BUILTIN_ERROR_SUBCLASS_NAMES = new Set < string > ( [
135+ const BUILTIN_ERROR_SUBCLASS_NAMES = new Set < string | undefined > ( [
139136 `EvalError` ,
140137 `RangeError` ,
141138 `ReferenceError` ,
@@ -145,18 +142,16 @@ const BUILTIN_ERROR_SUBCLASS_NAMES = new Set<string>([
145142 `AggregateError` ,
146143] )
147144
148- type BuiltinTypeFunction = ( value : object ) => BuiltinType | undefined
145+ type BuiltinTypeFunction = ( value : object ) => BuiltinType | `` | undefined
149146
150147const TYPE_TAG_FUNCTIONS : BuiltinTypeFunction [ ] = [
151- ...INTERNAL_SLOT_STATIC_NAMES . flatMap < BuiltinTypeFunction > ( ( [ type , name ] ) => {
152- const Class = globalThis [ type as keyof typeof globalThis ] as
153- | Record < string , ( value : unknown ) => boolean >
154- | undefined
155- if ( ! Class ) {
156- return [ ]
157- }
158- const func = Class [ name ] !
159- return value => ( func ( value ) ? type : undefined )
148+ ...INTERNAL_SLOT_PREDICATE_NAMES . flatMap < BuiltinTypeFunction > ( type => {
149+ const func = (
150+ globalThis [ type as keyof typeof globalThis ] as
151+ | Record < string , ( value : unknown ) => boolean >
152+ | undefined
153+ ) ?. [ `is${ type } ` ]
154+ return func ? value => ( func ( value ) ? type : `` ) : [ ]
160155 } ) ,
161156 ...INTERNAL_SLOT_PROTOTYPE_NAMES . flatMap < BuiltinTypeFunction > (
162157 ( [ type , name , args = [ ] ] ) => {
@@ -173,13 +168,14 @@ const TYPE_TAG_FUNCTIONS: BuiltinTypeFunction[] = [
173168 return [ ]
174169 }
175170 let descriptor : PropertyDescriptor | undefined
176- do {
177- descriptor = Object . getOwnPropertyDescriptor ( prototype , name )
178- if ( descriptor ) {
179- break
180- }
171+ for (
172+ ;
173+ prototype &&
174+ ! ( descriptor = Object . getOwnPropertyDescriptor ( prototype , name ) ) ;
181175 prototype = Object . getPrototypeOf ( prototype ) as object | null
182- } while ( prototype )
176+ ) {
177+ //
178+ }
183179 if ( ! descriptor ) {
184180 return [ ]
185181 }
@@ -192,9 +188,9 @@ const TYPE_TAG_FUNCTIONS: BuiltinTypeFunction[] = [
192188 // Each function above will do one of the following when applied to a
193189 // value not of the proper type:
194190 // 1. Throw
195- // 2. Return `null` or `undefined
191+ // 2. Return `null` or `undefined`
196192 try {
197- return func . apply ( value , args ) == null ? undefined : type
193+ return func . apply ( value , args ) == null ? `` : type
198194 } catch {
199195 return undefined
200196 }
@@ -203,7 +199,7 @@ const TYPE_TAG_FUNCTIONS: BuiltinTypeFunction[] = [
203199 ) ,
204200]
205201const typedArrayToStringTag = Object . getOwnPropertyDescriptor (
206- Object . getPrototypeOf ( Uint8Array . prototype ) ,
202+ Object . getPrototypeOf ( Int8Array . prototype ) ,
207203 Symbol . toStringTag ,
208204) ?. get
209205if ( typedArrayToStringTag ) {
@@ -212,26 +208,21 @@ if (typedArrayToStringTag) {
212208 )
213209}
214210TYPE_TAG_FUNCTIONS . push ( value => {
215- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
216- if ( ! ( Error . isError ?.( value ) ?? toStringType ( value ) == `Error` ) ) {
217- return undefined
211+ const type =
212+ ! ( Symbol . toStringTag in value ) &&
213+ Object . prototype . toString . call ( value ) . slice ( 8 , - 1 )
214+ if ( type == `Arguments` ) {
215+ return type
218216 }
219- const prototype = Object . getPrototypeOf ( value ) as { name ?: string } | null
220- if ( prototype === null ) {
221- return `Error`
217+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
218+ if ( Error . isError ?.( value ) ?? type == `Error` ) {
219+ const name = ( Object . getPrototypeOf ( value ) as { name ?: string } | null )
220+ ?. name
221+ return BUILTIN_ERROR_SUBCLASS_NAMES . has ( name )
222+ ? ( name as BuiltinType )
223+ : `Error`
222224 }
223- return BUILTIN_ERROR_SUBCLASS_NAMES . has ( prototype . name ! )
224- ? ( prototype . name as BuiltinType )
225- : `Error`
225+ return undefined
226226} )
227- TYPE_TAG_FUNCTIONS . push ( value => {
228- const type = toStringType ( value )
229- return type == `Arguments` ? type : undefined
230- } )
231-
232- const toStringType = ( value : object ) =>
233- Object . hasOwn ( value , Symbol . toStringTag )
234- ? ``
235- : Object . prototype . toString . call ( value ) . slice ( 8 , - 1 )
236227
237228export default builtinType
0 commit comments