import callsites from 'callsites'; import {inferLabel} from './lib/utils/infer-label'; import {Predicate} from './lib/predicates/predicate'; import {AnyPredicate} from './lib/predicates/any'; import {testSymbol, BasePredicate, isPredicate} from './lib/predicates/base-predicate'; import {StringPredicate} from './lib/predicates/string'; import {NumberPredicate} from './lib/predicates/number'; import {BooleanPredicate} from './lib/predicates/boolean'; import {ArrayPredicate} from './lib/predicates/array'; import {ObjectPredicate} from './lib/predicates/object'; import {DatePredicate} from './lib/predicates/date'; import {ErrorPredicate} from './lib/predicates/error'; import {MapPredicate} from './lib/predicates/map'; import {WeakMapPredicate} from './lib/predicates/weak-map'; import {SetPredicate} from './lib/predicates/set'; import {WeakSetPredicate} from './lib/predicates/weak-set'; type TypedArray = Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array; export interface Ow { /** * Test if the value matches the predicate. Throws an `ArgumentError` if the test fails. * * @param value Value to test. * @param predicate Predicate to test against. */ (value: T, predicate: BasePredicate): void; /** * Test if `value` matches the provided `predicate`. Throws an `ArgumentError` with the specified `label` if the test fails. * * @param value Value to test. * @param label Label which should be used in error messages. * @param predicate Predicate to test against. */ (value: T, label: string, predicate: BasePredicate): void; /** * Test the value to be a string. */ readonly string: StringPredicate; /** * Test the value to be a number. */ readonly number: NumberPredicate; /** * Test the value to be a boolean. */ readonly boolean: BooleanPredicate; /** * Test the value to be undefined. */ readonly undefined: Predicate; /** * Test the value to be null. */ readonly null: Predicate; /** * Test the value to be null or undefined. */ readonly nullOrUndefined: Predicate; /** * Test the value to be not a number. */ readonly nan: Predicate; /** * Test the value to be a Symbol. */ readonly symbol: Predicate; /** * Test the value to be an array. */ readonly array: ArrayPredicate; /** * Test the value to be an object. */ readonly object: ObjectPredicate; /** * Test the value to be a Date. */ readonly date: DatePredicate; /** * Test the value to be an Error. */ readonly error: ErrorPredicate; /** * Test the value to be a Map. */ readonly map: MapPredicate; /** * Test the value to be a WeakMap. */ readonly weakMap: WeakMapPredicate; /** * Test the value to be a Set. */ readonly set: SetPredicate; /** * Test the value to be a WeakSet. */ readonly weakSet: WeakSetPredicate; /** * Test the value to be a Function. */ readonly function: Predicate; /** * Test the value to be a Buffer. */ readonly buffer: Predicate; /** * Test the value to be a RegExp. */ readonly regExp: Predicate; /** * Test the value to be a Promise. */ readonly promise: Predicate>; /** * Test the value to be a typed array. */ readonly typedArray: Predicate; /** * Test the value to be a Int8Array. */ readonly int8Array: Predicate; /** * Test the value to be a Uint8Array. */ readonly uint8Array: Predicate; /** * Test the value to be a Uint8ClampedArray. */ readonly uint8ClampedArray: Predicate; /** * Test the value to be a Int16Array. */ readonly int16Array: Predicate; /** * Test the value to be a Uint16Array. */ readonly uint16Array: Predicate; /** * Test the value to be a Int32Array. */ readonly int32Array: Predicate; /** * Test the value to be a Uint32Array. */ readonly uint32Array: Predicate; /** * Test the value to be a Float32Array. */ readonly float32Array: Predicate; /** * Test the value to be a Float64Array. */ readonly float64Array: Predicate; /** * Test the value to be a ArrayBuffer. */ readonly arrayBuffer: Predicate; /** * Test the value to be a DataView. */ readonly dataView: Predicate; /** * Test the value to be Iterable. */ readonly iterable: Predicate>; /** * Returns `true` if the value matches the predicate, otherwise returns `false`. * * @param value Value to test. * @param predicate Predicate to test against. */ isValid(value: T, predicate: BasePredicate): value is T; /** * Create a reusable validator. * * @param predicate Predicate used in the validator function. */ create(predicate: BasePredicate): (value: T) => void; /** * Create a reusable validator. * * @param label Label which should be used in error messages. * @param predicate Predicate used in the validator function. */ create(label: string, predicate: BasePredicate): (value: T) => void; /** * Test that the value matches at least one of the given predicates. */ any(p1: BasePredicate): AnyPredicate; any(p1: BasePredicate, p2: BasePredicate): AnyPredicate; any(p1: BasePredicate, p2: BasePredicate, p3: BasePredicate): AnyPredicate; any(p1: BasePredicate, p2: BasePredicate, p3: BasePredicate, p4: BasePredicate): AnyPredicate; any(p1: BasePredicate, p2: BasePredicate, p3: BasePredicate, p4: BasePredicate, p5: BasePredicate): AnyPredicate; any(p1: BasePredicate, p2: BasePredicate, p3: BasePredicate, p4: BasePredicate, p5: BasePredicate, p6: BasePredicate): AnyPredicate; any(p1: BasePredicate, p2: BasePredicate, p3: BasePredicate, p4: BasePredicate, p5: BasePredicate, p6: BasePredicate, p7: BasePredicate): AnyPredicate; any(p1: BasePredicate, p2: BasePredicate, p3: BasePredicate, p4: BasePredicate, p5: BasePredicate, p6: BasePredicate, p7: BasePredicate, p8: BasePredicate): AnyPredicate; any(p1: BasePredicate, p2: BasePredicate, p3: BasePredicate, p4: BasePredicate, p5: BasePredicate, p6: BasePredicate, p7: BasePredicate, p8: BasePredicate, p9: BasePredicate): AnyPredicate; any(p1: BasePredicate, p2: BasePredicate, p3: BasePredicate, p4: BasePredicate, p5: BasePredicate, p6: BasePredicate, p7: BasePredicate, p8: BasePredicate, p9: BasePredicate, p10: BasePredicate): AnyPredicate; any(...predicate: BasePredicate[]): AnyPredicate; } const main = (value: T, labelOrPredicate: BasePredicate | string | undefined, predicate?: BasePredicate) => { let label: any = labelOrPredicate; let testPredicate: any = predicate; if (isPredicate(labelOrPredicate)) { label = inferLabel(callsites()); testPredicate = labelOrPredicate; } return testPredicate[testSymbol](value, main, label); }; Object.defineProperties(main, { isValid: { value: (value: T, predicate: BasePredicate) => { try { main(value, predicate); return true; } catch { return false; } } }, create: { value: (labelOrPredicate: BasePredicate | string | undefined, predicate?: BasePredicate) => (value: T) => { if (isPredicate(labelOrPredicate)) { return main(value, inferLabel(callsites()), labelOrPredicate); } return main(value, labelOrPredicate, predicate); } }, any: { value: (...predicates: BasePredicate[]) => new AnyPredicate(predicates) }, string: { get: () => new StringPredicate() }, number: { get: () => new NumberPredicate() }, boolean: { get: () => new BooleanPredicate() }, undefined: { get: () => new Predicate('undefined') }, null: { get: () => new Predicate('null') }, nullOrUndefined: { get: () => new Predicate('nullOrUndefined') }, nan: { get: () => new Predicate('nan') }, symbol: { get: () => new Predicate('symbol') }, array: { get: () => new ArrayPredicate() }, object: { get: () => new ObjectPredicate() }, date: { get: () => new DatePredicate() }, error: { get: () => new ErrorPredicate() }, map: { get: () => new MapPredicate() }, weakMap: { get: () => new WeakMapPredicate() }, set: { get: () => new SetPredicate() }, weakSet: { get: () => new WeakSetPredicate() }, function: { get: () => new Predicate('Function') }, buffer: { get: () => new Predicate('Buffer') }, regExp: { get: () => new Predicate('RegExp') }, promise: { get: () => new Predicate('Promise') }, typedArray: { get: () => new Predicate('TypedArray') }, int8Array: { get: () => new Predicate('Int8Array') }, uint8Array: { get: () => new Predicate('Uint8Array') }, uint8ClampedArray: { get: () => new Predicate('Uint8ClampedArray') }, int16Array: { get: () => new Predicate('Int16Array') }, uint16Array: { get: () => new Predicate('Uint16Array') }, int32Array: { get: () => new Predicate('Int32Array') }, uint32Array: { get: () => new Predicate('Uint32Array') }, float32Array: { get: () => new Predicate('Float32Array') }, float64Array: { get: () => new Predicate('Float64Array') }, arrayBuffer: { get: () => new Predicate('ArrayBuffer') }, dataView: { get: () => new Predicate('DataView') }, iterable: { get: () => new Predicate('Iterable') } }); export default main as Ow; export { BasePredicate, Predicate, AnyPredicate, StringPredicate, NumberPredicate, BooleanPredicate, ArrayPredicate, ObjectPredicate, DatePredicate, ErrorPredicate, MapPredicate, WeakMapPredicate, SetPredicate, WeakSetPredicate };