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

Enhance the functionality of the + operator #1749

Merged
merged 7 commits into from
Feb 20, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
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
91 changes: 83 additions & 8 deletions libraries/adaptive-expressions/src/builtInFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ export class BuiltInFunctions {
*/
public static verifyNumber(value: any, expression: Expression, _: number): string {
let error: string;
if (typeof value !== 'number' || Number.isNaN(value)) {
if (!BuiltInFunctions.isNumber(value)) {
error = `${ expression } is not a number.`;
}

Expand All @@ -231,15 +231,15 @@ export class BuiltInFunctions {
*/
public static verifyNumberOrNumericList(value: any, expression: Expression, _: number): string {
let error: string;
if (typeof value === 'number' && !Number.isNaN(value)) {
if (BuiltInFunctions.isNumber(value)) {
return error;
}

if (!Array.isArray(value)) {
error = `${ expression } is neither a list nor a number.`;
} else {
for (const elt of value) {
if (typeof elt !== 'number' || Number.isNaN(elt)) {
if (!BuiltInFunctions.isNumber(elt)) {
error = `${ elt } is not a number in ${ expression }.`;
break;
}
Expand All @@ -261,7 +261,7 @@ export class BuiltInFunctions {
error = `${ expression } is not a list.`;
} else {
for (const elt of value) {
if (typeof elt !== 'number' || Number.isNaN(elt)) {
if (!BuiltInFunctions.isNumber(elt)) {
error = `${ elt } is not a number in ${ expression }.`;
break;
}
Expand Down Expand Up @@ -340,6 +340,15 @@ export class BuiltInFunctions {
return error;
}

public static VerifyNumberOrStringOrNull(value: any, expression: Expression, _: number): string {
let error: string;
if (typeof value !== 'string' && value !== undefined && !BuiltInFunctions.isNumber(value) ) {
error = `${ expression } is neither a number nor string`;
}

return error;
}

/**
* Verify value is a number or string.
* @param value alue to check.
Expand All @@ -348,7 +357,7 @@ export class BuiltInFunctions {
*/
public static verifyNumberOrString(value: any, expression: Expression, _: number): string {
let error: string;
if (value === undefined || !(typeof value === 'number' && !Number.isNaN(value)) && typeof value !== 'string') {
if (value === undefined || (!BuiltInFunctions.isNumber(value) && typeof value !== 'string')) {
error = `${ expression } is not string or number.`;
}

Expand Down Expand Up @@ -512,6 +521,38 @@ export class BuiltInFunctions {
);
}

/**
* Generate an expression delegate that applies function on the accumulated value after verifying all children.
* @param func Function to apply.
* @param verify Function to check each arg for validity.
* @returns Delegate for evaluating an expression.
*/
public static applySequenceWithError(func: (arg0: any []) => any, verify?: VerifyExpression): EvaluateExpressionDelegate {
return BuiltInFunctions.applyWithError(
(args: any []): any => {
const binaryArgs: any[] = [undefined, undefined];
let soFar: any = args[0];
let value: any;
let error: string;
// tslint:disable-next-line: prefer-for-of
for (let i = 1; i < args.length; i++) {
binaryArgs[0] = soFar;
binaryArgs[1] = args[i];
({value, error} = func(binaryArgs));
if (error) {
return {value, error};
} else {
soFar = value;
}

}

return {value: soFar, error: undefined};
},
verify
);
}

/**
* Numeric operators that can have 1 or more args.
* @param type Expression type.
Expand Down Expand Up @@ -1064,6 +1105,10 @@ export class BuiltInFunctions {
}
}

private static isNumber(instance: any): boolean {
return instance !== undefined && instance !== null && typeof instance === 'number' && !Number.isNaN(instance);
}

private static isEmpty(instance: any): boolean {
let result: boolean;
if (instance === undefined) {
Expand Down Expand Up @@ -1721,7 +1766,6 @@ export class BuiltInFunctions {
const functions: ExpressionEvaluator[] = [
//Math
new ExpressionEvaluator(ExpressionType.Element, BuiltInFunctions.extractElement, ReturnType.Object, this.validateBinary),
BuiltInFunctions.multivariateNumeric(ExpressionType.Add, (args: any []): number => Number(args[0]) + Number(args[1])),
BuiltInFunctions.multivariateNumeric(ExpressionType.Subtract, (args: any []): number => Number(args[0]) - Number(args[1])),
BuiltInFunctions.multivariateNumeric(ExpressionType.Multiply, (args: any []): number => Number(args[0]) * Number(args[1])),
BuiltInFunctions.multivariateNumeric(
Expand Down Expand Up @@ -1815,6 +1859,37 @@ export class BuiltInFunctions {
BuiltInFunctions.verifyNumericList),
ReturnType.Number,
BuiltInFunctions.validateUnary),
new ExpressionEvaluator(
ExpressionType.Add,
BuiltInFunctions.applySequenceWithError(
(args: any []): any => {
let value: any;
let error: string;
const stringConcat = !BuiltInFunctions.isNumber(args[0]) || !BuiltInFunctions.isNumber(args[1]);
if (((args[0] === null || args[0] === undefined) && BuiltInFunctions.isNumber(args[1]))
|| ((args[1] === null || args[1] === undefined) && BuiltInFunctions.isNumber(args[0])))
{
error = 'Operator \'+\' or add cannot be applied to operands of type \'number\' and null object.';
}
else if (stringConcat) {
if ((args[0] === null || args[0] === undefined) && (args[1] === null || args[1] === undefined)) {
value = '';
} else if (args[0] === null || args[0] === undefined) {
value = args[1].toString();
} else if (args[1] === null || args[1] === undefined) {
value = args[0].toString();
} else {
value = args[0].toString() + args[1].toString();
}
} else {
value = args[0] + args[1];
}

return {value, error};
},
BuiltInFunctions.VerifyNumberOrStringOrNull),
ReturnType.Object,
(expression: Expression): void => BuiltInFunctions.validateArityAndAnyType(expression, 2, Number.MAX_SAFE_INTEGER)),
new ExpressionEvaluator(
ExpressionType.Count,
BuiltInFunctions.apply(
Expand Down Expand Up @@ -2580,7 +2655,7 @@ export class BuiltInFunctions {
(args: any []): any => {
let error: string;
const value: number = parseFloat(args[0]);
if (value === undefined || Number.isNaN(value)) {
if (!BuiltInFunctions.isNumber(value)) {
error = `parameter ${ args[0] } is not a valid number string.`;
}

Expand All @@ -2593,7 +2668,7 @@ export class BuiltInFunctions {
(args: any []): any => {
let error: string;
const value: number = parseInt(args[0], 10);
if (value === undefined || Number.isNaN(value)) {
if (!BuiltInFunctions.isNumber(value)) {
error = `parameter ${ args[0] } is not a valid number string.`;
}

Expand Down
7 changes: 4 additions & 3 deletions libraries/adaptive-expressions/tests/badExpression.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ const badExpressions =
'\'hello\'.length()',// not support currently

// Operators test
'\'1\' + 2', // params should be number
'istrue + 1', // params should be number or string
'one + two + nullObj', // Operator '+' or add cannot be applied to operands of type 'number' and null object.
'\'1\' * 2', // params should be number
'\'1\' - 2', // params should be number
'\'1\' / 2', // params should be number
Expand Down Expand Up @@ -140,9 +141,8 @@ const badExpressions =
'max()', // function need 1 or more than 1 parameters
'min(hello, one)', // param should be number
'min()', // function need 1 or more than 1 parameters
'add(hello, 2)', // param should be number
'add(istrue, 2)', // param should be number or string
'add()', // arg count doesn't match
'add(five, six)', // no such variables
'add(one)', // add function need two or more parameters
'sub(hello, 2)', // param should be number
'sub()', // arg count doesn't match
Expand Down Expand Up @@ -353,6 +353,7 @@ const scope = {
hello: 'hello',
world: 'world',
istrue: true,
nullObj: undefined,
bag:
{
three: 3.0,
Expand Down
10 changes: 10 additions & 0 deletions libraries/adaptive-expressions/tests/expression.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ const dataSource = [
['(1 + 2) * 3', 9],
['(one + two) * bag.three', 9.0, ['one', 'two', 'bag.three']],
['(one + two) * bag.set.four', 12.0, ['one', 'two', 'bag.set.four']],
['hello + nullObj', 'hello'],
['one + two + hello + world', '3helloworld'],
['one + two + hello + one + two', '3hello12'],

['2^2', 4.0],
['3^2^2', 81.0],
['one > 0.5 && two < 2.5', true],
Expand Down Expand Up @@ -73,10 +77,16 @@ const dataSource = [
['concat(hello,nullObj)', 'hello'],
['concat(\'hello\',\'world\')', 'helloworld'],
['concat("hello","world")', 'helloworld'],
['add(hello,world)', 'helloworld'],
['add(\'hello\',\'world\')', 'helloworld'],
['add(nullObj,\'world\')', 'world'],
['add(\'hello\',nullObj)', 'hello'],
['add("hello","world")', 'helloworld'],
['length(\'hello\')', 5],
['length(nullObj)', 0],
['length("hello")', 5],
['length(concat(hello,world))', 10],
['length(hello + world)', 10],
['count(\'hello\')', 5],
['count("hello")', 5],
['count(concat(hello,world))', 10],
Expand Down