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

Hond/indexof #1797

Merged
merged 3 commits into from
Feb 26, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
54 changes: 47 additions & 7 deletions libraries/adaptive-expressions/src/expressionFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -838,7 +838,7 @@ export class ExpressionFunctions {
} else if (typeof value === 'string'){
path = `['${ value }'].${ path }`;
} else {
return {path: undefined, left: undefined, error:`${ left.children[1].toString() } dones't return a int or string`};
return {path: undefined, left: undefined, error:`${ left.children[1].toString() } doesn't return an int or string`};
}

left = left.children[0];
Expand Down Expand Up @@ -1170,7 +1170,7 @@ export class ExpressionFunctions {
}

private static _and(expression: Expression, state: MemoryInterface): { value: any; error: string } {
let result = false;
let result = true;
let error: string;
for (const child of expression.children) {
({ value: result, error } = child.tryEvaluate(state));
Expand Down Expand Up @@ -2065,7 +2065,7 @@ export class ExpressionFunctions {
ExpressionType.Empty,
(args: any []): boolean => this.isEmpty(args[0]),
ExpressionFunctions.validateUnary,
ExpressionFunctions.verifyNumberOrString),
ExpressionFunctions.verifyContainer),
new ExpressionEvaluator(
ExpressionType.And,
(expression: Expression, state: MemoryInterface): { value: any; error: string } => ExpressionFunctions._and(expression, state),
Expand Down Expand Up @@ -2183,15 +2183,55 @@ export class ExpressionFunctions {
),
new ExpressionEvaluator(
ExpressionType.IndexOf,
ExpressionFunctions.apply((args: any []): number => ExpressionFunctions.parseStringOrNull(args[0]).indexOf(ExpressionFunctions.parseStringOrNull(args[1])), ExpressionFunctions.verifyStringOrNull),
(expression: Expression, state: any): { value: any; error: string } => {
let value = -1;
let error: string;
let args: any [];
({ args, error } = ExpressionFunctions.evaluateChildren(expression, state));
if (!error) {
if (args[0] == undefined || typeof args[0] === 'string') {
if (args[1] === undefined || typeof args[1] === 'string') {
value = ExpressionFunctions.parseStringOrNull(args[0]).indexOf(ExpressionFunctions.parseStringOrNull(args[1]));
} else {
error = `Can only look for indexof string in ${ expression }`;
}
} else if (Array.isArray(args[0])){
value = args[0].indexOf(args[1]);
} else {
error = `${ expression } works only on string or list.`;
}
}

return { value, error };
},
ReturnType.Number,
(expression: Expression): void => ExpressionFunctions.validateArityAndAnyType(expression, 2, 2, ReturnType.String)
(expression: Expression): void => ExpressionFunctions.validateArityAndAnyType(expression, 2, 2, ReturnType.String, ReturnType.Boolean, ReturnType.Number, ReturnType.Object)
),
new ExpressionEvaluator(
ExpressionType.LastIndexOf,
ExpressionFunctions.apply((args: any []): number => ExpressionFunctions.parseStringOrNull(args[0]).lastIndexOf(ExpressionFunctions.parseStringOrNull(args[1])), ExpressionFunctions.verifyStringOrNull),
(expression: Expression, state: any): { value: any; error: string } => {
let value = -1;
let error: string;
let args: any [];
({ args, error } = ExpressionFunctions.evaluateChildren(expression, state));
if (!error) {
if (args[0] == undefined || typeof args[0] === 'string') {
if (args[1] === undefined || typeof args[1] === 'string') {
value = ExpressionFunctions.parseStringOrNull(args[0]).lastIndexOf(ExpressionFunctions.parseStringOrNull(args[1]));
} else {
error = `Can only look for indexof string in ${ expression }`;
}
} else if (Array.isArray(args[0])){
value = args[0].lastIndexOf(args[1]);
} else {
error = `${ expression } works only on string or list.`;
}
}

return { value, error };
},
ReturnType.Number,
(expression: Expression): void => ExpressionFunctions.validateArityAndAnyType(expression, 2, 2, ReturnType.String)
(expression: Expression): void => ExpressionFunctions.validateArityAndAnyType(expression, 2, 2, ReturnType.String, ReturnType.Boolean, ReturnType.Number, ReturnType.Object)
),
new ExpressionEvaluator(
ExpressionType.Join,
Expand Down
2 changes: 1 addition & 1 deletion libraries/adaptive-expressions/src/extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class Extensions {
* Return all static paths to memory. If there is a computed element index, then the path is terminated there,
* but you might get other paths from the computed part as well.
* @param expression Expression to get references from.
* @returns Hash set of the static reference paths.
* @returns List of the static reference paths.
*/
public static references(expression: Expression): string[] {
const {path, refs} = this.referenceWalk(expression);
Expand Down
8 changes: 4 additions & 4 deletions libraries/adaptive-expressions/tests/badExpression.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ const badExpressions =
'newGuid(one)',// should have no parameters
'indexOf(hello)',// should have two parameters
'indexOf(hello, world, one)', // should have two parameters
'indexOf(hello, one)', // both parameters should be string
'indexOf(one, hello)', // both parameters should be string
'indexOf(hello, one)', // second parameter should be string
'indexOf(one, hello)', // first parameter should be list or string
'lastIndexOf(hello)',// should have two parameters
'lastIndexOf(hello, world, one)', // should have two parameters
'lastIndexOf(hello, one)', // both parameters should be string
'lastIndexOf(one, hello)', // both parameters should be string
'lastIndexOf(hello, one)', // second parameter should be string
'lastIndexOf(one, hello)', // first parameter should be list or string

// Logical comparison functions test
'greater(one, hello)', // string and integer are not comparable
Expand Down
8 changes: 8 additions & 0 deletions libraries/adaptive-expressions/tests/expression.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,19 @@ const dataSource = [
['indexOf(hello, \'-\')', -1],
['indexOf(nullObj, \'-\')', -1],
['indexOf(hello, nullObj)', 0],
['indexOf(json(\'["a", "b"]\'), "a")', 0],
['indexOf(json(\'["a", "b"]\'), \'c\')', -1],
['indexOf(createArray(\'abc\', \'def\', \'ghi\'), \'def\')', 1],
['indexOf(createArray(\'abc\', \'def\', \'ghi\'), \'klm\')', -1],
['lastIndexOf(nullObj, \'-\')', -1],
['lastIndexOf(hello, nullObj)', 5],
['lastIndexOf(newGuid(), \'-\')', 23],
['lastIndexOf(newGuid(), \'-\')', 23],
['lastIndexOf(hello, \'-\')', -1],
['lastIndexOf(json(\'["a", "b", "a"]\'), "a")', 2],
['lastIndexOf(json(\'["a", "b"]\'), \'c\')', -1],
['lastIndexOf(createArray(\'abc\', \'def\', \'ghi\', \'def\'), \'def\')', 3],
['lastIndexOf(createArray(\'abc\', \'def\', \'ghi\'), \'klm\')', -1],

// Logical comparison functions tests
['and(1 == 1, 1 < 2, 1 > 2)', false],
Expand Down