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

@lusc/truth-table

v5.2.1

Published

@lusc/truth-table can parse truth statements and generate truth tables. [Try it out](https://melusc.github.io/truth-table).

Downloads

112

Readme

@lusc/truth-table

@lusc/truth-table can parse truth statements and generate truth tables. Try it out.

A statement such as A AND B passed to generateTable will generate a table as seen below.

| A | B | A ∧ B | | ----- | ----- | ----- | | true | true | true | | true | false | false | | false | true | false | | false | false | false |

Though generating tables is what this package was made for, it can easily be used to only parse a truth statement and generate an AST representation of the statement. See parseOperation.

Supported Operators

  • AND
  • OR
  • XOR
  • NOT
  • IFF
  • IFTHEN

See operator-aliases.md for all the aliases.

Variables

A variable is made up of at least one character. It must contain only latin letters (a-z). Variables are case-insensitive. and, or, and so on are reserved and will be treated as operators.

IndexedError

This extends Error and has from and to indicating were in the string there is an error. from is inclusive, to is exclusive. The message describes what the issue is and from and to can be used, for example, to highlight the problematic characters.

parseOperation('(a & b');
/*
IndexedError {
	message: "Unmatched opening parens at position 0.",
	from: 0,
	to: 1
}
*/

parseOperation

function parseOperation(input: string): AST;

If there are invalid characters, unmatched parens, two operators following each other or similar it throws IndexedError. For all other errors Error is thrown.

It returns AST.

operationToString

operationToString takes an AST as input and returns a string representation of the AST. It doesn't return the original input but instead returns a standardised form.

Example:

const ast = parseOperation('a & b');
operationToString(ast); // === "(A ∧ B)"

generateTable

function generateTable(input: string, includeSteps = true): ParsedTable;

type ParsedTable = {
	columns: readonly string[];
	rows: readonly boolean[][];
	ast: AST;
};

input is the operation that will be parsed.

By default, it will show each step to get to the result. For example with a statement like (a & b) | c, it will first contain the columns for the variables, then the column for A ∧ B, then the column for (A ∧ B) ∨ C.

If includeSteps is set to false, it will omit all steps and only return columns of the variables and the full statement.

The input is passed to parseOperation so it will throw the same errors.

Example:

generateTable('a != b');
// returns
{
	columns: ['A', 'B', 'A ↮ B'],
	rows: [
		[ true,  true,  false ],
		[ true,  false, true  ],
		[ false, true,  true  ],
		[ false, false, false ]
	],
	ast: {...}
}

Operator

This is an enum used to identify the type of operation.

It's implementation is pretty straightforward:

enum Operator {
	iff = 'iff',
	ifthen = 'ifthen',
	not = 'not',
	and = 'and',
	xor = 'xor',
	or = 'or',
}

AST

This is a representation of any given truth statement. The shape of the AST is as follows.

For variables

A variable is made of any latin letters. Variables are case insensitive. Additionally, _ is permitted.

A variable matches the regex /^[a-z_]+$/i.

type AST = {
	type: 'variable';
	variable: string;
};

Not-operator

type AST = {
	type: 'operator';
	operator: Operator.not;
	values: [AST];
};

Other operators

type AST = {
	type: 'operator';
	operator: Operator.and | Operator.or | ...; // All operators except Operator.not
	values: [AST, AST];
};