Skip to content

Commit

Permalink
feat(utils): isTypedArray
Browse files Browse the repository at this point in the history
Signed-off-by: Lexus Drumgold <[email protected]>
  • Loading branch information
unicornware committed Jul 20, 2023
1 parent 07d08b2 commit 349e81e
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/utils/__tests__/is-typed-array.spec-d.ts
Original file line number Diff line number Diff line change
@@ -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<typeof testSubject>().guards.toEqualTypeOf<TypedArray>()
})
})
32 changes: 32 additions & 0 deletions src/utils/__tests__/is-typed-array.spec.ts
Original file line number Diff line number Diff line change
@@ -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<typeof testSubject>[] = [
[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)
})
})
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
45 changes: 45 additions & 0 deletions src/utils/is-typed-array.ts
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 349e81e

Please sign in to comment.