which-typed-array
Advanced tools
+16
| type TypedArrayName = | ||
| | 'Int8Array' | ||
| | 'Uint8Array' | ||
| | 'Uint8ClampedArray' | ||
| | 'Int16Array' | ||
| | 'Uint16Array' | ||
| | 'Int32Array' | ||
| | 'Uint32Array' | ||
| | 'Float32Array' | ||
| | 'Float64Array' | ||
| | 'BigInt64Array' | ||
| | 'BigUint64Array'; | ||
| declare function whichTypedArray(value: unknown): TypedArrayName | false | null; | ||
| export = whichTypedArray; |
| { | ||
| "compilerOptions": { | ||
| /* Visit https://aka.ms/tsconfig to read more about this file */ | ||
| /* Projects */ | ||
| /* Language and Environment */ | ||
| "target": "ESNext", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */ | ||
| // "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */ | ||
| // "noLib": true, /* Disable including any library files, including the default lib.d.ts. */ | ||
| "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */ | ||
| // "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */ | ||
| /* Modules */ | ||
| "module": "commonjs", /* Specify what module code is generated. */ | ||
| // "rootDir": "./", /* Specify the root folder within your source files. */ | ||
| // "moduleResolution": "node10", /* Specify how TypeScript looks up a file from a given module specifier. */ | ||
| // "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */ | ||
| // "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */ | ||
| // "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */ | ||
| "typeRoots": ["types"], /* Specify multiple folders that act like './node_modules/@types'. */ | ||
| "resolveJsonModule": true, /* Enable importing .json files. */ | ||
| // "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */ | ||
| /* JavaScript Support */ | ||
| "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */ | ||
| "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */ | ||
| "maxNodeModuleJsDepth": 0, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */ | ||
| /* Emit */ | ||
| "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */ | ||
| "declarationMap": true, /* Create sourcemaps for d.ts files. */ | ||
| "noEmit": true, /* Disable emitting files from a compilation. */ | ||
| /* Interop Constraints */ | ||
| "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */ | ||
| "esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */ | ||
| "forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */ | ||
| /* Type Checking */ | ||
| "strict": true, /* Enable all strict type-checking options. */ | ||
| /* Completeness */ | ||
| //"skipLibCheck": true /* Skip type checking all .d.ts files. */ | ||
| }, | ||
| "exclude": [ | ||
| "coverage" | ||
| ] | ||
| } |
+9
-0
@@ -8,2 +8,11 @@ # Changelog | ||
| ## [v1.1.14](https://github.com/inspect-js/which-typed-array/compare/v1.1.13...v1.1.14) - 2024-02-01 | ||
| ### Commits | ||
| - [patch] add types [`49c4d4c`](https://github.com/inspect-js/which-typed-array/commit/49c4d4c5db9bebb8d6f8c18a01047e44eea15e17) | ||
| - [Dev Deps] update `aud`, `npmignore`, `tape` [`e5fab7b`](https://github.com/inspect-js/which-typed-array/commit/e5fab7b3dc9df2bceb88f15c3d0a2c0176cf2567) | ||
| - [Deps] update `available-typed-arrays`, `call-bind` [`97e2b44`](https://github.com/inspect-js/which-typed-array/commit/97e2b44bad85c9183f1219e28211b3abd167677c) | ||
| - [Deps] update `has-tostringtag` [`1efa8bf`](https://github.com/inspect-js/which-typed-array/commit/1efa8bf910c080c14f011aa7c645ac88bc7a7078) | ||
| ## [v1.1.13](https://github.com/inspect-js/which-typed-array/compare/v1.1.12...v1.1.13) - 2023-10-19 | ||
@@ -10,0 +19,0 @@ |
+39
-18
@@ -18,3 +18,3 @@ 'use strict'; | ||
| var $indexOf = callBound('Array.prototype.indexOf', true) || function indexOf(array, value) { | ||
| var $indexOf = callBound('Array.prototype.indexOf', true) || /** @type {(array: readonly unknown[], value: unknown) => keyof array} */ function indexOf(array, value) { | ||
| for (var i = 0; i < array.length; i += 1) { | ||
@@ -27,2 +27,6 @@ if (array[i] === value) { | ||
| }; | ||
| /** @typedef {Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array | BigInt64Array | BigUint64Array} TypedArray */ | ||
| /** @typedef {'Int8Array' | 'Uint8Array' | 'Uint8ClampedArray' | 'Int16Array' | 'Uint16Array' | 'Int32Array' | 'Uint32Array' | 'Float32Array' | 'Float64Array' | 'BigInt64Array' | 'BigUint64Array'} TypedArrayName */ | ||
| /** @type {{ [k in `\$${TypedArrayName}`]?: (receiver: TypedArray) => string | typeof Uint8Array.prototype.slice.call | typeof Uint8Array.prototype.set.call } & { __proto__: null }} */ | ||
| var cache = { __proto__: null }; | ||
@@ -34,7 +38,10 @@ if (hasToStringTag && gOPD && getPrototypeOf) { | ||
| var proto = getPrototypeOf(arr); | ||
| // @ts-expect-error TS won't narrow inside a closure | ||
| var descriptor = gOPD(proto, Symbol.toStringTag); | ||
| if (!descriptor) { | ||
| var superProto = getPrototypeOf(proto); | ||
| // @ts-expect-error TS won't narrow inside a closure | ||
| descriptor = gOPD(superProto, Symbol.toStringTag); | ||
| } | ||
| // @ts-expect-error TODO: fix | ||
| cache['$' + typedArray] = callBind(descriptor.get); | ||
@@ -48,2 +55,3 @@ } | ||
| if (fn) { | ||
| // @ts-expect-error TODO: fix | ||
| cache['$' + typedArray] = callBind(fn); | ||
@@ -54,29 +62,42 @@ } | ||
| /** @type {import('.')} */ | ||
| var tryTypedArrays = function tryAllTypedArrays(value) { | ||
| var found = false; | ||
| forEach(cache, function (getter, typedArray) { | ||
| if (!found) { | ||
| try { | ||
| if ('$' + getter(value) === typedArray) { | ||
| found = $slice(typedArray, 1); | ||
| } | ||
| } catch (e) { /**/ } | ||
| /** @type {ReturnType<tryAllTypedArrays>} */ var found = false; | ||
| forEach( | ||
| // eslint-disable-next-line no-extra-parens | ||
| /** @type {Record<`\$${TypedArrayName}`, typeof cache>} */ /** @type {any} */ (cache), | ||
| /** @type {(getter: typeof cache, name: `\$${TypedArrayName}`) => void} */ function (getter, typedArray) { | ||
| if (!found) { | ||
| try { | ||
| // @ts-expect-error TODO: fix | ||
| if ('$' + getter(value) === typedArray) { | ||
| found = $slice(typedArray, 1); | ||
| } | ||
| } catch (e) { /**/ } | ||
| } | ||
| } | ||
| }); | ||
| ); | ||
| return found; | ||
| }; | ||
| /** @type {import('.')} */ | ||
| var trySlices = function tryAllSlices(value) { | ||
| var found = false; | ||
| forEach(cache, function (getter, name) { | ||
| if (!found) { | ||
| try { | ||
| getter(value); | ||
| found = $slice(name, 1); | ||
| } catch (e) { /**/ } | ||
| /** @type {ReturnType<tryAllSlices>} */ var found = false; | ||
| forEach( | ||
| // eslint-disable-next-line no-extra-parens | ||
| /** @type {any} */ (cache), | ||
| /** @type {(getter: typeof cache, name: `\$${TypedArrayName}`) => void} */ function (getter, name) { | ||
| if (!found) { | ||
| try { | ||
| // @ts-expect-error TODO: fix | ||
| getter(value); | ||
| found = $slice(name, 1); | ||
| } catch (e) { /**/ } | ||
| } | ||
| } | ||
| }); | ||
| ); | ||
| return found; | ||
| }; | ||
| /** @type {import('.')} */ | ||
| module.exports = function whichTypedArray(value) { | ||
@@ -83,0 +104,0 @@ if (!value || typeof value !== 'object') { return false; } |
+18
-7
| { | ||
| "name": "which-typed-array", | ||
| "version": "1.1.13", | ||
| "version": "1.1.14", | ||
| "author": { | ||
@@ -22,2 +22,3 @@ "name": "Jordan Harband", | ||
| "main": "index.js", | ||
| "types": "./index.d.ts", | ||
| "sideEffects": false, | ||
@@ -34,2 +35,3 @@ "scripts": { | ||
| "lint": "eslint --ext=js,mjs .", | ||
| "postlint": "tsc -p .", | ||
| "version": "auto-changelog && git add CHANGELOG.md", | ||
@@ -63,11 +65,19 @@ "postversion": "auto-changelog && git add CHANGELOG.md && git commit --no-edit --amend && git tag -f \"v$(node -e \"console.log(require('./package.json').version)\")\"" | ||
| "dependencies": { | ||
| "available-typed-arrays": "^1.0.5", | ||
| "call-bind": "^1.0.4", | ||
| "available-typed-arrays": "^1.0.6", | ||
| "call-bind": "^1.0.5", | ||
| "for-each": "^0.3.3", | ||
| "gopd": "^1.0.1", | ||
| "has-tostringtag": "^1.0.0" | ||
| "has-tostringtag": "^1.0.1" | ||
| }, | ||
| "devDependencies": { | ||
| "@ljharb/eslint-config": "^21.1.0", | ||
| "aud": "^2.0.3", | ||
| "@types/call-bind": "^1.0.5", | ||
| "@types/for-each": "^0.3.3", | ||
| "@types/gopd": "^1.0.3", | ||
| "@types/is-callable": "^1.1.2", | ||
| "@types/make-arrow-function": "^1.2.2", | ||
| "@types/make-generator-function": "^2.0.3", | ||
| "@types/node": "^20.11.14", | ||
| "@types/tape": "^5.6.4", | ||
| "aud": "^2.0.4", | ||
| "auto-changelog": "^2.4.0", | ||
@@ -79,6 +89,7 @@ "eslint": "=8.8.0", | ||
| "make-generator-function": "^2.0.0", | ||
| "npmignore": "^0.3.0", | ||
| "npmignore": "^0.3.1", | ||
| "nyc": "^10.3.2", | ||
| "safe-publish-latest": "^2.0.0", | ||
| "tape": "^5.7.1" | ||
| "tape": "^5.7.4", | ||
| "typescript": "^5.4.0-dev.20240131" | ||
| }, | ||
@@ -85,0 +96,0 @@ "testling": { |
+9
-1
@@ -27,2 +27,3 @@ 'use strict'; | ||
| t.test('non-number/string primitives', function (st) { | ||
| // @ts-expect-error | ||
| st.equal(false, whichTypedArray(), 'undefined is not typed array'); | ||
@@ -78,5 +79,9 @@ st.equal(false, whichTypedArray(null), 'null is not typed array'); | ||
| forEach(typedArrayNames, function (typedArray) { | ||
| // @ts-expect-error TODO: fix | ||
| if (typeof global[typedArray] === 'function') { | ||
| // @ts-expect-error TODO: fix | ||
| var fakeTypedArray = []; | ||
| // @ts-expect-error TODO: fix | ||
| fakeTypedArray[Symbol.toStringTag] = typedArray; | ||
| // @ts-expect-error TODO: fix | ||
| t.equal(false, whichTypedArray(fakeTypedArray), 'faked ' + typedArray + ' is not typed array'); | ||
@@ -90,5 +95,8 @@ } else { | ||
| /** @typedef {Int8ArrayConstructor | Uint8ArrayConstructor | Uint8ClampedArrayConstructor | Int16ArrayConstructor | Uint16ArrayConstructor | Int32ArrayConstructor | Uint32ArrayConstructor | Float32ArrayConstructor | Float64ArrayConstructor | BigInt64ArrayConstructor | BigUint64ArrayConstructor} TypedArrayConstructor */ | ||
| test('Typed Arrays', function (t) { | ||
| forEach(typedArrayNames, function (typedArray) { | ||
| var TypedArray = global[typedArray]; | ||
| // @ts-expect-error TODO: fix | ||
| /** @type {TypedArrayConstructor} */ var TypedArray = global[typedArray]; | ||
| if (isCallable(TypedArray)) { | ||
@@ -95,0 +103,0 @@ var arr = new TypedArray(10); |
41681
20.21%12
20%253
47.95%21
75%Updated
Updated