GitHub
deepbox/core

Utilities

Low-level validation and conversion utilities. Most users will not import these directly — they are used internally by all Deepbox modules.
shapeToSize
shapeToSize(shape: Shape): number

Computes the total number of elements from a shape array. Multiplies all dimensions together.

Parameters:
shape: Shape - Tensor shape (e.g., [2, 3, 4] → 24)
dtypeToTypedArrayCtor
dtypeToTypedArrayCtor(dtype: DType): TypedArrayConstructor

Maps a DType to its corresponding TypedArray constructor (e.g., 'float32' → Float32Array).

validateShape
validateShape(shape: unknown, name: string): readonly number[]

Validates that a value is a valid shape (array of non-negative integers). Throws DataValidationError on failure.

validateDtype
validateDtype(dtype: unknown, name: string): DType

Validates that a value is a supported DType string. Throws DataValidationError on failure.

validateDevice
validateDevice(device: unknown, name: string): Device

Validates that a value is a supported Device string.

normalizeAxis
normalizeAxis(axis: number, ndim: number): number

Normalizes a negative axis to a positive one (e.g., axis=-1 with ndim=3 → 2). Throws IndexError if out of bounds.

normalizeAxes
normalizeAxes(axes: number | number[] | null, ndim: number): number[]

Normalizes axis parameter that can be a single axis, array of axes, or null (all axes).

validatePositive
validatePositive(value: number, name: string): number

Validates that a number is positive (> 0). Throws InvalidParameterError otherwise.

validateNonNegative
validateNonNegative(value: number, name: string): number

Validates that a number is non-negative (>= 0).

validateInteger
validateInteger(value: number, name: string): number

Validates that a number is a finite safe integer.

validateRange
validateRange(value: number, min: number, max: number, name: string): number

Validates that a number falls within [min, max].

utils-example.ts
import { shapeToSize, normalizeAxis, validateShape } from "deepbox/core";shapeToSize([2, 3, 4]); // 24shapeToSize([]);         // 1 (scalar)normalizeAxis(-1, 3);  // 2normalizeAxis(0, 3);   // 0validateShape([2, 3], "myShape"); // [2, 3] — valid// validateShape([2, -1], "myShape"); // throws DataValidationError