@cloudflare/util-en-garde
Advanced tools
Comparing version 1.1.0 to 1.2.0
@@ -6,5 +6,10 @@ # Change Log | ||
# 1.1.0 (2019-10-14) | ||
# 1.2.0 (2019-10-15) | ||
### Bug Fixes | ||
* **stratus:** UI-2859: bump all package versions ([6db21c2](http://stash.cfops.it:7999/fe/stratus/commits/6db21c2)) | ||
### Features | ||
@@ -14,1 +19,2 @@ | ||
* **util-en-garde:** TSX-143 Add any and objectWithValuesOf guards ([b0bdaf4](http://stash.cfops.it:7999/fe/stratus/commits/b0bdaf4)) | ||
* **util-en-garde:** TSX-146 Add tuple higher order guard ([1ae33bf](http://stash.cfops.it:7999/fe/stratus/commits/1ae33bf)) |
@@ -19,2 +19,36 @@ import { TypeGuard, ObjectGuardDefinition, TypeOfArray } from './type-helpers'; | ||
arrayOf: <T>(typeGuard: TypeGuard<T>) => (obj: unknown) => obj is T[]; | ||
tuple: <TypeGuards_2 extends TypeGuard<any>[]>(...guards: TypeGuards_2) => (obj: unknown) => obj is { | ||
return: []; | ||
recurse: { | ||
return: [TypeFromGuard<import("@cloudflare/types/src").Head<TypeGuards_2>>]; | ||
recurse: { | ||
return: [TypeFromGuard<import("@cloudflare/types/src").Head<import("@cloudflare/types/src").Tail<TypeGuards_2>>>]; | ||
recurse: { | ||
return: [TypeFromGuard<import("@cloudflare/types/src").Head<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<TypeGuards_2>>>>]; | ||
recurse: { | ||
return: [TypeFromGuard<import("@cloudflare/types/src").Head<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<TypeGuards_2>>>>>]; | ||
recurse: { | ||
return: [TypeFromGuard<import("@cloudflare/types/src").Head<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<TypeGuards_2>>>>>>]; | ||
recurse: { | ||
return: [TypeFromGuard<import("@cloudflare/types/src").Head<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<TypeGuards_2>>>>>>>]; | ||
recurse: { | ||
return: [TypeFromGuard<import("@cloudflare/types/src").Head<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<TypeGuards_2>>>>>>>>]; | ||
recurse: { | ||
return: [TypeFromGuard<import("@cloudflare/types/src").Head<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<TypeGuards_2>>>>>>>>>]; | ||
recurse: { | ||
return: [TypeFromGuard<import("@cloudflare/types/src").Head<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<TypeGuards_2>>>>>>>>>>]; | ||
recurse: { | ||
return: [TypeFromGuard<import("@cloudflare/types/src").Head<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<TypeGuards_2>>>>>>>>>>>]; | ||
recurse: any[import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<TypeGuards_2>>>>>>>>>>> extends [] ? "return" : "recurse"]; | ||
}[import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<TypeGuards_2>>>>>>>>>> extends [] ? "return" : "recurse"]; | ||
}[import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<TypeGuards_2>>>>>>>>> extends [] ? "return" : "recurse"]; | ||
}[import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<TypeGuards_2>>>>>>>> extends [] ? "return" : "recurse"]; | ||
}[import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<TypeGuards_2>>>>>>> extends [] ? "return" : "recurse"]; | ||
}[import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<TypeGuards_2>>>>>> extends [] ? "return" : "recurse"]; | ||
}[import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<TypeGuards_2>>>>> extends [] ? "return" : "recurse"]; | ||
}[import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<TypeGuards_2>>>> extends [] ? "return" : "recurse"]; | ||
}[import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<TypeGuards_2>>> extends [] ? "return" : "recurse"]; | ||
}[import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<TypeGuards_2>> extends [] ? "return" : "recurse"]; | ||
}[import("@cloudflare/types/src").Tail<TypeGuards_2> extends [] ? "return" : "recurse"]; | ||
}[TypeGuards_2 extends [] ? "return" : "recurse"]; | ||
object: <Definition extends ObjectGuardDefinition>(definition: Definition) => (obj: unknown) => obj is { [Key_3 in keyof ({ [MandatoryKey in { [Key_1 in keyof { [Key in keyof Definition]: TypeFromGuard<Definition[Key]>; }]: undefined extends { [Key in keyof Definition]: TypeFromGuard<Definition[Key]>; }[Key_1] ? never : Key_1; }[keyof Definition]]: { [Key in keyof Definition]: TypeFromGuard<Definition[Key]>; }[MandatoryKey]; } & { [OptionalKey in { [Key_2 in keyof { [Key in keyof Definition]: TypeFromGuard<Definition[Key]>; }]: Key_2; }[Exclude<keyof Definition, { [Key_1 in keyof { [Key in keyof Definition]: TypeFromGuard<Definition[Key]>; }]: undefined extends { [Key in keyof Definition]: TypeFromGuard<Definition[Key]>; }[Key_1] ? never : Key_1; }[keyof Definition]>]]?: { [Key in keyof Definition]: TypeFromGuard<Definition[Key]>; }[OptionalKey] | undefined; })]: ({ [MandatoryKey in { [Key_1 in keyof { [Key in keyof Definition]: TypeFromGuard<Definition[Key]>; }]: undefined extends { [Key in keyof Definition]: TypeFromGuard<Definition[Key]>; }[Key_1] ? never : Key_1; }[keyof Definition]]: { [Key in keyof Definition]: TypeFromGuard<Definition[Key]>; }[MandatoryKey]; } & { [OptionalKey in { [Key_2 in keyof { [Key in keyof Definition]: TypeFromGuard<Definition[Key]>; }]: Key_2; }[Exclude<keyof Definition, { [Key_1 in keyof { [Key in keyof Definition]: TypeFromGuard<Definition[Key]>; }]: undefined extends { [Key in keyof Definition]: TypeFromGuard<Definition[Key]>; }[Key_1] ? never : Key_1; }[keyof Definition]>]]?: { [Key in keyof Definition]: TypeFromGuard<Definition[Key]>; }[OptionalKey] | undefined; })[Key_3]; }; | ||
@@ -21,0 +55,0 @@ exact: <Definition_1 extends ObjectGuardDefinition>(definition: Definition_1) => (obj: unknown) => obj is { [Key_7 in keyof ({ [MandatoryKey_1 in { [Key_5 in keyof { [Key_4 in keyof Definition_1]: TypeFromGuard<Definition_1[Key_4]>; }]: undefined extends { [Key_4 in keyof Definition_1]: TypeFromGuard<Definition_1[Key_4]>; }[Key_5] ? never : Key_5; }[keyof Definition_1]]: { [Key_4 in keyof Definition_1]: TypeFromGuard<Definition_1[Key_4]>; }[MandatoryKey_1]; } & { [OptionalKey_1 in { [Key_6 in keyof { [Key_4 in keyof Definition_1]: TypeFromGuard<Definition_1[Key_4]>; }]: Key_6; }[Exclude<keyof Definition_1, { [Key_5 in keyof { [Key_4 in keyof Definition_1]: TypeFromGuard<Definition_1[Key_4]>; }]: undefined extends { [Key_4 in keyof Definition_1]: TypeFromGuard<Definition_1[Key_4]>; }[Key_5] ? never : Key_5; }[keyof Definition_1]>]]?: { [Key_4 in keyof Definition_1]: TypeFromGuard<Definition_1[Key_4]>; }[OptionalKey_1] | undefined; })]: ({ [MandatoryKey_1 in { [Key_5 in keyof { [Key_4 in keyof Definition_1]: TypeFromGuard<Definition_1[Key_4]>; }]: undefined extends { [Key_4 in keyof Definition_1]: TypeFromGuard<Definition_1[Key_4]>; }[Key_5] ? never : Key_5; }[keyof Definition_1]]: { [Key_4 in keyof Definition_1]: TypeFromGuard<Definition_1[Key_4]>; }[MandatoryKey_1]; } & { [OptionalKey_1 in { [Key_6 in keyof { [Key_4 in keyof Definition_1]: TypeFromGuard<Definition_1[Key_4]>; }]: Key_6; }[Exclude<keyof Definition_1, { [Key_5 in keyof { [Key_4 in keyof Definition_1]: TypeFromGuard<Definition_1[Key_4]>; }]: undefined extends { [Key_4 in keyof Definition_1]: TypeFromGuard<Definition_1[Key_4]>; }[Key_5] ? never : Key_5; }[keyof Definition_1]>]]?: { [Key_4 in keyof Definition_1]: TypeFromGuard<Definition_1[Key_4]>; }[OptionalKey_1] | undefined; })[Key_7]; }; |
@@ -26,2 +26,52 @@ import { primitiveGuards, higherOrderGuards } from './core'; | ||
/** | ||
* Accepts any number of guards and returns a type guard that will only | ||
* match if the given value is a tuple where each value matches the guard | ||
* in that same position. | ||
* | ||
* @example | ||
* const isStringStringNumber = eg.tuple( | ||
* eg.string, | ||
* eg.string, | ||
* eg.number, | ||
* ) | ||
* | ||
* if (isStringStringNumber(["hi", "there", 4])) { | ||
* // ... | ||
* } | ||
*/ | ||
tuple: <TypeGuards extends TypeGuard<any>[]>(...params: TypeGuards) => GuardWithOptional<{ | ||
return: []; | ||
recurse: { | ||
return: [TypeFromGuard<import("@cloudflare/types/src").Head<TypeGuards>>]; | ||
recurse: { | ||
return: [TypeFromGuard<import("@cloudflare/types/src").Head<import("@cloudflare/types/src").Tail<TypeGuards>>>]; | ||
recurse: { | ||
return: [TypeFromGuard<import("@cloudflare/types/src").Head<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<TypeGuards>>>>]; | ||
recurse: { | ||
return: [TypeFromGuard<import("@cloudflare/types/src").Head<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<TypeGuards>>>>>]; | ||
recurse: { | ||
return: [TypeFromGuard<import("@cloudflare/types/src").Head<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<TypeGuards>>>>>>]; | ||
recurse: { | ||
return: [TypeFromGuard<import("@cloudflare/types/src").Head<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<TypeGuards>>>>>>>]; | ||
recurse: { | ||
return: [TypeFromGuard<import("@cloudflare/types/src").Head<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<TypeGuards>>>>>>>>]; | ||
recurse: { | ||
return: [TypeFromGuard<import("@cloudflare/types/src").Head<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<TypeGuards>>>>>>>>>]; | ||
recurse: { | ||
return: [TypeFromGuard<import("@cloudflare/types/src").Head<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<TypeGuards>>>>>>>>>>]; | ||
recurse: { | ||
return: [TypeFromGuard<import("@cloudflare/types/src").Head<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<TypeGuards>>>>>>>>>>>]; | ||
recurse: any[import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<TypeGuards>>>>>>>>>>> extends [] ? "return" : "recurse"]; | ||
}[import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<TypeGuards>>>>>>>>>> extends [] ? "return" : "recurse"]; | ||
}[import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<TypeGuards>>>>>>>>> extends [] ? "return" : "recurse"]; | ||
}[import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<TypeGuards>>>>>>>> extends [] ? "return" : "recurse"]; | ||
}[import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<TypeGuards>>>>>>> extends [] ? "return" : "recurse"]; | ||
}[import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<TypeGuards>>>>>> extends [] ? "return" : "recurse"]; | ||
}[import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<TypeGuards>>>>> extends [] ? "return" : "recurse"]; | ||
}[import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<TypeGuards>>>> extends [] ? "return" : "recurse"]; | ||
}[import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<TypeGuards>>> extends [] ? "return" : "recurse"]; | ||
}[import("@cloudflare/types/src").Tail<import("@cloudflare/types/src").Tail<TypeGuards>> extends [] ? "return" : "recurse"]; | ||
}[import("@cloudflare/types/src").Tail<TypeGuards> extends [] ? "return" : "recurse"]; | ||
}[TypeGuards extends [] ? "return" : "recurse"]>; | ||
/** | ||
* Accepts an object definition where each key must be a guard. Returns | ||
@@ -53,3 +103,3 @@ * a guard that checks that a given value is an object and that each key's | ||
*/ | ||
oneOf: <TypeGuards extends TypeGuard<any>[]>(...params: TypeGuards) => GuardWithOptional<TypeFromGuard<import("./type-helpers").TypeOfArray<TypeGuards>>>; | ||
oneOf: <TypeGuards_1 extends TypeGuard<any>[]>(...params: TypeGuards_1) => GuardWithOptional<TypeFromGuard<import("./type-helpers").TypeOfArray<TypeGuards_1>>>; | ||
/** | ||
@@ -59,3 +109,3 @@ * Accepts any number of guards as arguments. Returns a guard that checks if | ||
*/ | ||
all: <TypeGuards_1 extends TypeGuard<any>[]>(...params: TypeGuards_1) => GuardWithOptional<import("@cloudflare/types/src").UnionToIntersection<TypeFromGuard<import("./type-helpers").TypeOfArray<TypeGuards_1>>>>; | ||
all: <TypeGuards_2 extends TypeGuard<any>[]>(...params: TypeGuards_2) => GuardWithOptional<import("@cloudflare/types/src").UnionToIntersection<TypeFromGuard<import("./type-helpers").TypeOfArray<TypeGuards_2>>>>; | ||
/** | ||
@@ -62,0 +112,0 @@ * Accepts primitives and infers their types. Returns a guard that checks |
@@ -0,1 +1,2 @@ | ||
import { Reverse, Tail, Head, Prepend } from '@cloudflare/types'; | ||
export declare type TypeGuard<T> = (t: unknown) => t is T; | ||
@@ -27,2 +28,6 @@ export declare type TypeFromGuard<T extends TypeGuard<any>> = T extends TypeGuard<infer A> ? A : never; | ||
}; | ||
export declare type ExtractTupleTypeFromGuards<TypeGuards extends (TypeGuard<any>)[], Result extends any[] = []> = { | ||
return: Reverse<Result>; | ||
recurse: ExtractTupleTypeFromGuards<Tail<TypeGuards>, Prepend<TypeFromGuard<Head<TypeGuards>>, Result>>; | ||
}[TypeGuards extends [] ? 'return' : 'recurse']; | ||
export {}; |
@@ -104,2 +104,13 @@ function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); } | ||
}, | ||
tuple: function tuple() { | ||
for (var _len5 = arguments.length, guards = new Array(_len5), _key5 = 0; _key5 < _len5; _key5++) { | ||
guards[_key5] = arguments[_key5]; | ||
} | ||
return function (obj) { | ||
return Array.isArray(obj) && obj.length === guards.length && obj.every(function (value, idx) { | ||
return guards[idx](value); | ||
}); | ||
}; | ||
}, | ||
object: function object(definition) { | ||
@@ -106,0 +117,0 @@ return function (obj) { |
@@ -61,2 +61,20 @@ function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } | ||
/** | ||
* Accepts any number of guards and returns a type guard that will only | ||
* match if the given value is a tuple where each value matches the guard | ||
* in that same position. | ||
* | ||
* @example | ||
* const isStringStringNumber = eg.tuple( | ||
* eg.string, | ||
* eg.string, | ||
* eg.number, | ||
* ) | ||
* | ||
* if (isStringStringNumber(["hi", "there", 4])) { | ||
* // ... | ||
* } | ||
*/ | ||
tuple: addOptionalToHigherOrderGuard(higherOrderGuards.tuple), | ||
/** | ||
* Accepts an object definition where each key must be a guard. Returns | ||
@@ -63,0 +81,0 @@ * a guard that checks that a given value is an object and that each key's |
@@ -112,2 +112,13 @@ "use strict"; | ||
}, | ||
tuple: function tuple() { | ||
for (var _len5 = arguments.length, guards = new Array(_len5), _key5 = 0; _key5 < _len5; _key5++) { | ||
guards[_key5] = arguments[_key5]; | ||
} | ||
return function (obj) { | ||
return Array.isArray(obj) && obj.length === guards.length && obj.every(function (value, idx) { | ||
return guards[idx](value); | ||
}); | ||
}; | ||
}, | ||
object: function object(definition) { | ||
@@ -114,0 +125,0 @@ return function (obj) { |
@@ -115,2 +115,20 @@ "use strict"; | ||
/** | ||
* Accepts any number of guards and returns a type guard that will only | ||
* match if the given value is a tuple where each value matches the guard | ||
* in that same position. | ||
* | ||
* @example | ||
* const isStringStringNumber = eg.tuple( | ||
* eg.string, | ||
* eg.string, | ||
* eg.number, | ||
* ) | ||
* | ||
* if (isStringStringNumber(["hi", "there", 4])) { | ||
* // ... | ||
* } | ||
*/ | ||
tuple: (0, _optional.addOptionalToHigherOrderGuard)(_core.higherOrderGuards.tuple), | ||
/** | ||
* Accepts an object definition where each key must be a guard. Returns | ||
@@ -117,0 +135,0 @@ * a guard that checks that a given value is an object and that each key's |
{ | ||
"name": "@cloudflare/util-en-garde", | ||
"description": "", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"types": "./dist/index.d.ts", | ||
@@ -26,5 +26,5 @@ "main": "lib/index.js", | ||
"devDependencies": { | ||
"@cloudflare/types": "^1.2.11" | ||
"@cloudflare/types": "^2.0.0" | ||
}, | ||
"gitHead": "cf001fbfae351055c1121ced40344f9a8fd5d1f2" | ||
"gitHead": "c54d6a7d9b4b8f2512966f9bd6b11a6bf46e42f4" | ||
} |
@@ -5,3 +5,4 @@ import { | ||
ObjectGuardFromDefintion, | ||
TypeOfArray | ||
TypeOfArray, | ||
ExtractTupleTypeFromGuards | ||
} from './type-helpers'; | ||
@@ -58,2 +59,9 @@ import { TypeFromGuard } from '.'; | ||
tuple: <TypeGuards extends (TypeGuard<any>)[]>(...guards: TypeGuards) => ( | ||
obj: unknown | ||
): obj is ExtractTupleTypeFromGuards<TypeGuards> => | ||
Array.isArray(obj) && | ||
obj.length === guards.length && | ||
obj.every((value, idx) => guards[idx](value)), | ||
object: <Definition extends ObjectGuardDefinition>( | ||
@@ -60,0 +68,0 @@ definition: Definition |
@@ -27,2 +27,19 @@ import { primitiveGuards, higherOrderGuards } from './core'; | ||
/** | ||
* Accepts any number of guards and returns a type guard that will only | ||
* match if the given value is a tuple where each value matches the guard | ||
* in that same position. | ||
* | ||
* @example | ||
* const isStringStringNumber = eg.tuple( | ||
* eg.string, | ||
* eg.string, | ||
* eg.number, | ||
* ) | ||
* | ||
* if (isStringStringNumber(["hi", "there", 4])) { | ||
* // ... | ||
* } | ||
*/ | ||
tuple: addOptionalToHigherOrderGuard(higherOrderGuards.tuple), | ||
/** | ||
* Accepts an object definition where each key must be a guard. Returns | ||
@@ -29,0 +46,0 @@ * a guard that checks that a given value is an object and that each key's |
@@ -0,1 +1,3 @@ | ||
import { Reverse, Tail, Head, Prepend } from '@cloudflare/types'; | ||
export type TypeGuard<T> = (t: unknown) => t is T; | ||
@@ -42,1 +44,12 @@ | ||
}; | ||
export type ExtractTupleTypeFromGuards< | ||
TypeGuards extends (TypeGuard<any>)[], | ||
Result extends any[] = [] | ||
> = { | ||
return: Reverse<Result>; | ||
recurse: ExtractTupleTypeFromGuards< | ||
Tail<TypeGuards>, | ||
Prepend<TypeFromGuard<Head<TypeGuards>>, Result> | ||
>; | ||
}[TypeGuards extends [] ? 'return' : 'recurse']; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
68425
1026