New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

boolean-expression-solve

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

boolean-expression-solve

Boolean expression solver, simplifier, truth-table generator

latest
Source
npmnpm
Version
1.0.1
Version published
Weekly downloads
2
Maintainers
1
Weekly downloads
 
Created
Source

Boolean Expression Solver

npm version License: ISC GitHub issues

A powerful JavaScript library for solving, simplifying, and analyzing Boolean expressions. Generate truth tables, minimize expressions using the Quine-McCluskey algorithm, and validate Boolean logic with ease.

🚀 Features

  • Expression Simplification: Minimize Boolean expressions using advanced algorithms
  • Truth Table Generation: Create truth tables in both string and array formats
  • Variable Extraction: Automatically identify variables in expressions
  • Expression Validation: Check syntax and validity of Boolean expressions
  • Expression Evaluation: Evaluate expressions with specific variable values
  • Case Insensitive: Works with both uppercase and lowercase variables
  • Comprehensive Support: Handles complex nested expressions with parentheses

📦 Installation

npm install boolean-expression-solve

🎯 Quick Start

const { simplify, getTruthTable, getVariables, evaluate } = require('boolean-expression-solve');

// Simplify a Boolean expression
const simplified = simplify("A + A.B");
console.log(simplified); // Output: "A"

// Generate a truth table
const truthTable = getTruthTable("A + B", "string");
console.log(truthTable);
// Output:
// | A | B | A + B |
// |---|---|-------|
// | 0 | 0 |     0 |
// | 0 | 1 |     1 |
// | 1 | 0 |     1 |
// | 1 | 1 |     1 |

// Extract variables from expression
const variables = getVariables("(A + B').C");
console.log(variables); // Output: ['A', 'B', 'C']

// Evaluate expression with specific values
const result = evaluate("A + B", {A: 0, B: 1});
console.log(result); // Output: 1

📚 API Reference

simplify(expression)

Simplifies a Boolean expression to its minimal form using the Quine-McCluskey algorithm.

Parameters:

  • expression (string): The Boolean expression to simplify

Returns:

  • string: The simplified Boolean expression

Example:

simplify("A + A.B");           // Returns: "A"
simplify("(A+B).(A+B')");      // Returns: "A"
simplify("A.A' + B");          // Returns: "B"

getTruthTable(expression, type)

Generates a truth table for the given Boolean expression.

Parameters:

  • expression (string): The Boolean expression
  • type (string, optional): Output format - "string" or "array" (default: "string")

Returns:

  • string | Array: Formatted truth table string or array of objects

Example:

// String format
getTruthTable("A.B", "string");
// Returns formatted table string

// Array format  
getTruthTable("A.B", "array");
// Returns: [
//   { inputs: {A: 0, B: 0}, output: 0, expression: "A.B" },
//   { inputs: {A: 0, B: 1}, output: 0, expression: "A.B" },
//   { inputs: {A: 1, B: 0}, output: 0, expression: "A.B" },
//   { inputs: {A: 1, B: 1}, output: 1, expression: "A.B" }
// ]

getVariables(expression)

Extracts all unique variables from a Boolean expression.

Parameters:

  • expression (string): The Boolean expression to analyze

Returns:

  • Array<string>: Array of variable names sorted alphabetically

Example:

getVariables("A + B.C + A'");  // Returns: ['A', 'B', 'C']
getVariables("(X+Y').Z");      // Returns: ['X', 'Y', 'Z']

evaluate(expression, values)

Evaluates a Boolean expression for specific variable values.

Parameters:

  • expression (string): The Boolean expression to evaluate
  • values (Object): Object with variable names as keys and their values (0 or 1)

Returns:

  • number: The result of the expression (0 or 1)

Example:

evaluate("A + B", {A: 0, B: 1});     // Returns: 1
evaluate("A.B", {A: 1, B: 0});       // Returns: 0
evaluate("A'", {A: 1});              // Returns: 0

isValidExpression(expression)

Validates if a Boolean expression has correct syntax.

Parameters:

  • expression (string): The Boolean expression to validate

Returns:

  • boolean: True if expression is valid, false otherwise

Example:

isValidExpression("A + B");      // Returns: true
isValidExpression("A + + B");    // Returns: false

🔤 Supported Syntax

Variables

  • Uppercase letters: A, B, C, ..., Z
  • Case insensitive: a + b is treated as A + B

Operators

  • AND: . or implicit (e.g., AB = A.B)
  • OR: +
  • NOT: ' (postfix, e.g., A' means NOT A)

Constants

  • True: 1
  • False: 0

Grouping

  • Parentheses: ( and ) for grouping operations

Operator Precedence

  • NOT (') - Highest precedence
  • AND (.)
  • OR (+) - Lowest precedence

📝 Expression Examples

Input ExpressionSimplified OutputDescription
A + AAIdempotent law
A.A'0Complement law
A + A'1Complement law
A + A.BAAbsorption law
A.(A + B)AAbsorption law
(A+B).(A+B')ADistribution
A + 0AIdentity law
A.1AIdentity law
A + 11Domination law
A.00Domination law

🧪 Testing

Run the test suite to verify all functions work correctly:

npm test

The test suite includes:

  • Basic Boolean algebra laws
  • Complex expression simplification
  • Truth table generation
  • Variable extraction
  • Expression validation
  • Error handling
  • Performance tests

⚡ Performance

The library uses the Quine-McCluskey algorithm for optimal Boolean expression minimization. It can handle:

  • Small expressions (2-4 variables): Instantaneous
  • Medium expressions (5-8 variables): Under 100ms
  • Large expressions (9+ variables): Under 1 second

🤝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.

Development Setup

  • Clone the repository:
git clone https://github.com/prabhasha2006/boolean-expression-solve.git
cd boolean-expression-solve
  • Install dependencies:
npm install
  • Run tests:
npm test

📄 License

This project is licensed under the ISC License - see the LICENSE file for details.

🐛 Issues

Found a bug or have a feature request? Please create an issue on GitHub Issues.

👨‍💻 Author

K.Prabhasha

🙏 Acknowledgments

  • Quine-McCluskey algorithm for Boolean minimization
  • Boolean algebra principles and laws
  • Community feedback and contributions

Star this repository if it helped you!

Keywords

Boolean

FAQs

Package last updated on 15 Sep 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts