UNPKG

1.43 kBJavaScriptView Raw
1const {
2 Expr,
3 Gte,
4 Gt,
5 Or,
6 And,
7 Not,
8 Ne,
9 Lte,
10 Lt,
11 Eq,
12 Token,
13 Expression,
14 SetterExpression,
15 TopLevel,
16 Root,
17 Get,
18 Clone,
19 WrappedPrimitive,
20 TokenTypeData,
21 SourceTag,
22 cloneToken
23 } = require('./lang');
24
25const MathInverseMap = {
26 eq: Ne,
27 ne: Eq,
28 gt: Lte,
29 gte: Lt,
30 lt: Gte,
31 lte: Gt
32}
33
34const LogicInverseMap = {
35 and: Or,
36 or: And
37}
38
39function or(...conds) {
40 conds = conds.filter(t => t !== false)
41 if (conds.some(t => t === true)) {
42 return true;
43 }
44 if (conds.length === 0) {
45 return false;
46 }
47 if (conds.length === 1) {
48 return conds[0]
49 }
50 return Expr(Or, ...conds);
51 }
52
53 function and(...conds) {
54 conds = conds.filter(t => t !== true)
55 if (conds.some(t => t === false)) {
56 return false;
57 }
58 if (conds.length === 0) {
59 return true;
60 }
61 if (conds.length === 1) {
62 return conds[0]
63 }
64 return Expr(And, ...conds);
65 }
66
67 function not(cond) {
68 if (typeof cond === 'boolean') {
69 return !cond;
70 }
71 const tokenType = cond[0].$type;
72 if (MathInverseMap[tokenType]) {
73 return Expr(MathInverseMap[tokenType], ...cond.slice(1))
74 }
75 if (LogicInverseMap[tokenType]) {
76 return Expr(LogicInverseMap[tokenType], ...cond.slice(1).map(not))
77 }
78 return Expr(Not, cond);
79 }
80
81 module.exports = {or, and, not}
\No newline at end of file