Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support constant [] and {} expression #1734

Merged
merged 2 commits into from
Feb 19, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
init
  • Loading branch information
Danieladu committed Feb 19, 2020
commit 1812e791d351e7574785e114c4094925c9f048e6
44 changes: 42 additions & 2 deletions libraries/adaptive-expressions/src/builtInFunction.ts
Original file line number Diff line number Diff line change
@@ -1627,6 +1627,46 @@ export class BuiltInFunctions {
return {value: result, error};
}

private static isEqual(args: any []): boolean {
if (args.length === 0 ) {
return false;
}

if (args[0] === undefined || args[0] === null) {
return args[1] === undefined || args[1] === null;
}

if (Array.isArray(args[0]) && args[0].length === 0 && Array.isArray(args[1]) && args[1].length === 0) {
return true;
}

if (BuiltInFunctions.getPropertyCount(args[0]) === 0 && BuiltInFunctions.getPropertyCount(args[1]) === 0) {
return true;
}

try
{
return args[0] === args[1];
}
catch
{
return false;
}
}

private static getPropertyCount(obj: any): number {
let count = -1;
if (!Array.isArray(obj)) {
if (obj instanceof Map) {
count = obj.size;
} else if (typeof obj === 'object') {
count = Object.keys(obj).length;
}
}

return count;
}

// tslint:disable-next-line: max-func-body-length
private static buildFunctionLookup(): Map<string, ExpressionEvaluator> {
// tslint:disable-next-line: no-unnecessary-local-variable
@@ -1832,10 +1872,10 @@ export class BuiltInFunctions {
(args: any []): boolean => args[0] <= args[1], BuiltInFunctions.validateBinaryNumberOrString, BuiltInFunctions.verifyNumberOrString),
BuiltInFunctions.comparison(
ExpressionType.Equal,
(args: any []): boolean => args[0] === args[1], BuiltInFunctions.validateBinary),
this.isEqual, BuiltInFunctions.validateBinary),
BuiltInFunctions.comparison(
ExpressionType.NotEqual,
(args: any []): boolean => args[0] !== args[1], BuiltInFunctions.validateBinary),
(args: any []): boolean => !this.isEqual(args), BuiltInFunctions.validateBinary),
BuiltInFunctions.comparison(
ExpressionType.GreaterThan,
(args: any []): boolean => args[0] > args[1], BuiltInFunctions.validateBinaryNumberOrString, BuiltInFunctions.verifyNumberOrString),
3 changes: 3 additions & 0 deletions libraries/adaptive-expressions/src/parser/Expression.g4
Original file line number Diff line number Diff line change
@@ -17,6 +17,7 @@ expression

primaryExpression
: '(' expression ')' #parenthesisExp
| CONSTANT #constantAtom
| NUMBER #numericAtom
| STRING #stringAtom
| IDENTIFIER #idAtom
@@ -42,4 +43,6 @@ NEWLINE : '\r'? '\n' -> skip;

STRING : ('\'' (~'\'')* '\'') | ('"' (~'"')* '"');

CONSTANT : ('[' WHITESPACE* ']') | ('{' WHITESPACE* '}');

INVALID_TOKEN_DEFAULT_MODE : . ;
19 changes: 19 additions & 0 deletions libraries/adaptive-expressions/src/parser/expressionEngine.ts
Original file line number Diff line number Diff line change
@@ -117,6 +117,25 @@ export class ExpressionEngine implements ExpressionParserInterface {
}
}

public visitConstantAtom(context: ep.ConstantAtomContext): Expression {
let text: string = context.text;
if (text.startsWith('[') && text.endsWith(']')) {
text = text.substr(1, text.length - 2).trim();
if (text === '') {
return new Constant([]);
}
}

if (text.startsWith('{') && text.endsWith('}')) {
text = text.substr(1, text.length - 2).trim();
if (text === '') {
return new Constant({});
}
}

throw new Error(`Unrecognized constant: ${ text }`);
}

protected defaultResult = (): Expression => new Constant('');

private readonly MakeExpression = (type: string, ...children: Expression[]): Expression =>
Loading