Skip to content

arraypress/condition-evaluator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@arraypress/condition-evaluator

Evaluate rule conditions against data objects. Supports nested field access, 17+ operators, AND/OR logic. Zero dependencies.

Works in Node.js, Cloudflare Workers, Deno, Bun, and browsers.

Install

npm install @arraypress/condition-evaluator

Functions

evaluate(conditions, data, options?)

Evaluate an array of conditions against a data object.

import { evaluate } from '@arraypress/condition-evaluator';

// AND logic (default) — all conditions must pass
evaluate([
  { field: 'order.amount', operator: '>=', value: 5000 },
  { field: 'customer.country', operator: 'in', value: ['US', 'CA'] },
], { order: { amount: 7500 }, customer: { country: 'US' } });
// => true

// OR logic — any condition must pass
evaluate([
  { field: 'status', operator: '==', value: 'vip' },
  { field: 'total_spent', operator: '>=', value: 10000 },
], { status: 'regular', total_spent: 15000 }, { match: 'any' });
// => true

// Empty conditions always return true
evaluate([], data); // => true

evaluateCondition(value, operator, expected)

Evaluate a single condition.

import { evaluateCondition } from '@arraypress/condition-evaluator';

evaluateCondition(100, '>=', 50)                      // true
evaluateCondition('US', 'in', ['US', 'CA', 'GB'])     // true
evaluateCondition('hello world', 'contains', 'world') // true
evaluateCondition(5, 'between', [1, 10])               // true
evaluateCondition('test@gmail.com', 'ends_with', '@gmail.com') // true

evaluateDetailed(conditions, data)

Returns per-condition results for debugging or UI display.

import { evaluateDetailed } from '@arraypress/condition-evaluator';

evaluateDetailed([
  { field: 'age', operator: '>=', value: 18 },
  { field: 'country', operator: '==', value: 'US' },
], { age: 25, country: 'GB' });
// => [
//   { field: 'age', operator: '>=', value: 18, actualValue: 25, passed: true },
//   { field: 'country', operator: '==', value: 'US', actualValue: 'GB', passed: false },
// ]

resolveField(data, field)

Resolve a dotted field path from nested data.

import { resolveField } from '@arraypress/condition-evaluator';

resolveField({ customer: { country: 'US' } }, 'customer.country') // 'US'
resolveField({ items: ['a', 'b'] }, 'items.0')                    // 'a'
resolveField({ a: null }, 'a.b.c')                                // undefined

OPERATORS

Array of all supported operator strings.

Operators

Operator Aliases Description
== =, equals Loose equality
!= not_equals Not equal
=== strict_equals Strict equality
> gt Greater than
>= gte Greater than or equal
< lt Less than
<= lte Less than or equal
in Value in array
not_in Value not in array
contains String contains substring
not_contains String does not contain
starts_with String starts with
ends_with String ends with
matches Regex match
exists Non-null and non-empty
not_exists Null, undefined, or empty
between Number in range (inclusive)

Use Cases

  • Workflow engines (when order amount > $50, tag customer as VIP)
  • Discount rules (10% off for US customers who spent > $100)
  • Content filtering (show section only if user is in segment)
  • Automation triggers (fire webhook when condition met)
  • Form validation (custom field rules)

License

MIT

About

Evaluate rule conditions against data objects. 17+ operators, nested fields, AND/OR logic

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors