ts-data-forge
Version:
[](https://www.npmjs.com/package/ts-data-forge) [](https://www.npmjs.com/package/ts-data-forge) [ • 5.07 kB
JavaScript
import '../../number/branded-types/finite-number.mjs';
import '../../number/branded-types/int.mjs';
import '../../number/branded-types/int16.mjs';
import '../../number/branded-types/int32.mjs';
import '../../number/branded-types/non-negative-finite-number.mjs';
import '../../number/branded-types/non-negative-int16.mjs';
import '../../number/branded-types/non-negative-int32.mjs';
import '../../number/branded-types/non-zero-finite-number.mjs';
import '../../number/branded-types/non-zero-int.mjs';
import '../../number/branded-types/non-zero-int16.mjs';
import '../../number/branded-types/non-zero-int32.mjs';
import '../../number/branded-types/non-zero-safe-int.mjs';
import '../../number/branded-types/non-zero-uint16.mjs';
import '../../number/branded-types/non-zero-uint32.mjs';
import '../../number/branded-types/positive-finite-number.mjs';
import '../../number/branded-types/positive-int.mjs';
import '../../number/branded-types/positive-int16.mjs';
import '../../number/branded-types/positive-int32.mjs';
import '../../number/branded-types/positive-safe-int.mjs';
import '../../number/branded-types/positive-uint16.mjs';
import '../../number/branded-types/positive-uint32.mjs';
import '../../number/branded-types/safe-int.mjs';
import '../../number/branded-types/safe-uint.mjs';
import '../../number/branded-types/uint.mjs';
import '../../number/branded-types/uint16.mjs';
import { asUint32 } from '../../number/branded-types/uint32.mjs';
import '../../number/enum/int8.mjs';
import '../../number/enum/uint8.mjs';
import { Num } from '../../number/num.mjs';
import '../../number/refined-number-utils.mjs';
/**
* Type guard that checks if a value is an array.
*
* @example
*
* ```ts
* const maybeArray: unknown = [1, 2, 3];
*
* const maybeValue: unknown = 'Ada';
*
* assert.isTrue(Arr.isArray(maybeArray));
*
* assert.isFalse(Arr.isArray(maybeValue));
*
* if (Arr.isArray(maybeArray)) {
* assert.deepStrictEqual(maybeArray, [1, 2, 3]);
* }
* ```
*/
const isArray = (value) => Array.isArray(value);
// validation
/**
* Type guard that checks if an array is empty.
*
* @example
*
* ```ts
* const emptyNumbers: readonly number[] = [] as const;
*
* const words = ['Ada', 'Lovelace'] as const;
*
* assert.isTrue(Arr.isEmpty(emptyNumbers));
*
* assert.isFalse(Arr.isEmpty(words));
*
* if (Arr.isEmpty(emptyNumbers)) {
* assert.deepStrictEqual(emptyNumbers, []);
* }
* ```
*/
const isEmpty = (array) => array.length === 0;
/**
* Type guard that checks if an array is non-empty.
*
* @example
*
* ```ts
* const users: readonly { id: number }[] = [{ id: 1 }];
*
* const emptyUsers: readonly { id: number }[] = [];
*
* assert.isTrue(Arr.isNonEmpty(users));
*
* assert.isFalse(Arr.isNonEmpty(emptyUsers));
*
* if (Arr.isNonEmpty(users)) {
* assert.deepStrictEqual(users[0], { id: 1 });
* }
* ```
*/
const isNonEmpty = (array) => array.length > 0;
/**
* Checks if an array has a specific length.
*
* @example
*
* ```ts
* const pair: readonly number[] = [1, 2];
*
* const triple: readonly number[] = [1, 2, 3];
*
* assert.isTrue(Arr.isArrayOfLength(pair, 2));
*
* assert.isFalse(Arr.isArrayOfLength(triple, 2));
*
* if (Arr.isArrayOfLength(pair, 2)) {
* assert.deepStrictEqual(pair, [1, 2]);
* }
* ```
*/
const isArrayOfLength = (array, len) => array.length === len;
/**
* Checks if an array has at least a specific length.
*
* @example
*
* ```ts
* const queue: readonly string[] = ['task-1', 'task-2'];
*
* const emptyQueue: readonly string[] = [];
*
* assert.isTrue(Arr.isArrayAtLeastLength(queue, 1));
*
* assert.isFalse(Arr.isArrayAtLeastLength(emptyQueue, 1));
*
* if (Arr.isArrayAtLeastLength(queue, 1)) {
* assert.isTrue(queue[0] === 'task-1');
* }
* ```
*/
const isArrayAtLeastLength = (array, len) => array.length >= len;
function every(...args) {
switch (args.length) {
case 2: {
const [array, predicate] = args;
return array.every((a, i) => predicate(a, asUint32(i)));
}
case 1: {
const [predicate] = args;
return (array) => every(array, predicate);
}
}
}
function some(...args) {
switch (args.length) {
case 2: {
const [array, predicate] = args;
return array.some((a, i) => predicate(a, asUint32(i)));
}
case 1: {
const [predicate] = args;
return (array) => some(array, predicate);
}
}
}
/**
* Checks if an index is within the valid range of an array.
*
* @example
*
* ```ts
* const items = ['Ada', 'Grace', 'Katherine'] as const;
*
* assert.isTrue(Arr.indexIsInRange(items, 1));
*
* assert.isFalse(Arr.indexIsInRange(items, 3));
*
* if (Arr.indexIsInRange(items, 2)) {
* assert.isTrue(items[2] === 'Katherine');
* }
* ```
*/
const indexIsInRange = (array, index) => Num.isInRange(0, array.length)(index);
export { every, indexIsInRange, isArray, isArrayAtLeastLength, isArrayOfLength, isEmpty, isNonEmpty, some };
//# sourceMappingURL=array-utils-validation.mjs.map