-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
tring evaluation is done with a set of JSON parse and stringify on model. The model data might be unescaped and contain illegal chars for parsing, so need to escape them before. Add a check if value type is string and if it contains " and if so escape them with \ so they can be parsed.
- Loading branch information
Showing
2 changed files
with
43 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import evaluate from '../../../../src/common/utils/evaluate'; | ||
|
||
describe('evaluate', function() { | ||
const dataObject = { | ||
a: { | ||
a1: '{{token1}}', | ||
a2: '{{token2}}' | ||
}, | ||
b: { | ||
b1: '{{token3}}', | ||
b2: '{{token4}}' | ||
} | ||
}; | ||
const template = JSON.stringify(dataObject); | ||
const model = {token1: 1, token2: 2}; | ||
const modelWithDoubleQuotes = {token1: 'my expression with "double quotes"', token2: 2}; | ||
|
||
it('should evaluate and replace template tokens which have model values', function() { | ||
const evaluatedTemplate = evaluate(template, model); | ||
const evaluatedDataObj = JSON.parse(evaluatedTemplate); | ||
evaluatedDataObj.a.a1.should.be.equal('1'); | ||
evaluatedDataObj.a.a2.should.be.equal('2'); | ||
}); | ||
|
||
it("should evaluate and not replace template tokens which don't have model values", function() { | ||
const evaluatedTemplate = evaluate(template, model); | ||
const evaluatedDataObj = JSON.parse(evaluatedTemplate); | ||
evaluatedDataObj.b.b1.should.be.equal('{{token3}}'); | ||
evaluatedDataObj.b.b2.should.be.equal('{{token4}}'); | ||
}); | ||
|
||
it('should escape model values which have double quotes', function() { | ||
const evaluatedTemplate = evaluate(template, modelWithDoubleQuotes); | ||
const evaluatedDataObj = JSON.parse(evaluatedTemplate); | ||
evaluatedDataObj.a.a1.should.be.equal('my expression with "double quotes"'); | ||
evaluatedDataObj.a.a2.should.be.equal('2'); | ||
}); | ||
}); |