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

truth-table-ast

v1.0.7

Published

Formatters for truth tables

Downloads

21

Readme

Truth Table

license-info stars-infoa Last-Comitt

Comitts Year reposize-info

🔍 | What can this do?

This project is based on AST (Abstract Syntax Tree). It can both generate the AST and validate the syntax of the input, transforming it into a consumable data structure to generate any type of representation, such as CSV tables. In case of an error, it is possible to handle the error, allowing the display of the line and column where a syntax error is present. Below is an example of how to work with this library:

💫 Main Features:

  • Syntax validation and error location reporting
  • Abstract Syntax Tree (AST) generation
  • Support for generating truth tables from parsed logic expressions
  • Flexible output formats (e.g., CSV)

🚀 | Getting Started

To get a local copy up and running, follow these simple steps:

Prerequisites Node.js (v20+ recommended)

NPM (recommended)

Installation

npm i truth-table-ast

📚 | Usage

After installing this package globally, you can use the following commands:

📟 | Terminal:

tt -p "p ˅ (p ^ q)"
# Or use this if your operating system doesn't detect the package
npx truth-table -p "p ˅ (p ^ q)" -o table.csv

📄 | Help:

Usage: ttt [options]

  Options:

   -h --help        Show all available arguments
   -o --output      File where the truth table will be saved
   -d --display     How the data will be displayed in the table, supports: boolean, number
   -t --type        Type of file the table will be saved in (csv | text)
   -p --proposition Define proposition to generate truth table

👨‍💻 | Code:

Here's an example of how to work with this library:

// ESM:
import { AST, Structure, Table } from 'truth-table-ast'
// Communjs:
// const { AST, Structure, Table } = require('truth-table-ast')

// In communjs, you'll need to put the code in an anonymous function for the asynchronous functions to work
// void (async () => {
  // code here
// })()

const input = 'p ^ (p v ~q)'
const parser = new AST(input)
const ast = parser.parse()

if (AST.isError(ast)) throw new Error(JSON.stringify(ast, null, 2))
await parser.save('ast.json')

const structure = new Structure(ast).generate()
await structure.save('structure.json')

const table = new Table({
  structure,
  display: 'boolean',
  // type: 'csv'
})//.create('table.csv')

// const content = table.csv()
// const content = table.markdown()

table.type = 'markdown'
await table.create('table.md')

table.type = 'csv'
await table.create('table.csv')

✨ | Outputs

📜 | AST:

The AST (Abstract Syntax Tree) generated from the input of p ^ (p v ~q):

[
  {
    "value": "p",
    "type": "Proposition",
    "negatived": false,
    "loc": {
      "start": {
        "line": 1,
        "column": 0
      },
      "end": {
        "line": 1,
        "column": 0
      }
    }
  },
  {
    "type": "Operation",
    "value": "^",
    "key": "Conjunction",
    "loc": {...}
  },
  {
    "type": "SubExpression",
    "body": [
      {
        "value": "p",
        "type": "Proposition",
        "negatived": false,
        "loc": {...}
      },
      {
        "type": "Operation",
        "value": "v",
        "key": "Disjunction",
        "loc": {...}
      },
      {
        "value": "q",
        "type": "Proposition",
        "negatived": true,
        "loc": {...}
      }
    ],
    "negatived": false,
    "loc": {...}
  }
]

📃 | Structure:

The structured data generated from the AST to generate the truth table:

[
  {
    "type": "Variable",
    "element": "p",
    "value": true,
    "column": 0,
    "row": 0,
    "position": "0x0"
  },
  {
    "type": "Variable",
    "element": "q",
    "value": true,
    "column": 1,
    "row": 0,
    "position": "0x1"
  },
  {
    "type": "VariableNegative",
    "element": "~q",
    "value": false,
    "column": 2,
    "row": 0,
    "position": "0x2"
  },
  {
    "type": "Result",
    "element": "(p v ~q)",
    "value": true,
    "column": 3,
    "row": 0,
    "position": "0x3"
  },
  {
    "type": "Result",
    "element": "p ^ (p v ~q)",
    "value": true,
    "column": 4,
    "row": 0,
    "position": "0x4"
  },
  {...}
]

📋 | Truth Table:

| p | q | ~q | (p v ~q) |p ^ (p v ~q)| |------------|------------|------------|------------|------------| | true | true | false | true | true | | true | false | true | true | true | | false | true | false | false | false | | false | false | true | true | false |

🤝 | Contributing

Contributions are what make the open-source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

📝 | License

Distributed under the MIT License. See LICENSE.txt for more information.