engine-cache
Advanced tools
Comparing version 0.3.2 to 0.3.3
54
index.js
@@ -6,6 +6,5 @@ 'use strict'; | ||
var Helpers = require('helper-cache'); | ||
var _ = require('lodash'); | ||
var extend = _.extend; | ||
var extend = require('mixin-deep'); | ||
var forOwn = require('for-own'); | ||
/** | ||
@@ -75,2 +74,3 @@ * ```js | ||
options.hasOwnProperty('render') || | ||
options.hasOwnProperty('renderSync') || | ||
options.hasOwnProperty('renderFile'))) { | ||
@@ -86,2 +86,5 @@ var opts = fn; | ||
engine.render = fn.render; | ||
if (fn.renderSync) { | ||
engine.renderSync = fn.renderSync; | ||
} | ||
} else if (typeof fn === 'object') { | ||
@@ -92,10 +95,10 @@ engine = fn || this.noop; | ||
engine.options = fn.options || options || {}; | ||
engine.options = engine.options || fn.options || options || {}; | ||
engine.helpers = new Helpers(options); | ||
if (typeof engine.render !== 'function') { | ||
throw new Error('Engines are expected to have a `render` method.'); | ||
if (typeof engine.render !== 'function' && typeof engine.renderSync !== 'function') { | ||
throw new Error('Engines are expected to have a `render` or `renderSync` method.'); | ||
} | ||
this.wrapEngine(engine); | ||
this.decorate(engine); | ||
@@ -118,3 +121,3 @@ if (ext[0] !== '.') { | ||
* ```js | ||
* engines.wrapEngine(engine); | ||
* engines.decorate(engine); | ||
* ``` | ||
@@ -124,7 +127,9 @@ * | ||
* @return {Object} The wrapped engine. | ||
* @api public | ||
* @api private | ||
*/ | ||
Engines.prototype.wrapEngine = function(engine) { | ||
debug('[wrapEngine]', arguments); | ||
Engines.prototype.decorate = function(engine) { | ||
debug('[decorate]', arguments); | ||
var renderSync = engine.renderSync; | ||
var render = engine.render; | ||
@@ -138,5 +143,5 @@ | ||
var opts = extend({}, options); | ||
var opts = options || {}; | ||
opts.helpers = extend({}, engine.helpers, opts.helpers); | ||
opts.helpers = extend({}, engine.helpers, opts.helpers); | ||
return render.call(this, str, opts, function (err, content) { | ||
@@ -147,2 +152,9 @@ if (err) return callback(err); | ||
}; | ||
engine.renderSync = function(str, options) { | ||
var opts = options || {}; | ||
opts.helpers = extend({}, engine.helpers, opts.helpers); | ||
return renderSync(str, opts); | ||
}; | ||
}; | ||
@@ -168,8 +180,12 @@ | ||
_.forIn(obj, function (value, key) { | ||
if (value.hasOwnProperty('render')) { | ||
this.register(key, value); | ||
var engines = Object.keys(obj); | ||
var len = engines.length; | ||
for (var i = 0; i < len; i++) { | ||
var name = engines[i]; | ||
var engine = obj[name]; | ||
if (name !== 'clearCache') { | ||
this.register(name, engine); | ||
} | ||
}, this); | ||
} | ||
return this; | ||
@@ -200,2 +216,3 @@ }; | ||
if (ext[0] !== '.') { | ||
@@ -209,3 +226,2 @@ ext = '.' + ext; | ||
} | ||
return engine; | ||
@@ -212,0 +228,0 @@ }; |
{ | ||
"name": "engine-cache", | ||
"description": "express.js inspired template-engine manager.", | ||
"version": "0.3.2", | ||
"version": "0.3.3", | ||
"homepage": "https://github.com/jonschlinkert/engine-cache", | ||
@@ -46,3 +46,2 @@ "author": { | ||
"generator", | ||
"gray-matter", | ||
"lo-dash", | ||
@@ -74,4 +73,4 @@ "lodash", | ||
"consolidate": "^0.10.0", | ||
"engine-lodash": "^0.1.0", | ||
"engines": "^0.3.1", | ||
"engine-lodash": "^0.2.0", | ||
"engines": "^0.4.0", | ||
"handlebars": "^2.0.0-beta.1", | ||
@@ -87,5 +86,6 @@ "mocha": "*", | ||
"engine-noop": "^0.1.0", | ||
"helper-cache": "^0.2.0", | ||
"lodash": "^2.4.1" | ||
"for-own": "^0.1.2", | ||
"helper-cache": "^0.2.3", | ||
"mixin-deep": "^0.1.0" | ||
} | ||
} |
@@ -42,15 +42,4 @@ # engine-cache [![NPM version](https://badge.fury.io/js/engine-cache.png)](http://badge.fury.io/js/engine-cache) | ||
### [.wrapEngine](index.js#L122) | ||
### [.load](index.js#L169) | ||
Wrap an engine to extend the helpers object and other native methods or functionality. | ||
* `engine` **{Object}**: The engine to wrap. | ||
* `returns` **{Object}** `engine`: The wrapped engine. | ||
```js | ||
engines.wrapEngine(engine); | ||
``` | ||
### [.load](index.js#L153) | ||
Load an object of engines onto the `cache`. Mostly useful for testing, but exposed as a public method. | ||
@@ -65,3 +54,3 @@ | ||
### [.get](index.js#L182) | ||
### [.get](index.js#L202) | ||
@@ -80,3 +69,3 @@ Return the engine stored by `ext`. If no `ext` is passed, the entire cache is returned. | ||
### [.helpers](index.js#L220) | ||
### [.helpers](index.js#L240) | ||
@@ -99,3 +88,3 @@ Get and set helpers for the given `ext` (engine). If no `ext` is passed, the entire helper cache is returned. | ||
### [.clear](index.js#L239) | ||
### [.clear](index.js#L259) | ||
@@ -125,4 +114,4 @@ Remove `ext` engine from the cache, or if no value is specified the entire cache is reset. | ||
_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on August 29, 2014._ | ||
_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on October 14, 2014._ | ||
[helper-cache]: https://github.com/jonschlinkert/helper-cache |
@@ -16,3 +16,10 @@ /*! | ||
beforeEach(function() { | ||
engines.clear(); | ||
// engines.clear(); | ||
/** | ||
* Load engine objects from `engines` in node_modules. | ||
* Engines must be installed individually, just like | ||
* consolidate. | ||
*/ | ||
engines.load(require('engines')); | ||
@@ -23,3 +30,3 @@ }); | ||
describe('.load()', function() { | ||
describe('.load() render', function() { | ||
it('should load the cache with engines.', function() { | ||
@@ -33,4 +40,28 @@ engines.get('lodash').should.have.property('render'); | ||
it('should render content with a loaded engine: lodash.', function() { | ||
var lodash = engines.get('.lodash'); | ||
lodash.renderSync('<%= name %>', ctx).should.equal('Jon Schlinkert'); | ||
}); | ||
it('should render content with a loaded engine: handlebars.', function() { | ||
var hbs = engines.get('handlebars'); | ||
hbs.renderSync('{{ name }}', ctx).should.equal('Jon Schlinkert'); | ||
}); | ||
it('should render content with a loaded engine: swig.', function() { | ||
var hbs = engines.get('swig'); | ||
hbs.renderSync('{{ name }}', ctx).should.equal('Jon Schlinkert'); | ||
}); | ||
}); | ||
describe('.load() renderSync', function() { | ||
it('should load the cache with engines.', function() { | ||
engines.get('lodash').should.have.property('renderSync'); | ||
engines.get('underscore').should.have.property('renderSync'); | ||
engines.get('handlebars').should.have.property('renderSync'); | ||
engines.get('swig').should.have.property('renderSync'); | ||
}); | ||
it('should render content with a loaded engine: lodash.', function() { | ||
var lodash = engines.get('lodash'); | ||
lodash.render('<%= name %>', ctx).should.equal('Jon Schlinkert'); | ||
lodash.renderSync('<%= name %>', ctx).should.equal('Jon Schlinkert'); | ||
}); | ||
@@ -40,3 +71,3 @@ | ||
var hbs = engines.get('handlebars'); | ||
hbs.render('{{ name }}', ctx).should.equal('Jon Schlinkert'); | ||
hbs.renderSync('{{ name }}', ctx).should.equal('Jon Schlinkert'); | ||
}); | ||
@@ -46,5 +77,5 @@ | ||
var hbs = engines.get('swig'); | ||
hbs.render('{{ name }}', ctx).should.equal('Jon Schlinkert'); | ||
hbs.renderSync('{{ name }}', ctx).should.equal('Jon Schlinkert'); | ||
}); | ||
}); | ||
}); |
@@ -14,7 +14,7 @@ /*! | ||
var consolidate = require('consolidate'); | ||
var _engines = require('engines'); | ||
describe('engines defaults', function () { | ||
before(function () { | ||
engines.init(); | ||
engines = new Engines(); | ||
}); | ||
@@ -34,11 +34,3 @@ | ||
}); | ||
it('should render content with handlebars.', function() { | ||
engines.register('hbs', _engines.handlebars); | ||
var hbs = engines.get('hbs'); | ||
var content = hbs.render('{{name}}', {name: 'Jon Schlinkert'}); | ||
content.should.equal('Jon Schlinkert'); | ||
}); | ||
}); | ||
}); |
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
29676
23
711
5
112
+ Addedfor-own@^0.1.2
+ Addedmixin-deep@^0.1.0
+ Addedis-plain-object@0.1.0(transitive)
+ Addedmixin-deep@0.1.0(transitive)
- Removedlodash@^2.4.1
Updatedhelper-cache@^0.2.3