diff --git a/src/utils/__tests__/is-typed-array.spec-d.ts b/src/utils/__tests__/is-typed-array.spec-d.ts new file mode 100644 index 00000000..91054f41 --- /dev/null +++ b/src/utils/__tests__/is-typed-array.spec-d.ts @@ -0,0 +1,13 @@ +/** + * @file Type Tests - isTypedArray + * @module tutils/utils/tests/unit-d/isTypedArray + */ + +import type { TypedArray } from '#src/types' +import type testSubject from '../is-typed-array' + +describe('unit-d:utils/isTypedArray', () => { + it('should guard TypedArray', () => { + expectTypeOf().guards.toEqualTypeOf() + }) +}) diff --git a/src/utils/__tests__/is-typed-array.spec.ts b/src/utils/__tests__/is-typed-array.spec.ts new file mode 100644 index 00000000..4d56ccb0 --- /dev/null +++ b/src/utils/__tests__/is-typed-array.spec.ts @@ -0,0 +1,32 @@ +/** + * @file Unit Tests - isTypedArray + * @module tutils/utils/tests/unit/isTypedArray + */ + +import testSubject from '../is-typed-array' + +describe('unit:utils/isTypedArray', () => { + it('should return false if value is not TypedArray instance', () => { + expect(testSubject(new ArrayBuffer(8))).to.be.false + }) + + it('should return true if value is TypedArray instance', () => { + // Arrange + const cases: Parameters[] = [ + [new BigInt64Array()], + [new BigUint64Array()], + [new Float32Array()], + [new Float64Array()], + [new Int8Array()], + [new Int16Array()], + [new Int32Array()], + [new Uint8Array()], + [new Uint8ClampedArray()], + [new Uint16Array()], + [new Uint32Array()] + ] + + // Act + Expect + cases.forEach(([value]) => expect(testSubject(value)).to.be.true) + }) +}) diff --git a/src/utils/index.ts b/src/utils/index.ts index 8b5eb1f2..86ab4c46 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -69,6 +69,7 @@ export { default as isSet } from './is-set' export { default as isString } from './is-string' export { default as isSymbol } from './is-symbol' export { default as isTrue } from './is-true' +export { default as isTypedArray } from './is-typed-array' export { default as isUint16Array } from './is-uint16-array' export { default as isUint32Array } from './is-uint32-array' export { default as isUint8Array } from './is-uint8-array' diff --git a/src/utils/is-typed-array.ts b/src/utils/is-typed-array.ts new file mode 100644 index 00000000..fbbc65f6 --- /dev/null +++ b/src/utils/is-typed-array.ts @@ -0,0 +1,45 @@ +/** + * @file Utilities - isTypedArray + * @module tutils/utils/isTypedArray + */ + +import type { TypedArray } from '#src/types' +import isBigInt64Array from './is-big-int64-array' +import isBigUint64Array from './is-big-uint64-array' +import isFloat32Array from './is-float32-array' +import isFloat64Array from './is-float64-array' +import isInt16Array from './is-int16-array' +import isInt32Array from './is-int32-array' +import isInt8Array from './is-int8-array' +import isUint16Array from './is-uint16-array' +import isUint32Array from './is-uint32-array' +import isUint8Array from './is-uint8-array' +import isUint8ClampedArray from './is-uint8-clamped-array' + +/** + * Checks if `value` is a {@linkcode TypedArray} instance. + * + * @see https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/TypedArray + * + * @todo examples + * + * @param {unknown} value - Value to check + * @return {value is TypedArray} `true` if `value` is `TypedArray` instance + */ +const isTypedArray = (value: unknown): value is TypedArray => { + return ( + isBigInt64Array(value) || + isBigUint64Array(value) || + isFloat32Array(value) || + isFloat64Array(value) || + isInt8Array(value) || + isInt16Array(value) || + isInt32Array(value) || + isUint8Array(value) || + isUint8ClampedArray(value) || + isUint16Array(value) || + isUint32Array(value) + ) +} + +export default isTypedArray