Taming the Beast of Complex Expressions in Node.js with @shelf/evaluate-expressions

Vlad Holubiev
Published in
3 min readNov 16, 2023

--

A Developer’s Dilemma: Simplifying Complexity

Picture this: you’re deep in the trenches of developing a sophisticated application. Your latest challenge? Implementing a rule engine that’s as dynamic as it is intricate. The users of your application can craft their own rules, weaving together complex expressions like modern-day alchemists. You’re staring at your screen, the clock ticking, wondering how to decode this puzzle without turning your code into an incomprehensible labyrinth. This is where @shelf/evaluate-expressions enters the scene, like a knight in shining armor.

Unveiling the Hero: @shelf/evaluate-expressions

Imagine a tool so potent yet so elegant that it transforms the daunting task of evaluating multifaceted expressions into a walk in the park. @shelf/evaluate-expressions is that tool.

It's a Node.js library crafted by Andrii Bakanov for developers who dare to venture beyond the ordinary, enabling them to evaluate expressions brimming with rules and joiners effortlessly.

Discover the Magic on GitHub

Why Choose @shelf/evaluate-expressions?

Think of this library as your Swiss Army knife for scenarios where complexity is the norm. Whether you're architecting a rule engine, devising a decision-making system, or anything in between, @shelf/evaluate-expressions is your ally. It's perfect for data filtering, conditional UI rendering, and much more.

The Secret Sauce: How Does It Work?

At its core, @shelf/evaluate-expressions employs a recursive approach. Picture a skilled detective unraveling a mystery, delving deeper into each clue. This method is like that, but for code, efficiently dissecting and evaluating expressions, no matter how nested or intricate they are.

The Toolbox: Operators and Joiners

Equipped with versatile operators and joiners, this library is like a linguist fluent in the language of expressions. It understands:

  1. eq (equals)
  2. neq (not equals)
  3. contains
  4. not_contains

And it speaks in and & or joiners, ensuring that your expressions are interpreted just as intended.

Lightweight Yet Mighty

Imagine a tool so feather-light yet robust, carrying just under 3KB of bundle size. It’s like packing a powerful gadget that fits in your pocket. Crafted in TypeScript, it offers the finesse of static typing with enhanced readability and maintainability.

And when it comes to security, @shelf/evaluate-expressions is like a fortified castle, impervious to the common perils of dynamic code execution or web content manipulation. It sticks to its core mission – evaluating expressions safely and reliably.

Getting Started: A Simple Guide

Unleashing the power of @shelf/evaluate-expressions is as simple as defining an expression object with a joiner and an array of rules. Whether it’s an and or an or joiner, it weaves your rules into a coherent expression. Here’s a sneak peek:

import type {Expression} from '@shelf/evaluate-expressions';
import {evaluateExpression} from '@shelf/evaluate-expressions';

const expression: Expression = {
joiner: 'and',
rules: [
{
joiner: 'or',
rules: [
{
variableId: 'variable-id-c',
operator: 'neq',
value: 'c',
},
{
variableId: 'variable-id-b',
operator: 'eq',
value: 'b',
},
],
},
{
variableId: 'variable-id-b',
operator: 'not_contains',
value: 'some',
},
{
variableId: 'variable-id-a',
operator: 'contains',
value: 'a',
},
],
};

const variablesWithValue = [
{
id: 'variable-id-a',
value: 'some-a',
},
{
id: 'variable-id-b',
value: 'b',
},
{
id: 'variable-id-c',
value: 'c',
},
];

const result = evaluateExpression(expression, variablesWithValue);

console.log(result); // true

Wrapping Up: Your New Secret Weapon

@shelf/evaluate-expressions isn't just a tool; it's your secret weapon in the world of complex expression evaluation. It's lightweight, secure, and user-friendly, making it an invaluable addition to your developer toolkit. So next time you're faced with a formidable expression challenge, let @shelf/evaluate-expressions be your guide to a simpler, more efficient solution.

--

--