benmarch / spel2js

Parse Spring Expression Language in JavaScript
Other
69 stars 19 forks source link

`contains` method is not working. #7

Closed Cicko closed 4 years ago

Cicko commented 4 years ago

When I am trying to evaluate the following expression:

{'PHONE','EMPLOYMENT_PHONE','WORK_PHONE'}.contains(classification)

Error is threw: image

It looks that the contains method is not supported

Here I leave all data needed to reproduce it:

import { SpelExpressionEvaluator } from 'spel2js';

const context = {
    classification: 'PHONE',
};
const expr = `{'PHONE','EMPLOYMENT_PHONE','WORK_PHONE'}.contains(classification)`;
const compiledPrecondition = SpelExpressionEvaluator.compile(expr);
const result = compiledPrecondition.eval(context);
Cicko commented 4 years ago

I guess is not working because contains is an Array method part of the core library which doesn't work in Javascript. As workaround I solved it by transforming it into a matches expression:

`classification matches '^PHONE$ | ^EMPLOYMENT_PHONE$ | ^WORK_PHONE$'`
benmarch commented 4 years ago

@Cicko, good catch, this should be a quick fix.

benmarch commented 4 years ago

@Cicko, this was actually an issue with how arguments are evaluated, which is why you were getting the "classification does not exist" error. However, contains was also not implemented, so I went ahead and did that. This is fixed in 0.2.7.