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

parity indicesAndValues builtin function #1731

Merged
merged 2 commits into from
Feb 19, 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
24 changes: 24 additions & 0 deletions libraries/adaptive-expressions/src/builtInFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1319,6 +1319,27 @@ export class BuiltInFunctions {
};
}

private static indicesAndValues(expression: Expression, state: any): {value: any; error: string} {
let result: object = undefined;
let error: string = undefined;
let value: any = undefined;
({value, error} = expression.children[0].tryEvaluate(state));
if (error === undefined) {
if (Array.isArray(value)) {
const tempList = [];
for (let i = 0; i < value.length; i++) {
tempList.push({index: i, value: value[i]});
}

result = tempList;
} else {
error = `${ expression.children[0] } is not array.`;
}
}

return {value: result, error};
}

private static toBinary(stringToConvert: string): string {
let result = '';
for (const element of stringToConvert) {
Expand Down Expand Up @@ -1824,6 +1845,9 @@ export class BuiltInFunctions {
ReturnType.Object,
(expression: Expression): void => BuiltInFunctions.validateOrder(expression, [ReturnType.String], ReturnType.Object)
),
new ExpressionEvaluator(ExpressionType.IndicesAndValues,
(expression: Expression, state: any): {value: any; error: string} => BuiltInFunctions.indicesAndValues(expression, state),
ReturnType.Object, BuiltInFunctions.validateUnary),
BuiltInFunctions.comparison(
ExpressionType.LessThan,
(args: any []): boolean => args[0] < args[1], BuiltInFunctions.validateBinaryNumberOrString, BuiltInFunctions.verifyNumberOrString),
Expand Down
1 change: 1 addition & 0 deletions libraries/adaptive-expressions/src/expressionType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ export class ExpressionType {
public static readonly SubArray: string = 'subArray';
public static readonly SortBy: string = 'sortBy';
public static readonly SortByDescending: string = 'sortByDescending';
public static readonly IndicesAndValues: string = 'indicesAndValues';

// Misc
public static readonly Constant: string = 'Constant';
Expand Down
2 changes: 2 additions & 0 deletions libraries/adaptive-expressions/tests/badExpression.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,8 @@ const badExpressions =
'where(items, add(1), item)', // Second paramter of where is not an identifier
'where(items, 1, item)', // Second paramter error
'where(items, x, sum(x))', // third paramter error
'indicesAndValues(items, 1)', // should only have one parameter
'indicesAndValues(1)', // shoud have array param
'union(one, two)', // should have collection param
'intersection(one, two)', // should have collection param
'skip(hello)', // should have two parameters
Expand Down
2 changes: 2 additions & 0 deletions libraries/adaptive-expressions/tests/expression.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,7 @@ const dataSource = [
['first(createArray(0, 1, 2))', 0],
['first(1)', undefined],
['first(nestedItems).x', 1, ['nestedItems']],
['first(where(indicesAndValues(items), elt, elt.index > 1)).value', 'two'],
['join(items,\',\')', 'zero,one,two'],
['join(createArray(\'a\', \'b\', \'c\'), \'.\')', 'a.b.c'],
['join(createArray(\'a\', \'b\', \'c\'), \',\', \' and \')', 'a,b and c'],
Expand All @@ -385,6 +386,7 @@ const dataSource = [
['join(foreach(items, item, concat(item, string(count(items)))), \',\')', 'zero3,one3,two3', ['items']],
['join(foreach(doubleNestedItems, items, join(foreach(items, item, item.x), ",")), ",")', '1,2,3'],
['join(foreach(doubleNestedItems, items, join(foreach(items, item, concat(y, string(item.x))), ",")), ",")', 'y1,y2,y3'],
['join(foreach(indicesAndValues(items), item, item.value), ",")', 'zero,one,two'],
['count(where(doubleNestedItems, items, count(where(items, item, item.x == 1)) == 1))', 1],
['count(where(doubleNestedItems, items, count(where(items, item, count(items) == 1)) == 1))', 1],
['join(select(items, item, item), \',\')', 'zero,one,two'],
Expand Down