Comparing version 0.1.4 to 0.1.5
{ | ||
"name": "template", | ||
"version": "0.1.4", | ||
"version": "0.1.5", | ||
"main": [ | ||
@@ -5,0 +5,0 @@ "index.js" |
47
index.js
@@ -19,7 +19,3 @@ /* | ||
// Mixin non-conflict methods to `_` namespace | ||
_.mixin(_.str.exports()); | ||
// Defaults passed to 'delim' lib | ||
@@ -29,9 +25,15 @@ var defaults = {body: '', beginning: '', end: '', flags: 'g'}; | ||
// Process templates | ||
var template = function(text, data, options) { | ||
data = Object.create(data || {}); | ||
var template = function(str, data, options) { | ||
data = _.extend({}, data); | ||
// Delimiter options | ||
var opts = _.extend({}, defaults, options); | ||
var opts = _.defaults({}, options, defaults); | ||
// Mixin non-conflict methods to `_` namespace | ||
if(opts.nonconflict && opts.nonconflict === true) { | ||
_.mixin(_.str.exports()); | ||
} | ||
var settings = _.extend({variable: opts.variable}, opts.settings); | ||
var original = text; | ||
var original = str; | ||
@@ -43,14 +45,14 @@ // Look for templates to process until no more can be found | ||
// Inspired by grunt.template | ||
while (text.indexOf(opts.delims[0]) >= 0) { | ||
text = _.template(text, data, settings); | ||
if (text === original) {break;} | ||
while (str.indexOf(opts.delims[0]) >= 0) { | ||
str = _.template(str, data, settings); | ||
if (str === original) {break;} | ||
} | ||
} else { | ||
// If no custom delimiters are provided, use the defaults. | ||
while (text.indexOf('${') >= 0 || text.indexOf('%>') >= 0) { | ||
text = _.template(text, data, settings); | ||
if (text === original) {break;} | ||
while (str.indexOf('${') >= 0 || str.indexOf('%>') >= 0) { | ||
str = _.template(str, data, settings); | ||
if (str === original) {break;} | ||
} | ||
} | ||
return text; | ||
return str; | ||
}; | ||
@@ -60,18 +62,17 @@ | ||
// Read files and process any templates therein | ||
template.read = function(text, data, options) { | ||
text = file.readFileSync(text); | ||
return template(text, data, options); | ||
template.read = function(filepath, data, options) { | ||
filepath = file.readFileSync(filepath); | ||
return template(filepath, data, options); | ||
}; | ||
// Copy files and process any templates therein | ||
template.copy = function (text, dest, data, options) { | ||
template.copy = function (filepath, dest, data, options) { | ||
var opts = _.extend({}, {process: true}, options || {}); | ||
text = file.readFileSync(text); | ||
filepath = file.readFileSync(filepath); | ||
if(opts.process === true) { | ||
text = template(text, data, opts); | ||
filepath = template(filepath, data, opts); | ||
} | ||
file.writeFileSync(dest, text, opts); | ||
file.writeFileSync(dest, filepath, opts); | ||
}; | ||
module.exports = template; |
{ | ||
"name": "template", | ||
"description": "An easy-to-use utility library for processing Lo-dash templates.", | ||
"version": "0.1.4", | ||
"version": "0.1.5", | ||
"homepage": "https://github.com/jonschlinkert/template", | ||
@@ -6,0 +6,0 @@ "author": { |
@@ -26,3 +26,2 @@ /* | ||
describe('Mixin methods from underscore.string:', function () { | ||
@@ -37,3 +36,3 @@ it('should slugify the string with _.str namespace', function () { | ||
var tmpl = fixture('_slugify.tmpl'); | ||
var actual = template(tmpl); | ||
var actual = template(tmpl, null, {nonconflict: true}); | ||
expect(actual).to.eql('this-should-be-slugified'); | ||
@@ -50,3 +49,3 @@ }); | ||
var tmpl = fixture('_titleize.tmpl'); | ||
var actual = template(tmpl); | ||
var actual = template(tmpl, null, {nonconflict: true}); | ||
expect(actual).to.eql('This Should Be Titleized'); | ||
@@ -53,0 +52,0 @@ }); |
Sorry, the diff of this file is not supported yet
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
14861
248