SpEL2JS Parser

0.2.9 · abandoned · verified Sun Apr 19

spel2js is a JavaScript library designed to parse and evaluate Spring Expression Language (SpEL) expressions within a defined context. Its primary purpose is to allow single-page applications to mirror server-side authorization logic, reducing duplication and inconsistencies in UI-related permissions. The library provides a JavaScript implementation of the SpEL parser, aiming to mimic the behavior documented for Spring Framework. It exports a singleton object containing `StandardContext` for creating evaluation contexts (which manage `authentication` and `principal` objects) and `SpelExpressionEvaluator` for compiling and executing SpEL expressions. The latest published version is 0.2.9, but significant development activity appears to have ceased around 2016 (judging by the release notes and `bower` references), indicating it is not actively maintained. It targets Node.js environments `>=8` but its age suggests potential compatibility challenges with modern JavaScript ecosystems and build tools.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up an evaluation context, define local variables, and then evaluate a Spring Expression Language (SpEL) expression using `SpelExpressionEvaluator.eval()` or a pre-compiled expression.

import { StandardContext, SpelExpressionEvaluator } from 'spel2js';

// Mock Spring Security Authentication object and a principal object
const mockAuthentication = {
  details: {
    name: 'Darth Vader'
  },
  isAuthenticated: true
};

const mockPrincipal = {
  username: 'dvader',
  roles: ['SITH_LORD', 'EMPIRE_ADMIRAL']
};

// Locals represent specific objects in the expression context
const locals = {
  toDoList: {
    owner: 'Darth Vader',
    items: ['Destroy Alderaan', 'Build Death Star']
  }
};

const expression = '#toDoList.owner == authentication.details.name && authentication.isAuthenticated';

// Create an evaluation context with mock data
const spelContext = StandardContext.create(mockAuthentication, mockPrincipal);

// Evaluate the expression directly
const result = SpelExpressionEvaluator.eval(expression, spelContext, locals);

console.log(`Expression: "${expression}"`);
console.log(`Result: ${result}`); // Expected: true

// Example of pre-compiling an expression for reuse
const compiledExpression = SpelExpressionEvaluator.compile('#toDoList.items.size() > 1');
const compiledResult = compiledExpression.eval(spelContext, locals);
console.log(`Compiled expression result: ${compiledResult}`); // Expected: true

view raw JSON →