-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(types): Add isTypedArray() and isNumericArray() (#314)
- Loading branch information
Showing
13 changed files
with
128 additions
and
8 deletions.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Overview | ||
|
||
Minimal set of math types, intended to be used with very low cost (bundle size impact) | ||
across other frameworks. | ||
|
||
## Installation | ||
|
||
```bash | ||
npm install @math.gl/types | ||
``` |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Array Types | ||
|
||
## Types | ||
|
||
### `TypedArray` | ||
|
||
Any javascript typed array | ||
|
||
### `NumericArray` | ||
|
||
Any javascript typed array, or any javascript array containing numbers | ||
|
||
## Utilities | ||
|
||
### `isTypedArray(value: unknown): TypedArray | null` | ||
|
||
Avoids type problems with the `ArrayBuffer.isView()` check. | ||
|
||
### `isNumericArray(value: unknown): TypedArray | null` | ||
|
||
Avoids type problems with the `ArrayBuffer.isView()` check. | ||
|
||
Note: only the type of the first element in a standard array is checked to be a `number`. |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export {TypedArray, NumberArray, NumericArray} from './number-array'; | ||
export type {TypedArray, NumericArray, NumberArray} from './array-types'; | ||
export {isTypedArray, isNumericArray} from './is-array'; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import {TypedArray, NumericArray} from './array-types'; | ||
|
||
/** | ||
* Check is an array is a typed array | ||
* @param value value to be tested | ||
* @returns input as TypedArray, or null | ||
*/ | ||
export function isTypedArray(value: unknown): TypedArray | null { | ||
return ArrayBuffer.isView(value) && !(value instanceof DataView) ? (value as TypedArray) : null; | ||
} | ||
|
||
/** | ||
* Check is an array is a numeric array (typed array or array of numbers) | ||
* @param value value to be tested | ||
* @returns input as NumericArray, or null | ||
*/ | ||
export function isNumericArray(value: unknown): NumericArray | null { | ||
if (Array.isArray(value)) { | ||
return value.length === 0 || typeof value[0] === 'number' ? (value as number[]) : null; | ||
} | ||
return isTypedArray(value); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// math.gl, MIT license | ||
|
||
import './is-array.spec'; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// math.gl, MIT license | ||
|
||
import test, {Test} from 'tape-promise/tape'; | ||
import {isTypedArray, isNumericArray} from '@math.gl/types'; | ||
|
||
const TEST_CASES: {value: unknown; isTypedArray: boolean; isNumericArray: boolean}[] = [ | ||
{value: new Float32Array(1), isTypedArray: true, isNumericArray: true}, | ||
{value: new Uint8Array(2), isTypedArray: true, isNumericArray: true}, | ||
{value: [], isTypedArray: false, isNumericArray: true}, | ||
{value: [100, 100], isTypedArray: false, isNumericArray: true}, | ||
{value: ['a'], isTypedArray: false, isNumericArray: false}, | ||
{value: new ArrayBuffer(4), isTypedArray: false, isNumericArray: false}, | ||
{value: new DataView(new ArrayBuffer(16)), isTypedArray: false, isNumericArray: false}, | ||
{value: undefined, isTypedArray: false, isNumericArray: false}, | ||
{value: null, isTypedArray: false, isNumericArray: false}, | ||
{value: {}, isTypedArray: false, isNumericArray: false}, | ||
{value: {length: 0}, isTypedArray: false, isNumericArray: false}, | ||
{value: 1, isTypedArray: false, isNumericArray: false}, | ||
{value: NaN, isTypedArray: false, isNumericArray: false}, | ||
{value: 'NaN', isTypedArray: false, isNumericArray: false}, | ||
{value: '', isTypedArray: false, isNumericArray: false} | ||
]; | ||
|
||
test('math.gl#isTypedArray', (t) => { | ||
for (const tc of TEST_CASES) { | ||
t.equal( | ||
Boolean(isTypedArray(tc.value)), | ||
tc.isTypedArray, | ||
`isTypedArray(${JSON.stringify(tc.value)}) => ${tc.isTypedArray}` | ||
); | ||
} | ||
t.end(); | ||
}); | ||
|
||
test('math.gl#isNumericArray', (t) => { | ||
for (const tc of TEST_CASES) { | ||
t.equal( | ||
Boolean(isNumericArray(tc.value)), | ||
tc.isNumericArray, | ||
`isNumericArray(${JSON.stringify(tc.value)}) => ${tc.isNumericArray}` | ||
); | ||
} | ||
t.end(); | ||
}); |
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
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