-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathindex.js
57 lines (50 loc) · 1.49 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import * as util from "./lib/util";
import * as specs from "./lib/spec";
import Spec from "./lib/spec/spec";
import { catImpl as cat, altImpl as alt } from "./lib/regex";
import * as predicates from "./lib/predicates";
import * as symbols from "./lib/symbols";
import get from "lodash.get";
import flatten from "lodash.flattendeep";
const specsAndPreds = Object.assign(
{
cat,
alt
},
specs,
predicates
);
export { specsAndPreds as spec, symbols as symbol };
export { valid, conform } from "./lib/util";
export { Spec as AbstractSpec };
export function explainData(spec, value) {
const problems = flatten(util.explain(spec, [], [], value)).filter(x => !!x);
return problems.map(problem => {
problem.predicateName = util.getName(problem.predicate);
return problem;
});
}
export function problemStr(problem) {
let str = `${problem.via.join(" → ")}: ${
problem.predicateName
} failed for ${JSON.stringify(
get(problem.value, problem.path, problem.value)
)}`;
if (problem.path.length > 0) {
str += ` at [${problem.path.join(", ")}]`;
}
return str + ".";
}
export function explain(spec, value) {
explainData(spec, value).forEach(problem => console.log(problemStr(problem))); // eslint-disable-line no-console
}
export function explainStr(spec, value) {
return explainData(spec, value)
.map(problem => problemStr(problem))
.join("\n");
}
export function assert(spec, value) {
if (!util.valid(spec, value)) {
throw new Error(explainStr(spec, value));
}
}