1 | import callsites from 'callsites';
|
2 | import { inferLabel } from './utils/infer-label.js';
|
3 | import { isPredicate } from './predicates/base-predicate.js';
|
4 | import modifiers from './modifiers.js';
|
5 | import predicates from './predicates.js';
|
6 | import test from './test.js';
|
7 | const ow = (value, labelOrPredicate, predicate) => {
|
8 | if (!isPredicate(labelOrPredicate) && typeof labelOrPredicate !== 'string') {
|
9 | throw new TypeError(`Expected second argument to be a predicate or a string, got \`${typeof labelOrPredicate}\``);
|
10 | }
|
11 | if (isPredicate(labelOrPredicate)) {
|
12 |
|
13 | const stackFrames = callsites();
|
14 | test(value, () => inferLabel(stackFrames), labelOrPredicate);
|
15 | return;
|
16 | }
|
17 | test(value, labelOrPredicate, predicate);
|
18 | };
|
19 | Object.defineProperties(ow, {
|
20 | isValid: {
|
21 | value(value, predicate) {
|
22 | try {
|
23 | test(value, '', predicate);
|
24 | return true;
|
25 | }
|
26 | catch {
|
27 | return false;
|
28 | }
|
29 | },
|
30 | },
|
31 | create: {
|
32 | value: (labelOrPredicate, predicate) => (value, label) => {
|
33 | if (isPredicate(labelOrPredicate)) {
|
34 | const stackFrames = callsites();
|
35 | test(value, label ?? (() => inferLabel(stackFrames)), labelOrPredicate);
|
36 | return;
|
37 | }
|
38 | test(value, label ?? (labelOrPredicate), predicate);
|
39 | },
|
40 | },
|
41 | });
|
42 |
|
43 |
|
44 |
|
45 | const _ow = predicates(modifiers(ow));
|
46 | export default _ow;
|
47 | export * from './predicates.js';
|
48 | export { ArgumentError } from './argument-error.js';
|
49 | export { Predicate } from './predicates/predicate.js';
|