Skip to content

Commit e05d535

Browse files
committed
[Refactor] use get-proto, improve types
1 parent 490791a commit e05d535

File tree

5 files changed

+50
-22
lines changed

5 files changed

+50
-22
lines changed

.eslintrc

+1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55

66
"rules": {
77
"max-statements-per-line": [2, { "max": 2 }],
8+
"no-extra-parens": 0,
89
},
910
}

index.js

+21-20
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,15 @@ var availableTypedArrays = require('available-typed-arrays');
55
var callBind = require('call-bind');
66
var callBound = require('call-bound');
77
var gOPD = require('gopd');
8+
var getProto = require('get-proto');
89

9-
/** @type {(O: object) => string} */
1010
var $toString = callBound('Object.prototype.toString');
1111
var hasToStringTag = require('has-tostringtag/shams')();
1212

1313
var g = typeof globalThis === 'undefined' ? global : globalThis;
1414
var typedArrays = availableTypedArrays();
1515

1616
var $slice = callBound('String.prototype.slice');
17-
var getPrototypeOf = Object.getPrototypeOf; // require('getprototypeof');
1817

1918
/** @type {<T = unknown>(array: readonly T[], value: unknown) => number} */
2019
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
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} */
3130
var 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) {
6063
var 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) {
8183
var 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
}

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
"call-bind": "^1.0.8",
6565
"call-bound": "^1.0.4",
6666
"for-each": "^0.3.5",
67+
"get-proto": "^1.0.1",
6768
"gopd": "^1.2.0",
6869
"has-tostringtag": "^1.0.2"
6970
},
@@ -122,7 +123,8 @@
122123
},
123124
"publishConfig": {
124125
"ignore": [
125-
".github/workflows"
126+
".github/workflows",
127+
"types.d.ts"
126128
]
127129
}
128130
}

tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"extends": "@ljharb/tsconfig",
33
"compilerOptions": {
4-
"target": "ES2021",
4+
"target": "ESNext",
55
},
66
"exclude": [
77
"coverage"

types.d.ts

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import type { TypedArray, TypedArrayName } from '.';
2+
3+
declare function ToStringTagGetter(receiver: TypedArray): TypedArrayName;
4+
declare function ToStringTagGetter(receiver: unknown): undefined;
5+
6+
declare function BoundSlice(receier: TypedArray, ...args: Parameters<TypedArray['slice']>): ReturnType<TypedArray['slice']>;
7+
8+
declare function BoundSet(receier: TypedArray, ...args: Parameters<TypedArray['set']>): ReturnType<TypedArray['set']>;
9+
10+
type Getter = typeof ToStringTagGetter | typeof BoundSlice | typeof BoundSet;
11+
12+
type Cache = {
13+
[k in `\$${TypedArrayName}`]?: Getter;
14+
} & {
15+
__proto__: null;
16+
};
17+
18+
export type {
19+
ToStringTagGetter,
20+
BoundSlice,
21+
BoundSet,
22+
Getter,
23+
Cache,
24+
};

0 commit comments

Comments
 (0)