UNPKG

2.96 kBJavaScriptView Raw
1function is(v) {
2 return typeof v === "boolean";
3}
4
5function imply(a, b) {
6 return !a || b;
7}
8
9function nimply(a, b) {
10 return !imply(a, b);
11}
12
13const RNUM = /^[-+]?\d+$/;
14const RTRU = /\b(?:t|y|1)\b|\b(?:\+|ay|go|on|up)|(?:tru|acc|asc|day|for|hot|inc|joy|new|pos|top|win|yes|dawn|full|safe|grow|high|just|real|some|know|live|love|open|pure|shin|warm|wis[de]|activ|admit|advan|agree|begin|brigh|build|creat|early|enter|float|f(?:i|ou)nd|grant|light|north|prett|prese|publi|start|succe|victr)/gi;
15const RFAL = /\b(?:f|n|0)\b|(?:fal|off|dim|end|low|old|back|cold|cool|dark|dead|decr|desc|dirt|down|dull|dusk|exit|late|sink|ugly|absen|botto|close|finis|night|priva|south|wrong)/gi;
16const RNEG = /\b(?:-|na|no|un|in|aft|bad|dis|lie|non|ben[dt]|den[iy]|empt|fail|fake|hate|los[es]|stop|decli|defea|destr|never|negat|refus|rejec|forget|shr[iu]nk|against|is.?nt|can.?(?:no)?t)|(?:hind)/gi;
17function parse(s) {
18 if (RNUM.test(s))
19 return parseInt(s, 10) > 0;
20 var t = s.search(RTRU) >= 0;
21 var f = s.search(RFAL) >= 0;
22 var n = (s.match(RNEG) || []).length % 2 === 1;
23 return nimply(f, t) === n;
24}
25
26function not(a) {
27 return !a;
28}
29
30function eq(a, b) {
31 return a === b;
32}
33
34function neq(a, b) {
35 return !eq(a, b);
36}
37
38function imp(a, b) {
39 return imply(a, b);
40}
41
42function eqv(a, b) {
43 return eq(a, b);
44}
45
46function and(a = true, b = true, c = true, d = true, e = true, f = true, g = true, h = true) {
47 return a && b && c && d && e && f && g && h;
48}
49
50function nand(a = true, b = true, c = true, d = true, e = true, f = true, g = true, h = true) {
51 return !and(a, b, c, d, e, f, g, h);
52}
53
54function or(a = false, b = false, c = false, d = false, e = false, f = false, g = false, h = false) {
55 return a || b || c || d || e || f || g || h;
56}
57
58function nor(a = false, b = false, c = false, d = false, e = false, f = false, g = false, h = false) {
59 return !or(a, b, c, d, e, f, g, h);
60}
61
62function xor(a = false, b = false, c = false, d = false, e = false, f = false, g = false, h = false) {
63 return a !== b !== c !== d !== e !== f !== g !== h;
64}
65
66function xnor(a = false, b = false, c = false, d = false, e = false, f = false, g = false, h = false) {
67 return !xor(a, b, c, d, e, f, g, h);
68}
69
70function count(a = false, b = false, c = false, d = false, e = false, f = false, g = false, h = false) {
71 return (a ? 1 : 0) + (b ? 1 : 0) + (c ? 1 : 0) + (d ? 1 : 0) +
72 (e ? 1 : 0) + (f ? 1 : 0) + (g ? 1 : 0) + (h ? 1 : 0);
73}
74
75function select(i, a = false, b = false, c = false, d = false, e = false, f = false, g = false, h = false) {
76 switch (i) {
77 case 0: return a;
78 case 1: return b;
79 case 2: return c;
80 case 3: return d;
81 case 4: return e;
82 case 5: return f;
83 case 6: return g;
84 case 7: return h;
85 default: return false;
86 }
87}
88
89export { and, count, eq, eqv, imp, imply, is, nand, neq, nimply, nor, not, or, parse, select, xnor, xor };