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

[PORT][Expression]Enable list concatenation for function "concat" #2386

Merged
merged 2 commits into from
Jun 22, 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
47 changes: 32 additions & 15 deletions libraries/adaptive-expressions/src/expressionFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1962,6 +1962,20 @@ export class ExpressionFunctions {
return res;
}

private static commonStringify(input: any): string {
if (input === null || input === undefined) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(input === null || input === undefined [](start = 11, length = 38)

!input would be clearer and more idiomatic. It is also fine for the empty string case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but it is not fine for the boolean type false

return '';
}

if (Array.isArray(input)) {
return input.toString();
} else if (typeof input === 'object') {
return JSON.stringify(input);
} else {
return input.toString();
}
}

private static getStandardFunctions(): ReadonlyMap<string, ExpressionEvaluator> {
const functions: ExpressionEvaluator[] = [
//Math
Expand Down Expand Up @@ -2302,23 +2316,26 @@ export class ExpressionFunctions {
ExpressionFunctions.validateUnary),
new ExpressionEvaluator(
ExpressionType.Concat,
ExpressionFunctions.apply((args: any[]): string => {
let result = '';
for (const arg of args) {
if (arg !== undefined && arg !== null) {
if (Array.isArray(arg)) {
result += arg.toString();
} else if (typeof arg === 'object') {
result += JSON.stringify(arg);
} else {
result += arg.toString();
}
}
ExpressionFunctions.applySequence((args: any[]): string => {
const firstItem = args[0];
const secondItem = args[1];
const isFirstList = Array.isArray(firstItem);
const isSecondList = Array.isArray(secondItem);

if ((firstItem === null || firstItem === undefined)
&& (secondItem === null || secondItem === undefined)) {
return undefined;
} else if ((firstItem === null || firstItem === undefined) && isSecondList){
return secondItem;
} else if ((secondItem === null || secondItem === undefined) && isFirstList){
return firstItem;
} else if (isFirstList && isSecondList){
return firstItem.concat(secondItem);
} else {
return ExpressionFunctions.commonStringify(firstItem) + ExpressionFunctions.commonStringify(secondItem);
}

return result;
}),
ReturnType.String,
ReturnType.String | ReturnType.Array,
ExpressionFunctions.validateAtLeastOne),
new ExpressionEvaluator(
ExpressionType.Length,
Expand Down
3 changes: 3 additions & 0 deletions libraries/adaptive-expressions/tests/expressionParser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,9 @@ const dataSource = [
['contains(items, \'hi\')', false],
['contains(bag, \'three\')', true],
['contains(bag, \'xxx\')', false],
['concat(null, [1, 2], null)', [1, 2]],
['concat(createArray(1, 2), createArray(3, 4))', [1, 2, 3, 4]],
['concat([\'a\', \'b\'], [\'b\', \'c\'], [\'c\', \'d\'])', ['a', 'b', 'b', 'c', 'c', 'd']],
['count(split(hello,\'e\'))', 2],
['count(createArray(\'h\', \'e\', \'l\', \'l\', \'o\'))', 5],
['empty(\'\')', true],
Expand Down