Skip to content

Commit 6b0ccbd

Browse files
committed
Merge pull request #102 from nsanitate/master
fix(Parser): accept falsy values
2 parents 7249af0 + 3fb2335 commit 6b0ccbd

File tree

3 files changed

+8
-2
lines changed

3 files changed

+8
-2
lines changed

bundles/ng2-translate.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ System.registerDynamic("src/translate.parser", [], true, function($__require, ex
396396
key = '';
397397
do {
398398
key += keys.shift();
399-
if (target[key] && (typeof target[key] === 'object' || !keys.length)) {
399+
if (target[key] !== undefined && (typeof target[key] === 'object' || !keys.length)) {
400400
target = target[key];
401401
key = '';
402402
} else if (!keys.length) {

src/translate.parser.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export class Parser {
3232
key = '';
3333
do {
3434
key += keys.shift();
35-
if (target[key] && (typeof target[key] === 'object' || !keys.length)) {
35+
if (target[key] !== undefined && (typeof target[key] === 'object' || !keys.length)) {
3636
target = target[key];
3737
key = '';
3838
} else if (!keys.length) {

tests/translate.parser.spec.ts

+6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ export function main() {
1616
expect(parser.interpolate("This is a {{ key }}", {key: "value"})).toEqual("This is a value");
1717
});
1818

19+
it('should interpolate with falsy values', () => {
20+
expect(parser.interpolate("This is a {{ key }}", {key: ""})).toEqual("This is a ");
21+
expect(parser.interpolate("This is a {{ key }}", {key: 0})).toEqual("This is a 0");
22+
expect(parser.interpolate("This is a {{ key }}", {key: null})).toEqual("This is a null");
23+
});
24+
1925
it('should interpolate with object properties', () => {
2026
expect(parser.interpolate("This is a {{ key1.key2 }}", {key1: {key2: "value2"}})).toEqual("This is a value2");
2127
expect(parser.interpolate("This is a {{ key1.key2.key3 }}", {key1: {key2: {key3: "value3"}}})).toEqual("This is a value3");

0 commit comments

Comments
 (0)