@jaimermxd/logic-gates
Version:
A simple package introducing logic gates
31 lines (30 loc) • 1.15 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.and = exports.AndGate = void 0;
const BaseGate_1 = require("./BaseGate");
const InvalidInputError_1 = require("../utils/InvalidInputError");
const product_1 = require("../utils/product");
class AndGate extends BaseGate_1.BaseGate {
setOutput() {
this.output = this.input.every(Boolean);
}
setTruthTable() {
this.truthTable.forEach((row, index) => this.truthTable[index].push(row.every(Boolean)));
}
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.every(Boolean)));
return table;
}
checkInputValidity(input) {
if (input.length < 2)
throw new InvalidInputError_1.default("'input' must contain at least two values");
}
}
exports.AndGate = AndGate;
function and(input) {
return AndGate.create(input).output;
}
exports.and = and;