proplogic
v1.0.1
Published
Propositional logic expressions validator and parser.
Maintainers
Readme
proplogic
A lightweight JavaScript library for tokenizing, formatting, and validating propositional logic expressions.
Installation
npm install proplogicQuick Start
import { lexp } from 'proplogic';
lexp.validate('P∧Q'); // true
lexp.validate('P∧'); // false
lexp.validate('P & Q'); // trueAPI
lexp.validate(expression)
Validates whether a string is a well-formed propositional logic formula.
Parameters
expression{string}— the formula to validate.
Returns {boolean} — true if the formula is well-formed, false otherwise.
Whitespace is ignored. Invalid or unrecognized characters return false.
lexp.validate('P∧Q'); // true
lexp.validate('(P∨Q)→R'); // true
lexp.validate('¬(¬P)'); // true
lexp.validate('∧P'); // false
lexp.validate('P∧∧Q'); // false
lexp.validate(')(P)'); // falselexp.tokenize(expression)
Tokenizes a propositional logic expression into an array of typed tokens.
Parameters
expression{string}— the expression to tokenize.
Returns {Array<{type: string, value: string}>} — an array of token objects, or false if the expression contains invalid characters.
Throws Error - Expression can not be formatted.
Token types: operand, operator, not, open, close.
lexp.tokenize('P∧Q');
// [
// { type: 'operand', value: 'P' },
// { type: 'operator', value: '∧' },
// { type: 'operand', value: 'Q' }
// ]
lexp.tokenize('P & Q');
// [
// { type: 'operand', value: 'P' },
// { type: 'operator', value: '∧' }, ← alias normalized
// { type: 'operand', value: 'Q' }
// ]lexp.format(expression)
Normalizes an expression by stripping whitespace and replacing ASCII aliases with their canonical Unicode symbols.
Parameters
expression{string}— the expression to format.
Returns {string} — the normalized expression.
Throws Error — Expression can not te formatted.
lexp.format('P & Q'); // 'P∧Q'
lexp.format('P | Q'); // 'P∨Q'
lexp.format('!P > Q'); // '¬P→Q'
lexp.format('P = Q'); // 'P↔Q'confusablesTable
An exported object mapping ASCII aliases and Unicode lookalikes to their canonical logic symbols. Useful for displaying supported input formats to users.
import { confusablesTable } from 'proplogic';
console.log(confusablesTable);
// { 'V': '∨', 'v': '∨', '&': '∧', '^': '∧', '~': '¬', '!': '¬', ... }Supported Symbols
Operators
| Symbol | Meaning | ASCII Aliases |
|--------|---------|---------------|
| ∧ | Conjunction (AND) | &, ^, · |
| ∨ | Disjunction (OR) | v, V, \|, ∥ |
| → | Implication | >, ⇒, ⊃ |
| ↔ | Biconditional | =, ⇔, ⟺ |
| ↮ | Not Equals | ≢, ⇎ |
| ⊻ | Exclusive OR (XOR) | +, ⊕ |
| ⊽ | NAND | ↑ |
| ⊽ | NOR | ↓ |
| ¬ | Negation (NOT) | ~, ! |
Grouping
| Symbol | Meaning | Aliases |
|--------|---------|---------|
| ( | Open parenthesis | [, {, (, ﹙ |
| ) | Close parenthesis | ], }, ), ﹚ |
Operands
Any single character matching [A-Za-z] or falling in the Unicode Greek block (U+0370–U+03FF) is treated as an operand (e.g., P, q, α, φ).
Testing
npm run testTests are run with Bun. Make sure you have Bun installed before running them.
curl -fsSL https://bun.sh/install | bashContributing
Feel free to open a PR or create an issue at repository link.
Additions to the characters map or confusables are welcome, as well as code changes.
License
MIT
