Skip to content

Latest commit

 

History

History
479 lines (319 loc) · 17.8 KB

File metadata and controls

479 lines (319 loc) · 17.8 KB

Expression Packs Reference

This document provides a comprehensive overview of all expression packs available in JSON Expressions and the expressions they contain.

Quick Start: Most users should start with the basePack which includes the most commonly used expressions. Add additional packs as needed for your specific use cases.

Pack Selection Guide

Choose packs based on your application's needs:

Available Packs

basePack - Essential Expressions

Use case: Essential expressions for most applications. Includes data access, basic conditionals, comparisons, boolean logic, and common array operations.

When to use: Start here for almost all applications. This pack covers 80% of common use cases.

import { createExpressionEngine, basePack } from "json-expressions";
const engine = createExpressionEngine({ packs: [basePack] });

Data Access

  • $get - Access object properties by path (supports dot notation, wildcards, array paths)
  • $prop - Fast simple property access (2.5x faster than $get, no path features)
  • $identity - Return input unchanged (identity function)

Conditionals

  • $case - Multi-branch conditional with literal and predicate support
  • $if - Simple if-then-else conditional

Comparisons

  • $eq - Test equality
  • $gt - Test greater than
  • $gte - Test greater than or equal
  • $lt - Test less than
  • $lte - Test less than or equal
  • $ne - Test not equal

Boolean Logic

  • $and - Logical AND operation
  • $not - Logical NOT operation
  • $or - Logical OR operation

Array Operations

  • $filter - Filter array by predicate
  • $filterBy - Filter array by object property criteria
  • $map - Transform array elements

Validation

  • $exists - Test if object property exists
  • $isEmpty - Test if value is empty
  • $isPresent - Test if value is present (not null/undefined)
  • $matchesAll - Test if object matches all property criteria (AND logic)
  • $matchesAny - Test if object matches any property criteria (OR logic)

Flow Control

  • $debug - Debug expression evaluation with logging
  • $default - Provide default value for null/undefined
  • $pipe - Chain expressions in sequence

mathPack - Arithmetic Operations

Use case: Mathematical calculations, arithmetic operations, and basic statistical functions.

When to use: Applications involving numerical computations, calculations, or mathematical transformations.

import { createExpressionEngine, mathPack } from "json-expressions";
const engine = createExpressionEngine({ packs: [mathPack] });

Basic Arithmetic

Mathematical Functions

Aggregations

  • $count - Count elements in array
  • $max - Find maximum value
  • $mean - Calculate average
  • $min - Find minimum value
  • $sum - Sum of numbers

comparisonPack - Scalar Comparison Operations

Use case: Basic comparison operations for WHERE clause logic and validation.

When to use: Applications focusing on filtering, validation, or conditional logic based on scalar comparisons.

import { createExpressionEngine, comparisonPack } from "json-expressions";
const engine = createExpressionEngine({ packs: [comparisonPack] });

Basic Comparisons

  • $eq - Test equality
  • $gt - Test greater than
  • $gte - Test greater than or equal
  • $lt - Test less than
  • $lte - Test less than or equal
  • $ne - Test not equal

Range & Membership

  • $between - Test if value is within range
  • $in - Test if value is in array
  • $nin - Test if value is not in array

Value Validation

  • $exists - Test if object property exists
  • $isEmpty - Test if value is empty
  • $isPresent - Test if value is present (not null/undefined)

arrayPack - Array Manipulation

Use case: Array manipulation toolkit for data transformation and processing.

When to use: Applications that work extensively with arrays, lists, or collections requiring transformation, filtering, or manipulation.

import { createExpressionEngine, arrayPack } from "json-expressions";
const engine = createExpressionEngine({ packs: [arrayPack] });

Core Transformations

  • $filter - Filter array by predicate
  • $filterBy - Filter array by object property criteria
  • $find - Find first matching element
  • $map - Transform array elements

Predicates & Testing

  • $all - Test if all elements match predicate
  • $any - Test if any element matches predicate

Advanced Operations

Array Modifications

  • $concat - Concatenate multiple arrays
  • $join - Join array elements into string
  • $reverse - Reverse array order
  • $unique - Remove duplicate elements

Array Slicing

  • $skip - Skip first N elements
  • $take - Take first N elements

Array Accessors

  • $first - Get first element of array
  • $last - Get last element of array

Data Extraction

  • $pluck - Extract property values from object array

Utility

Grouping

  • $groupBy - Group array elements by criteria

objectPack - Key-Value Manipulation

Use case: Object and dictionary operations for property manipulation and introspection.

When to use: Applications working with complex objects, needing property selection, merging, or object transformation.

import { createExpressionEngine, objectPack } from "json-expressions";
const engine = createExpressionEngine({ packs: [objectPack] });

Property Access

  • $select - Select and transform object properties

Property Selection

  • $omit - Create object without specified properties
  • $pick - Create object with only specified properties

Object Combination

  • $merge - Merge multiple objects

Object Introspection

  • $fromPairs - Create object from key-value pairs
  • $keys - Get object property names
  • $pairs - Convert object to key-value pairs
  • $values - Get object property values

stringPack - String Operations

Use case: String processing and transformation functions.

When to use: Applications involving text processing, formatting, or string manipulation.

import { createExpressionEngine, stringPack } from "json-expressions";
const engine = createExpressionEngine({ packs: [stringPack] });

String Transformations

String Operations


filteringPack - Data Filtering Toolkit

Use case: WHERE clause logic and data filtering operations.

When to use: Applications with complex filtering requirements, search functionality, or business rule evaluation.

import { createExpressionEngine, filteringPack } from "json-expressions";
const engine = createExpressionEngine({ packs: [filteringPack] });

Basic Comparisons

  • $between - Test if value is within range
  • $eq - Test equality
  • $gt - Test greater than
  • $gte - Test greater than or equal
  • $lt - Test less than
  • $lte - Test less than or equal
  • $ne - Test not equal

Logic Operations

  • $and - Logical AND operation
  • $not - Logical NOT operation
  • $or - Logical OR operation

Array Filtering

  • $all - Test if all elements match predicate
  • $any - Test if any element matches predicate
  • $filter - Filter array by predicate
  • $filterBy - Filter array by object property criteria
  • $find - Find first matching element

Object Filtering

Membership Tests

  • $in - Test if value is in array
  • $nin - Test if value is not in array

Value & Existence Checks

  • $exists - Test if object property exists
  • $isEmpty - Test if value is empty
  • $isPresent - Test if value is present (not null/undefined)

Pattern Matching


projectionPack - Data Transformation

Use case: SELECT clause operations and data transformation for reshaping and projecting data.

When to use: Applications that need to transform, reshape, or project data into different formats or structures.

import { createExpressionEngine, projectionPack } from "json-expressions";
const engine = createExpressionEngine({ packs: [projectionPack] });

Field Access

  • $get - Access object properties by path
  • $select - Select and transform object properties

Array Transformations

  • $concat - Concatenate multiple arrays
  • $filter - Filter array by predicate
  • $flatMap - Map and flatten results
  • $join - Join array elements into string
  • $map - Transform array elements
  • $pluck - Extract property values from object array
  • $unique - Remove duplicate elements

String Transformations

Conditionals for Computed Fields

  • $case - Multi-branch conditional with literal and predicate support
  • $if - Simple if-then-else conditional

Comparison Operations

  • $eq - Test equality
  • $gt - Test greater than
  • $gte - Test greater than or equal
  • $in - Test if value is in array
  • $lt - Test less than
  • $lte - Test less than or equal
  • $ne - Test not equal
  • $nin - Test if value is not in array

aggregationPack - Statistical Functions

Use case: Statistical and aggregation functions for data analysis and summarization.

When to use: Applications requiring statistical analysis, data summarization, or aggregation operations.

import { createExpressionEngine, aggregationPack } from "json-expressions";
const engine = createExpressionEngine({ packs: [aggregationPack] });

Basic Aggregations

  • $count - Count elements in array
  • $max - Find maximum value
  • $min - Find minimum value
  • $sum - Sum of numbers

Statistical Measures

  • $mean - Calculate average

Array Accessors

  • $first - Get first element of array
  • $last - Get last element of array

Grouping Operations

  • $groupBy - Group array elements by criteria

temporalPack - Date and Time Operations

Use case: Date and time manipulation, calculations, and formatting. All operations work with ISO 8601 strings.

When to use: Applications that need to work with dates and times - scheduling, expiration logic, date formatting, time-based business rules.

import { createExpressionEngine, temporalPack } from "json-expressions";
const engine = createExpressionEngine({ packs: [temporalPack] });

Parsing and Formatting

Date Arithmetic

Date Differences

Date Boundaries

Date Comparisons

  • $isAfter - Check if first date is after second
  • $isBefore - Check if first date is before second
  • $isSameDay - Check if dates are on same day

Date Predicates

Date Components

  • $year - Extract year from date
  • $month - Extract month from date (1-12)
  • $day - Extract day from date
  • $hour - Extract hour from date (0-23)
  • $minute - Extract minute from date
  • $second - Extract second from date
  • $dayOfWeek - Get day of week (0=Sunday, 6=Saturday)
  • $dayOfYear - Get day of year (1-365/366)

Related Documentation