logi.js is a JavaScript library for working with Boolean algebra, logical expressions, truth tables, the Quine-McCluskey algorithm, timing diagrams, and more.
$ npm install logi.js<scriptsrc="https://cdn.jsdelivr.net/npm/logi.js/dist/logi.min.js"></script>The most basic usage is to define a logical expression as a string and parse it to get an object.
import{VAR,Parser}from'logi.js';// If you are using CDN// const { VAR, Parser } = logi;// Define the expression as a stringconstexp="(~A + B) * (C + D)";// (!A | B), (A & B'), (A XOR B), etc.// Define the variablesconstA=newVAR('A');constB=newVAR('B');constC=newVAR('C');constD=newVAR('D');// Create a new parserconstparser=newParser(exp,{ A, B, C, D });// Parse the expressionconsttree=parser.parse();console.log(tree);// AND(OR(NOT(A), B), OR(C, D)) <- Objectconsole.log(tree.toString());// ( ~A + B ) * ( C + D )console.log(tree.toObjectString());// new AND(new OR(new NOT(A), B), new OR(C, D))console.log(tree.toTex());// ( \overline{A} + B ) \cdot ( C + D )Alternatively, you can use the Parser without specifying variables.
import{Parser}from'logi.js';// const { Parser } = logi; (CDN)constexp="(A + B) * (C + D)";constparser=newParser(exp);consttree=parser.parse();console.log(tree.toString());import{VAR,NOT,AND,OR,XOR}from'logi.js';constexp=newAND(newXOR(newVAR('A'),newVAR('B')),newOR(newNOT(newVAR('C')),newVAR('D')));console.log(exp.toString());// (A ^ B) * ( ~C + D )import{VAR,Parser}from'logi.js';// Calculate the expressionconstexp="(A OR B') & (~C | 1) ⋃ false";// Support various formatsconstvariables={A: newVAR('A',1),B: newVAR('B',0),C: newVAR('C',1),};constparser=newParser(exp,variables);consttree=parser.parse();console.log(tree.toString());// ( ( A + ~B ) * ( ~C + 1 ) + 0 )console.log(tree.calculate());// 1You can generate a truth table from the parsed object.
// Generate the truth tableconsttruthTable=newTruthTable(tree,{ A, B, C, D });consttable=truthTable.get();console.log(table);// ↓ Output// [// { A: 0, B: 0, C: 0, D: 0, result: 0 },// { A: 0, B: 0, C: 0, D: 1, result: 1 },// { A: 0, B: 0, C: 1, D: 0, result: 1 },// { A: 0, B: 0, C: 1, D: 1, result: 1 },// { A: 0, B: 1, C: 0, D: 0, result: 0 },// { A: 0, B: 1, C: 0, D: 1, result: 1 },// { A: 0, B: 1, C: 1, D: 0, result: 1 },// { A: 0, B: 1, C: 1, D: 1, result: 1 },// { A: 1, B: 0, C: 0, D: 0, result: 0 },// { A: 1, B: 0, C: 0, D: 1, result: 0 },// { A: 1, B: 0, C: 1, D: 0, result: 0 },// { A: 1, B: 0, C: 1, D: 1, result: 0 },// { A: 1, B: 1, C: 0, D: 0, result: 0 },// { A: 1, B: 1, C: 0, D: 1, result: 1 },// { A: 1, B: 1, C: 1, D: 0, result: 1 },// { A: 1, B: 1, C: 1, D: 1, result: 1 }// ]You can use the Quine-McCluskey algorithm to simplify the logical expression.
import{QMC}from'logi.js';// Quine-McCluskey Algorithm// Wikipedia: https://en.wikipedia.org/wiki/Quine–McCluskey_algorithmconstmt=[4,8,10,11,12,15]// mintermsconstdc=[9,14]// don't careconstqmc=newQMC();constresult=qmc.solve(mt,dc);// Returns an array of stringsfor(leti=0;i<result.length;i++){console.log(result[i].toString());}// ↓ Output// A * ~B + A * C + B * ~C * ~D// A * C + A * ~D + B * ~C * ~DYou can also simplify the expression directly from the string.
constexp='~AB + ~B + ~BD';constqmc=newQMC();constresult=qmc.solveFromExp(exp)console.log(result[0].toString());// ~A + ~BYou can create a timing diagram from a logical expression.
import{TimingDiagram}from'logi.js';consttable=[{A: 0,B: 0,result: 0},{A: 0,B: 1,result: 1},{A: 1,B: 0,result: 1},{A: 1,B: 1,result: 0},];constaData=[0,0,1,1,0,0,1,1];constbData=[0,0,0,0,1,1,1,1];consttimingDiagram=newTimingDiagram();timingDiagram.setData(table,aData,bData);console.log(timingDiagram.getOutput());// [ 0, 0, 1, 1, 1, 1, 0, 0 ]timingDiagram.draw()// ↓ Output// Time | @ | B | A |// ------------------------// 0 | ┃ | ┃ | ┃ |// 1 | ┃ | ┃ | ┃ |// 2 | ‾┃ | ┃ | ‾┃ |// 3 | ┃ | ┃ | ┃ |// 4 | ┃ | ‾┃ | ┃‾ |// 5 | ┃ | ┃ | ┃ |// 6 | ┃‾ | ┃ | ‾┃ |// 7 | ┃ | ┃ | ┃ |You can use the Tokenizer to get the tokens of a logical expression.
import{Tokenizer}from'logi.js';// There are many useful functionsconsttokenizer=newTokenizer("(A + B) * (~C ^ D)",{andToken: "&",orToken: "|",notToken: "!",xorToken: "⊕",leftParenthesisToken: '{',rightParenthesisToken: '}'});consttokens=tokenizer.getTokens();console.log(tokens);// ↓ Output// [// '{', 'A', '|', 'B',// '}', '&', '{', '!',// 'C', '⊕', 'D', '}'// ]console.log(tokens.join(' '));// { A | B } & { ! C ⊕ D }import{VAR,Parser,TruthTable,Converter}from'logi.js';constexp="A + B"constA=newVAR('A');constB=newVAR('B');constparser=newParser(exp,{ A, B });consttree=parser.parse();consttruthTable=newTruthTable(tree,{ A, B });consttable=truthTable.get()// Create a new instance of the Converter classconstconverter=newConverter();// Get the true values of the truth table in binaryconsttrueBinaries=converter.getTrueBinaries(table);console.log(trueBinaries);// Get the false values of the truth table in binaryconstfalseBinaries=converter.getFalseBinaries(table);console.log(falseBinaries);// Get the true values of the truth table in decimalconsttrueDecimals=converter.getTrueDecimals(table);console.log(trueDecimals);// Get the false values of the truth table in decimalconstfalseDecimals=converter.getFalseDecimals(table);console.log(falseDecimals);// Convert binary string to decimalconstdecimal=converter.binToDec('1010');console.log(decimal);// Convert decimal string to binaryconstbinary=converter.decToBin('10');console.log(binary);