import {Predicate, Context} from './lib/predicates/predicate'; import {AnyPredicate} from './lib/predicates/any'; import {testSymbol} 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'; import {optional} from './lib/operators/optional'; /** * @hidden */ export type TypedArray = Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array; export interface Operators { /** * Make the value optional. */ readonly optional: Ow; } export interface Ow { /** * Test if the value matches the predicate. * * @param value Value to test. * @param predicate Predicate to test against. */ (value: T, predicate: Predicate): void; /** * Create a reusable validator. * * @param predicate Predicate used in the validator function. */ create(predicate: Predicate): (value: T) => void; /** * Test that the value matches at least one of the given predicates. */ any(p1: Predicate): Predicate; any(p1: Predicate, p2: Predicate): Predicate; any(p1: Predicate, p2: Predicate, p3: Predicate): Predicate; any(p1: Predicate, p2: Predicate, p3: Predicate, p4: Predicate): Predicate; any(p1: Predicate, p2: Predicate, p3: Predicate, p4: Predicate, p5: Predicate): Predicate; any(p1: Predicate, p2: Predicate, p3: Predicate, p4: Predicate, p5: Predicate, p6: Predicate): Predicate; any(p1: Predicate, p2: Predicate, p3: Predicate, p4: Predicate, p5: Predicate, p6: Predicate, p7: Predicate): Predicate; any(p1: Predicate, p2: Predicate, p3: Predicate, p4: Predicate, p5: Predicate, p6: Predicate, p7: Predicate, p8: Predicate): Predicate; any(p1: Predicate, p2: Predicate, p3: Predicate, p4: Predicate, p5: Predicate, p6: Predicate, p7: Predicate, p8: Predicate, p9: Predicate): Predicate; any(p1: Predicate, p2: Predicate, p3: Predicate, p4: Predicate, p5: Predicate, p6: Predicate, p7: Predicate, p8: Predicate, p9: Predicate, p10: Predicate): Predicate; any(...predicate: Predicate[]): Predicate; /** * 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>; } const ow = (context?: Context) => { const main = (value: T, predicate: Predicate | AnyPredicate) => (predicate as any)[testSymbol](value, main); Object.defineProperties(main, { create: { value: (predicate: Predicate) => (value: T) => main(value, predicate) }, any: { value: (...predicates: Predicate[]) => new AnyPredicate(predicates) }, optional: { get: () => optional(ow, context) }, string: { get: () => new StringPredicate(context) }, number: { get: () => new NumberPredicate(context) }, boolean: { get: () => new BooleanPredicate(context) }, undefined: { get: () => new Predicate('undefined', context) }, null: { get: () => new Predicate('null', context) }, nullOrUndefined: { get: () => new Predicate('nullOrUndefined', context) }, nan: { get: () => new Predicate('nan', context) }, symbol: { get: () => new Predicate('symbol', context) }, array: { get: () => new ArrayPredicate(context) }, object: { get: () => new ObjectPredicate(context) }, date: { get: () => new DatePredicate(context) }, error: { get: () => new ErrorPredicate(context) }, map: { get: () => new MapPredicate(context) }, weakMap: { get: () => new WeakMapPredicate(context) }, set: { get: () => new SetPredicate(context) }, weakSet: { get: () => new WeakSetPredicate(context) }, function: { get: () => new Predicate('function', context) }, buffer: { get: () => new Predicate('buffer', context) }, regExp: { get: () => new Predicate('regExp', context) }, promise: { get: () => new Predicate('promise', context) }, typedArray: { get: () => new Predicate('typedArray', context) }, int8Array: { get: () => new Predicate('int8Array', context) }, uint8Array: { get: () => new Predicate('uint8Array', context) }, uint8ClampedArray: { get: () => new Predicate('uint8ClampedArray', context) }, int16Array: { get: () => new Predicate('int16Array', context) }, uint16Array: { get: () => new Predicate('uint16Array', context) }, int32Array: { get: () => new Predicate('int32Array', context) }, uint32Array: { get: () => new Predicate('uint32Array', context) }, float32Array: { get: () => new Predicate('float32Array', context) }, float64Array: { get: () => new Predicate('float64Array', context) }, arrayBuffer: { get: () => new Predicate('arrayBuffer', context) }, dataView: { get: () => new Predicate('dataView', context) }, iterable: { get: () => new Predicate('iterable', context) } }); return main as Ow & Operators; }; export default ow();