From f4c7fb313a248531e660a274ac0e2a48f03bdab4 Mon Sep 17 00:00:00 2001 From: Cristian Douce Date: Tue, 30 Jul 2013 01:39:30 -0300 Subject: [PATCH 1/2] Add support for chained tokens replacement --- index.js | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index d1b5ad9..7e62ad6 100644 --- a/index.js +++ b/index.js @@ -25,7 +25,7 @@ function t(str, obj){ obj = obj || {}; if (t[lang]) str = t[lang][str] || str; return str.replace(/\{([^}]+)\}/g, function(_, name){ - return obj[name] || _; + return get(name, obj) || _; }); } @@ -40,4 +40,17 @@ function t(str, obj){ exports.lang = function(code){ if (0 == arguments.length) return lang; lang = code; -}; \ No newline at end of file +}; + +/** + * Get Object's path value + * + * @param {String} path + * @param {Object} obj + * @return {Mixed} + * @api private + */ + +function get (path, obj) { + return new Function('_', 'return _.' + path)(obj); +} \ No newline at end of file From 0e2e0cbb6e01438c8e9c2265f4c5b749d7c1ef80 Mon Sep 17 00:00:00 2001 From: Cristian Douce Date: Tue, 30 Jul 2013 01:39:43 -0300 Subject: [PATCH 2/2] Add tests for chained tokens --- test/index.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/index.js b/test/index.js index 10360dd..1a91de5 100644 --- a/test/index.js +++ b/test/index.js @@ -11,6 +11,8 @@ describe('t(str)', function(){ t('Hello {name}').should.equal('Hello {name}'); t('{name} is {age} year(s) old', { name: 'Tobi', age: 2 }) .should.equal('Tobi is 2 year(s) old'); + t('{name.first} is from {name.last}\'s family.', { name: { first: 'Tobi', last: 'Goldman' } }) + .should.equal('Tobi is from Goldman\'s family.'); }) })