boolean-expression-solve
Version:
Boolean expression solver, simplifier, truth-table generator
256 lines (186 loc) โข 7.14 kB
Markdown
# Boolean Expression Solver
[](https://www.npmjs.com/package/boolean-expression-solve)
[](https://opensource.org/licenses/ISC)
[](https://github.com/prabhasha2006/boolean-expression-solve/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
```bash
npm install boolean-expression-solve
```
## ๐ฏ Quick Start
```javascript
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:**
```javascript
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:**
```javascript
// 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:**
```javascript
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:**
```javascript
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:**
```javascript
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
1. **NOT** (`'`) - Highest precedence
2. **AND** (`.`)
3. **OR** (`+`) - Lowest precedence
## ๐ Expression Examples
| Input Expression | Simplified Output | Description |
|------------------|------------------|-------------|
| `A + A` | `A` | Idempotent law |
| `A.A'` | `0` | Complement law |
| `A + A'` | `1` | Complement law |
| `A + A.B` | `A` | Absorption law |
| `A.(A + B)` | `A` | Absorption law |
| `(A+B).(A+B')` | `A` | Distribution |
| `A + 0` | `A` | Identity law |
| `A.1` | `A` | Identity law |
| `A + 1` | `1` | Domination law |
| `A.0` | `0` | Domination law |
## ๐งช Testing
Run the test suite to verify all functions work correctly:
```bash
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
1. Clone the repository:
```bash
git clone https://github.com/prabhasha2006/boolean-expression-solve.git
cd boolean-expression-solve
```
2. Install dependencies:
```bash
npm install
```
3. Run tests:
```bash
npm test
```
## ๐ License
This project is licensed under the ISC License - see the [LICENSE](LICENSE) file for details.
## ๐ Issues
Found a bug or have a feature request? Please create an issue on [GitHub Issues](https://github.com/prabhasha2006/boolean-expression-solve/issues).
## ๐จโ๐ป Author
**K.Prabhasha**
- GitHub: [@prabhasha2006](https://github.com/prabhasha2006)
## ๐ Acknowledgments
- Quine-McCluskey algorithm for Boolean minimization
- Boolean algebra principles and laws
- Community feedback and contributions
---
โญ **Star this repository if it helped you!**