UNPKG

5.24 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.ArgumentError = exports.AnyPredicate = exports.DataViewPredicate = exports.ArrayBufferPredicate = exports.TypedArrayPredicate = exports.WeakSetPredicate = exports.SetPredicate = exports.WeakMapPredicate = exports.MapPredicate = exports.ErrorPredicate = exports.DatePredicate = exports.ObjectPredicate = exports.ArrayPredicate = exports.BooleanPredicate = exports.NumberPredicate = exports.StringPredicate = exports.Predicate = void 0;
4const callsites_1 = require("callsites");
5const infer_label_1 = require("./utils/infer-label");
6const predicate_1 = require("./predicates/predicate");
7Object.defineProperty(exports, "Predicate", { enumerable: true, get: function () { return predicate_1.Predicate; } });
8const base_predicate_1 = require("./predicates/base-predicate");
9const modifiers_1 = require("./modifiers");
10const predicates_1 = require("./predicates");
11const test_1 = require("./test");
12const generate_stack_1 = require("./utils/generate-stack");
13const ow = (value, labelOrPredicate, predicate) => {
14 const stack = generate_stack_1.generateStackTrace();
15 if (!base_predicate_1.isPredicate(labelOrPredicate) && typeof labelOrPredicate !== 'string') {
16 throw new TypeError(`Expected second argument to be a predicate or a string, got \`${typeof labelOrPredicate}\``);
17 }
18 if (base_predicate_1.isPredicate(labelOrPredicate)) {
19 // If the second argument is a predicate, infer the label
20 const stackFrames = callsites_1.default();
21 test_1.default(value, () => infer_label_1.inferLabel(stackFrames), labelOrPredicate, stack);
22 return;
23 }
24 test_1.default(value, labelOrPredicate, predicate, stack);
25};
26Object.defineProperties(ow, {
27 isValid: {
28 value: (value, predicate) => {
29 try {
30 ow(value, predicate);
31 return true;
32 }
33 catch (_a) {
34 return false;
35 }
36 }
37 },
38 create: {
39 value: (labelOrPredicate, predicate) => (value, label) => {
40 const stack = generate_stack_1.generateStackTrace();
41 if (base_predicate_1.isPredicate(labelOrPredicate)) {
42 const stackFrames = callsites_1.default();
43 test_1.default(value, label !== null && label !== void 0 ? label : (() => infer_label_1.inferLabel(stackFrames)), labelOrPredicate, stack);
44 return;
45 }
46 test_1.default(value, label !== null && label !== void 0 ? label : (labelOrPredicate), predicate, stack);
47 }
48 }
49});
50// Can't use `export default predicates(modifiers(ow)) as Ow` because the variable needs a type annotation to avoid a compiler error when used:
51// Assertions require every name in the call target to be declared with an explicit type annotation.ts(2775)
52// See https://github.com/microsoft/TypeScript/issues/36931 for more details.
53const _ow = predicates_1.default(modifiers_1.default(ow));
54exports.default = _ow;
55var predicates_2 = require("./predicates");
56Object.defineProperty(exports, "StringPredicate", { enumerable: true, get: function () { return predicates_2.StringPredicate; } });
57Object.defineProperty(exports, "NumberPredicate", { enumerable: true, get: function () { return predicates_2.NumberPredicate; } });
58Object.defineProperty(exports, "BooleanPredicate", { enumerable: true, get: function () { return predicates_2.BooleanPredicate; } });
59Object.defineProperty(exports, "ArrayPredicate", { enumerable: true, get: function () { return predicates_2.ArrayPredicate; } });
60Object.defineProperty(exports, "ObjectPredicate", { enumerable: true, get: function () { return predicates_2.ObjectPredicate; } });
61Object.defineProperty(exports, "DatePredicate", { enumerable: true, get: function () { return predicates_2.DatePredicate; } });
62Object.defineProperty(exports, "ErrorPredicate", { enumerable: true, get: function () { return predicates_2.ErrorPredicate; } });
63Object.defineProperty(exports, "MapPredicate", { enumerable: true, get: function () { return predicates_2.MapPredicate; } });
64Object.defineProperty(exports, "WeakMapPredicate", { enumerable: true, get: function () { return predicates_2.WeakMapPredicate; } });
65Object.defineProperty(exports, "SetPredicate", { enumerable: true, get: function () { return predicates_2.SetPredicate; } });
66Object.defineProperty(exports, "WeakSetPredicate", { enumerable: true, get: function () { return predicates_2.WeakSetPredicate; } });
67Object.defineProperty(exports, "TypedArrayPredicate", { enumerable: true, get: function () { return predicates_2.TypedArrayPredicate; } });
68Object.defineProperty(exports, "ArrayBufferPredicate", { enumerable: true, get: function () { return predicates_2.ArrayBufferPredicate; } });
69Object.defineProperty(exports, "DataViewPredicate", { enumerable: true, get: function () { return predicates_2.DataViewPredicate; } });
70Object.defineProperty(exports, "AnyPredicate", { enumerable: true, get: function () { return predicates_2.AnyPredicate; } });
71var argument_error_1 = require("./argument-error");
72Object.defineProperty(exports, "ArgumentError", { enumerable: true, get: function () { return argument_error_1.ArgumentError; } });