engine-cache
Advanced tools
Comparing version 0.16.2 to 0.17.0
221
index.js
@@ -41,4 +41,4 @@ 'use strict'; | ||
Engines.prototype.setEngine = function (ext, fn, opts) { | ||
if (opts && isEngine(opts)) { | ||
Engines.prototype.setEngine = function(ext, fn, opts) { | ||
if (opts && utils.isEngine(opts)) { | ||
var temp = fn; | ||
@@ -50,6 +50,5 @@ fn = opts; | ||
var engine = {}, msg; | ||
var engine = {}; | ||
if (typeof fn !== 'object' && typeof fn !== 'function') { | ||
msg = expected('setEngine', 'engine').toBe(['object', 'function']); | ||
throw new TypeError(msg); | ||
throw new TypeError('expected an object or function'); | ||
} | ||
@@ -71,4 +70,3 @@ | ||
if (typeof engine.render !== 'function' && typeof engine.renderSync !== 'function') { | ||
msg = expected('setEngine', 'engine').toHave(['render', 'renderSync method']); | ||
throw new Error(msg); | ||
throw new Error('expected engine to have a render or renderSync method'); | ||
} | ||
@@ -83,4 +81,4 @@ | ||
engine.name = engine.name || engine.options.name || stripExt(ext); | ||
engine.options.ext = formatExt(ext); | ||
engine.name = engine.name || engine.options.name || utils.stripExt(ext); | ||
engine.options.ext = utils.formatExt(ext); | ||
@@ -113,6 +111,6 @@ // decorate wrapped methods for async helper handling | ||
if (!ext) return; | ||
var engine = this.cache[formatExt(ext)]; | ||
var engine = this.cache[utils.formatExt(ext)]; | ||
if (typeof engine === 'undefined' && this.options.defaultEngine) { | ||
if (typeof this.options.defaultEngine === 'string') { | ||
return this.cache[formatExt(this.options.defaultEngine)]; | ||
return this.cache[utils.formatExt(this.options.defaultEngine)]; | ||
} else { | ||
@@ -144,33 +142,2 @@ return this.options.defaultEngine; | ||
/** | ||
* Merge the local engine helpers with the options helpers. | ||
* | ||
* @param {Object} `options` Options passed into `render` or `compile` | ||
* @return {Object} Options object with merged helpers | ||
*/ | ||
function mergeHelpers(engine, options) { | ||
/*jshint validthis:true */ | ||
if (typeof options !== 'object') { | ||
var msg = expected('mergeHelpers', 'options').toBe('object'); | ||
throw new TypeError(msg); | ||
} | ||
try { | ||
var opts = utils.merge({}, options); | ||
var helpers = utils.merge({}, engine.helpers, opts.helpers); | ||
if (typeof helpers === 'object') { | ||
for (var key in helpers) { | ||
engine.asyncHelpers.set(key, helpers[key]); | ||
} | ||
} | ||
opts.helpers = engine.asyncHelpers.get({wrap: opts.async}); | ||
return opts; | ||
} catch(err) { | ||
err.message = error('mergeHelpers', err.message); | ||
throw new Error(err); | ||
} | ||
} | ||
/** | ||
* Wrapped compile function for all engines loaded onto engine-cache. | ||
@@ -198,10 +165,6 @@ * If possible, compiles the string with the original engine's compile function. | ||
if (!opts) opts = {}; | ||
var self = this; | ||
var helpers = mergeHelpers(engine, opts); | ||
var compiled = compile | ||
? compile(str, helpers) | ||
: null; | ||
var compiled = compile ? compile(str, helpers) : null; | ||
return function (locals, cb) { | ||
return function(locals, cb) { | ||
if (typeof locals === 'function') { | ||
@@ -227,3 +190,3 @@ cb = locals; | ||
if (opts && typeof opts.mergeFn === 'function') { | ||
data = otps.mergeFn(helpers, locals); | ||
data = opts.mergeFn(helpers, locals); | ||
} else { | ||
@@ -237,3 +200,3 @@ data = utils.extend({}, locals, helpers); | ||
return render(str, data, function (err, str) { | ||
return render(str, data, function(err, str) { | ||
if (err) return cb(err); | ||
@@ -250,3 +213,3 @@ return engine.asyncHelpers.resolveIds(str, cb); | ||
* ```js | ||
* engine.render('<%= foo %>', {foo: 'bar'}, function (err, content) { | ||
* engine.render('<%= foo %>', {foo: 'bar'}, function(err, content) { | ||
* //=> bar | ||
@@ -261,35 +224,38 @@ * }); | ||
engine.render = function wrappedRender(str, opts, cb) { | ||
if (typeof opts === 'function') { | ||
cb = opts; | ||
opts = {}; | ||
engine.render = function wrappedRender(str, locals, cb) { | ||
if (typeof locals === 'function') { | ||
cb = locals; | ||
locals = {}; | ||
} | ||
var fn, msg; | ||
if (typeof cb !== 'function') { | ||
msg = expected('render', 'callback').toBe('function'); | ||
throw new TypeError(msg); | ||
throw new TypeError('expected a callback function'); | ||
} | ||
if (typeof str === 'function') { | ||
fn = str; | ||
fn(opts, cb); | ||
str(locals, cb); | ||
return; | ||
} | ||
if (typeof str === 'string') { | ||
var tmp = opts.async; | ||
opts.async = true; | ||
fn = this.compile(str, opts); | ||
return fn(opts, function(err, content) { | ||
opts.async = tmp; | ||
if (err) return cb(err); | ||
cb(null, content); | ||
}); | ||
if (typeof str !== 'string') { | ||
cb(new TypeError('expected a string or compiled function')); | ||
return; | ||
} | ||
msg = expected('render', 'str').toBe(['string', 'compiled function']); | ||
return cb(new TypeError(msg)); | ||
var async = locals.async; | ||
locals.async = true; | ||
// compile the template to create a function | ||
var renderFn = this.compile(str, locals); | ||
// call the function to render templates | ||
renderFn(locals, function(err, content) { | ||
if (err) { | ||
cb(err); | ||
return; | ||
} | ||
// reset original `async` value | ||
locals.async = async; | ||
cb(null, content); | ||
}); | ||
}; | ||
@@ -313,15 +279,13 @@ | ||
if (typeof str === 'function') { | ||
var fn = str; | ||
return fn(options); | ||
return str(options); | ||
} | ||
if (typeof str !== 'string') { | ||
var msg = expected('renderSync', 'str').toBe(['string', 'compiled function']); | ||
throw new TypeError(msg); | ||
throw new TypeError('expected a string or compiled function'); | ||
} | ||
var opts = utils.merge({}, options); | ||
opts.helpers = utils.merge({}, this.helpers, opts.helpers); | ||
str = this.compile(str, opts); | ||
return str(opts); | ||
var context = utils.extend({}, options); | ||
context.helpers = utils.merge({}, this.helpers, context.helpers); | ||
var render = this.compile(str, context); | ||
return render(context); | ||
}; | ||
@@ -331,2 +295,25 @@ }; | ||
/** | ||
* Merge the local engine helpers with the options helpers. | ||
* | ||
* @param {Object} `options` Options passed into `render` or `compile` | ||
* @return {Object} Options object with merged helpers | ||
*/ | ||
function mergeHelpers(engine, options) { | ||
if (typeof options !== 'object') { | ||
throw new TypeError('expected an object'); | ||
} | ||
var opts = utils.merge({}, options); | ||
var helpers = utils.merge({}, engine.helpers, opts.helpers); | ||
if (typeof helpers === 'object') { | ||
for (var key in helpers) { | ||
engine.asyncHelpers.set(key, helpers[key]); | ||
} | ||
} | ||
opts.helpers = engine.asyncHelpers.get({wrap: opts.async}); | ||
return opts; | ||
} | ||
/** | ||
* Load an object of engines onto the `cache`. | ||
@@ -347,5 +334,4 @@ * Mostly useful for testing, but exposed as | ||
for (var key in engines) { | ||
var engine = engines[key]; | ||
if (key !== 'clearCache') { | ||
this.setEngine(key, engine); | ||
if (engines.hasOwnProperty(key) && key !== 'clearCache') { | ||
this.setEngine(key, engines[key]); | ||
} | ||
@@ -384,6 +370,6 @@ } | ||
Engines.prototype.engineInspect = function (engine) { | ||
Engines.prototype.engineInspect = function(engine) { | ||
var inspect = ['"' + engine.name + '"']; | ||
inspect.push('<ext "' + engine.options.ext + '">'); | ||
var exts = utils.arrayify(engine.options.ext).join(', '); | ||
inspect.push('<ext "' + exts + '">'); | ||
engine.inspect = function() { | ||
@@ -393,62 +379,1 @@ return '<Engine ' + inspect.join(' ') + '>'; | ||
}; | ||
/** | ||
* Utils | ||
*/ | ||
function isEngine(opts) { | ||
return typeof opts === 'function' | ||
|| has(opts, 'render') | ||
|| has(opts, 'renderSync') | ||
|| has(opts, 'renderFile'); | ||
} | ||
function error(method, msg) { | ||
return 'engine-cache "' + method + '" ' + msg; | ||
} | ||
function expected(method, prop) { | ||
var res = {}; | ||
function msg(type, prop, args) { | ||
args = arrayify(args).map(function (arg, i) { | ||
if (i === 0) return article(arg) + ' ' + arg; | ||
return arg; | ||
}).join(' or '); | ||
return 'expected "' + prop + '" to ' + type + ' ' + args + '.'; | ||
} | ||
res.toBe = function (args) { | ||
return error(method, msg('be', prop, args)); | ||
}; | ||
res.toHave = function (args) { | ||
return error(method, msg('have', prop, args)); | ||
}; | ||
return res; | ||
} | ||
function has(obj, key) { | ||
return obj.hasOwnProperty(key); | ||
} | ||
function arrayify(val) { | ||
return Array.isArray(val) ? val : [val]; | ||
} | ||
function formatExt(ext) { | ||
if (!ext) return; | ||
if (ext && ext.charAt(0) !== '.') { | ||
return '.' + ext; | ||
} | ||
return ext; | ||
} | ||
function stripExt(str) { | ||
if (str.charAt(0) === '.') { | ||
return str.slice(1); | ||
} | ||
return str; | ||
} | ||
function article(word) { | ||
var n = /^[aeiou]/.test(word); | ||
return n ? 'an' : 'a'; | ||
} |
{ | ||
"name": "engine-cache", | ||
"description": "express.js inspired template-engine manager.", | ||
"version": "0.16.2", | ||
"version": "0.17.0", | ||
"homepage": "https://github.com/jonschlinkert/engine-cache", | ||
"author": "Jon Schlinkert (https://github.com/jonschlinkert)", | ||
"maintainers": [ | ||
"Brian Woodward (https://github.com/doowb)" | ||
], | ||
"contributors": [ | ||
{ | ||
"name": "Brian Woodward", | ||
"url": "https://github.com/doowb" | ||
} | ||
"Brian Woodward (https://github.com/doowb)" | ||
], | ||
"maintainers": [ | ||
{ | ||
"name": "Brian Woodward", | ||
"url": "https://github.com/doowb" | ||
} | ||
], | ||
"repository": "jonschlinkert/engine-cache", | ||
@@ -44,6 +38,7 @@ "bugs": { | ||
"consolidate": "^0.14.1", | ||
"engine-base": "^0.1.2", | ||
"engine-handlebars": "^0.8.0", | ||
"engine-lodash": "^0.8.2", | ||
"engines": "^0.4.0", | ||
"gulp": "^3.9.1", | ||
"gulp-format-md": "^0.1.9", | ||
"gulp-istanbul": "^0.10.4", | ||
@@ -56,3 +51,2 @@ "gulp-jshint": "^1.11.2", | ||
"mocha": "^2.4.5", | ||
"should": "^8.3.1", | ||
"swig": "^1.4.2", | ||
@@ -99,9 +93,23 @@ "underscore": "^1.8.3" | ||
"assemble", | ||
"async-helpers", | ||
"engines", | ||
"helper-cache", | ||
"async-helpers", | ||
"templates", | ||
"engines" | ||
"templates" | ||
] | ||
} | ||
}, | ||
"toc": false, | ||
"layout": "default", | ||
"tasks": [ | ||
"readme" | ||
], | ||
"plugins": [ | ||
"gulp-format-md" | ||
], | ||
"lint": { | ||
"reflinks": true | ||
}, | ||
"reflinks": [ | ||
"verb" | ||
] | ||
} | ||
} |
53
utils.js
@@ -1,6 +0,47 @@ | ||
var lazy = require('lazy-cache')(require); | ||
lazy('extend-shallow', 'extend'); | ||
lazy('mixin-deep', 'merge'); | ||
lazy('async-helpers', 'AsyncHelpers'); | ||
lazy('helper-cache', 'Helpers'); | ||
module.exports = lazy; | ||
'use strict'; | ||
var utils = require('lazy-cache')(require); | ||
var fn = require; | ||
require = utils; | ||
require('extend-shallow', 'extend'); | ||
require('mixin-deep', 'merge'); | ||
require('async-helpers', 'AsyncHelpers'); | ||
require('helper-cache', 'Helpers'); | ||
require = fn; | ||
utils.isString = function(val) { | ||
return val && typeof val === 'string'; | ||
}; | ||
utils.arrayify = function(val) { | ||
return val ? (Array.isArray(val) ? val : [val]) : []; | ||
}; | ||
utils.formatExt = function(ext) { | ||
if (!utils.isString(ext)) return ''; | ||
if (ext.charAt(0) !== '.') { | ||
return '.' + ext; | ||
} | ||
return ext; | ||
}; | ||
utils.stripExt = function(str) { | ||
if (!utils.isString(str)) return ''; | ||
if (str.charAt(0) === '.') { | ||
str = str.slice(1); | ||
} | ||
return str; | ||
}; | ||
utils.isEngine = function(options) { | ||
return typeof options === 'function' | ||
|| options.hasOwnProperty('render') | ||
|| options.hasOwnProperty('renderSync') | ||
|| options.hasOwnProperty('renderFile'); | ||
}; | ||
/** | ||
* Expose `utils` | ||
*/ | ||
module.exports = utils; |
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
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
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
13850
4
352
1
0
2