@6river/reason-guard
Advanced tools
Comparing version 6.0.1 to 6.0.2
@@ -8,2 +8,3 @@ "use strict"; | ||
const instanceGuards_1 = require("./instanceGuards"); | ||
const arrayHasTypeConfirmation = 'is array of expected type'; | ||
const arrayHasType = (itemGuard) => (0, Checker_1.checkerToGuard)((input, context) => { | ||
@@ -18,3 +19,3 @@ for (let i = 0; i < input.length; i++) { | ||
} | ||
return `is array of expected type`; | ||
return arrayHasTypeConfirmation; | ||
}); | ||
@@ -21,0 +22,0 @@ exports.arrayHasType = arrayHasType; |
@@ -1,4 +0,4 @@ | ||
export declare const getInstanceTypeCheck: <INST>(ctor: new (...args: any[]) => INST) => import("./NegatableGuard").NegatableGuard<unknown, INST, unknown>; | ||
export declare function getInstanceTypeCheck<INST>(ctor: new (...args: any[]) => INST): import("./NegatableGuard").NegatableGuard<unknown, INST, unknown>; | ||
export declare const isDate: import("./NegatableGuard").NegatableGuard<unknown, Date, unknown>; | ||
export declare const isArray: import("./NegatableGuard").NegatableGuard<unknown, unknown[], unknown>; | ||
//# sourceMappingURL=instanceGuards.d.ts.map |
@@ -6,11 +6,15 @@ "use strict"; | ||
const ReasonGuard_1 = require("./ReasonGuard"); | ||
const getInstanceTypeCheck = (ctor) => (0, Checker_1.checkerToGuard)((input) => { | ||
if (!(input instanceof ctor)) { | ||
return (0, ReasonGuard_1.errorLike)(`not a ${ctor.name}`); | ||
} | ||
return `a ${ctor.name}`; | ||
}); | ||
function getInstanceTypeCheck(ctor) { | ||
const confirmation = `a ${ctor.name}`; | ||
const error = (0, ReasonGuard_1.errorLike)(`not a ${ctor.name}`); | ||
return (0, Checker_1.checkerToGuard)((input) => { | ||
if (!(input instanceof ctor)) { | ||
return error; | ||
} | ||
return confirmation; | ||
}); | ||
} | ||
exports.getInstanceTypeCheck = getInstanceTypeCheck; | ||
exports.isDate = (0, exports.getInstanceTypeCheck)(Date); | ||
exports.isArray = (0, exports.getInstanceTypeCheck)(Array); | ||
exports.isDate = getInstanceTypeCheck(Date); | ||
exports.isArray = getInstanceTypeCheck(Array); | ||
//# sourceMappingURL=instanceGuards.js.map |
@@ -28,5 +28,5 @@ import { ReasonGuard } from './ReasonGuard'; | ||
export type PropertyGuards<FROM extends object, TO extends FROM> = RequiredGuards<FROM, TO> & OptionalGuards<FROM, TO>; | ||
export declare const objectHasDefinition: <FROM extends object, TO extends FROM>(definition: PropertyGuards<FROM, TO>) => ReasonGuard<FROM, TO>; | ||
export declare function objectHasDefinition<FROM extends object, TO extends FROM>(definition: PropertyGuards<FROM, TO>): ReasonGuard<FROM, TO>; | ||
export declare const isObjectWithDefinition: <TO extends object>(definition: PropertyGuards<object, TO>) => import("./NegatableGuard").NegatableGuard<unknown, TO, unknown>; | ||
export {}; | ||
//# sourceMappingURL=objectGuards.d.ts.map |
@@ -7,27 +7,13 @@ "use strict"; | ||
const primitiveGuards_1 = require("./primitiveGuards"); | ||
function checkDefinition(definition, input, output, confirmations, context) { | ||
function checkDefinition(definition, input, output, confirmations, context = []) { | ||
let anyPassed = false; | ||
let anyFailed = false; | ||
// Here be dragons | ||
// While typescript accepts this done in pieces, | ||
// it won't accept it as a one-liner. | ||
const unifiedDefs = definition; | ||
function checkProperty(k) { | ||
const propertyDefinition = unifiedDefs[k]; | ||
if (propertyDefinition) { | ||
if (propertyDefinition(k)(input, output, confirmations, context)) { | ||
anyPassed = true; | ||
} | ||
else { | ||
anyFailed = true; | ||
} | ||
for (let i = 0; i < definition.length; ++i) { | ||
if (definition[i](input, output, confirmations, context)) { | ||
anyPassed = true; | ||
} | ||
else { | ||
anyFailed = true; | ||
} | ||
} | ||
// if k in keyof FROM, then hasProperty is redundant, but that's not something we can express here | ||
// TODO: we could cache these property lists for performance | ||
Object.getOwnPropertyNames(definition).forEach(checkProperty); | ||
// repeat! | ||
Object.getOwnPropertySymbols(definition).forEach(checkProperty); | ||
// rule is wrong, doesn't understand calls to checkProperty | ||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition | ||
if (!anyPassed && !anyFailed) { | ||
@@ -39,7 +25,28 @@ output?.push((0, ReasonGuard_1.errorLike)('definition had no guards')); | ||
} | ||
// this would save typing, but can't figure out how to use it below (syntax-wise) | ||
// type PropertyGuardBuilder<FROM, TO extends FROM> = (definition: PropertyGuards<FROM, TO>) => ReasonGuard<FROM, TO>; | ||
exports.objectHasDefinition = ((definition) => (input, output, confirmations, context = []) => checkDefinition(definition, input, output, confirmations, context)); | ||
const isObjectWithDefinition = (definition) => (0, Combinators_1.thenGuard)(primitiveGuards_1.isObject, (0, exports.objectHasDefinition)(definition)); | ||
function instantiatePropertyGuards(definition) { | ||
const instantiated = []; | ||
const unified = definition; | ||
for (const key of Object.getOwnPropertyNames(definition)) { | ||
const factory = unified[key]; | ||
if (factory) { | ||
instantiated.push(factory(key)); | ||
} | ||
} | ||
for (const key of Object.getOwnPropertySymbols(definition)) { | ||
const factory = unified[key]; | ||
if (factory) { | ||
instantiated.push(factory(key)); | ||
} | ||
} | ||
return instantiated; | ||
} | ||
function objectHasDefinition(definition) { | ||
const instantiated = instantiatePropertyGuards(definition); | ||
// typescript won't allow us to inline this below | ||
const typed = (checkDefinition); | ||
return typed.bind(undefined, instantiated); | ||
} | ||
exports.objectHasDefinition = objectHasDefinition; | ||
const isObjectWithDefinition = (definition) => (0, Combinators_1.thenGuard)(primitiveGuards_1.isObject, objectHasDefinition(definition)); | ||
exports.isObjectWithDefinition = isObjectWithDefinition; | ||
//# sourceMappingURL=objectGuards.js.map |
@@ -8,3 +8,3 @@ import { NegatableGuard } from './NegatableGuard'; | ||
export type NarrowPropertyGuard<FROM_PROP_TYPE, DEST_PROP_TYPE extends FROM_PROP_TYPE = FROM_PROP_TYPE> = <T extends PropertyKey>(p: T) => NegatableGuard<Record<T, FROM_PROP_TYPE>, Record<T, DEST_PROP_TYPE>>; | ||
export declare const hasProperty: <T extends PropertyKey>(p: T) => NegatableGuard<unknown, Record<T, unknown>, Partial<Record<T, never>>>; | ||
export declare function hasProperty<T extends PropertyKey>(p: T): NegatableGuard<unknown, Record<T, unknown>, Partial<Record<T, never>>>; | ||
export declare const propertyHasType: <FROMT, T extends string | number | symbol, TOT extends FROMT, TO extends Record<T, TOT>>(itemGuard: ReasonGuard<FROMT, TOT>, p: T) => NegatableGuard<Record<T, FROMT>, Pick<TO, T>, Record<T, FROMT>>; | ||
@@ -11,0 +11,0 @@ export declare const narrowedProperty: <FROM_PROP_TYPE, TO_PROP_TYPE extends FROM_PROP_TYPE>(g: ReasonGuard<FROM_PROP_TYPE, TO_PROP_TYPE>) => NarrowPropertyGuard<FROM_PROP_TYPE, TO_PROP_TYPE>; |
@@ -9,11 +9,13 @@ "use strict"; | ||
const primitiveGuards_1 = require("./primitiveGuards"); | ||
const hasProperty = (p) => (0, Checker_1.checkerToGuard)((input, context) => { | ||
const x = input; | ||
// if (x[p] === undefined) return errorLike(`property ${p} is undefined`); | ||
// if (x[p] === null) return errorLike(`property ${p} is null`); // is this right? | ||
if (!(p in x)) { | ||
return new ContextError_1.ContextError(`property ${String(p)} is not present`, (0, Checker_1.pushContext)(p, context)); | ||
} | ||
return `property ${String(p)} is present`; | ||
}); | ||
function hasProperty(p) { | ||
const confirmationMsg = `property ${String(p)} is present`; | ||
const errorMsg = `property ${String(p)} is not present`; | ||
return (0, Checker_1.checkerToGuard)((input, context) => { | ||
const x = input; | ||
if (!(p in x)) { | ||
return new ContextError_1.ContextError(errorMsg, (0, Checker_1.pushContext)(p, context)); | ||
} | ||
return confirmationMsg; | ||
}); | ||
} | ||
exports.hasProperty = hasProperty; | ||
@@ -32,10 +34,10 @@ const propertyHasType = (itemGuard, p) => (0, Checker_1.checkerToGuard)((input, context) => { | ||
exports.narrowedProperty = narrowedProperty; | ||
const requiredProperty = (g) => (p) => (0, Combinators_1.thenGuard)((0, exports.hasProperty)(p), (0, exports.propertyHasType)(g, p)); | ||
const requiredProperty = (g) => (p) => (0, Combinators_1.thenGuard)(hasProperty(p), (0, exports.propertyHasType)(g, p)); | ||
exports.requiredProperty = requiredProperty; | ||
const optionalProperty = (g) => (p) => (0, Combinators_1.orGuard)((0, Combinators_1.notGuard)((0, exports.hasProperty)(p)), (0, Combinators_1.orGuard)((0, exports.requiredProperty)(primitiveGuards_1.isUndefined)(p), (0, exports.requiredProperty)(g)(p))); | ||
const optionalProperty = (g) => (p) => (0, Combinators_1.orGuard)((0, Combinators_1.notGuard)(hasProperty(p)), (0, Combinators_1.orGuard)((0, exports.requiredProperty)(primitiveGuards_1.isUndefined)(p), (0, exports.requiredProperty)(g)(p))); | ||
exports.optionalProperty = optionalProperty; | ||
const strictOptionalProperty = (g) => (p) => (0, Combinators_1.orGuard)((0, Combinators_1.notGuard)((0, exports.hasProperty)(p)), (0, exports.requiredProperty)(g)(p)); | ||
const strictOptionalProperty = (g) => (p) => (0, Combinators_1.orGuard)((0, Combinators_1.notGuard)(hasProperty(p)), (0, exports.requiredProperty)(g)(p)); | ||
exports.strictOptionalProperty = strictOptionalProperty; | ||
const hasArrayProperty = (itemGuard) => (p) => (0, Combinators_1.thenGuard)((0, exports.hasProperty)(p), (0, exports.propertyHasType)((0, arrayHasType_1.isArrayOfType)(itemGuard), p)); | ||
const hasArrayProperty = (itemGuard) => (p) => (0, Combinators_1.thenGuard)(hasProperty(p), (0, exports.propertyHasType)((0, arrayHasType_1.isArrayOfType)(itemGuard), p)); | ||
exports.hasArrayProperty = hasArrayProperty; | ||
//# sourceMappingURL=propertyGuards.js.map |
@@ -43,3 +43,3 @@ import { ReasonGuard } from './ReasonGuard'; | ||
export declare const numberIsSafeInteger: import("./NegatableGuard").NegatableGuard<number, number, number>; | ||
export declare const isStrictEqual: <T>(value: T) => import("./NegatableGuard").NegatableGuard<unknown, T, unknown>; | ||
export declare function isStrictEqual<T>(value: T): import("./NegatableGuard").NegatableGuard<unknown, T, unknown>; | ||
type Literable = string | symbol | number; | ||
@@ -46,0 +46,0 @@ type ArrayToLiteral<T> = T extends ReadonlyArray<infer U> ? U : never; |
@@ -101,11 +101,15 @@ "use strict"; | ||
exports.numberIsSafeInteger = (0, Combinators_1.andGuard)(exports.numberIsInteger, (0, exports.interval)('>=', Number.MIN_SAFE_INTEGER)(Number.MAX_SAFE_INTEGER, '<=')); | ||
const isStrictEqual = (value) => (0, Checker_1.checkerToGuard)((input) => { | ||
// have to use String() because of Symbols | ||
if (value === input) { | ||
return `is exactly ${String(value)}`; | ||
} | ||
else { | ||
return (0, ReasonGuard_1.errorLike)(`is not exactly ${String(input)}`); | ||
} | ||
}); | ||
function isStrictEqual(value) { | ||
const confirmation = `is exactly ${String(value)}`; | ||
const error = (0, ReasonGuard_1.errorLike)(`is not exactly ${String(value)}`); | ||
return (0, Checker_1.checkerToGuard)((input) => { | ||
// have to use String() because of Symbols | ||
if (value === input) { | ||
return confirmation; | ||
} | ||
else { | ||
return error; | ||
} | ||
}); | ||
} | ||
exports.isStrictEqual = isStrictEqual; | ||
@@ -112,0 +116,0 @@ exports.isLiterable = (0, Combinators_1.orGuard)((0, Combinators_1.orGuard)(primitiveGuards_1.isString, primitiveGuards_1.isSymbol), primitiveGuards_1.isNumber); |
@@ -68,3 +68,3 @@ { | ||
}, | ||
"version": "6.0.1" | ||
"version": "6.0.2" | ||
} |
@@ -8,2 +8,3 @@ import { checkerToGuard, pushContext } from './Checker'; | ||
const arrayHasTypeConfirmation = 'is array of expected type'; | ||
export const arrayHasType = <TO>(itemGuard: ReasonGuard<unknown, TO>) => | ||
@@ -27,3 +28,3 @@ checkerToGuard<unknown[], TO[]>((input: unknown[], context?: PropertyKey[]) => { | ||
} | ||
return `is array of expected type`; | ||
return arrayHasTypeConfirmation; | ||
}); | ||
@@ -30,0 +31,0 @@ |
import { checkerToGuard } from './Checker'; | ||
import { errorLike } from './ReasonGuard'; | ||
export const getInstanceTypeCheck = <INST>(ctor: new (...args: any[]) => INST) => | ||
checkerToGuard<unknown, INST>((input: unknown) => { | ||
export function getInstanceTypeCheck<INST>(ctor: new (...args: any[]) => INST) { | ||
const confirmation = `a ${ctor.name}`; | ||
const error = errorLike(`not a ${ctor.name}`); | ||
return checkerToGuard<unknown, INST>((input: unknown) => { | ||
if (!(input instanceof ctor)) { | ||
return errorLike(`not a ${ctor.name}`); | ||
return error; | ||
} | ||
return `a ${ctor.name}`; | ||
return confirmation; | ||
}); | ||
} | ||
export const isDate = getInstanceTypeCheck(Date); | ||
export const isArray = getInstanceTypeCheck(Array); |
@@ -43,3 +43,2 @@ import { thenGuard } from './Combinators'; | ||
>; | ||
/** | ||
@@ -51,8 +50,18 @@ * A mapping from property names to factories for guards on those properties | ||
// we can't express this type precisely without a lot of hacks. It would end up | ||
// being a long tuple list of every property in TO. Since it isn't exported, and | ||
// we won't be able to make use of that type information where it is used, we | ||
// skip that part. We don't need the keys being checked to be part of this list, | ||
// because they are embedded in the guards. | ||
type PropertyGuardList<FROM extends object, TO extends FROM> = ReasonGuard< | ||
FROM, | ||
FROM & Partial<TO> | ||
>[]; | ||
function checkDefinition<FROM extends object, TO extends FROM>( | ||
definition: PropertyGuards<FROM, TO>, | ||
definition: PropertyGuardList<FROM, TO>, | ||
input: FROM, | ||
output?: ErrorLike[], | ||
confirmations?: string[], | ||
context?: PropertyKey[], | ||
context: PropertyKey[] = [], | ||
): input is TO { | ||
@@ -62,26 +71,10 @@ let anyPassed = false; | ||
// Here be dragons | ||
// While typescript accepts this done in pieces, | ||
// it won't accept it as a one-liner. | ||
const unifiedDefs: OptionalGuards<FROM, TO> = definition; | ||
function checkProperty<K extends keyof TO>(k: K) { | ||
const propertyDefinition = unifiedDefs[k]; | ||
if (propertyDefinition) { | ||
if (propertyDefinition(k)(input, output, confirmations, context)) { | ||
anyPassed = true; | ||
} else { | ||
anyFailed = true; | ||
} | ||
for (let i = 0; i < definition.length; ++i) { | ||
if (definition[i](input, output, confirmations, context)) { | ||
anyPassed = true; | ||
} else { | ||
anyFailed = true; | ||
} | ||
} | ||
// if k in keyof FROM, then hasProperty is redundant, but that's not something we can express here | ||
// TODO: we could cache these property lists for performance | ||
(Object.getOwnPropertyNames(definition) as (keyof TO)[]).forEach(checkProperty); | ||
// repeat! | ||
(Object.getOwnPropertySymbols(definition) as (keyof TO)[]).forEach(checkProperty); | ||
// rule is wrong, doesn't understand calls to checkProperty | ||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition | ||
if (!anyPassed && !anyFailed) { | ||
@@ -94,14 +87,32 @@ output?.push(errorLike('definition had no guards')); | ||
// this would save typing, but can't figure out how to use it below (syntax-wise) | ||
// type PropertyGuardBuilder<FROM, TO extends FROM> = (definition: PropertyGuards<FROM, TO>) => ReasonGuard<FROM, TO>; | ||
function instantiatePropertyGuards<FROM extends object, TO extends FROM>( | ||
definition: PropertyGuards<FROM, TO>, | ||
): PropertyGuardList<FROM, TO> { | ||
const instantiated: PropertyGuardList<FROM, TO> = []; | ||
const unified: OptionalGuards<FROM, TO> = definition; | ||
for (const key of Object.getOwnPropertyNames(definition) as (keyof TO)[]) { | ||
const factory = unified[key]; | ||
if (factory) { | ||
instantiated.push(factory(key)); | ||
} | ||
} | ||
for (const key of Object.getOwnPropertySymbols(definition) as (keyof TO)[]) { | ||
const factory = unified[key]; | ||
if (factory) { | ||
instantiated.push(factory(key)); | ||
} | ||
} | ||
return instantiated; | ||
} | ||
export const objectHasDefinition = < | ||
<FROM extends object, TO extends FROM>( | ||
definition: PropertyGuards<FROM, TO>, | ||
) => ReasonGuard<FROM, TO> | ||
>((definition) => | ||
(input, output, confirmations, context = []) => | ||
checkDefinition(definition, input, output, confirmations, context)); | ||
export function objectHasDefinition<FROM extends object, TO extends FROM>( | ||
definition: PropertyGuards<FROM, TO>, | ||
): ReasonGuard<FROM, TO> { | ||
const instantiated = instantiatePropertyGuards(definition); | ||
// typescript won't allow us to inline this below | ||
const typed = checkDefinition<FROM, TO>; | ||
return typed.bind(undefined, instantiated) as ReasonGuard<FROM, TO>; | ||
} | ||
export const isObjectWithDefinition = <TO extends object>(definition: PropertyGuards<object, TO>) => | ||
thenGuard<unknown, object, TO>(isObject, objectHasDefinition(definition)); |
@@ -31,14 +31,15 @@ import { checkerToGuard, pushContext } from './Checker'; | ||
export const hasProperty = <T extends PropertyKey>(p: T) => | ||
checkerToGuard<unknown, Record<T, unknown>, Partial<Record<T, never>>>( | ||
export function hasProperty<T extends PropertyKey>(p: T) { | ||
const confirmationMsg = `property ${String(p)} is present`; | ||
const errorMsg = `property ${String(p)} is not present`; | ||
return checkerToGuard<unknown, Record<T, unknown>, Partial<Record<T, never>>>( | ||
(input: unknown, context?: PropertyKey[]) => { | ||
const x: any = input; | ||
// if (x[p] === undefined) return errorLike(`property ${p} is undefined`); | ||
// if (x[p] === null) return errorLike(`property ${p} is null`); // is this right? | ||
if (!(p in x)) { | ||
return new ContextError(`property ${String(p)} is not present`, pushContext(p, context)); | ||
return new ContextError(errorMsg, pushContext(p, context)); | ||
} | ||
return `property ${String(p)} is present`; | ||
return confirmationMsg; | ||
}, | ||
); | ||
} | ||
@@ -45,0 +46,0 @@ export const propertyHasType = < |
@@ -129,11 +129,14 @@ import { checkerToGuard } from './Checker'; | ||
export const isStrictEqual = <T>(value: T) => | ||
checkerToGuard<unknown, T>((input) => { | ||
export function isStrictEqual<T>(value: T) { | ||
const confirmation = `is exactly ${String(value)}`; | ||
const error = errorLike(`is not exactly ${String(value)}`); | ||
return checkerToGuard<unknown, T>((input) => { | ||
// have to use String() because of Symbols | ||
if (value === input) { | ||
return `is exactly ${String(value)}`; | ||
return confirmation; | ||
} else { | ||
return errorLike(`is not exactly ${String(input)}`); | ||
return error; | ||
} | ||
}); | ||
} | ||
@@ -140,0 +143,0 @@ // You can have boolean literal types, but they don't work with Record<>, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
302976
3420