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 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.'); }) })