Skip to content

Commit

Permalink
Merge pull request #826 from alexmchardy/fix-interpolation-escapevalu…
Browse files Browse the repository at this point in the history
…e-default

Fix Interpolator.escapeValue defaulting to undefined in some cases
  • Loading branch information
jamuhl committed Oct 17, 2016
2 parents dbf3693 + d367309 commit 11f0595
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Interpolator.js
Expand Up @@ -17,7 +17,7 @@ class Interpolator {

const iOpts = options.interpolation;

this.escapeValue = iOpts.escapeValue;
this.escapeValue = iOpts.escapeValue !== undefined ? iOpts.escapeValue : true;

this.prefix = iOpts.prefix ? utils.regexEscape(iOpts.prefix) : iOpts.prefixEscaped || '{{';
this.suffix = iOpts.suffix ? utils.regexEscape(iOpts.suffix) : iOpts.suffixEscaped || '}}';
Expand Down
65 changes: 65 additions & 0 deletions test/interpolation.spec.js
Expand Up @@ -26,6 +26,71 @@ describe('Interpolator', () => {
});
});

describe('interpolate() - options', () => {
var tests = [
{
options: {interpolation: {}},
expected: {escapeValue: true, prefix: '{{', suffix: '}}', formatSeparator: ',', unescapePrefix: '-', unescapeSuffix: '', nestingPrefix: '\\$t\\(', nestingSuffix: '\\)' }
},
{
options: {},
expected: {escapeValue: true, prefix: '{{', suffix: '}}', formatSeparator: ',', unescapePrefix: '-', unescapeSuffix: '', nestingPrefix: '\\$t\\(', nestingSuffix: '\\)' }
},
{
description: 'uses and regex escapes prefix and suffix',
options: {interpolation: {prefix: '(*(', suffix: ')*)', prefixEscaped: '\\(\\^\\(', suffixEscaped: ')\\^\\)'}},
expected: {prefix: '\\(\\*\\(', suffix: '\\)\\*\\)'}
},
{
description: 'uses prefixEscaped and suffixEscaped if prefix and suffix not provided',
options: {interpolation: {prefixEscaped: '<<', suffixEscaped: '>>'}},
expected: {prefix: '<<', suffix: '>>'}
},
{
description: 'uses unescapePrefix if provided',
options: {interpolation: {unescapePrefix: '=>'}},
expected: {unescapePrefix: '=>', unescapeSuffix: ''}
},
{
description: 'uses unescapeSuffix if provided',
options: {interpolation: {unescapeSuffix: '<='}},
expected: {unescapePrefix: '', unescapeSuffix: '<='}
},
{
description: 'uses unescapeSuffix if both unescapePrefix and unescapeSuffix are provided',
options: {interpolation: {unescapePrefix: '=>', unescapeSuffix: '<='}},
expected: {unescapePrefix: '', unescapeSuffix: '<='}
},
{
description: 'uses and regex escapes nestingPrefix and nestingSuffix',
options: {interpolation: {nestingPrefix: 'nest(', nestingSuffix: ')nest', nestingPrefixEscaped: 'neste\\(', nestingSuffixEscaped: '\\)neste'}},
expected: {nestingPrefix: 'nest\\(', nestingSuffix: '\\)nest'}
},
{
description: 'uses nestingPrefixEscaped and nestingSuffixEscaped if nestingPrefix and nestingSuffix not provided',
options: {interpolation: {nestingPrefixEscaped: 'neste\\(', nestingSuffixEscaped: '\\)neste'}},
expected: {nestingPrefix: 'neste\\(', nestingSuffix: '\\)neste'}
}
];

tests.forEach((test) => {

describe(test.description || 'when called with ' + JSON.stringify(test.options), () => {
var ip;

before(() => {
ip = new Interpolator(test.options);
});

Object.keys(test.expected).forEach((key) => {
it(key + ' is set correctly', () => {
expect(ip[key]).to.eql(test.expected[key]);
});
});
});
});
});

describe('interpolate() - with formatter', () => {
var ip;

Expand Down

0 comments on commit 11f0595

Please sign in to comment.