extra-boolean
Version:
Boolean data type has two possible truth values to represent logic.
86 lines (83 loc) • 3.12 kB
JavaScript
;
function is(v) {
return typeof v === "boolean";
}
const RNUMBER = /^[-+]?\d+$/;
const RTRUE = /\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;
const RFALSE = /\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;
const RNEGATE = /\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;
function parse(s) {
if (RNUMBER.test(s))
return parseInt(s, 10) > 0;
var t = s.search(RTRUE) >= 0;
var f = s.search(RFALSE) >= 0;
var n = (s.match(RNEGATE) || []).length % 2 === 1;
return nimply(f, t) === n;
}
function not(a) {
return !a;
}
function imply(a, b) {
return !a || b;
}
function nimply(a, b) {
return !imply(a, b);
}
function eq(a, b) {
return a === b;
}
function neq(a, b) {
return !eq(a, b);
}
function and(a = true, b = true, c = true, d = true, e = true, f = true, g = true, h = true) {
return a && b && c && d && e && f && g && h;
}
function nand(a = true, b = true, c = true, d = true, e = true, f = true, g = true, h = true) {
return !and(a, b, c, d, e, f, g, h);
}
function or(a = false, b = false, c = false, d = false, e = false, f = false, g = false, h = false) {
return a || b || c || d || e || f || g || h;
}
function nor(a = false, b = false, c = false, d = false, e = false, f = false, g = false, h = false) {
return !or(a, b, c, d, e, f, g, h);
}
function xor(a = false, b = false, c = false, d = false, e = false, f = false, g = false, h = false) {
return a !== b !== c !== d !== e !== f !== g !== h;
}
function xnor(a = false, b = false, c = false, d = false, e = false, f = false, g = false, h = false) {
return !xor(a, b, c, d, e, f, g, h);
}
function count(a = false, b = false, c = false, d = false, e = false, f = false, g = false, h = false) {
return (a ? 1 : 0) + (b ? 1 : 0) + (c ? 1 : 0) + (d ? 1 : 0) +
(e ? 1 : 0) + (f ? 1 : 0) + (g ? 1 : 0) + (h ? 1 : 0);
}
function select(i, a = false, b = false, c = false, d = false, e = false, f = false, g = false, h = false) {
switch (i) {
case 0: return a;
case 1: return b;
case 2: return c;
case 3: return d;
case 4: return e;
case 5: return f;
case 6: return g;
case 7: return h;
default: return false;
}
}
exports.and = and;
exports.count = count;
exports.eq = eq;
exports.eqv = eq;
exports.imp = imply;
exports.imply = imply;
exports.is = is;
exports.nand = nand;
exports.neq = neq;
exports.nimply = nimply;
exports.nor = nor;
exports.not = not;
exports.or = or;
exports.parse = parse;
exports.select = select;
exports.xnor = xnor;
exports.xor = xor;