@jaimermxd/logic-gates
Version:
A simple package introducing logic gates
31 lines (30 loc) • 1.21 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.xor = exports.XorGate = void 0;
const BaseGate_1 = require("./BaseGate");
const InvalidInputError_1 = require("../utils/InvalidInputError");
const product_1 = require("../utils/product");
class XorGate extends BaseGate_1.BaseGate {
setOutput() {
this.output = this.input.filter(Boolean).length % 2 === 1;
}
setTruthTable() {
this.truthTable.forEach((row, index) => this.truthTable[index].push(row.filter(Boolean).length % 2 === 1));
}
static getTruthTable(inputs = 2) {
if (inputs < 2)
throw new InvalidInputError_1.default("'inputs' must be greater than or equal to 2");
let table = product_1.default([false, true], inputs);
table.forEach((row, index) => table[index].push(row.filter(Boolean).length % 2 === 1));
return table;
}
checkInputValidity(input) {
if (input.length < 2)
throw new InvalidInputError_1.default("'input' must contain at least two values");
}
}
exports.XorGate = XorGate;
function xor(input) {
return XorGate.create(input).output;
}
exports.xor = xor;