Skip to content

Commit

Permalink
port toString (#1862)
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmicshuai authored Mar 6, 2020
1 parent b03ef78 commit 3d05f87
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
20 changes: 18 additions & 2 deletions libraries/adaptive-expressions/src/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,25 @@ export class Constant extends Expression {
}

if (typeof this.value === 'string') {
return `'${ this.value }'`;
if (this.value.includes('\\')) {
this.value = this.value.replace(/\\/g, '\\\\');
}

return this.value.includes(`'`) ? `"${ this.value }"` : `'${ this.value }'`;
}

if (typeof this.value === 'number') {
return this.value.toString();
}

if (Array.isArray(this.value)) {
this.value = '[' + this.value.join(' ') + ']';
}

if(typeof this.value === 'object') {
this.value = JSON.stringify(this.value);
}

return !this.value ? undefined : this.value.toString();
return this.value === undefined ? undefined : this.value.toString();
}
}
15 changes: 15 additions & 0 deletions libraries/adaptive-expressions/tests/expressionParser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,21 @@ describe('expression parser functional test', () => {
assert.fail(errorMessage);
}
}

//ToString re-parse
const newExpr = Expression.parse(parsed.toString());
const newActual = newExpr.tryEvaluate(scope).value;
if (Array.isArray(actual) && Array.isArray(newActual)) {
const [isSuccess, errorMessage] = isArraySame(actual, newActual);
if (!isSuccess) {
assert.fail(errorMessage);
}
} else if (typeof newActual === 'number') {
assert(parseFloat(actual) === newActual, `actual is: ${ actual } for case ${ input }`);
}
else {
assert(actual === newActual, `actual is: ${ actual } for case ${ input }`);
}
}
});

Expand Down

0 comments on commit 3d05f87

Please sign in to comment.