Type-safe validation helpers for array values.
The guard module provides runtime checks for determining whether values are arrays and arrays of specific types. It helps enforce correctness and type narrowing when working with data that should be arrays, including non-empty arrays and arrays of numbers, strings, or nested arrays.
import atomix from '@nasriya/atomix';
const arraysGuard = atomix.dataTypes.array.guard;| API | Description |
|---|---|
| isArray | Checks if a value is an array |
| isNotEmpty | Checks if an array is non-empty |
| isArrayOf | Creates a guard for arrays of a specific type |
| isArrayOfNumbers | Checks if a value is an array of numbers |
| isArrayOfStrings | Checks if a value is an array of strings |
| isArrayOfArrays | Checks if a value is an array of arrays |
Signature: isArray(value: unknown): value is Array<any>
Checks if the provided value is an array.
arraysGuard.isArray([1, 2, 3]); // true
arraysGuard.isArray('string'); // falseSignature: isNotEmpty<T extends Array<T>>(value: unknown): value is NonEmptyArray<T>
Checks if the value is a non-empty array.
arraysGuard.isNotEmpty([]); // false
arraysGuard.isNotEmpty([1, 2, 3]); // trueSignature: isArrayOf<T>(itemGuard: (item: unknown) => item is T): (value: unknown) => value is T[]
Creates a guard function to check arrays of a specific type.
const isArrayOfNumbers = arraysGuard.isArrayOf((v): v is number => typeof v === 'number');
isArrayOfNumbers([1, 2, 3]); // true
isArrayOfNumbers([1, '2', 3]); // falseSignature: readonly isArrayOfNumbers
Checks if the value is an array of numbers.
arraysGuard.isArrayOfNumbers([1, 2, 3]); // true
arraysGuard.isArrayOfNumbers([1, '2', 3]); // falseSignature: readonly isArrayOfStrings
Checks if the value is an array of strings.
arraysGuard.isArrayOfStrings(['a', 'b', 'c']); // true
arraysGuard.isArrayOfStrings(['a', 2, 'c']); // falseSignature: readonly isArrayOfArrays
Checks if the value is an array of arrays.
arraysGuard.isArrayOfArrays([[1], [2, 3]]); // true
arraysGuard.isArrayOfArrays([[1], 2]); // false