Thanks to visit codestin.com
Credit goes to github.com

Skip to content

boolia is a tiny, safe boolean-expression engine. It parses human-readable rules with dotted identifiers, comparisons, custom functions, and logical operators, then evaluates them against plain dict data.

Notifications You must be signed in to change notification settings

joaofreires/boolia-ts

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Boolia

A tiny, safe boolean-expression language with dotted paths, functions, and rule books for TypeScript.

Overview

Boolia is a lightweight expression language that allows you to evaluate complex boolean expressions with support for:

  • Dotted path notation for accessing nested object properties
  • Custom functions for extending evaluation capabilities
  • Rule books for organizing and managing collections of rules
  • Tag-based evaluation for conditional logic
  • Safe evaluation with proper error handling

Installation

npm install boolia

Quick Start

Basic Expressions

import { evaluate } from 'boolia';

// Simple boolean logic
evaluate('true and false or true'); // true

// Comparisons and membership
const ctx = { user: { age: 21, roles: ['admin', 'ops'] } };
evaluate("user.age >= 18 and 'admin' in user.roles", { context: ctx }); // true

Dotted Path Access

// Access nested object properties
const context = { house: { light: { on: false } } };
evaluate('house.light.on', { context }); // false

// Works with class instances and methods
class User {
  name = 'João';
  isActive() { return true; }
}

const ctx = { user: new User() };
evaluate('user.name and user.isActive', { context: ctx }); // true

Tag-Based Evaluation

const tags = new Set(['admin', 'premium']);
evaluate('admin and premium', { tags }); // true
evaluate('admin and guest', { tags }); // false

Custom Functions

Register your own functions to extend evaluation capabilities:

import { evaluate, DEFAULT_FUNCTIONS } from 'boolia';

// Register a custom function
DEFAULT_FUNCTIONS.register('starts_with', (subject, prefix) => 
  String(subject ?? '').startsWith(String(prefix ?? ''))
);

const ctx = { user: { name: 'João', email: '[email protected]' } };
const expr = "starts_with(user.name, 'Jo') and matches(user.email, '.*@acme.com')";
evaluate(expr, { context: ctx }); // true

Rule Books

Organize complex logic using rule books:

import { RuleBook, RuleGroup } from 'boolia';

const rules = new RuleBook();

// Add individual rules
rules.add('is_adult', 'user.age >= 18');
rules.add('has_discount', 'user.discount');
rules.add('is_premium', 'user.tier == "premium"');

// Create rule groups
rules.register('eligible_for_offer', 
  new RuleGroup('all', ['is_adult', 'has_discount'])
);

rules.register('priority_customer',
  new RuleGroup('any', ['is_premium', 'eligible_for_offer'])
);

// Evaluate rules
const ctx = { user: { age: 22, discount: true, tier: 'standard' } };
rules.evaluate('eligible_for_offer', { context: ctx }); // true
rules.evaluate('priority_customer', { context: ctx }); // true

Rule Groups

Rule groups allow you to combine multiple rules with different logic:

  • all mode: All rules must be true (AND logic)
  • any mode: At least one rule must be true (OR logic)
// Nested rule groups
const rules = new RuleBook();
rules.add('is_adult', 'user.age >= 18');
rules.add('has_payment', 'user.payment_method');
rules.add('is_vip', 'user.vip');

const eligibility = new RuleGroup('all', ['is_adult', 'has_payment']);
rules.register('can_purchase', new RuleGroup('any', [eligibility, 'is_vip']));

Serialization

Rule books can be serialized to/from JSON for persistence:

import { rulebookToJson, rulebookFromJson } from 'boolia';

const rules = new RuleBook();
rules.add('example', 'user.active');

// Serialize to JSON
const json = rulebookToJson(rules);

// Deserialize from JSON
const restored = rulebookFromJson(json);

Error Handling

Boolia provides specific error types for different scenarios:

import { 
  MissingVariableError, 
  RulebookError, 
  RulebookValidationError 
} from 'boolia';

try {
  evaluate('unknown_variable', { context: {} });
} catch (error) {
  if (error instanceof MissingVariableError) {
    console.log('Variable not found:', error.variable);
  }
}

API Reference

Core Functions

  • evaluate(expression, options?) - Evaluate a boolean expression
  • compileExpr(expression) - Compile an expression for reuse
  • compileRule(name, expression) - Compile a named rule

Rule Management

  • RuleBook - Container for rules and rule groups
  • RuleGroup - Logical grouping of rules with AND/OR semantics
  • Rule - Individual rule with name and expression

Options

The EvaluateOptions interface supports:

  • context?: any - Variable context for evaluation
  • tags?: Set<string> - Available tags for tag-based logic
  • missingPolicy?: MissingPolicy - How to handle missing variables

Contributing

This project uses:

  • TypeScript for type safety
  • Vitest for testing
  • ESLint for code quality
# Install dependencies
npm install

# Run tests
npm test

# Build the project
npm run build

License

MIT © João Freires

Links

About

boolia is a tiny, safe boolean-expression engine. It parses human-readable rules with dotted identifiers, comparisons, custom functions, and logical operators, then evaluates them against plain dict data.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published