engine-cache
Advanced tools
Comparing version 0.17.0 to 0.18.0
261
index.js
@@ -24,2 +24,4 @@ 'use strict'; | ||
this.cache = engines || {}; | ||
this.AsyncHelpers = this.options.AsyncHelpers || require('async-helpers'); | ||
this.Helpers = this.options.Helpers || require('helper-cache'); | ||
} | ||
@@ -43,44 +45,14 @@ | ||
Engines.prototype.setEngine = function(ext, fn, opts) { | ||
if (opts && utils.isEngine(opts)) { | ||
var temp = fn; | ||
fn = opts; | ||
opts = temp; | ||
temp = null; | ||
} | ||
var engine = normalizeEngine(fn, opts); | ||
var engine = {}; | ||
if (typeof fn !== 'object' && typeof fn !== 'function') { | ||
throw new TypeError('expected an object or function'); | ||
} | ||
engine.render = fn.render || fn; | ||
if (fn.renderFile || fn.__express) { | ||
engine.renderFile = fn.renderFile || fn.__express; | ||
} | ||
for (var key in fn) { | ||
engine[key] = fn[key]; | ||
} | ||
// extend `engine` with any other properties on `fn` | ||
engine.options = utils.merge({}, engine.options, fn.opts, opts); | ||
if (typeof engine.render !== 'function' && typeof engine.renderSync !== 'function') { | ||
throw new Error('expected engine to have a render or renderSync method'); | ||
} | ||
// create helper caches for the engine | ||
var AsyncHelpers = this.options.AsyncHelpers || utils.AsyncHelpers; | ||
var Helpers = this.options.Helpers || utils.Helpers; | ||
engine.helpers = new Helpers(opts); | ||
engine.asyncHelpers = new AsyncHelpers(opts); | ||
engine.name = engine.name || engine.options.name || utils.stripExt(ext); | ||
engine.helpers = new this.Helpers(engine.options); | ||
engine.asyncHelpers = new this.AsyncHelpers(engine.options); | ||
engine.name = utils.stripExt(engine.name || engine.options.name || ext); | ||
engine.options.ext = utils.formatExt(ext); | ||
// decorate wrapped methods for async helper handling | ||
this.decorate(engine); | ||
this.engineInspect(engine); | ||
decorate(engine); | ||
// add custom inspect method | ||
inspect(engine); | ||
// set the engine on the cache | ||
this.cache[engine.options.ext] = engine; | ||
@@ -91,2 +63,23 @@ return this; | ||
/** | ||
* Add an object of engines onto `engines.cache`. | ||
* | ||
* ```js | ||
* engines.setEngines(require('consolidate')) | ||
* ``` | ||
* | ||
* @param {Object} `obj` Engines to load. | ||
* @return {Object} `Engines` to enable chaining. | ||
* @api public | ||
*/ | ||
Engines.prototype.setEngines = function(engines) { | ||
for (var key in engines) { | ||
if (engines.hasOwnProperty(key) && key !== 'clearCache') { | ||
this.setEngine(key, engines[key]); | ||
} | ||
} | ||
return this; | ||
}; | ||
/** | ||
* Return the engine stored by `ext`. If no `ext` | ||
@@ -96,6 +89,7 @@ * is passed, undefined is returned. | ||
* ```js | ||
* var consolidate = require('consolidate') | ||
* engine.setEngine('hbs', consolidate.handlebars); | ||
* var consolidate = require('consolidate'); | ||
* var engine = engine.setEngine('hbs', consolidate.handlebars); | ||
* | ||
* engine.getEngine('hbs'); | ||
* var engine = engine.getEngine('hbs'); | ||
* console.log(engine); | ||
* // => {render: [function], renderFile: [function]} | ||
@@ -115,5 +109,4 @@ * ``` | ||
return this.cache[utils.formatExt(this.options.defaultEngine)]; | ||
} else { | ||
return this.options.defaultEngine; | ||
} | ||
return this.options.defaultEngine; | ||
} | ||
@@ -124,2 +117,60 @@ return engine; | ||
/** | ||
* Get and set helpers for the given `ext` (engine). If no | ||
* `ext` is passed, the entire helper cache is returned. | ||
* | ||
* **Example:** | ||
* | ||
* ```js | ||
* var helpers = engines.helpers('hbs'); | ||
* helpers.addHelper('bar', function() {}); | ||
* helpers.getEngineHelper('bar'); | ||
* helpers.getEngineHelper(); | ||
* ``` | ||
* | ||
* See [helper-cache] for any related issues, API details, and documentation. | ||
* | ||
* @param {String} `ext` The helper cache to get and set to. | ||
* @return {Object} Object of helpers for the specified engine. | ||
* @api public | ||
*/ | ||
Engines.prototype.helpers = function(ext) { | ||
return this.getEngine(ext).helpers; | ||
}; | ||
/** | ||
* Normalize engine so that `.render` is a property on the `engine` object. | ||
*/ | ||
function normalizeEngine(fn, options) { | ||
if (utils.isEngine(options)) { | ||
return normalizeEngine(options, fn); | ||
} | ||
if (!utils.isObject(fn) && typeof fn !== 'function') { | ||
throw new TypeError('expected an object or function'); | ||
} | ||
var engine = {}; | ||
engine.render = fn.render || fn; | ||
engine.options = utils.extend({}, options); | ||
if (typeof engine.render !== 'function') { | ||
throw new Error('expected engine to have a render method'); | ||
} | ||
for (var key in fn) { | ||
if (key === 'options') { | ||
engine.options = utils.extend({}, engine.options, fn[key]); | ||
continue; | ||
} | ||
if (key === '__express') { | ||
engine.renderFile = fn[key]; | ||
} | ||
engine[key] = fn[key]; | ||
} | ||
return engine; | ||
} | ||
/** | ||
* Wrap engines to extend the helpers object and other | ||
@@ -137,3 +188,4 @@ * native methods or functionality. | ||
Engines.prototype.decorate = function(engine) { | ||
function decorate(engine) { | ||
// save a reference to original methods | ||
var renderSync = engine.renderSync; | ||
@@ -160,3 +212,3 @@ var render = engine.render; | ||
engine.compile = function wrappedCompile(str, opts) { | ||
engine.compile = function engineCompile(str, settings) { | ||
if (typeof str === 'function') { | ||
@@ -166,4 +218,8 @@ return str; | ||
if (!opts) opts = {}; | ||
var helpers = mergeHelpers(engine, opts); | ||
if (typeof settings !== 'undefined' && !utils.isObject(settings)) { | ||
throw new TypeError('expected settings to be an object or undefined'); | ||
} | ||
settings = utils.extend({}, settings); | ||
var helpers = mergeHelpers(engine, settings); | ||
var compiled = compile ? compile(str, helpers) : null; | ||
@@ -189,8 +245,9 @@ | ||
helpers = mergeHelpers(engine, opts); | ||
helpers = mergeHelpers(engine, settings); | ||
var data = {}; | ||
if (opts && typeof opts.mergeFn === 'function') { | ||
data = opts.mergeFn(helpers, locals); | ||
if (settings && typeof settings.mergeFn === 'function') { | ||
data = settings.mergeFn(helpers, locals); | ||
} else { | ||
data = utils.extend({}, locals, helpers); | ||
data = utils.merge({}, locals, helpers); | ||
} | ||
@@ -202,5 +259,8 @@ | ||
return render(str, data, function(err, str) { | ||
if (err) return cb(err); | ||
return engine.asyncHelpers.resolveIds(str, cb); | ||
render(str, data, function(err, str) { | ||
if (err) { | ||
cb(err); | ||
return; | ||
} | ||
engine.asyncHelpers.resolveIds(str, cb); | ||
}); | ||
@@ -225,3 +285,3 @@ }; | ||
engine.render = function wrappedRender(str, locals, cb) { | ||
engine.render = function engineRender(str, locals, cb) { | ||
if (typeof locals === 'function') { | ||
@@ -250,6 +310,6 @@ cb = locals; | ||
// compile the template to create a function | ||
var renderFn = this.compile(str, locals); | ||
var fn = this.compile(str, locals); | ||
// call the function to render templates | ||
renderFn(locals, function(err, content) { | ||
fn(locals, function(err, content) { | ||
if (err) { | ||
@@ -275,9 +335,9 @@ cb(err); | ||
* @param {String|Function} `str` Original string to compile or function to use to render. | ||
* @param {Object} `options` Options/locals to pass to compiled function for rendering. | ||
* @param {Object} `locals` Locals or options to pass to compiled function for rendering. | ||
* @return {String} Returns rendered content. | ||
*/ | ||
engine.renderSync = function wrappedRenderSync(str, options) { | ||
engine.renderSync = function wrappedRenderSync(str, locals) { | ||
if (typeof str === 'function') { | ||
return str(options); | ||
return str(locals); | ||
} | ||
@@ -289,3 +349,3 @@ | ||
var context = utils.extend({}, options); | ||
var context = utils.extend({}, locals); | ||
context.helpers = utils.merge({}, this.helpers, context.helpers); | ||
@@ -295,5 +355,18 @@ var render = this.compile(str, context); | ||
}; | ||
}; | ||
} | ||
/** | ||
* Add a custom inspect function onto the engine. | ||
*/ | ||
function inspect(engine) { | ||
var inspect = ['"' + engine.name + '"']; | ||
var exts = utils.arrayify(engine.options.ext).join(', '); | ||
inspect.push('<ext "' + exts + '">'); | ||
engine.inspect = function() { | ||
return '<Engine ' + inspect.join(' ') + '>'; | ||
}; | ||
} | ||
/** | ||
* Merge the local engine helpers with the options helpers. | ||
@@ -314,3 +387,5 @@ * | ||
for (var key in helpers) { | ||
engine.asyncHelpers.set(key, helpers[key]); | ||
if (helpers.hasOwnProperty(key)) { | ||
engine.asyncHelpers.set(key, helpers[key]); | ||
} | ||
} | ||
@@ -321,61 +396,1 @@ } | ||
} | ||
/** | ||
* Load an object of engines onto the `cache`. | ||
* Mostly useful for testing, but exposed as | ||
* a public method. | ||
* | ||
* ```js | ||
* engines.load(require('consolidate')) | ||
* ``` | ||
* | ||
* @param {Object} `obj` Engines to load. | ||
* @return {Object} `Engines` to enable chaining. | ||
* @api public | ||
*/ | ||
Engines.prototype.load = function(engines) { | ||
for (var key in engines) { | ||
if (engines.hasOwnProperty(key) && key !== 'clearCache') { | ||
this.setEngine(key, engines[key]); | ||
} | ||
} | ||
return this; | ||
}; | ||
/** | ||
* Get and set helpers for the given `ext` (engine). If no | ||
* `ext` is passed, the entire helper cache is returned. | ||
* | ||
* **Example:** | ||
* | ||
* ```js | ||
* var helpers = engines.helpers('hbs'); | ||
* helpers.addHelper('bar', function() {}); | ||
* helpers.getEngineHelper('bar'); | ||
* helpers.getEngineHelper(); | ||
* ``` | ||
* | ||
* See [helper-cache] for any related issues, API details, and documentation. | ||
* | ||
* @param {String} `ext` The helper cache to get and set to. | ||
* @return {Object} Object of helpers for the specified engine. | ||
* @api public | ||
*/ | ||
Engines.prototype.helpers = function(ext) { | ||
return this.getEngine(ext).helpers; | ||
}; | ||
/** | ||
* Decorate a custom inspect function onto the engine. | ||
*/ | ||
Engines.prototype.engineInspect = function(engine) { | ||
var inspect = ['"' + engine.name + '"']; | ||
var exts = utils.arrayify(engine.options.ext).join(', '); | ||
inspect.push('<ext "' + exts + '">'); | ||
engine.inspect = function() { | ||
return '<Engine ' + inspect.join(' ') + '>'; | ||
}; | ||
}; |
{ | ||
"name": "engine-cache", | ||
"description": "express.js inspired template-engine manager.", | ||
"version": "0.17.0", | ||
"version": "0.18.0", | ||
"homepage": "https://github.com/jonschlinkert/engine-cache", | ||
@@ -33,2 +33,3 @@ "author": "Jon Schlinkert (https://github.com/jonschlinkert)", | ||
"helper-cache": "^0.7.2", | ||
"isobject": "^2.1.0", | ||
"lazy-cache": "^2.0.1", | ||
@@ -35,0 +36,0 @@ "mixin-deep": "^1.1.3" |
19
utils.js
@@ -6,6 +6,10 @@ 'use strict'; | ||
require = utils; | ||
/** | ||
* Utils | ||
*/ | ||
require('extend-shallow', 'extend'); | ||
require('isobject', 'isObject'); | ||
require('mixin-deep', 'merge'); | ||
require('async-helpers', 'AsyncHelpers'); | ||
require('helper-cache', 'Helpers'); | ||
require = fn; | ||
@@ -37,7 +41,8 @@ | ||
utils.isEngine = function(options) { | ||
return typeof options === 'function' | ||
|| options.hasOwnProperty('render') | ||
|| options.hasOwnProperty('renderSync') | ||
|| options.hasOwnProperty('renderFile'); | ||
utils.isEngine = function(val) { | ||
if (typeof val === 'function') return true; | ||
if (!utils.isObject(val)) return false; | ||
return val.hasOwnProperty('render') | ||
|| val.hasOwnProperty('renderSync') | ||
|| val.hasOwnProperty('renderFile'); | ||
}; | ||
@@ -44,0 +49,0 @@ |
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
14273
370
6
+ Addedisobject@^2.1.0
+ Addedisarray@1.0.0(transitive)
+ Addedisobject@2.1.0(transitive)