Skip to content

Commit

Permalink
chore: Clean up types (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
ibgreen authored Aug 3, 2024
1 parent 1a4dbbc commit ebb6049
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 25 deletions.
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
**/dist*

modules/core/src/classes/base/math-array.ts
modules/types/src/bounds-types.ts
36 changes: 28 additions & 8 deletions docs/modules/types/api-reference/array-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,46 @@

math.gl provides a number of numeric array types.

Working with a mix of typed arrays and standard JavaScript arrays containing numbers.
TypeScript types to simplify working with a mix of typed arrays and standard JavaScript arrays containing numbers.

## Types

### `TypedArray`

Any JavaScript typed array
Any JavaScript typed array.

### `NumberArray`

A classic JavaScript array containing numbers. Included for completeness, it is recommended to just use the type `number[]` in this case.

### `NumericArray`

Any JavaScript typed array, or any JavaScript array containing numbers
Any JavaScript typed array, or any classic JavaScript array containing numbers

### `NumberArray2-NumberArray16`

JavaScript number arrays of specific lengths.

## Utilities

### `isTypedArray(value: unknown): TypedArray | null`
### `isTypedArray(value: unknown): value as TypedArray`

Checks if a value is a typed array.

Remarks:
- Avoids type narrowing problems with `ArrayBuffer.isView()` (which accepts `DataViews` that do not support array methods).

### `isNumberArray(value: unknown): value as NumberArray`

Checks if a value is a classic JavaScript array of numbers.

Avoids type problems with the `ArrayBuffer.isView()` check.
Remarks:
- Only the type of the first element in a standard array is checked to be a `number`.

### `isNumericArray(value: unknown): TypedArray | null`
### `isNumericArray(value: unknown): value as NumericArray`

Avoids type problems with the `ArrayBuffer.isView()` check.
Checks if a value is either a classic JavaScript array of numbers or a typed array.

Note: only the type of the first element in a standard array is checked to be a `number`.
Remarks:
- Avoids type narrowing problems with `ArrayBuffer.isView()` (which accepts `DataViews` that do not support array methods).
- Only the type of the first element in a standard array is checked to be a `number`.
2 changes: 1 addition & 1 deletion modules/core/src/classes/base/math-array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export abstract class MathArray extends Array<number> {
return Array.isArray(arrayOrObject)
? this.copy(arrayOrObject)
: // @ts-ignore
this.fromObject(arrayOrObject);
this.fromObject(arrayOrObject);
}

to<T extends NumericArray | Record<string, unknown>>(arrayOrObject: T): T {
Expand Down
6 changes: 3 additions & 3 deletions modules/core/src/lib/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
// SPDX-License-Identifier: MIT
// Copyright (c) vis.gl contributors

import {NumberArray} from '@math.gl/types';
import {NumericArray} from '@math.gl/types';
import {config} from './common';

export function validateVector(v: NumberArray, length: number): boolean {
export function validateVector(v: NumericArray, length: number): boolean {
if (v.length !== length) {
return false;
}
Expand All @@ -25,7 +25,7 @@ export function checkNumber(value: unknown): number {
return value as number;
}

export function checkVector<T extends NumberArray>(
export function checkVector<T extends NumericArray>(
v: T,
length: number,
callerName: string = ''
Expand Down
6 changes: 3 additions & 3 deletions modules/types/src/array-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ export type TypedArrayConstructor =
export type NumericArray = TypedArray | number[];

/**
* TypeScript type covering all typed arrays and classic arrays consisting of numbers
* @note alias for NumericArray
* TypeScript type for classic arrays consisting of numbers
* @note Included for completeness / orthogonality, prefer `number[]` in actual use
*/
export type NumberArray = TypedArray | number[];
export type NumberArray = number[];

/** Array with exactly 1 number */
export type NumberArray1 = [number];
Expand Down
6 changes: 3 additions & 3 deletions modules/types/src/bounds-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function getBounds2D(bounds: Bounds): Bounds2D {
return isBounds2D(bounds)
? bounds
: [
[bounds[0][0], bounds[0][1]],
[bounds[1][0], bounds[1][1]]
];
[bounds[0][0], bounds[0][1]],
[bounds[1][0], bounds[1][1]]
];
}
3 changes: 2 additions & 1 deletion modules/types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export type {
NumberArray12,
NumberArray16
} from './array-types';
export {isTypedArray, isNumericArray} from './is-array';

export {isTypedArray, isNumberArray, isNumericArray} from './is-array';

export type {Bounds, Bounds2D, Bounds3D} from './bounds-types';
21 changes: 15 additions & 6 deletions modules/types/src/is-array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,34 @@
// SPDX-License-Identifier: MIT
// Copyright (c) vis.gl contributors

import {TypedArray, NumericArray} from './array-types';
import {TypedArray, NumericArray, NumberArray} from './array-types';

/**
* Check is an array is a typed array
* @param value value to be tested
* @returns input as TypedArray, or null
* @returns input with type narrowed to TypedArray, or null
*/
export function isTypedArray(value: unknown): value is TypedArray {
return ArrayBuffer.isView(value) && !(value instanceof DataView);
}

/**
* Check is an array is a numeric array (typed array or array of numbers)
* Check is an array is an array of numbers)
* @param value value to be tested
* @returns input as NumericArray, or null
* @returns input with type narrowed to NumberArray, or null
*/
export function isNumericArray(value: unknown): value is NumericArray {
export function isNumberArray(value: unknown): value is NumberArray {
if (Array.isArray(value)) {
return value.length === 0 || typeof value[0] === 'number';
}
return isTypedArray(value);
return false;
}

/**
* Check is an array is a numeric array (typed array or array of numbers)
* @param value value to be tested
* @returns input with type narrowed to NumericArray, or null
*/
export function isNumericArray(value: unknown): value is NumericArray {
return isTypedArray(value) || isNumberArray(value);
}

0 comments on commit ebb6049

Please sign in to comment.