Skip to content

Commit

Permalink
Fix expression built-in function errors and test errors (#1824)
Browse files Browse the repository at this point in the history
* fix test error

* replace flat function

* fix typo
  • Loading branch information
Danieladu authored Mar 2, 2020
1 parent b7ee025 commit cddae34
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 14 deletions.
29 changes: 24 additions & 5 deletions libraries/adaptive-expressions/src/expressionFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1430,8 +1430,8 @@ export class ExpressionFunctions {
}

result = tempList;
} if (typeof value === 'object') {
const tempList = [];
} else if (typeof value === 'object') {
const tempList = [];
for (let [index, val] of Object.entries(value)) {
tempList.push({index: index, value: val});
}
Expand Down Expand Up @@ -1793,6 +1793,25 @@ export class ExpressionFunctions {
return count;
}

private static flatten(arr: any[], dept: number): any[]{
dept = typeof dept === 'undefined' ? 1 : dept;
if (typeof dept !== 'number') {
return;
}

let res = JSON.parse(JSON.stringify(arr));

let reduceArr = (_arr): any => _arr.reduce((prevItem, curItem): any => prevItem.concat(curItem),[]);

for (let i = 0; i < dept; i++) {
let hasArrayItem = res.some((item): boolean => Array.isArray(item));
if (hasArrayItem) {
res = reduceArr(res);
}
}
return res;
}

// tslint:disable-next-line: max-func-body-length
private static buildFunctionLookup(): Map<string, ExpressionEvaluator> {
// tslint:disable-next-line: no-unnecessary-local-variable
Expand Down Expand Up @@ -2024,9 +2043,9 @@ export class ExpressionFunctions {
ExpressionType.Flatten,
ExpressionFunctions.apply(
args => {
let array = args[0]
let depth = args.length > 1 ? args[1] : 100
return (array as any).flat(depth)
let array = args[0];
let depth = args.length > 1 ? args[1] : 100;
return ExpressionFunctions.flatten(array, depth);
}),
ReturnType.Object,
(expression: Expression): void => ExpressionFunctions.validateOrder(expression, [ReturnType.Number], ReturnType.Object)
Expand Down
19 changes: 13 additions & 6 deletions libraries/adaptive-expressions/tests/expression.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,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]
['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 @@ -466,10 +466,9 @@ 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]],

['flatten(createArray(1,createArray(2),createArray(createArray(3, 4), createArray(5,6))))', [1, 2, 3, 4, 5, 6]],
['flatten(createArray(1,createArray(2),createArray(createArray(3, 4), createArray(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 Expand Up @@ -703,7 +702,15 @@ var isArraySame = (actual, expected) => { //return [isSuccess, errorMessage]
if (actual.length !== expected.length) return [false, `expected length: ${ expected.length }, actual length: ${ actual.length }`];

for (let i = 0; i < actual.length; i++) {
if (actual[i] !== expected[i]) return [false, `actual is: ${ actual[i] }, expected is: ${ expected[i] }`];
if (Array.isArray(actual[i]) && Array.isArray(expected[i])) {
if (!isArraySame(actual[i], expected[i])) {
return [false, `actual is: ${ actual[i] }, expected is: ${ expected[i] }`]
}
} else if (Array.isArray(actual[i]) || Array.isArray(expected[i])){
return [false, `actual is: ${ actual[i] }, expected is: ${ expected[i] }`]
} else if (actual[i] !== expected[i]) {
return [false, `actual is: ${ actual[i] }, expected is: ${ expected[i] }`];
}
}

return [true, ''];
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
"clean": "lerna run clean",
"functional-test": "lerna run build && nyc mocha \"libraries/functional-tests/tests/*.test.js\"",
"browser-functional-test": "cd libraries/browser-functional-tests && node nightwatch.js -e",
"test": "lerna run build && nyc mocha \"libraries/bot*/tests/**/*.test.js\"",
"test:coveralls": "lerna run build && nyc mocha \"libraries/bot*/tests/**/*.test.js\" && nyc report --reporter=text-lcov | coveralls",
"test-coverage": "nyc mocha \"libraries/bot*/tests/**/*.test.js\"",
"test": "lerna run build && nyc mocha \"libraries/@(adaptive*|bot*)/tests/**/*.test.js\"",
"test:coveralls": "lerna run build && nyc mocha \"libraries/@(adaptive*|bot*)/tests/**/*.test.js\" && nyc report --reporter=text-lcov | coveralls",
"test-coverage": "nyc mocha \"libraries/@(adaptive*|bot*)/tests/**/*.test.js\"",
"upload-coverage": "nyc report --reporter=text-lcov | coveralls",
"build-docs": "lerna run build-docs",
"eslint": "eslint ./libraries/*/src/*.ts ./libraries/*/src/**/*.ts",
Expand Down

0 comments on commit cddae34

Please sign in to comment.