Skip to content

Commit

Permalink
feat(api): add typedArrayType() classifier
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Feb 1, 2021
1 parent 4ea77ef commit 5c81fd8
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions packages/api/src/api/typedarray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,20 @@ export function typedArray<T extends Type | GLType>(type: T, ...xs: any[]) {
return new (<any>TYPEDARRAY_CTORS[type])(...xs);
}

/**
* Takes an {@link NumericArray} and returns its corresponding {@link Type} ID.
* Standard JS arrays will default to {@link Type.F64}.
*
* @param x
*/
export const typedArrayType = (x: NumericArray) => {
if (Array.isArray(x)) return Type.F64;
for (let id in TYPEDARRAY_CTORS) {
if (x instanceof (<any>TYPEDARRAY_CTORS)[id]) return <Type>Number(id);
}
return Type.F64;
};

/**
* Returns the smallest possible *unsigned* int type enum for given `x`.
* E.g. if `x <= 256`, the function returns `Type.U8`.
Expand Down

0 comments on commit 5c81fd8

Please sign in to comment.