Skip to content

Commit e04362a

Browse files
authored
Merge pull request #1046 from manixate/patch-1
Remove unnecessary warning when value is empty string
2 parents 7bba35e + 794beea commit e04362a

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

src/Interpolator.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,16 @@ class Interpolator {
9292
// regular escape on demand
9393
while (match = this.regexp.exec(str)) {
9494
value = handleFormat(match[1].trim());
95-
if (typeof value !== 'string') value = utils.makeString(value);
96-
if (!value) {
95+
if (value === undefined) {
9796
if (typeof this.options.missingInterpolationHandler === 'function') {
9897
const temp = this.options.missingInterpolationHandler(str, match);
9998
value = typeof temp === 'string' ? temp : '';
10099
} else {
101100
this.logger.warn(`missed to pass in variable ${match[1]} for interpolating ${str}`);
102101
value = '';
103102
}
103+
} else if (typeof value !== 'string') {
104+
value = utils.makeString(value);
104105
}
105106
value = this.escapeValue ? regexSafe(this.escape(value)) : regexSafe(value);
106107
str = str.replace(match[0], value);

test/interpolation.spec.js

+23-2
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ describe('Interpolator', () => {
252252
});
253253
});
254254

255-
describe("interpolate() - with missing interpolation value", () => {
255+
describe("interpolate() - with undefined interpolation value", () => {
256256
var ip;
257257
var tests = [
258258
{args: ['{{test}}'], expected: ''},
@@ -275,7 +275,7 @@ describe('Interpolator', () => {
275275
});
276276
});
277277

278-
describe("interpolate() - with missing interpolation value - filled by missingInterpolationHandler", () => {
278+
describe("interpolate() - with undefined interpolation value - filled by missingInterpolationHandler", () => {
279279
var ip;
280280
var tests = [
281281
{args: ['{{test}}'], expected: 'test'},
@@ -298,4 +298,25 @@ describe('Interpolator', () => {
298298
});
299299
});
300300
});
301+
302+
describe("interpolate() - with null interpolation value - not filled by missingInterpolationHandler", () => {
303+
var ip;
304+
var tests = [
305+
{args: ['{{test}}', {test: null}], expected: ''},
306+
];
307+
308+
before(() => {
309+
ip = new Interpolator({
310+
missingInterpolationHandler: (str, match) => {
311+
return 'test'
312+
}
313+
});
314+
});
315+
316+
tests.forEach((test) => {
317+
it('correctly interpolates for ' + JSON.stringify(test.args) + ' args', () => {
318+
expect(ip.interpolate.apply(ip, test.args)).to.eql(test.expected);
319+
});
320+
});
321+
});
301322
});

0 commit comments

Comments
 (0)