@@ -5,16 +5,15 @@ var availableTypedArrays = require('available-typed-arrays');
55var callBind = require ( 'call-bind' ) ;
66var callBound = require ( 'call-bound' ) ;
77var gOPD = require ( 'gopd' ) ;
8+ var getProto = require ( 'get-proto' ) ;
89
9- /** @type {(O: object) => string } */
1010var $toString = callBound ( 'Object.prototype.toString' ) ;
1111var hasToStringTag = require ( 'has-tostringtag/shams' ) ( ) ;
1212
1313var g = typeof globalThis === 'undefined' ? global : globalThis ;
1414var typedArrays = availableTypedArrays ( ) ;
1515
1616var $slice = callBound ( 'String.prototype.slice' ) ;
17- var getPrototypeOf = Object . getPrototypeOf ; // require('getprototypeof');
1817
1918/** @type {<T = unknown>(array: readonly T[], value: unknown) => number } */
2019var $indexOf = callBound ( 'Array.prototype.indexOf' , true ) || function indexOf ( array , value ) {
@@ -26,18 +25,18 @@ var $indexOf = callBound('Array.prototype.indexOf', true) || function indexOf(ar
2625 return - 1 ;
2726} ;
2827
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 } */
3130var cache = { __proto__ : null } ;
32- if ( hasToStringTag && gOPD && getPrototypeOf ) {
31+ if ( hasToStringTag && gOPD && getProto ) {
3332 forEach ( typedArrays , function ( typedArray ) {
3433 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 ) ;
3736 // @ts -expect-error TS won't narrow inside a closure
3837 var descriptor = gOPD ( proto , Symbol . toStringTag ) ;
39- if ( ! descriptor ) {
40- var superProto = getPrototypeOf ( proto ) ;
38+ if ( ! descriptor && proto ) {
39+ var superProto = getProto ( proto ) ;
4140 // @ts -expect-error TS won't narrow inside a closure
4241 descriptor = gOPD ( superProto , Symbol . toStringTag ) ;
4342 }
@@ -50,8 +49,12 @@ if (hasToStringTag && gOPD && getPrototypeOf) {
5049 var arr = new g [ typedArray ] ( ) ;
5150 var fn = arr . slice || arr . set ;
5251 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+ ) ;
5558 }
5659 } ) ;
5760}
@@ -60,15 +63,14 @@ if (hasToStringTag && gOPD && getPrototypeOf) {
6063var tryTypedArrays = function tryAllTypedArrays ( value ) {
6164 /** @type {ReturnType<typeof tryAllTypedArrays> } */ var found = false ;
6265 forEach (
63- // eslint-disable-next-line no-extra-parens
64- /** @type {Record<`\$${TypedArrayName}`, Getter> } */ /** @type {any } */ ( cache ) ,
66+ /** @type {Record<`\$${import('.').TypedArrayName}`, Getter> } */ ( cache ) ,
6567 /** @type {(getter: Getter, name: `\$${import('.').TypedArrayName}`) => void } */
6668 function ( getter , typedArray ) {
6769 if ( ! found ) {
6870 try {
69- // @ts -expect-error TODO: fix
71+ // @ts -expect-error a throw is fine here
7072 if ( '$' + getter ( value ) === typedArray ) {
71- found = $slice ( typedArray , 1 ) ;
73+ found = /** @type { import('.').TypedArrayName } */ ( $slice ( typedArray , 1 ) ) ;
7274 }
7375 } catch ( e ) { /**/ }
7476 }
@@ -81,14 +83,13 @@ var tryTypedArrays = function tryAllTypedArrays(value) {
8183var trySlices = function tryAllSlices ( value ) {
8284 /** @type {ReturnType<typeof tryAllSlices> } */ var found = false ;
8385 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 ) {
8788 if ( ! found ) {
8889 try {
89- // @ts -expect-error TODO: fix
90+ // @ts -expect-error a throw is fine here
9091 getter ( value ) ;
91- found = $slice ( name , 1 ) ;
92+ found = /** @type { import('.').TypedArrayName } */ ( $slice ( name , 1 ) ) ;
9293 } catch ( e ) { /**/ }
9394 }
9495 }
0 commit comments