Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions __tests__/is-typed-array.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { isTypedArray } from "@extremejs/utils";

it("should check if value is a typed array", () => {
expect(isTypedArray(new Int8Array)).toBe(true);

expect(isTypedArray(new Uint8Array)).toBe(true);

expect(isTypedArray(new Uint8ClampedArray)).toBe(true);

expect(isTypedArray(new Int16Array)).toBe(true);

expect(isTypedArray(new Uint16Array)).toBe(true);

expect(isTypedArray(new Int32Array)).toBe(true);

expect(isTypedArray(new Uint32Array)).toBe(true);

expect(isTypedArray(new Float32Array)).toBe(true);

expect(isTypedArray(new Float64Array)).toBe(true);

expect(isTypedArray(new BigInt64Array)).toBe(true);

expect(isTypedArray(new BigUint64Array)).toBe(true);

expect(isTypedArray([])).toBe(false);
});
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export { default as isSet } from "./is-set.js";
export { default as isString } from "./is-string.js";
export { default as isSymbol } from "./is-symbol.js";
export { default as isTypeOf } from "./is-type-of.js";
export { default as isTypedArray } from "./is-typed-array.js";
export { default as isUndefined } from "./is-undefined.js";
export { default as isWeakMap } from "./is-weak-map.js";
export { default as isWeakSet } from "./is-weak-set.js";
Expand Down
36 changes: 36 additions & 0 deletions src/is-typed-array.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import isArrayLikeObject from "./is-array-like-object.js";
import objectToString from "./object-to-string.js";

const TYPED_ARRAYS = [
"[object Int8Array]",
"[object Uint8Array]",
"[object Uint8ClampedArray]",
"[object Int16Array]",
"[object Uint16Array]",
"[object Int32Array]",
"[object Uint32Array]",
"[object Float32Array]",
"[object Float64Array]",
"[object BigInt64Array]",
"[object BigUint64Array]",
];

/**
* Checks if `value` is classified as a typed array.
* @group Array
* @since 1.0.0
* @param value
* @example
* isTypedArray(new Uint8Array);
* // => true
* @example
* isTypedArray([]);
* // => false
*/
export default function isTypedArray<Value>(value: Value): Value extends TypedArrayT ? true : false {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return (isArrayLikeObject(value) && TYPED_ARRAYS.includes(objectToString(value))) as any;
}

export type TypedArrayT = BigInt64Array | BigUint64Array | Float32Array | Float64Array
| Int8Array | Int16Array | Int32Array | Uint8Array | Uint8ClampedArray | Uint16Array | Uint32Array;