Skip to content

Commit

Permalink
Add is.TupleLike#156
Browse files Browse the repository at this point in the history
  • Loading branch information
Myfeetarefreezing committed Jul 21, 2023
1 parent 94dc715 commit da74c93
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
16 changes: 16 additions & 0 deletions source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,19 @@ export type ArrayLike<T> = {
const isValidLength = (value: unknown): value is number => is.safeInteger(value) && value >= 0;
is.arrayLike = <T = unknown>(value: unknown): value is ArrayLike<T> => !is.nullOrUndefined(value) && !is.function_(value) && isValidLength((value as ArrayLike<T>).length);

type TypeGuard<T> = (value: unknown) => value is T;

// eslint-disable-next-line @typescript-eslint/ban-types
type ResolveTypesOfTypeGuardsTuple<TypeGuardsOfT, ResultOfT extends unknown[] = [] > = TypeGuardsOfT extends [TypeGuard<infer U>, ...infer TOthers] ? ResolveTypesOfTypeGuardsTuple<TOthers, [...ResultOfT, U]> : TypeGuardsOfT extends undefined[] ? ResultOfT : never;

is.tupleLike = <T extends Array<TypeGuard<unknown>>>(value: unknown, guards: [...T]): value is ResolveTypesOfTypeGuardsTuple<T> => {
if (is.array(guards) && is.array(value) && guards.length === value.length ) {

Check failure on line 335 in source/index.ts

View workflow job for this annotation

GitHub Actions / Node.js 18

There should be no space before this paren.

Check failure on line 335 in source/index.ts

View workflow job for this annotation

GitHub Actions / Node.js 16

There should be no space before this paren.

Check failure on line 335 in source/index.ts

View workflow job for this annotation

GitHub Actions / Node.js 14

There should be no space before this paren.
return guards.every((guard, index) => guard(value[index]));
}

return false;
};

is.inRange = (value: number, range: number | number[]): value is number => {
if (is.number(range)) {
return value >= Math.min(0, range) && value <= Math.max(range, 0);
Expand Down Expand Up @@ -482,6 +495,7 @@ export const enum AssertionTypeDescription {
safeInteger = 'integer', // eslint-disable-line @typescript-eslint/no-duplicate-enum-values
plainObject = 'plain object',
arrayLike = 'array-like',
tupleLike = 'tuple-like',
typedArray = 'TypedArray',
domElement = 'HTMLElement',
nodeStream = 'Node.js Stream',
Expand Down Expand Up @@ -579,6 +593,7 @@ type Assert = {
plainObject: <Value = unknown>(value: unknown) => asserts value is Record<PropertyKey, Value>;
typedArray: (value: unknown) => asserts value is TypedArray;
arrayLike: <T = unknown>(value: unknown) => asserts value is ArrayLike<T>;
tupleLike: <T extends Array<TypeGuard<unknown>>>(value: unknown, guards: [...T]) => asserts value is ResolveTypesOfTypeGuardsTuple<T>;
domElement: (value: unknown) => asserts value is HTMLElement;
observable: (value: unknown) => asserts value is ObservableLike;
nodeStream: (value: unknown) => asserts value is NodeStream;
Expand Down Expand Up @@ -687,6 +702,7 @@ export const assert: Assert = {
plainObject: <Value = unknown>(value: unknown): asserts value is Record<PropertyKey, Value> => assertType(is.plainObject(value), AssertionTypeDescription.plainObject, value),
typedArray: (value: unknown): asserts value is TypedArray => assertType(is.typedArray(value), AssertionTypeDescription.typedArray, value),
arrayLike: <T = unknown>(value: unknown): asserts value is ArrayLike<T> => assertType(is.arrayLike(value), AssertionTypeDescription.arrayLike, value),
tupleLike: <T extends Array<TypeGuard<unknown>>>(value: unknown, guards: [...T]): asserts value is ResolveTypesOfTypeGuardsTuple<T> => assertType(is.tupleLike(value, guards), AssertionTypeDescription.tupleLike, value),
domElement: (value: unknown): asserts value is HTMLElement => assertType(is.domElement(value), AssertionTypeDescription.domElement, value),
observable: (value: unknown): asserts value is ObservableLike => assertType(is.observable(value), 'Observable', value),
nodeStream: (value: unknown): asserts value is NodeStream => assertType(is.nodeStream(value), AssertionTypeDescription.nodeStream, value),
Expand Down
37 changes: 37 additions & 0 deletions test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1445,6 +1445,43 @@ test('is.arrayLike', t => {
});
});

test('is.tupleLike', t => {
(function () {
t.false(is.tupleLike(arguments, [])); // eslint-disable-line prefer-rest-params
})();

t.true(is.tupleLike([], []));
t.true(is.tupleLike([1, '2', true, {}, [], undefined, null], [is.number, is.string, is.boolean, is.object, is.array, is.undefined, is.nullOrUndefined]));
t.false(is.tupleLike('unicorn', [is.string]));

t.false(is.tupleLike({}, []));
t.false(is.tupleLike(() => {}, [is.function_]));
t.false(is.tupleLike(new Map(), [is.map]));

(function () {
t.throws(function () {
assert.tupleLike(arguments, []); // eslint-disable-line prefer-rest-params
});
})();

t.notThrows(() => {
assert.tupleLike([], []);
});
t.throws(() => {
assert.tupleLike('unicorn', [is.string]);
});

t.throws(() => {
assert.tupleLike({}, [is.object]);
});
t.throws(() => {
assert.tupleLike(() => {}, [is.function_]);
});
t.throws(() => {
assert.tupleLike(new Map(), [is.map]);
});
});

test('is.inRange', t => {
const x = 3;

Expand Down

0 comments on commit da74c93

Please sign in to comment.