Comparing version 3.4.1 to 3.5.0
@@ -26,2 +26,3 @@ export declare type BoundCoercionFn = () => BoundCoercionFn; | ||
export declare const makeTrait: <U>(value: U) => <V>() => U & Trait<V>; | ||
export declare function softAssert(cond: boolean): asserts cond; | ||
export declare function makeValidator<U, V extends U>({ test }: { | ||
@@ -36,2 +37,3 @@ test: StrictTest<U, V>; | ||
export declare function makeCoercionFn(target: any, key: any): CoercionFn; | ||
export declare function makeLazyCoercionFn(fn: CoercionFn, orig: any, generator: () => any): BoundCoercionFn; | ||
export declare function makeSetter(target: any, key: any): (v: any) => void; | ||
@@ -56,2 +58,6 @@ export declare function plural(n: number, singular: string, plural: string): string; | ||
}) => StrictValidator<unknown, InferType<T>[]>; | ||
export declare const isSet: <T extends StrictValidator<any, any>>(spec: T, { delimiter }?: { | ||
delimiter?: string | RegExp | undefined; | ||
}) => StrictValidator<unknown, Set<InferType<T>>>; | ||
export declare const isMap: <TKey extends StrictValidator<any, any>, TValue extends StrictValidator<any, any>>(keySpec: TKey, valueSpec: TValue) => StrictValidator<unknown, Map<InferType<TKey>, InferType<TValue>>>; | ||
declare type AnyStrictValidatorTuple = AnyStrictValidator[] | []; | ||
@@ -58,0 +64,0 @@ declare type InferTypeFromTuple<T extends AnyStrictValidatorTuple> = { |
112
lib/index.js
@@ -17,2 +17,5 @@ 'use strict'; | ||
}; | ||
function softAssert(cond) { | ||
// It's a soft assert; we tell TypeScript about the type, but we don't need to check it | ||
} | ||
function makeValidator({ test }) { | ||
@@ -49,2 +52,13 @@ return makeTrait(test)(); | ||
} | ||
function makeLazyCoercionFn(fn, orig, generator) { | ||
const commit = () => { | ||
fn(generator()); | ||
return revert; | ||
}; | ||
const revert = () => { | ||
fn(orig); | ||
return commit; | ||
}; | ||
return commit; | ||
} | ||
function makeSetter(target, key) { | ||
@@ -204,2 +218,3 @@ return (v) => { | ||
var _a; | ||
const originalValue = value; | ||
if (typeof value === `string` && typeof delimiter !== `undefined`) { | ||
@@ -210,3 +225,2 @@ if (typeof (state === null || state === void 0 ? void 0 : state.coercions) !== `undefined`) { | ||
value = value.split(delimiter); | ||
state.coercions.push([(_a = state.p) !== null && _a !== void 0 ? _a : `.`, state.coercion.bind(null, value)]); | ||
} | ||
@@ -223,5 +237,97 @@ } | ||
} | ||
if (value !== originalValue) | ||
state.coercions.push([(_a = state.p) !== null && _a !== void 0 ? _a : `.`, state.coercion.bind(null, value)]); | ||
return valid; | ||
}, | ||
}); | ||
const isSet = (spec, { delimiter } = {}) => { | ||
const isArrayValidator = isArray(spec, { delimiter }); | ||
return makeValidator({ | ||
test: (value, state) => { | ||
var _a, _b; | ||
if (Object.getPrototypeOf(value).toString() === `[object Set]`) { | ||
if (typeof (state === null || state === void 0 ? void 0 : state.coercions) !== `undefined`) { | ||
if (typeof (state === null || state === void 0 ? void 0 : state.coercion) === `undefined`) | ||
return pushError(state, `Unbound coercion result`); | ||
const originalValues = [...value]; | ||
const coercedValues = [...value]; | ||
if (!isArrayValidator(coercedValues, Object.assign(Object.assign({}, state), { coercion: undefined }))) | ||
return false; | ||
const updateValue = () => coercedValues.some((val, t) => val !== originalValues[t]) | ||
? new Set(coercedValues) | ||
: value; | ||
state.coercions.push([(_a = state.p) !== null && _a !== void 0 ? _a : `.`, makeLazyCoercionFn(state.coercion, value, updateValue)]); | ||
return true; | ||
} | ||
else { | ||
let valid = true; | ||
for (const subValue of value) { | ||
valid = spec(subValue, Object.assign({}, state)) && valid; | ||
if (!valid && (state === null || state === void 0 ? void 0 : state.errors) == null) { | ||
break; | ||
} | ||
} | ||
return valid; | ||
} | ||
} | ||
if (typeof (state === null || state === void 0 ? void 0 : state.coercions) !== `undefined`) { | ||
if (typeof (state === null || state === void 0 ? void 0 : state.coercion) === `undefined`) | ||
return pushError(state, `Unbound coercion result`); | ||
const store = { value }; | ||
if (!isArrayValidator(value, Object.assign(Object.assign({}, state), { coercion: makeCoercionFn(store, `value`) }))) | ||
return false; | ||
state.coercions.push([(_b = state.p) !== null && _b !== void 0 ? _b : `.`, makeLazyCoercionFn(state.coercion, value, () => new Set(store.value))]); | ||
return true; | ||
} | ||
return pushError(state, `Expected a set (got ${getPrintable(value)})`); | ||
} | ||
}); | ||
}; | ||
const isMap = (keySpec, valueSpec) => { | ||
const isArrayValidator = isArray(isTuple([keySpec, valueSpec])); | ||
return makeValidator({ | ||
test: (value, state) => { | ||
var _a, _b; | ||
if (Object.getPrototypeOf(value).toString() === `[object Map]`) { | ||
if (typeof (state === null || state === void 0 ? void 0 : state.coercions) !== `undefined`) { | ||
if (typeof (state === null || state === void 0 ? void 0 : state.coercion) === `undefined`) | ||
return pushError(state, `Unbound coercion result`); | ||
const originalValues = [...value]; | ||
const coercedValues = [...value]; | ||
if (!isArrayValidator(coercedValues, Object.assign(Object.assign({}, state), { coercion: undefined }))) | ||
return false; | ||
const updateValue = () => coercedValues.some((val, t) => val[0] !== originalValues[t][0] || val[1] !== originalValues[t][1]) | ||
? new Map(coercedValues) | ||
: value; | ||
state.coercions.push([(_a = state.p) !== null && _a !== void 0 ? _a : `.`, makeLazyCoercionFn(state.coercion, value, updateValue)]); | ||
return true; | ||
} | ||
else { | ||
let valid = true; | ||
for (const [key, subValue] of value) { | ||
valid = keySpec(key, Object.assign({}, state)) && valid; | ||
if (!valid && (state === null || state === void 0 ? void 0 : state.errors) == null) { | ||
break; | ||
} | ||
valid = valueSpec(subValue, Object.assign(Object.assign({}, state), { p: computeKey(state, key) })) && valid; | ||
if (!valid && (state === null || state === void 0 ? void 0 : state.errors) == null) { | ||
break; | ||
} | ||
} | ||
return valid; | ||
} | ||
} | ||
if (typeof (state === null || state === void 0 ? void 0 : state.coercions) !== `undefined`) { | ||
if (typeof (state === null || state === void 0 ? void 0 : state.coercion) === `undefined`) | ||
return pushError(state, `Unbound coercion result`); | ||
const store = { value }; | ||
if (!isArrayValidator(value, Object.assign(Object.assign({}, state), { coercion: makeCoercionFn(store, `value`) }))) | ||
return false; | ||
state.coercions.push([(_b = state.p) !== null && _b !== void 0 ? _b : `.`, makeLazyCoercionFn(state.coercion, value, () => new Map(store.value))]); | ||
return true; | ||
} | ||
return pushError(state, `Expected a map (got ${getPrintable(value)})`); | ||
} | ||
}); | ||
}; | ||
const isTuple = (spec, { delimiter } = {}) => { | ||
@@ -682,2 +788,3 @@ const lengthValidator = hasExactLength(spec.length); | ||
exports.isLowerCase = isLowerCase; | ||
exports.isMap = isMap; | ||
exports.isNegative = isNegative; | ||
@@ -691,2 +798,3 @@ exports.isNullable = isNullable; | ||
exports.isPositive = isPositive; | ||
exports.isSet = isSet; | ||
exports.isString = isString; | ||
@@ -699,2 +807,3 @@ exports.isTuple = isTuple; | ||
exports.makeCoercionFn = makeCoercionFn; | ||
exports.makeLazyCoercionFn = makeLazyCoercionFn; | ||
exports.makeSetter = makeSetter; | ||
@@ -707,2 +816,3 @@ exports.makeTrait = makeTrait; | ||
exports.simpleKeyRegExp = simpleKeyRegExp; | ||
exports.softAssert = softAssert; | ||
exports.uuid4RegExp = uuid4RegExp; |
{ | ||
"name": "typanion", | ||
"version": "3.4.1", | ||
"version": "3.5.0", | ||
"main": "lib/index", | ||
@@ -18,3 +18,3 @@ "license": "MIT", | ||
"@types/mocha": "^7.0.2", | ||
"chai": "^4.2.0", | ||
"chai": "^4.3.4", | ||
"mocha": "^8.0.1", | ||
@@ -21,0 +21,0 @@ "rollup": "^2.17.0", |
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
81297
1712