npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

logi.js

v0.1.5

Published

logi.js is a JavaScript library for working with Boolean algebra, logical expressions, truth tables, the Quine-McCluskey algorithm, timing diagrams, and more.

Downloads

15

Readme

logi.js

logi.js is a JavaScript library for working with Boolean algebra, logical expressions, truth tables, the Quine-McCluskey algorithm, timing diagrams, and more.

Installation

NPM

$ npm install logi.js

CDN

<script src="https://cdn.jsdelivr.net/npm/logi.js/dist/logi.min.js"></script>

Usage

Parse a string

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 string
const exp = "(~A + B) * (C + D)"; // (!A | B), (A & B'), (A XOR B), etc.
// Define the variables
const A = new VAR('A');
const B = new VAR('B');
const C = new VAR('C');
const D = new VAR('D');
// Create a new parser
const parser = new Parser(exp, { A, B, C, D });
// Parse the expression
const tree = parser.parse();

console.log(tree); // AND(OR(NOT(A), B), OR(C, D))   <- Object
console.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)
const exp = "(A + B) * (C + D)";
const parser = new Parser(exp);
const tree = parser.parse();
console.log(tree.toString());

Create a logical expression object directly

import { VAR, NOT, AND, OR, XOR } from 'logi.js';
const exp = new AND(new XOR(new VAR('A'), new VAR('B')), new OR(new NOT(new VAR('C')), new VAR('D')));
console.log(exp.toString()); // (A ^ B) * ( ~C + D )

Calculate the expression

import { VAR, Parser } from 'logi.js';
// Calculate the expression
const exp = "(A OR B') & (~C | 1) ⋃ false"; // Support various formats
const variables = {
    A: new VAR('A', 1),
    B: new VAR('B', 0),
    C: new VAR('C', 1),
};
const parser = new Parser(exp, variables);
const tree = parser.parse();
console.log(tree.toString()); // ( ( A + ~B ) * ( ~C + 1 ) + 0 )
console.log(tree.calculate()); // 1

Truth Table

You can generate a truth table from the parsed object.

// Generate the truth table
const truthTable = new TruthTable(tree, { A, B, C, D });
const table = 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 }
// ]

Quine-McCluskey Algorithm

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_algorithm
const mt = [4, 8, 10, 11, 12, 15] // minterms
const dc = [9, 14] // don't care
const qmc = new QMC();
const result = qmc.solve(mt, dc); // Returns an array of strings

for (let i = 0; i < result.length; i++) {
  console.log(result[i].toString());
}
// ↓ Output
// A * ~B + A * C + B * ~C * ~D
// A * C + A * ~D + B * ~C * ~D

You can also simplify the expression directly from the string.

const exp = '~AB + ~B + ~BD';
const qmc = new QMC();
const result = qmc.solveFromExp(exp)
console.log(result[0].toString()); // ~A + ~B

Timing Diagram

You can create a timing diagram from a logical expression.

import { TimingDiagram } from 'logi.js';

const table = [
    { A: 0, B: 0, result: 0 },
    { A: 0, B: 1, result: 1 },
    { A: 1, B: 0, result: 1 },
    { A: 1, B: 1, result: 0 },
];
const aData = [0, 0, 1, 1, 0, 0, 1, 1];
const bData = [0, 0, 0, 0, 1, 1, 1, 1];

const timingDiagram = new TimingDiagram();
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  | ┃   |   ┃ |   ┃ |

Tokenizer

You can use the Tokenizer to get the tokens of a logical expression.

import { Tokenizer } from 'logi.js';

// There are many useful functions
const tokenizer = new Tokenizer(
    "(A + B) * (~C ^ D)",
    {
        andToken: "&",
        orToken: "|",
        notToken: "!",
        xorToken: "⊕",
        leftParenthesisToken: '{',
        rightParenthesisToken: '}'
    }
);
const tokens = tokenizer.getTokens();
console.log(tokens);
// ↓ Output
// [
//     '{', 'A', '|', 'B',
//     '}', '&', '{', '!',
//     'C', '⊕', 'D', '}'
// ]
console.log(tokens.join(' ')); // { A | B } & { ! C ⊕ D }

Converter

import { VAR, Parser, TruthTable, Converter } from 'logi.js';

const exp = "A + B"
const A = new VAR('A');
const B = new VAR('B');
const parser = new Parser(exp, { A, B });
const tree = parser.parse();
const truthTable = new TruthTable(tree, { A, B });
const table = truthTable.get()
// Create a new instance of the Converter class
const converter = new Converter();
// Get the true values of the truth table in binary
const trueBinaries = converter.getTrueBinaries(table);
console.log(trueBinaries);
// Get the false values of the truth table in binary
const falseBinaries = converter.getFalseBinaries(table);
console.log(falseBinaries);
// Get the true values of the truth table in decimal
const trueDecimals = converter.getTrueDecimals(table);
console.log(trueDecimals);
// Get the false values of the truth table in decimal
const falseDecimals = converter.getFalseDecimals(table);
console.log(falseDecimals);
// Convert binary string to decimal
const decimal = converter.binToDec('1010');
console.log(decimal);
// Convert decimal string to binary
const binary = converter.decToBin('10');
console.log(binary);