conditional-loom
v1.0.6
Published
A lightweight TypeScript library for composing and evaluating complex conditional logic with short-circuit evaluation.
Readme
Conditional Loom
A lightweight TypeScript library for composing and evaluating complex conditional logic with short-circuit evaluation.
Features
- 🎯 Compose complex conditions using
and,or, andnotoperators - ⚡ Short-circuit evaluation for better performance
- 🔒 Type-safe with full TypeScript support
- 🪶 Zero dependencies
- 📦 Small bundle size
Installation
npm install conditional-loomUsage
import { it, and, or, not } from "conditional-loom";
// Simple conditions
const isValid = it(() => someValue > 0);
// Complex nested conditions
const complexCondition = and(
or(
() => value > 0,
() => value < 100,
),
not(() => value === 50),
);
const result = it(complexCondition);Composing Conditions
You can compose complex conditions using the following operators:
and
Evaluates to true only if all conditions are true:
const condition = and(
() => value > 0,
() => value < 100,
);or
Evaluates to true if any condition is true:
const condition = or(
() => value < 40,
() => value > 60,
);not
Inverts the result of conditions:
const condition = not(() => value === 0);Short-Circuit Evaluation
The library implements short-circuit evaluation for better performance. For example, in an and operation, if any condition returns false, the remaining conditions won't be evaluated.
API Reference
it(...conditions: Predicate[]): boolean
The main function that evaluates conditions and returns a boolean result.
and(...conditions: Predicate[]): Predicate
Combines multiple conditions with logical AND.
or(...conditions: Predicate[]): Predicate
Combines multiple conditions with logical OR.
not(...conditions: Predicate[]): Predicate
Inverts the result of conditions.
Contributing
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
License
This project is licensed under the MIT License - see the LICENSE file for details.
Author
Anton Kochev
