UNPKG

4.68 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 ow = (value, labelOrPredicate, predicate) => {
13 if (!base_predicate_1.isPredicate(labelOrPredicate) && typeof labelOrPredicate !== 'string') {
14 throw new TypeError(`Expected second argument to be a predicate or a string, got \`${typeof labelOrPredicate}\``);
15 }
16 if (base_predicate_1.isPredicate(labelOrPredicate)) {
17 // If the second argument is a predicate, infer the label
18 const stackFrames = callsites_1.default();
19 test_1.default(value, () => infer_label_1.inferLabel(stackFrames), labelOrPredicate);
20 return;
21 }
22 test_1.default(value, labelOrPredicate, predicate);
23};
24Object.defineProperties(ow, {
25 isValid: {
26 value: (value, predicate) => {
27 try {
28 ow(value, predicate);
29 return true;
30 }
31 catch (_a) {
32 return false;
33 }
34 }
35 },
36 create: {
37 value: (labelOrPredicate, predicate) => (value, label) => {
38 if (base_predicate_1.isPredicate(labelOrPredicate)) {
39 const stackFrames = callsites_1.default();
40 test_1.default(value, label !== null && label !== void 0 ? label : (() => infer_label_1.inferLabel(stackFrames)), labelOrPredicate);
41 return;
42 }
43 test_1.default(value, label !== null && label !== void 0 ? label : labelOrPredicate, predicate);
44 }
45 }
46});
47exports.default = predicates_1.default(modifiers_1.default(ow));
48var predicates_2 = require("./predicates");
49Object.defineProperty(exports, "StringPredicate", { enumerable: true, get: function () { return predicates_2.StringPredicate; } });
50Object.defineProperty(exports, "NumberPredicate", { enumerable: true, get: function () { return predicates_2.NumberPredicate; } });
51Object.defineProperty(exports, "BooleanPredicate", { enumerable: true, get: function () { return predicates_2.BooleanPredicate; } });
52Object.defineProperty(exports, "ArrayPredicate", { enumerable: true, get: function () { return predicates_2.ArrayPredicate; } });
53Object.defineProperty(exports, "ObjectPredicate", { enumerable: true, get: function () { return predicates_2.ObjectPredicate; } });
54Object.defineProperty(exports, "DatePredicate", { enumerable: true, get: function () { return predicates_2.DatePredicate; } });
55Object.defineProperty(exports, "ErrorPredicate", { enumerable: true, get: function () { return predicates_2.ErrorPredicate; } });
56Object.defineProperty(exports, "MapPredicate", { enumerable: true, get: function () { return predicates_2.MapPredicate; } });
57Object.defineProperty(exports, "WeakMapPredicate", { enumerable: true, get: function () { return predicates_2.WeakMapPredicate; } });
58Object.defineProperty(exports, "SetPredicate", { enumerable: true, get: function () { return predicates_2.SetPredicate; } });
59Object.defineProperty(exports, "WeakSetPredicate", { enumerable: true, get: function () { return predicates_2.WeakSetPredicate; } });
60Object.defineProperty(exports, "TypedArrayPredicate", { enumerable: true, get: function () { return predicates_2.TypedArrayPredicate; } });
61Object.defineProperty(exports, "ArrayBufferPredicate", { enumerable: true, get: function () { return predicates_2.ArrayBufferPredicate; } });
62Object.defineProperty(exports, "DataViewPredicate", { enumerable: true, get: function () { return predicates_2.DataViewPredicate; } });
63Object.defineProperty(exports, "AnyPredicate", { enumerable: true, get: function () { return predicates_2.AnyPredicate; } });
64var argument_error_1 = require("./argument-error");
65Object.defineProperty(exports, "ArgumentError", { enumerable: true, get: function () { return argument_error_1.ArgumentError; } });