Last active
August 23, 2021 01:03
-
-
Save petamoriken/6982e7469994a8880bcbef6198203042 to your computer and use it in GitHub Desktop.
Strict TypedArray Type Check inspired by util.types implementation of Node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @file Strict TypedArray Type Check inspired by Node.js util.types implementation | |
* @see https://github.com/nodejs/node/blob/v16.x/lib/internal/util/types.js | |
*/ | |
const TypedArrayPrototype = Object.getPrototypeOf(Uint8Array).prototype; | |
const getTypedArrayPrototypeSybolToStringTag = Object.getOwnPropertyDescriptor(TypedArrayPrototype, Symbol.toStringTag).get; | |
/** | |
* @param {unknown} value | |
* @returns {value is Uint8Array|Uint8ClampedArray|Uint16Array|Uint32Array|Int8Array|Int16Array|Int32Array|Float32Array|Float64Array|BigUint64Array|BigInt64Array} | |
*/ | |
export function isTypedArray(value) { | |
return getTypedArrayPrototypeSybolToStringTag.call(value) !== undefined; | |
} | |
/** | |
* @param {unknown} value | |
* @returns {value is Uint8Array} | |
*/ | |
export function isUint8Array(value) { | |
return getTypedArrayPrototypeSybolToStringTag.call(value) === 'Uint8Array'; | |
} | |
/** | |
* @param {unknown} value | |
* @returns {value is Uint8ClampedArray} | |
*/ | |
export function isUint8ClampedArray(value) { | |
return getTypedArrayPrototypeSybolToStringTag.call(value) === 'Uint8ClampedArray'; | |
} | |
/** | |
* @param {unknown} value | |
* @returns {value is Uint16Array} | |
*/ | |
export function isUint16Array(value) { | |
return getTypedArrayPrototypeSybolToStringTag.call(value) === 'Uint16Array'; | |
} | |
/** | |
* @param {unknown} value | |
* @returns {value is Uint32Array} | |
*/ | |
export function isUint32Array(value) { | |
return getTypedArrayPrototypeSybolToStringTag.call(value) === 'Uint32Array'; | |
} | |
/** | |
* @param {unknown} value | |
* @returns {value is Int8Array} | |
*/ | |
export function isInt8Array(value) { | |
return getTypedArrayPrototypeSybolToStringTag.call(value) === 'Int8Array'; | |
} | |
/** | |
* @param {unknown} value | |
* @returns {value is Int16Array} | |
*/ | |
export function isInt16Array(value) { | |
return getTypedArrayPrototypeSybolToStringTag.call(value) === 'Int16Array'; | |
} | |
/** | |
* @param {unknown} value | |
* @returns {value is Int32Array} | |
*/ | |
export function isInt32Array(value) { | |
return getTypedArrayPrototypeSybolToStringTag.call(value) === 'Int32Array'; | |
} | |
/** | |
* @param {unknown} value | |
* @returns {value is Float32Array} | |
*/ | |
export function isFloat32Array(value) { | |
return getTypedArrayPrototypeSybolToStringTag.call(value) === 'Float32Array'; | |
} | |
/** | |
* @param {unknown} value | |
* @returns {value is Float64Array} | |
*/ | |
export function isFloat64Array(value) { | |
return getTypedArrayPrototypeSybolToStringTag.call(value) === 'Float64Array'; | |
} | |
/** | |
* @param {unknown} value | |
* @returns {value is BigInt64Array} | |
*/ | |
export function isBigInt64Array(value) { | |
return getTypedArrayPrototypeSybolToStringTag.call(value) === 'BigInt64Array'; | |
} | |
/** | |
* @param {unknown} value | |
* @returns {value is BigUint64Array} | |
*/ | |
export function isBigUint64Array(value) { | |
return getTypedArrayPrototypeSybolToStringTag.call(value) === 'BigUint64Array'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment