( value: unknown, )
| 30 | * @returns Its native brand, or undefined for other values and failed inspections. |
| 31 | */ |
| 32 | export function getNativeObjectType( |
| 33 | value: unknown, |
| 34 | ): NativeObjectType | undefined { |
| 35 | if (value === null || typeof value !== 'object') return undefined |
| 36 | try { |
| 37 | if (Array.isArray(value) || ArrayBuffer.isView(value)) return undefined |
| 38 | const length = Object.getOwnPropertyDescriptor(value, 'length') |
| 39 | if ( |
| 40 | typeof length?.value === 'number' && |
| 41 | !length.enumerable && |
| 42 | !length.writable && |
| 43 | !length.configurable && |
| 44 | hasNativeBrand(value, 'String') |
| 45 | ) { |
| 46 | return 'String' |
| 47 | } |
| 48 | const parent = Object.getPrototypeOf(value) |
| 49 | if (parent === null) { |
| 50 | if (hasNativeBrand(value, 'Map')) return 'Map' |
| 51 | if (hasNativeBrand(value, 'Set')) return 'Set' |
| 52 | return undefined |
| 53 | } |
| 54 | if (parent === Object.prototype) return undefined |
| 55 | let prototype: object | null = value |
| 56 | for (let depth = 0; prototype && depth <= 500; depth++) { |
| 57 | const tag = Object.getOwnPropertyDescriptor( |
| 58 | prototype, |
| 59 | Symbol.toStringTag, |
| 60 | )?.value |
| 61 | if ((tag === 'Map' || tag === 'Set') && hasNativeBrand(value, tag)) { |
| 62 | return tag |
| 63 | } |
| 64 | prototype = Object.getPrototypeOf(prototype) |
| 65 | } |
| 66 | return undefined |
| 67 | } catch { |
| 68 | return undefined |
| 69 | } |
| 70 | } |
no test coverage detected