Comparing version 1.0.4 to 1.0.5
10
index.js
@@ -6,7 +6,9 @@ "use strict"; | ||
module.exports = function fmtr(str, obj) { | ||
try { | ||
return _.template(_.isString(str) ? str : '', { interpolate: /\$\{([^\}]+)\}/gm })(obj); | ||
} catch (err) { | ||
return (_.isString(str) ? str : '').replace(/\$\{([^\}]+)\}/gm, ''); | ||
if (typeof str !== "string") { | ||
return ''; | ||
} | ||
return str.replace(/\$\{([^\}]+)\}/gm, function (match, p1) { | ||
return _.get(obj, p1) || ''; | ||
}); | ||
}; |
{ | ||
"name": "fmtr", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"description": "awesome string formatter", | ||
@@ -27,8 +27,8 @@ "main": "index.js", | ||
"dependencies": { | ||
"lodash": "^4.11.1" | ||
"lodash": "^4.17.1" | ||
}, | ||
"devDependencies": { | ||
"expect.js": "^0.3.1", | ||
"mocha": "^2.4.5", | ||
"jshint": "^2.9.1" | ||
"mocha": "^3.1.2", | ||
"jshint": "^2.9.4" | ||
}, | ||
@@ -35,0 +35,0 @@ "jshintConfig": { |
@@ -30,4 +30,19 @@ "use strict"; | ||
expect(fmtr('Xx ${foobar} xX', { baz: 123 })).to.be('Xx xX'); | ||
expect(fmtr('Xx ${foo} ${bar} xX', { bar: 123 })).to.be('Xx 123 xX'); | ||
}); | ||
it('should support deep paths', function () { | ||
expect(fmtr('${foo.bar}', { foo: { bar: 1 } })).to.be('1'); | ||
expect(fmtr('${foo[1]}', { foo: [ 1, 2, 3 ] })).to.be('2'); | ||
}); | ||
it('should handle the example from the README.md', function () { | ||
var user = { | ||
username: 'alice', | ||
spouse: 'bob', | ||
enemy: 'eve' | ||
}; | ||
expect(fmtr('${username} is married to ${spouse} and hates ${enemy}', user)).to.be("alice is married to bob and hates eve"); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5028
47
Updatedlodash@^4.17.1