@6river/reason-guard
Advanced tools
Comparing version 1.2.1 to 1.3.0
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.checkerToGuard = ((checker) => (input, e = [], c = []) => { | ||
try { | ||
c.push(checker(input)); | ||
return true; | ||
} | ||
catch (err) { | ||
e.push(err); | ||
return false; | ||
} | ||
}); | ||
const NegatableGuard_1 = require("./NegatableGuard"); | ||
exports.checkerToGuard = (checker) => NegatableGuard_1.buildNegatable(() => getRawGuard(checker), () => getRawNegation(checker)); | ||
function getRawGuard(checker) { | ||
return (input, e = [], c = []) => { | ||
try { | ||
c.push(checker(input)); | ||
return true; | ||
} | ||
catch (err) { | ||
e.push(err); | ||
return false; | ||
} | ||
}; | ||
} | ||
function getRawNegation(checker) { | ||
return (input, e = [], c = []) => { | ||
try { | ||
const innerConf = checker(input); | ||
try { | ||
throw new Error(`negation of: ${innerConf}`); | ||
} | ||
catch (err) { | ||
e.push(err); | ||
return false; | ||
} | ||
} | ||
catch (err) { | ||
c.push(err.message); | ||
return true; | ||
} | ||
}; | ||
} | ||
//# sourceMappingURL=Checker.js.map |
@@ -9,2 +9,13 @@ "use strict"; | ||
exports.constantGuards = (result) => result ? trueGuard : falseGuard; | ||
const unnegatableTrueGuard = | ||
// eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars | ||
(input, es = [], cs = []) => { | ||
cs.push('true'); | ||
return true; | ||
}; | ||
const unnegatableFalseGuard = (input, es = []) => { | ||
es.push(new Error('false')); | ||
return false; | ||
}; | ||
exports.unnegatableConstantGuards = (result) => result ? unnegatableTrueGuard : unnegatableFalseGuard; | ||
//# sourceMappingURL=constantGuards.js.map |
@@ -8,3 +8,3 @@ "use strict"; | ||
__export(require("./Checker")); | ||
__export(require("./combinators")); | ||
__export(require("./Combinators")); | ||
__export(require("./instanceGuards")); | ||
@@ -14,3 +14,6 @@ __export(require("./primitiveGuards")); | ||
__export(require("./objectGuards")); | ||
__export(require("./ReasonGuard")); | ||
__export(require("./restrictingGuards")); | ||
__export(require("./constantGuards")); | ||
__export(require("./NegatableGuard")); | ||
//# sourceMappingURL=index.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const propertyGuards_1 = require("./propertyGuards"); | ||
const combinators_1 = require("./combinators"); | ||
const Combinators_1 = require("./Combinators"); | ||
// this shouldn't be needed, but if the property voodoo goes wrong it might be | ||
@@ -19,3 +19,3 @@ // needs to be a factory functionfor the generic parameterization to work? | ||
function checkProperty(k) { | ||
if (combinators_1.thenGuard(propertyGuards_1.hasProperty(k), | ||
if (Combinators_1.thenGuard(propertyGuards_1.hasProperty(k), | ||
// sadly this requires some `any`ing because the type system doesn't know which value `k` has | ||
@@ -22,0 +22,0 @@ propertyGuards_1.propertyHasType(definition[k])(k))(input, output, confirmations)) { |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const Checker_1 = require("./Checker"); | ||
const combinators_1 = require("./combinators"); | ||
const Combinators_1 = require("./Combinators"); | ||
const primitiveGuards_1 = require("./primitiveGuards"); | ||
@@ -36,10 +36,10 @@ const instanceGuards_1 = require("./instanceGuards"); | ||
}); | ||
exports.hasNumberProperty = (p) => combinators_1.thenGuard(exports.hasProperty(p), exports.propertyHasType(primitiveGuards_1.isNumber)(p)); | ||
exports.hasStringProperty = (p) => combinators_1.thenGuard(exports.hasProperty(p), exports.propertyHasType(primitiveGuards_1.isString)(p)); | ||
exports.hasBooleanProperty = (p) => combinators_1.thenGuard(exports.hasProperty(p), exports.propertyHasType(primitiveGuards_1.isBoolean)(p)); | ||
exports.hasFunctionProperty = (p) => combinators_1.thenGuard(exports.hasProperty(p), exports.propertyHasType(primitiveGuards_1.isFunction)(p)); | ||
exports.hasDateProperty = (p) => combinators_1.thenGuard(exports.hasProperty(p), exports.propertyHasType(instanceGuards_1.isDate)(p)); | ||
exports.hasUndefinedProperty = (p) => combinators_1.thenGuard(exports.hasProperty(p), propertyIsUndefined(p)); | ||
exports.hasNullProperty = (p) => combinators_1.thenGuard(exports.hasProperty(p), propertyIsNull(p)); | ||
exports.hasArrayProperty = (itemGuard) => (p) => combinators_1.thenGuard(combinators_1.thenGuard(exports.hasProperty(p), exports.propertyHasType(instanceGuards_1.isArray)(p)), exports.propertyHasType(arrayHasType_1.arrayHasType(itemGuard))(p)); | ||
exports.hasNumberProperty = (p) => Combinators_1.thenGuard(exports.hasProperty(p), exports.propertyHasType(primitiveGuards_1.isNumber)(p)); | ||
exports.hasStringProperty = (p) => Combinators_1.thenGuard(exports.hasProperty(p), exports.propertyHasType(primitiveGuards_1.isString)(p)); | ||
exports.hasBooleanProperty = (p) => Combinators_1.thenGuard(exports.hasProperty(p), exports.propertyHasType(primitiveGuards_1.isBoolean)(p)); | ||
exports.hasFunctionProperty = (p) => Combinators_1.thenGuard(exports.hasProperty(p), exports.propertyHasType(primitiveGuards_1.isFunction)(p)); | ||
exports.hasDateProperty = (p) => Combinators_1.thenGuard(exports.hasProperty(p), exports.propertyHasType(instanceGuards_1.isDate)(p)); | ||
exports.hasUndefinedProperty = (p) => Combinators_1.thenGuard(exports.hasProperty(p), propertyIsUndefined(p)); | ||
exports.hasNullProperty = (p) => Combinators_1.thenGuard(exports.hasProperty(p), propertyIsNull(p)); | ||
exports.hasArrayProperty = (itemGuard) => (p) => Combinators_1.thenGuard(Combinators_1.thenGuard(exports.hasProperty(p), exports.propertyHasType(instanceGuards_1.isArray)(p)), exports.propertyHasType(arrayHasType_1.arrayHasType(itemGuard))(p)); | ||
//# sourceMappingURL=propertyGuards.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.cloneGuard = (g) => (input, es, cs) => g(input, es, cs); | ||
//# sourceMappingURL=ReasonGuard.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const Checker_1 = require("./Checker"); | ||
const combinators_1 = require("./combinators"); | ||
const Combinators_1 = require("./Combinators"); | ||
const primitiveGuards_1 = require("./primitiveGuards"); | ||
@@ -30,4 +30,4 @@ exports.numberIsInteger = Checker_1.checkerToGuard((input) => { | ||
}); | ||
exports.numberIsAtMost = (maximum) => combinators_1.orGuard(exports.numberIsLessThan(maximum), exports.numberIs(maximum)); | ||
exports.numberIsAtLeast = (minimum) => combinators_1.orGuard(exports.numberIsGreaterThan(minimum), exports.numberIs(minimum)); | ||
exports.numberIsAtMost = (maximum) => Combinators_1.orGuard(exports.numberIsLessThan(maximum), exports.numberIs(maximum)); | ||
exports.numberIsAtLeast = (minimum) => Combinators_1.orGuard(exports.numberIsGreaterThan(minimum), exports.numberIs(minimum)); | ||
exports.numberIsLessThanOrEqual = exports.numberIsAtMost; | ||
@@ -61,5 +61,5 @@ exports.numberIsGreaterThanOrEqual = exports.numberIsAtLeast; | ||
}; | ||
exports.interval = (bottomType, bottomValue) => (topValue, topType) => combinators_1.thenGuard(primitiveGuards_1.isNumber, combinators_1.andGuard(bottomSymbols[bottomType] === OPEN ? exports.numberIsGreaterThan(bottomValue) : exports.numberIsAtLeast(bottomValue), topSymbols[topType] === OPEN ? exports.numberIsLessThan(topValue) : exports.numberIsAtMost(topValue))); | ||
exports.integralInterval = (bottomType, bottomValue) => (topValue, topType) => combinators_1.thenGuard(exports.interval(bottomType, bottomValue)(topValue, topType), exports.numberIsInteger); | ||
exports.isLiterable = combinators_1.orGuard(combinators_1.orGuard(primitiveGuards_1.isString, primitiveGuards_1.isSymbol), primitiveGuards_1.isNumber); | ||
exports.interval = (bottomType, bottomValue) => (topValue, topType) => Combinators_1.thenGuard(primitiveGuards_1.isNumber, Combinators_1.andGuard(bottomSymbols[bottomType] === OPEN ? exports.numberIsGreaterThan(bottomValue) : exports.numberIsAtLeast(bottomValue), topSymbols[topType] === OPEN ? exports.numberIsLessThan(topValue) : exports.numberIsAtMost(topValue))); | ||
exports.integralInterval = (bottomType, bottomValue) => (topValue, topType) => Combinators_1.thenGuard(exports.interval(bottomType, bottomValue)(topValue, topType), exports.numberIsInteger); | ||
exports.isLiterable = Combinators_1.orGuard(Combinators_1.orGuard(primitiveGuards_1.isString, primitiveGuards_1.isSymbol), primitiveGuards_1.isNumber); | ||
/** | ||
@@ -66,0 +66,0 @@ * Check that a value is a string literal type given the list of values. |
@@ -30,3 +30,3 @@ "use strict"; | ||
context('array property', function () { | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
// eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars | ||
testGuardMaker(property.hasArrayProperty((x) => true), 7); | ||
@@ -33,0 +33,0 @@ }); |
@@ -58,3 +58,3 @@ { | ||
}, | ||
"version": "1.2.1" | ||
"version": "1.3.0" | ||
} |
import {ReasonGuard} from './ReasonGuard'; | ||
import {NegatableGuard, buildNegatable} from './NegatableGuard'; | ||
export type Checker<FROM> = (input: FROM) => string; | ||
export const checkerToGuard = <(<FROM, TO extends FROM>(checker: Checker<FROM>) => | ||
ReasonGuard<FROM, TO>)>((checker) => (input, e = [], c = []) => { | ||
export const checkerToGuard: <FROM, TO extends FROM, N extends FROM = FROM>( | ||
checker: Checker<FROM> | ||
) => NegatableGuard<FROM, TO, N> = (checker) => buildNegatable( | ||
() => getRawGuard(checker), | ||
() => getRawNegation(checker) | ||
); | ||
function getRawGuard<FROM, TO extends FROM>(checker: Checker<FROM>): ReasonGuard<FROM, TO> { | ||
return (input, e = [], c = []): input is TO => { | ||
try { | ||
@@ -14,2 +22,20 @@ c.push(checker(input)); | ||
} | ||
}); | ||
}; | ||
} | ||
function getRawNegation<FROM, TO extends FROM>(checker: Checker<FROM>): ReasonGuard<FROM, TO> { | ||
return (input, e = [], c = []): input is TO => { | ||
try { | ||
const innerConf = checker(input); | ||
try { | ||
throw new Error(`negation of: ${innerConf}`); | ||
} catch (err) { | ||
e.push(err); | ||
return false; | ||
} | ||
} catch (err) { | ||
c.push(err.message); | ||
return true; | ||
} | ||
}; | ||
} |
import {ReasonGuard} from './ReasonGuard'; | ||
import {checkerToGuard} from './Checker'; | ||
import {NegatableGuard} from './NegatableGuard'; | ||
const trueGuard: ReasonGuard<unknown, unknown> = | ||
const trueGuard: NegatableGuard<unknown, unknown, never> = | ||
checkerToGuard(() => 'true'); | ||
const falseGuard: ReasonGuard<unknown, never> = | ||
const falseGuard: NegatableGuard<unknown, never, unknown> = | ||
checkerToGuard(() => { | ||
@@ -12,3 +13,19 @@ throw new Error('false'); | ||
export const constantGuards: (result: boolean) => ReasonGuard<unknown, unknown> = | ||
export const constantGuards: (result: boolean) => NegatableGuard<unknown, unknown, unknown> = | ||
(result: boolean) => result ? trueGuard : falseGuard; | ||
const unnegatableTrueGuard: ReasonGuard<unknown, unknown> = | ||
// eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars | ||
(input, es = [], cs = []): input is unknown => { | ||
cs.push('true'); | ||
return true; | ||
}; | ||
const unnegatableFalseGuard: ReasonGuard<unknown, never> = | ||
(input, es = []): input is never => { | ||
es.push(new Error('false')); | ||
return false; | ||
}; | ||
export const unnegatableConstantGuards: (result: boolean) => ReasonGuard<unknown, unknown> = | ||
(result) => result ? unnegatableTrueGuard : unnegatableFalseGuard; |
export * from './arrayHasType'; | ||
export * from './Checker'; | ||
export * from './combinators'; | ||
export * from './Combinators'; | ||
export * from './instanceGuards'; | ||
@@ -10,1 +10,3 @@ export * from './primitiveGuards'; | ||
export * from './restrictingGuards'; | ||
export * from './constantGuards'; | ||
export * from './NegatableGuard'; |
import {ReasonGuard} from './ReasonGuard'; | ||
import {hasProperty, propertyHasType} from './propertyGuards'; | ||
import {thenGuard} from './combinators'; | ||
import {thenGuard} from './Combinators'; | ||
@@ -5,0 +5,0 @@ // NOTE: for this one you HAVE to have K as a parameter |
import {ReasonGuard} from './ReasonGuard'; | ||
import {checkerToGuard} from './Checker'; | ||
import {thenGuard} from './combinators'; | ||
import {thenGuard} from './Combinators'; | ||
import {isNumber, isString, isBoolean, isFunction} from './primitiveGuards'; | ||
@@ -30,14 +30,16 @@ import {isDate, isArray} from './instanceGuards'; | ||
const propertyIsUndefined = | ||
<T extends string | number | symbol>(p: T) => checkerToGuard<unknown, { [P in T]?: undefined }>((input: unknown) => { | ||
const x: any = input; | ||
if (x[p] !== undefined) throw new Error(`property ${p} is not undefined`); | ||
return `property ${p} is undefined`; | ||
}); | ||
<T extends string | number | symbol>(p: T) => | ||
checkerToGuard<{[P in T]: unknown}, {[P in T]: undefined}>((input: unknown) => { | ||
const x: any = input; | ||
if (x[p] !== undefined) throw new Error(`property ${p} is not undefined`); | ||
return `property ${p} is undefined`; | ||
}); | ||
const propertyIsNull = | ||
<T extends string | number | symbol>(p: T) => checkerToGuard<unknown, { [P in T]: null }>((input: unknown) => { | ||
const x: any = input; | ||
if (x[p] !== null) throw new Error(`property ${p} is not null`); | ||
return `property ${p} is null`; | ||
}); | ||
<T extends string | number | symbol>(p: T) => | ||
checkerToGuard<{[P in T]: unknown}, { [P in T]: null }>((input: unknown) => { | ||
const x: any = input; | ||
if (x[p] !== null) throw new Error(`property ${p} is not null`); | ||
return `property ${p} is null`; | ||
}); | ||
@@ -44,0 +46,0 @@ export const hasNumberProperty = <T extends string | number | symbol>(p: T) => |
@@ -6,1 +6,7 @@ export type ReasonGuard<FROM, TO extends FROM> = ( | ||
) => input is TO; | ||
export const cloneGuard = | ||
<FROM, TO extends FROM> | ||
(g: ReasonGuard<FROM, TO>): ReasonGuard<FROM, TO> => | ||
(input, es, cs): input is TO => | ||
g(input, es, cs); |
import {checkerToGuard} from './Checker'; | ||
import {andGuard, orGuard, thenGuard} from './combinators'; | ||
import {andGuard, orGuard, thenGuard} from './Combinators'; | ||
import {ReasonGuard} from './ReasonGuard'; | ||
@@ -111,4 +111,4 @@ import {isNumber, isSymbol, isString} from './primitiveGuards'; | ||
export const isLiterable = orGuard<unknown, Literable>( | ||
orGuard<unknown, string|symbol>( | ||
export const isLiterable = orGuard( | ||
orGuard( | ||
isString, | ||
@@ -115,0 +115,0 @@ isSymbol |
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
200791
83
1464