@6river/reason-guard
Advanced tools
Comparing version 1.0.0 to 1.0.1
"use strict"; | ||
function __export(m) { | ||
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; | ||
} | ||
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; | ||
} | ||
}); | ||
exports.hasProperty = (p) => exports.checkerToGuard((input) => { | ||
const x = input; | ||
// if (x[p] === undefined) throw new Error(`property ${p} is undefined`); | ||
// if (x[p] === null) throw new Error(`property ${p} is null`); // is this right? | ||
if (!(p in x)) | ||
throw new Error(`property ${p} is not present`); | ||
return `property ${p} is present`; | ||
}); | ||
exports.propertyHasType = (itemGuard) => (p) => exports.checkerToGuard((input) => { | ||
const innerErrors = []; | ||
const innerConfs = []; | ||
if (!itemGuard(input[p], innerErrors, innerConfs)) { | ||
throw new Error(`property ${p}: ${innerErrors[0].message}`); | ||
} | ||
return `property ${p}: ${innerConfs[0]}`; | ||
}); | ||
exports.arrayHasType = (itemGuard) => exports.checkerToGuard((input) => { | ||
for (let i = 0; i < input.length; i++) { | ||
const innerErrors = []; | ||
const innerConfs = []; | ||
if (!itemGuard(input[i], innerErrors, innerConfs)) { | ||
throw new Error(`element ${i}: ${innerErrors[0].message}`); | ||
} | ||
} | ||
return `is array of expected type`; | ||
}); | ||
exports.propertyIsUndefined = (p) => exports.checkerToGuard((input) => { | ||
const x = input; | ||
if (x[p] !== undefined) | ||
throw new Error(`property ${p} is not undefined`); | ||
return `property ${p} is undefined`; | ||
}); | ||
exports.propertyIsNull = (p) => exports.checkerToGuard((input) => { | ||
const x = input; | ||
if (x[p] !== null) | ||
throw new Error(`property ${p} is not null`); | ||
return `property ${p} is null`; | ||
}); | ||
// Dangerous -- do not export! | ||
// We cannot guarantee that "x: PRIM" -> "typeof x === 'prim'"! | ||
function getPrimitiveTypeCheck(prim) { | ||
return exports.checkerToGuard((input) => { | ||
if (typeof input !== prim) | ||
throw new Error(`not a ${prim}`); | ||
return `a ${prim}`; | ||
}); | ||
} | ||
const getInstanceTypeCheck = (ctor) => exports.checkerToGuard((input) => { | ||
if (!(input instanceof ctor)) | ||
throw new Error(`not a ${ctor.name}`); | ||
return `a ${ctor.name}`; | ||
}); | ||
exports.isNumber = getPrimitiveTypeCheck('number'); | ||
exports.isString = getPrimitiveTypeCheck('string'); | ||
exports.isBoolean = getPrimitiveTypeCheck('boolean'); | ||
exports.isFunction = getPrimitiveTypeCheck('function'); | ||
exports.isDate = getInstanceTypeCheck(Date); | ||
exports.isArray = getInstanceTypeCheck(Array); | ||
exports.thenGuard = ((left, right) => { | ||
return (input, output, confirmations) => { | ||
return left(input, output, confirmations) && right(input, output, confirmations); | ||
}; | ||
}); | ||
exports.andGuard = ((left, right) => { | ||
return (input, output, confirmations) => { | ||
return left(input, output, confirmations) && right(input, output, confirmations); | ||
}; | ||
}); | ||
exports.notGuard = ((inner) => { | ||
return (input, output, confirmations) => { | ||
try { | ||
const innerErrors = []; | ||
const innerConfs = []; | ||
if (inner(input, innerErrors, innerConfs)) { | ||
throw new Error(innerConfs[0]); | ||
} | ||
else { | ||
confirmations.push(innerErrors[0].message); | ||
return true; | ||
} | ||
} | ||
catch (err) { | ||
output.push(err); | ||
return false; | ||
} | ||
}; | ||
}); | ||
exports.orGuard = ((left, right) => { | ||
return (input, output, confirmations) => { | ||
try { | ||
const innerErrors = []; | ||
const innerConfs = []; | ||
if (left(input, innerErrors, innerConfs)) { | ||
confirmations.push(innerConfs[0]); | ||
return true; | ||
} | ||
innerConfs.splice(0); | ||
if (right(input, innerErrors, innerConfs)) { | ||
confirmations.push(innerConfs[0]); | ||
return true; | ||
} | ||
throw new Error(`${innerErrors[0].message}, and ${innerErrors[1].message}`); | ||
} | ||
catch (err) { | ||
output.push(err); | ||
return false; | ||
} | ||
}; | ||
}); | ||
exports.hasNumberProperty = (p) => exports.thenGuard(exports.hasProperty(p), exports.propertyHasType(exports.isNumber)(p)); | ||
exports.hasStringProperty = (p) => exports.thenGuard(exports.hasProperty(p), exports.propertyHasType(exports.isString)(p)); | ||
exports.hasBooleanProperty = (p) => exports.thenGuard(exports.hasProperty(p), exports.propertyHasType(exports.isBoolean)(p)); | ||
exports.hasFunctionProperty = (p) => exports.thenGuard(exports.hasProperty(p), exports.propertyHasType(exports.isFunction)(p)); | ||
exports.hasDateProperty = (p) => exports.thenGuard(exports.hasProperty(p), exports.propertyHasType(exports.isDate)(p)); | ||
exports.hasArrayProperty = (itemGuard) => (p) => exports.thenGuard(exports.thenGuard(exports.hasProperty(p), exports.propertyHasType(exports.isArray)(p)), exports.propertyHasType(exports.arrayHasType(itemGuard))(p)); | ||
__export(require("./arrayHasType")); | ||
__export(require("./Checker")); | ||
__export(require("./combinators")); | ||
__export(require("./instanceGuards")); | ||
__export(require("./primitiveGuards")); | ||
__export(require("./propertyGuards")); | ||
//# sourceMappingURL=index.js.map |
@@ -52,3 +52,3 @@ { | ||
}, | ||
"version": "1.0.0" | ||
"version": "1.0.1" | ||
} |
173
src/index.ts
@@ -1,166 +0,7 @@ | ||
export type ReasonGuard<FROM, TO extends FROM> = (input: FROM, output: Error[], confirmations: string[]) => input is TO; | ||
export type ReasonCheck<FROM> = (input: FROM, output: Error[], confirmations: string[]) => boolean; | ||
export type Checker<FROM> = (input: FROM) => string; | ||
export const checkerToGuard = <(<FROM, TO extends FROM>(checker: Checker<FROM>) => | ||
ReasonGuard<FROM, TO>)>((checker) => (input, e, c) => { | ||
try { | ||
c.push(checker(input)); | ||
return true; | ||
} catch (err) { | ||
e.push(err); | ||
return false; | ||
} | ||
}); | ||
export const hasProperty = | ||
<T extends string | number | symbol>(p: T) => checkerToGuard<unknown, { [P in T]: unknown }>((input: unknown) => { | ||
const x: any = input; | ||
// if (x[p] === undefined) throw new Error(`property ${p} is undefined`); | ||
// if (x[p] === null) throw new Error(`property ${p} is null`); // is this right? | ||
if (!(p in x)) throw new Error(`property ${p} is not present`); | ||
return `property ${p} is present`; | ||
}); | ||
export const propertyHasType = | ||
<FROM, TO extends FROM>(itemGuard: ReasonGuard<FROM, TO>) => | ||
<T extends string | number | symbol>(p: T) => | ||
checkerToGuard<{ [P in T]: FROM }, { [P in T]: TO }>((input: { [P in T]: FROM }) => { | ||
const innerErrors: Error[] = []; | ||
const innerConfs: string[] = []; | ||
if (!itemGuard(input[p], innerErrors, innerConfs)) { | ||
throw new Error(`property ${p}: ${innerErrors[0].message}`); | ||
} | ||
return `property ${p}: ${innerConfs[0]}`; | ||
}); | ||
export const arrayHasType = | ||
<TO>(itemGuard: ReasonGuard<unknown, TO>) => | ||
checkerToGuard<unknown[], TO[]>((input: unknown[]) => { | ||
for (let i = 0; i < input.length; i++) { | ||
const innerErrors: Error[] = []; | ||
const innerConfs: string[] = []; | ||
if (!itemGuard(input[i], innerErrors, innerConfs)) { | ||
throw new Error(`element ${i}: ${innerErrors[0].message}`); | ||
} | ||
} | ||
return `is array of expected type`; | ||
}); | ||
export 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`; | ||
}); | ||
export 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`; | ||
}); | ||
type Primitive = 'string'|'number'|'bigint'|'boolean'|'symbol'|'undefined'|'object'|'function'; | ||
// Dangerous -- do not export! | ||
// We cannot guarantee that "x: PRIM" -> "typeof x === 'prim'"! | ||
function getPrimitiveTypeCheck<PRIM>(prim: Primitive) { | ||
return checkerToGuard<unknown, PRIM>((input: unknown) => { | ||
if (typeof input !== prim) throw new Error(`not a ${prim}`); | ||
return `a ${prim}`; | ||
}); | ||
} | ||
const getInstanceTypeCheck = | ||
<INST>(ctor: new(...args: any[]) => INST) => | ||
checkerToGuard<unknown, INST>((input: unknown) => { | ||
if (!(input instanceof ctor)) throw new Error(`not a ${ctor.name}`); | ||
return `a ${ctor.name}`; | ||
}); | ||
export const isNumber = getPrimitiveTypeCheck<number>('number'); | ||
export const isString = getPrimitiveTypeCheck<string>('string'); | ||
export const isBoolean = getPrimitiveTypeCheck<boolean>('boolean'); | ||
export const isFunction = getPrimitiveTypeCheck<Function>('function'); | ||
export const isDate = getInstanceTypeCheck(Date); | ||
export const isArray = getInstanceTypeCheck(Array); | ||
export const thenGuard = | ||
<(<FROM, MID extends FROM, TO extends MID>(left: ReasonGuard<FROM, MID>, right: ReasonGuard<MID, TO>) => | ||
ReasonGuard<FROM, TO>)>((left, right) => { | ||
return (input, output, confirmations) => { | ||
return left(input, output, confirmations) && right(input, output, confirmations); | ||
}; | ||
}); | ||
export const andGuard = | ||
<(<FROM, LEFT extends FROM, RIGHT extends FROM>(left: ReasonGuard<FROM, LEFT>, right: ReasonGuard<FROM, RIGHT>) => | ||
ReasonGuard<FROM, LEFT&RIGHT>)>((left, right) => { | ||
return (input, output, confirmations) => { | ||
return left(input, output, confirmations) && right(input, output, confirmations); | ||
}; | ||
}); | ||
export const notGuard = | ||
<(<FROM, TO extends FROM>(inner: ReasonGuard<FROM, TO>) => | ||
ReasonGuard<FROM, Exclude<FROM, TO>>)>((inner) => { | ||
return (input, output, confirmations) => { | ||
try { | ||
const innerErrors: Error[] = []; | ||
const innerConfs: string[] = []; | ||
if (inner(input, innerErrors, innerConfs)) { | ||
throw new Error(innerConfs[0]); | ||
} else { | ||
confirmations.push(innerErrors[0].message); | ||
return true; | ||
} | ||
} catch (err) { | ||
output.push(err); | ||
return false; | ||
} | ||
}; | ||
}); | ||
export const orGuard = | ||
<(<FROM, TO extends FROM>(left: ReasonGuard<FROM, TO>, right: ReasonGuard<FROM, TO>) => | ||
ReasonGuard<FROM, TO>)>((left, right) => { | ||
return (input, output, confirmations) => { | ||
try { | ||
const innerErrors: Error[] = []; | ||
const innerConfs: string[] = []; | ||
if (left(input, innerErrors, innerConfs)) { | ||
confirmations.push(innerConfs[0]); | ||
return true; | ||
} | ||
innerConfs.splice(0); | ||
if (right(input, innerErrors, innerConfs)) { | ||
confirmations.push(innerConfs[0]); | ||
return true; | ||
} | ||
throw new Error(`${innerErrors[0].message}, and ${innerErrors[1].message}`); | ||
} catch (err) { | ||
output.push(err); | ||
return false; | ||
} | ||
}; | ||
}); | ||
export const hasNumberProperty = <T extends string | number | symbol>(p: T) => | ||
thenGuard(hasProperty(p), propertyHasType(isNumber)(p)); | ||
export const hasStringProperty = <T extends string | number | symbol>(p: T) => | ||
thenGuard(hasProperty(p), propertyHasType(isString)(p)); | ||
export const hasBooleanProperty = <T extends string | number | symbol>(p: T) => | ||
thenGuard(hasProperty(p), propertyHasType(isBoolean)(p)); | ||
export const hasFunctionProperty = <T extends string | number | symbol>(p: T) => | ||
thenGuard(hasProperty(p), propertyHasType(isFunction)(p)); | ||
export const hasDateProperty = <T extends string | number | symbol>(p: T) => | ||
thenGuard(hasProperty(p), propertyHasType(isDate)(p)); | ||
export const hasArrayProperty = | ||
<TO>(itemGuard: ReasonGuard<unknown, TO>) => | ||
<T extends string | number | symbol>(p: T) => | ||
thenGuard( | ||
thenGuard(hasProperty(p), propertyHasType(isArray)(p)), | ||
propertyHasType(arrayHasType(itemGuard))(p) | ||
); | ||
export * from './arrayHasType'; | ||
export * from './Checker'; | ||
export * from './combinators'; | ||
export * from './instanceGuards'; | ||
export * from './primitiveGuards'; | ||
export * from './propertyGuards'; | ||
export * from './ReasonGuard'; |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
27431
33
392
1