Skip to content

Commit

Permalink
Initial check-in of flatten, unique and (#1813)
Browse files Browse the repository at this point in the history
indicesAndValues working over objects.
  • Loading branch information
chrimc62 authored Feb 28, 2020
1 parent 3776cd1 commit 05ead6e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
28 changes: 26 additions & 2 deletions libraries/adaptive-expressions/src/expressionFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1430,8 +1430,15 @@ export class ExpressionFunctions {
}

result = tempList;
} else {
error = `${ expression.children[0] } is not array.`;
} if (typeof value === 'object') {
const tempList = [];
for (let [index, val] of Object.entries(value)) {
tempList.push({index: index, value: val});
}

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

Expand Down Expand Up @@ -2013,6 +2020,23 @@ export class ExpressionFunctions {
ReturnType.Object,
(expression: Expression): void => ExpressionFunctions.validateOrder(expression, [ReturnType.String], ReturnType.Object)
),
new ExpressionEvaluator(
ExpressionType.Flatten,
ExpressionFunctions.apply(
args => {
let array = args[0]
let depth = args.length > 1 ? args[1] : 100
return (array as any).flat(depth)
}),
ReturnType.Object,
(expression: Expression): void => ExpressionFunctions.validateOrder(expression, [ReturnType.Number], ReturnType.Object)
),
new ExpressionEvaluator(
ExpressionType.Unique,
ExpressionFunctions.apply(args => [... new Set(args[0])]),
ReturnType.Object,
(expression: Expression): void => ExpressionFunctions.validateOrder(expression, [], ReturnType.Object)
),
new ExpressionEvaluator(ExpressionType.IndicesAndValues,
(expression: Expression, state: any): {value: any; error: string} => ExpressionFunctions.indicesAndValues(expression, state),
ReturnType.Object, ExpressionFunctions.validateUnary),
Expand Down
2 changes: 2 additions & 0 deletions libraries/adaptive-expressions/src/expressionType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ export class ExpressionType {
public static readonly SortBy: string = 'sortBy';
public static readonly SortByDescending: string = 'sortByDescending';
public static readonly IndicesAndValues: string = 'indicesAndValues';
public static readonly Flatten: string = 'flatten';
public static readonly Unique: string = 'unique';

// Misc
public static readonly Constant: string = 'Constant';
Expand Down
6 changes: 5 additions & 1 deletion libraries/adaptive-expressions/tests/expression.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,7 @@ const dataSource = [
['first(1)', undefined],
['first(nestedItems).x', 1, ['nestedItems']],
['first(where(indicesAndValues(items), elt, elt.index > 1)).value', 'two'],
['first(where(indicesAndValues(bag), elt, elt.index === "three")).value', 3.0]
['join(items,\',\')', 'zero,one,two'],
['join(createArray(\'a\', \'b\', \'c\'), \'.\')', 'a.b.c'],
['join(createArray(\'a\', \'b\', \'c\'), \',\', \' and \')', 'a,b and c'],
Expand Down Expand Up @@ -463,7 +464,10 @@ const dataSource = [
['sortBy(nestedItems, \'x\')[0].x', 1],
['sortByDescending(items)', ['zero', 'two', 'one']],
['sortByDescending(nestedItems, \'x\')[0].x', 3],

['flatten(createArray([1, [2], [[3, 4], [5, 6]]))', [1, 2, 3, 4, 5, 6]],
['flatten(createArray([1, [2], [[3, 4], [5, 6]], 1))', [1, 2, [3, 4], [5, 6]]],
['unique(createArray([1, 5, 1]))', [1, 5]],

// Object manipulation and construction functions tests
['string(addProperty(json(\'{"key1":"value1"}\'), \'key2\',\'value2\'))', '{"key1":"value1","key2":"value2"}'],
['string(setProperty(json(\'{"key1":"value1"}\'), \'key1\',\'value2\'))', '{"key1":"value2"}'],
Expand Down

0 comments on commit 05ead6e

Please sign in to comment.