engine-cache
Advanced tools
Comparing version 0.1.3 to 0.2.0
132
index.js
'use strict'; | ||
var debug = require('debug')('engine-cache'); | ||
var Helpers = require('helper-cache'); | ||
var _ = require('lodash'); | ||
var debug = require('debug')('engine-cache'); | ||
@@ -10,3 +11,4 @@ | ||
* ```js | ||
* var engines = require('engine-cache') | ||
* var Engines = require('engine-cache'); | ||
* var engines = new Engines(); | ||
* ``` | ||
@@ -18,5 +20,4 @@ * | ||
function engines (options) { | ||
engines.init(options); | ||
return engines; | ||
function Engines (engines) { | ||
this.init(engines); | ||
} | ||
@@ -26,31 +27,11 @@ | ||
/** | ||
* Options cache | ||
* Initialize default configuration. | ||
* | ||
* @type {Object} | ||
*/ | ||
engines.options = {}; | ||
/** | ||
* Engine cache | ||
* | ||
* @type {Object} | ||
*/ | ||
engines.cache = {}; | ||
/** | ||
* Initialize defaults. | ||
* | ||
* @api private | ||
*/ | ||
engines.init = function(opts) { | ||
Engines.prototype.init = function(engines) { | ||
debug('init', arguments); | ||
this.options = {}; | ||
this.cache = {}; | ||
this.engines = engines || {}; | ||
this.defaultEngines(); | ||
this.extend(opts); | ||
}; | ||
@@ -65,6 +46,5 @@ | ||
engines.defaultEngines = function() { | ||
Engines.prototype.defaultEngines = function() { | ||
debug('defaultEngines', arguments); | ||
this.register('tmpl', require('./defaults/lodash')); | ||
this.register('*', require('./defaults/noop')); | ||
this.register('*', require('engine-noop')); | ||
}; | ||
@@ -91,3 +71,3 @@ | ||
engines.register = function (ext, options, fn) { | ||
Engines.prototype.register = function (ext, options, fn) { | ||
var args = [].slice.call(arguments); | ||
@@ -118,2 +98,3 @@ | ||
engine.options = fn.options || options || {}; | ||
engine.helpers = new Helpers(); | ||
@@ -129,4 +110,4 @@ if (typeof engine.render !== 'function') { | ||
debug('[registered] %s: %j', ext, engine); | ||
this.engines[ext] = engine; | ||
this.cache[ext] = engine; | ||
return this; | ||
@@ -150,3 +131,3 @@ }; | ||
engines.load = function(obj) { | ||
Engines.prototype.load = function(obj) { | ||
debug('[load]', arguments); | ||
@@ -180,8 +161,7 @@ | ||
engines.get = function(ext) { | ||
Engines.prototype.get = function(ext) { | ||
if (!ext) { | ||
return this.cache; | ||
return this.engines; | ||
} | ||
ext = ext || this.noop; | ||
if (ext[0] !== '.') { | ||
@@ -191,6 +171,7 @@ ext = '.' + ext; | ||
var engine = this.cache[ext]; | ||
var engine = this.engines[ext]; | ||
if (!engine) { | ||
engine = this.cache['*']; | ||
engine = this.engines['.*']; | ||
} | ||
return engine; | ||
@@ -201,2 +182,16 @@ }; | ||
/** | ||
* Get and set helpers for the given `ext` (engine). If no | ||
* `ext` is passed, the entire helper cache is returned. | ||
* | ||
* @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.get(ext).helpers; | ||
}; | ||
/** | ||
* Remove `ext` engine from the cache, or if no value is | ||
@@ -215,3 +210,3 @@ * specified the entire cache is reset. | ||
engines.clear = function(ext) { | ||
Engines.prototype.clear = function(ext) { | ||
if (ext) { | ||
@@ -221,61 +216,14 @@ if (ext[0] !== '.') { | ||
} | ||
delete this.cache[ext]; | ||
delete this.engines[ext]; | ||
} else { | ||
this.cache = {}; | ||
this.engines = {}; | ||
} | ||
}; | ||
/** | ||
* Set or get an option. | ||
* Export `Engines` | ||
* | ||
* ```js | ||
* engines.option('a', true) | ||
* engines.option('a') | ||
* // => true | ||
* ``` | ||
* | ||
* @param {String} `key` | ||
* @param {*} `value` | ||
* @return {object} `engines` to enable chaining. | ||
* @api public | ||
* @type {Object} | ||
*/ | ||
engines.option = function(key, value) { | ||
var args = [].slice.call(arguments); | ||
if (args.length === 1 && typeof key === 'string') { | ||
return this.options[key]; | ||
} | ||
if (typeof key === 'object') { | ||
_.extend.apply(_, [this.options].concat(args)); | ||
return this; | ||
} | ||
this.options[key] = value; | ||
return this; | ||
}; | ||
/** | ||
* Extend the options with the given `obj`. | ||
* | ||
* ```js | ||
* engines.extend({a: 'b'}) | ||
* engines.option('a') | ||
* // => 'b' | ||
* ``` | ||
* | ||
* @param {Object} `obj` | ||
* @return {object} `engines` to enable chaining. | ||
* @api public | ||
*/ | ||
engines.extend = function(obj) { | ||
this.options = _.extend({}, this.options, obj); | ||
return this; | ||
}; | ||
module.exports = engines; | ||
module.exports = Engines; |
{ | ||
"name": "engine-cache", | ||
"description": "express.js inspired template-engine manager.", | ||
"version": "0.1.3", | ||
"version": "0.2.0", | ||
"homepage": "https://github.com/jonschlinkert/engine-cache", | ||
@@ -24,9 +24,6 @@ "author": { | ||
"keywords": [ | ||
"docs", | ||
"documentation", | ||
"generate", | ||
"generator", | ||
"markdown", | ||
"templates", | ||
"verb" | ||
"engines", | ||
"template", | ||
"cache", | ||
"engine" | ||
], | ||
@@ -42,4 +39,4 @@ "main": "index.js", | ||
"engine-lodash": "^0.1.0", | ||
"engine-noop": "^0.1.0", | ||
"engines": "^0.3.1", | ||
"handlebars": "^2.0.0-alpha.4", | ||
"mocha": "*", | ||
@@ -49,8 +46,9 @@ "should": "^4.0.4", | ||
"verb": ">= 0.2.6", | ||
"verb-tag-jscomments": ">= 0.1.4" | ||
"verb-tag-jscomments": "^0.2.2" | ||
}, | ||
"dependencies": { | ||
"debug": "^1.0.4", | ||
"helper-cache": "^0.1.0", | ||
"lodash": "^2.4.1" | ||
} | ||
} |
@@ -19,3 +19,3 @@ # engine-cache [![NPM version](https://badge.fury.io/js/engine-cache.png)](http://badge.fury.io/js/engine-cache) | ||
## API | ||
### [engines](index.js#L17) | ||
### [Engines](index.js#L19) | ||
@@ -25,6 +25,7 @@ * `options` **{Object}**: Default options to use. | ||
```js | ||
var engines = require('engine-cache') | ||
var Engines = require('engine-cache'); | ||
var engines = new Engines(); | ||
``` | ||
### [.register](index.js#L87) | ||
### [.register](index.js#L67) | ||
@@ -47,3 +48,3 @@ Register the given view engine callback `fn` as `ext`. | ||
### [.load](index.js#L143) | ||
### [.load](index.js#L124) | ||
@@ -59,3 +60,3 @@ Load an object of engines onto the `cache`. Mostly useful for testing, but exposed as a public method. | ||
### [.get](index.js#L172) | ||
### [.get](index.js#L153) | ||
@@ -74,4 +75,12 @@ Return the engine stored by `ext`. If no `ext` is passed, the entire cache is returned. | ||
### [.clear](index.js#L204) | ||
### [.helpers](index.js#L180) | ||
* `ext` **{String}**: The helper cache to get and set to. | ||
* `returns` **{Object}**: Object of helpers for the specified engine. | ||
Get and set helpers for the given `ext` (engine). If no | ||
`ext` is passed, the entire helper cache is returned. | ||
### [.clear](index.js#L199) | ||
Remove `ext` engine from the cache, or if no value is specified the entire cache is reset. | ||
@@ -87,29 +96,2 @@ | ||
### [.option](index.js#L231) | ||
Set or get an option. | ||
* `key` **{String}** | ||
* `value` **{*}** | ||
* `returns` **{object}** `engines`: to enable chaining. | ||
```js | ||
engines.option('a', true) | ||
engines.option('a') | ||
// => true | ||
``` | ||
### [.extend](index.js#L262) | ||
Extend the options with the given `obj`. | ||
* `obj` **{Object}** | ||
* `returns` **{object}** `engines`: to enable chaining. | ||
```js | ||
engines.extend({a: 'b'}) | ||
engines.option('a') | ||
// => 'b' | ||
``` | ||
## Author | ||
@@ -128,2 +110,2 @@ | ||
_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on August 25, 2014._ | ||
_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on August 28, 2014._ |
@@ -11,4 +11,6 @@ /*! | ||
var should = require('should'); | ||
var engines = require('..'); | ||
var Engines = require('..'); | ||
var engines = new Engines(); | ||
describe('engines register', function() { | ||
@@ -34,20 +36,20 @@ beforeEach(function() { | ||
engines.cache.should.have.property('.a'); | ||
engines.cache.should.have.property('.b'); | ||
engines.cache.should.have.property('.c'); | ||
engines.cache.should.have.property('.d'); | ||
Object.keys(engines.cache).length.should.equal(4); | ||
engines.engines.should.have.property('.a'); | ||
engines.engines.should.have.property('.b'); | ||
engines.engines.should.have.property('.c'); | ||
engines.engines.should.have.property('.d'); | ||
Object.keys(engines.engines).length.should.equal(4); | ||
engines.clear('a'); | ||
engines.cache.should.not.have.property('.a'); | ||
engines.cache.should.have.property('.b'); | ||
Object.keys(engines.cache).length.should.equal(3); | ||
engines.engines.should.not.have.property('.a'); | ||
engines.engines.should.have.property('.b'); | ||
Object.keys(engines.engines).length.should.equal(3); | ||
engines.clear('b'); | ||
engines.cache.should.not.have.property('.a'); | ||
engines.cache.should.not.have.property('.b'); | ||
Object.keys(engines.cache).length.should.equal(2); | ||
engines.engines.should.not.have.property('.a'); | ||
engines.engines.should.not.have.property('.b'); | ||
Object.keys(engines.engines).length.should.equal(2); | ||
}); | ||
}); | ||
}); |
@@ -11,4 +11,6 @@ /*! | ||
var should = require('should'); | ||
var engines = require('..'); | ||
var Engines = require('..'); | ||
var engines = new Engines(); | ||
describe('engines defaults', function () { | ||
@@ -24,10 +26,12 @@ before(function () { | ||
it('should render content with the default engine.', function () { | ||
var noop = engines.get('tmpl'); | ||
noop.render('<%= a %>', {a: 'A'}, function(err, content) { | ||
content.should.equal('A'); | ||
it('should render content with the default noop engine.', function () { | ||
var noop = engines.get('*'); | ||
noop.render('<%= a %>', function(err, content) { | ||
content.should.equal('<%= a %>'); | ||
}); | ||
}); | ||
it('should render content with the default engine.', function () { | ||
it('should synchronously render content with the default noop engine.', function () { | ||
var noop = engines.get('*'); | ||
@@ -37,7 +41,3 @@ noop.renderSync('foo').should.equal('foo'); | ||
it('should render content with the default engine.', function () { | ||
var noop = engines.get('tmpl'); | ||
noop.renderSync('<%= name %>', {name: 'Jon'}).should.equal('Jon'); | ||
}); | ||
}); | ||
}); |
@@ -11,4 +11,6 @@ /*! | ||
var should = require('should'); | ||
var engines = require('..'); | ||
var Engines = require('..'); | ||
var engines = new Engines(); | ||
describe('engines register', function() { | ||
@@ -34,3 +36,3 @@ beforeEach(function() { | ||
Object.keys(engines.cache).length.should.equal(4); | ||
Object.keys(engines.engines).length.should.equal(4); | ||
@@ -37,0 +39,0 @@ engines.get('a').should.have.property('render'); |
@@ -11,5 +11,5 @@ /*! | ||
var should = require('should'); | ||
var engines = require('..'); | ||
var Engines = require('..'); | ||
var engines = new Engines(); | ||
describe('engines init', function() { | ||
@@ -22,9 +22,7 @@ beforeEach(function() { | ||
it('should set defaults on the `options` object.', function() { | ||
engines.init({x: 'x', y: 'y', z: 'z'}) | ||
engines.init(); | ||
engines.options.should.have.property('x'); | ||
engines.options.should.have.property('y'); | ||
engines.options.should.have.property('z'); | ||
engines.engines['.*'].should.be.an.object; | ||
}); | ||
}); | ||
}); |
@@ -11,3 +11,4 @@ /*! | ||
var should = require('should'); | ||
var engines = require('..'); | ||
var Engines = require('..'); | ||
var engines = new Engines(); | ||
@@ -14,0 +15,0 @@ describe('engines load', function() { |
@@ -12,3 +12,4 @@ /*! | ||
var should = require('should'); | ||
var engines = require('..'); | ||
var Engines = require('..'); | ||
var engines = new Engines(); | ||
@@ -21,3 +22,3 @@ describe('engines register', function() { | ||
describe('.register()', function() { | ||
it('should register engines to the `cache` object.', function() { | ||
it('should register engines to the `engines` object.', function() { | ||
engines.register('a', { | ||
@@ -36,7 +37,7 @@ render: function () {} | ||
engines.cache.should.have.property('.a'); | ||
engines.cache.should.have.property('.b'); | ||
engines.cache.should.have.property('.c'); | ||
engines.cache.should.have.property('.d'); | ||
Object.keys(engines.cache).length.should.equal(4); | ||
engines.engines.should.have.property('.a'); | ||
engines.engines.should.have.property('.b'); | ||
engines.engines.should.have.property('.c'); | ||
engines.engines.should.have.property('.d'); | ||
Object.keys(engines.engines).length.should.equal(4); | ||
}); | ||
@@ -58,7 +59,7 @@ | ||
engines.cache.should.have.property('.a'); | ||
engines.cache.should.have.property('.b'); | ||
engines.cache.should.have.property('.c'); | ||
engines.cache.should.have.property('.d'); | ||
Object.keys(engines.cache).length.should.equal(4); | ||
engines.engines.should.have.property('.a'); | ||
engines.engines.should.have.property('.b'); | ||
engines.engines.should.have.property('.c'); | ||
engines.engines.should.have.property('.d'); | ||
Object.keys(engines.engines).length.should.equal(4); | ||
}); | ||
@@ -86,9 +87,9 @@ | ||
engines.cache.should.have.property('.a'); | ||
engines.cache.should.have.property('.b'); | ||
engines.cache.should.have.property('.c'); | ||
engines.cache.should.have.property('.d'); | ||
Object.keys(engines.cache).length.should.equal(4); | ||
engines.engines.should.have.property('.a'); | ||
engines.engines.should.have.property('.b'); | ||
engines.engines.should.have.property('.c'); | ||
engines.engines.should.have.property('.d'); | ||
Object.keys(engines.engines).length.should.equal(4); | ||
}); | ||
}); | ||
}); |
@@ -12,3 +12,4 @@ /*! | ||
var lodash = require('engine-lodash'); | ||
var engines = require('..'); | ||
var Engines = require('..'); | ||
var engines = new Engines(); | ||
@@ -15,0 +16,0 @@ describe('engines', function() { |
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
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
0
20399
3
20
490
104
+ Addedhelper-cache@^0.1.0
+ Addedhelper-cache@0.1.4(transitive)