Skip to content

Commit

Permalink
fix(parser): accept dots in json keys
Browse files Browse the repository at this point in the history
Fixes #53
  • Loading branch information
ribizli authored and ocombe committed Mar 15, 2016
1 parent 591ee4d commit 7bcd6f5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/translate.parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,20 @@ export class Parser {
*/
public getValue(target: any, key: string): string {
let keys = key.split('.');
try {
for (let k of keys) {
target = target[k];
key = '';
do {
key += keys.shift();
if (target[key]) {
target = target[key];
key = '';
} else if (!keys.length) {
target = undefined;
} else {
key += '.';
}
return target;
} catch (e) {
return;
}
} while (keys.length);

return target;
}

}
7 changes: 7 additions & 0 deletions tests/translate.parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ export function main() {
expect(parser.getValue({key1: {key2: {key3: "value3"}}}, 'key1.key2.key3')).toEqual("value3");
expect(parser.getValue({key1: {key2: {key3: "value3"}}}, 'key1.keyWrong.key3')).not.toBeDefined();
expect(parser.getValue({key1: {key2: {key3: "value3"}}}, 'key1.key2.keyWrong')).not.toBeDefined();


expect(parser.getValue({'key1.key2': {key3: "value3"}}, 'key1.key2.key3')).toEqual("value3");
expect(parser.getValue({key1: {'key2.key3': "value3"}}, 'key1.key2.key3')).toEqual("value3");
expect(parser.getValue({'key1.key2.key3': "value3"}, 'key1.key2.key3')).toEqual("value3");
expect(parser.getValue({'key1.key2': {key3: "value3"}}, 'key1.key2.keyWrong')).not.toBeDefined();

});
});
}

0 comments on commit 7bcd6f5

Please sign in to comment.