@@ -5,16 +5,15 @@ var availableTypedArrays = require('available-typed-arrays');
5
5
var callBind = require ( 'call-bind' ) ;
6
6
var callBound = require ( 'call-bound' ) ;
7
7
var gOPD = require ( 'gopd' ) ;
8
+ var getProto = require ( 'get-proto' ) ;
8
9
9
- /** @type {(O: object) => string } */
10
10
var $toString = callBound ( 'Object.prototype.toString' ) ;
11
11
var hasToStringTag = require ( 'has-tostringtag/shams' ) ( ) ;
12
12
13
13
var g = typeof globalThis === 'undefined' ? global : globalThis ;
14
14
var typedArrays = availableTypedArrays ( ) ;
15
15
16
16
var $slice = callBound ( 'String.prototype.slice' ) ;
17
- var getPrototypeOf = Object . getPrototypeOf ; // require('getprototypeof');
18
17
19
18
/** @type {<T = unknown>(array: readonly T[], value: unknown) => number } */
20
19
var $indexOf = callBound ( 'Array.prototype.indexOf' , true ) || function indexOf ( array , value ) {
@@ -26,18 +25,18 @@ var $indexOf = callBound('Array.prototype.indexOf', true) || function indexOf(ar
26
25
return - 1 ;
27
26
} ;
28
27
29
- /** @typedef {(receiver: import('.').TypedArray) => string | typeof Uint8Array.prototype.slice.call | typeof Uint8Array.prototype.set.call } Getter */
30
- /** @type {{ [k in `\$${ import('.').TypedArrayName}`]?: Getter } & { __proto__: null } } */
28
+ /** @typedef {import('./types ').Getter } Getter */
29
+ /** @type {import('./types ').Cache } */
31
30
var cache = { __proto__ : null } ;
32
- if ( hasToStringTag && gOPD && getPrototypeOf ) {
31
+ if ( hasToStringTag && gOPD && getProto ) {
33
32
forEach ( typedArrays , function ( typedArray ) {
34
33
var arr = new g [ typedArray ] ( ) ;
35
- if ( Symbol . toStringTag in arr ) {
36
- var proto = getPrototypeOf ( arr ) ;
34
+ if ( Symbol . toStringTag in arr && getProto ) {
35
+ var proto = getProto ( arr ) ;
37
36
// @ts -expect-error TS won't narrow inside a closure
38
37
var descriptor = gOPD ( proto , Symbol . toStringTag ) ;
39
- if ( ! descriptor ) {
40
- var superProto = getPrototypeOf ( proto ) ;
38
+ if ( ! descriptor && proto ) {
39
+ var superProto = getProto ( proto ) ;
41
40
// @ts -expect-error TS won't narrow inside a closure
42
41
descriptor = gOPD ( superProto , Symbol . toStringTag ) ;
43
42
}
@@ -50,8 +49,12 @@ if (hasToStringTag && gOPD && getPrototypeOf) {
50
49
var arr = new g [ typedArray ] ( ) ;
51
50
var fn = arr . slice || arr . set ;
52
51
if ( fn ) {
53
- // @ts -expect-error TODO: fix
54
- cache [ '$' + typedArray ] = callBind ( fn ) ;
52
+ cache [
53
+ /** @type {`$${import('.').TypedArrayName}` } */ ( '$' + typedArray )
54
+ ] = /** @type {import('./types').BoundSlice | import('./types').BoundSet } */ (
55
+ // @ts -expect-error TODO FIXME
56
+ callBind ( fn )
57
+ ) ;
55
58
}
56
59
} ) ;
57
60
}
@@ -60,15 +63,14 @@ if (hasToStringTag && gOPD && getPrototypeOf) {
60
63
var tryTypedArrays = function tryAllTypedArrays ( value ) {
61
64
/** @type {ReturnType<typeof tryAllTypedArrays> } */ var found = false ;
62
65
forEach (
63
- // eslint-disable-next-line no-extra-parens
64
- /** @type {Record<`\$${TypedArrayName}`, Getter> } */ /** @type {any } */ ( cache ) ,
66
+ /** @type {Record<`\$${import('.').TypedArrayName}`, Getter> } */ ( cache ) ,
65
67
/** @type {(getter: Getter, name: `\$${import('.').TypedArrayName}`) => void } */
66
68
function ( getter , typedArray ) {
67
69
if ( ! found ) {
68
70
try {
69
- // @ts -expect-error TODO: fix
71
+ // @ts -expect-error a throw is fine here
70
72
if ( '$' + getter ( value ) === typedArray ) {
71
- found = $slice ( typedArray , 1 ) ;
73
+ found = /** @type { import('.').TypedArrayName } */ ( $slice ( typedArray , 1 ) ) ;
72
74
}
73
75
} catch ( e ) { /**/ }
74
76
}
@@ -81,14 +83,13 @@ var tryTypedArrays = function tryAllTypedArrays(value) {
81
83
var trySlices = function tryAllSlices ( value ) {
82
84
/** @type {ReturnType<typeof tryAllSlices> } */ var found = false ;
83
85
forEach (
84
- // eslint-disable-next-line no-extra-parens
85
- /** @type {Record<`\$${TypedArrayName}`, Getter> } */ /** @type {any } */ ( cache ) ,
86
- /** @type {(getter: typeof cache, name: `\$${import('.').TypedArrayName}`) => void } */ function ( getter , name ) {
86
+ /** @type {Record<`\$${import('.').TypedArrayName}`, Getter> } */ ( cache ) ,
87
+ /** @type {(getter: Getter, name: `\$${import('.').TypedArrayName}`) => void } */ function ( getter , name ) {
87
88
if ( ! found ) {
88
89
try {
89
- // @ts -expect-error TODO: fix
90
+ // @ts -expect-error a throw is fine here
90
91
getter ( value ) ;
91
- found = $slice ( name , 1 ) ;
92
+ found = /** @type { import('.').TypedArrayName } */ ( $slice ( name , 1 ) ) ;
92
93
} catch ( e ) { /**/ }
93
94
}
94
95
}
0 commit comments