Skip to content

Commit

Permalink
add types
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Feb 20, 2024
1 parent 805cee2 commit 9eecdd2
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 16 deletions.
17 changes: 17 additions & 0 deletions index.d.ts
@@ -0,0 +1,17 @@
type TypedArray =
| Int8Array
| Uint8Array
| Uint8ClampedArray
| Int16Array
| Uint16Array
| Int32Array
| Uint32Array
| Float32Array
| Float64Array
| BigInt64Array
| BigUint64Array;

declare function typedArrayByteOffset(value: TypedArray): number;
declare function typedArrayByteOffset(value: unknown): false;

export = typedArrayByteOffset;
16 changes: 14 additions & 2 deletions index.js
Expand Up @@ -5,22 +5,30 @@ var callBind = require('call-bind');

var typedArrays = require('available-typed-arrays')();

/** @typedef {Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array | BigInt64Array | BigUint64Array} TypedArray */
/** @typedef {(x: TypedArray) => number} ByteOffsetGetter */

/** @type {Object.<typeof typedArrays, ByteOffsetGetter>} */
var getters = {};
var hasProto = require('has-proto')();

var gOPD = require('gopd');
var oDP = Object.defineProperty;
if (gOPD) {
/** @type {ByteOffsetGetter} */
var getByteOffset = function (x) {
return x.byteOffset;
};
forEach(typedArrays, function (typedArray) {
// In Safari 7, Typed Array constructors are typeof object
if (typeof global[typedArray] === 'function' || typeof global[typedArray] === 'object') {
var Proto = global[typedArray].prototype;
// @ts-expect-error TS can't guarantee the callback is invoked sync
var descriptor = gOPD(Proto, 'byteOffset');
if (!descriptor && hasProto) {
// @ts-expect-error hush, TS, every object has a dunder proto
var superProto = Proto.__proto__; // eslint-disable-line no-proto
// @ts-expect-error TS can't guarantee the callback is invoked sync
descriptor = gOPD(superProto, 'byteOffset');
}
// Opera 12.16 has a magic byteOffset data property on instances AND on Proto
Expand All @@ -29,6 +37,7 @@ if (gOPD) {
} else if (oDP) {
// this is likely an engine where instances have a magic byteOffset data property
var arr = new global[typedArray](2);
// @ts-expect-error TS can't guarantee the callback is invoked sync
descriptor = gOPD(arr, 'byteOffset');
if (descriptor && descriptor.configurable) {
oDP(arr, 'length', { value: 3 });
Expand All @@ -41,9 +50,10 @@ if (gOPD) {
});
}

/** @type {ByteOffsetGetter} */
var tryTypedArrays = function tryAllTypedArrays(value) {
var foundOffset;
forEach(getters, function (getter) {
/** @type {number} */ var foundOffset;
forEach(getters, /** @type {(getter: ByteOffsetGetter) => void} */ function (getter) {
if (typeof foundOffset !== 'number') {
try {
var offset = getter(value);
Expand All @@ -53,11 +63,13 @@ var tryTypedArrays = function tryAllTypedArrays(value) {
} catch (e) {}
}
});
// @ts-expect-error TS can't guarantee the callback is invoked sync
return foundOffset;
};

var isTypedArray = require('is-typed-array');

/** @type {import('.')} */
module.exports = function typedArrayByteOffset(value) {
if (!isTypedArray(value)) {
return false;
Expand Down
13 changes: 12 additions & 1 deletion package.json
Expand Up @@ -7,6 +7,7 @@
".": "./index.js",
"./package.json": "./package.json"
},
"types": "./index.d.ts",
"sideEffects": false,
"scripts": {
"prepack": "npmignore --auto --commentLines=autogenerated",
Expand Down Expand Up @@ -64,6 +65,14 @@
},
"devDependencies": {
"@ljharb/eslint-config": "^21.1.0",
"@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/object-inspect": "^1.8.4",
"@types/tape": "^5.6.4",
"aud": "^2.0.4",
"auto-changelog": "^2.4.0",
"eslint": "=8.8.0",
Expand All @@ -75,8 +84,10 @@
"npmignore": "^0.3.1",
"nyc": "^10.3.2",
"object-inspect": "^1.13.1",
"possible-typed-array-names": "^1.0.0",
"safe-publish-latest": "^2.0.0",
"tape": "^5.7.5"
"tape": "^5.7.5",
"typescript": "^5.4.0-dev.20240219"
},
"engines": {
"node": ">= 0.4"
Expand Down
15 changes: 2 additions & 13 deletions test/index.js
Expand Up @@ -8,22 +8,11 @@ var arrowFn = require('make-arrow-function')();
var forEach = require('for-each');
var inspect = require('object-inspect');

var typedArrayNames = [
'Int8Array',
'Uint8Array',
'Uint8ClampedArray',
'Int16Array',
'Uint16Array',
'Int32Array',
'Uint32Array',
'Float32Array',
'Float64Array',
'BigInt64Array',
'BigUint64Array'
];
var typedArrayNames = require('possible-typed-array-names');

test('not arrays', function (t) {
t.test('non-number/string primitives', function (st) {
// @ts-expect-error
st.equal(false, typedArrayByteOffset(), 'undefined is not typed array');
st.equal(false, typedArrayByteOffset(null), 'null is not typed array');
st.equal(false, typedArrayByteOffset(false), 'false is not typed array');
Expand Down
49 changes: 49 additions & 0 deletions tsconfig.json
@@ -0,0 +1,49 @@
{
"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"
]
}

0 comments on commit 9eecdd2

Please sign in to comment.