helper-cache
Advanced tools
Comparing version 0.6.0 to 0.7.0
133
index.js
@@ -13,8 +13,8 @@ /*! | ||
/** | ||
* Create an instance of `Helpers`, optionally passing | ||
* Create an instance of `HelperCache`, optionally passing | ||
* default `options`. | ||
* | ||
* ```js | ||
* var Helpers = require('helper-cache'); | ||
* var helpers = new Helpers(); | ||
* var HelperCache = require('helper-cache'); | ||
* var helpers = new HelperCache(); | ||
* ``` | ||
@@ -28,18 +28,12 @@ * | ||
function Helpers(options) { | ||
if (!(this instanceof Helpers)) { | ||
return new Helpers(options); | ||
function HelperCache(opts) { | ||
if (!(this instanceof HelperCache)) { | ||
return new HelperCache(opts); | ||
} | ||
var opts = _.defaults({}, options, { | ||
bind: false, | ||
thisArg: null | ||
}); | ||
defineGetter(this, 'options', function () { | ||
return opts; | ||
return _.extend({bind: false, thisArg: null }, opts); | ||
}); | ||
} | ||
/** | ||
@@ -61,3 +55,3 @@ * Register a helper. | ||
defineGetter(Helpers.prototype, 'addHelper', function () { | ||
defineGetter(HelperCache.prototype, 'addHelper', function () { | ||
return function (name, fn, thisArg) { | ||
@@ -71,9 +65,20 @@ thisArg = thisArg || this.options.thisArg; | ||
if (typeof name !== 'string') { | ||
_.extend(this, name); | ||
if (typeof name === 'object') { | ||
for (var key in name) { | ||
this.addHelper(key, name[key], thisArg); | ||
} | ||
} else { | ||
// when `thisArg` and binding is turned on | ||
if (thisArg && this.options.bind) { | ||
this[name] = _.bind(fn, thisArg); | ||
if (this.options.bind && typeof thisArg === 'object') { | ||
if (typeof fn === 'object') { | ||
var res = {}; | ||
for (var prop in fn) { | ||
if (fn.hasOwnProperty(prop)) { | ||
res[prop] = _.bind(fn[prop], thisArg); | ||
} | ||
} | ||
this[name] = res; | ||
} else { | ||
this[name] = _.bind(fn, thisArg); | ||
} | ||
} else { | ||
@@ -89,3 +94,2 @@ this[name] = fn; | ||
/** | ||
@@ -107,6 +111,6 @@ * Register an async helper. | ||
defineGetter(Helpers.prototype, 'addAsyncHelper', function () { | ||
return function(key, fn, thisArg) { | ||
defineGetter(HelperCache.prototype, 'addAsyncHelper', function () { | ||
return function(name, fn, thisArg) { | ||
// `addAsyncHelpers` handles functions | ||
if (typeof key === 'function') { | ||
if (typeof name === 'function') { | ||
return this.addAsyncHelpers.call(this, arguments); | ||
@@ -116,15 +120,35 @@ } | ||
// pass each key/value pair to `addAsyncHelper` | ||
if (typeof key !== 'string') { | ||
_.forOwn(key, function (value, k) { | ||
this.addAsyncHelper(k, value, thisArg); | ||
}, this); | ||
if (typeof name === 'object') { | ||
for (var key in name) { | ||
if (name.hasOwnProperty(key)) { | ||
this.addAsyncHelper(key, name[key], thisArg); | ||
} | ||
} | ||
} else { | ||
fn.async = true; | ||
this.addHelper(key, fn, thisArg); | ||
// when `thisArg` and binding is turned on | ||
if (this.options.bind && typeof thisArg === 'object') { | ||
if (typeof fn === 'object') { | ||
var res = {}; | ||
for (var prop in fn) { | ||
if (fn.hasOwnProperty(prop)) { | ||
var val = fn[prop]; | ||
val.async = true; | ||
res[prop] = _.bind(val, thisArg); | ||
} | ||
} | ||
this[name] = res; | ||
} else { | ||
fn.async = true; | ||
this[name] = _.bind(fn, thisArg); | ||
} | ||
} else { | ||
fn.async = true; | ||
this[name] = fn; | ||
} | ||
} | ||
return this; | ||
} | ||
}.bind(this); | ||
}); | ||
/** | ||
@@ -148,3 +172,3 @@ * Load an object of helpers. | ||
defineGetter(Helpers.prototype, 'addHelpers', function () { | ||
defineGetter(HelperCache.prototype, 'addHelpers', function () { | ||
return function (helpers, thisArg) { | ||
@@ -159,17 +183,11 @@ thisArg = thisArg || this.options.thisArg; | ||
// allow binding each helper if enabled | ||
var o = {}; | ||
_.forIn(helpers, function (value, key) { | ||
if (thisArg && this.options.bind) { | ||
o[key] = _.bind(value, thisArg); | ||
} else { | ||
o[key] = value; | ||
for (var key in helpers) { | ||
if (helpers.hasOwnProperty(key)) { | ||
this.addHelper(key, helpers[key], thisArg); | ||
} | ||
}, this); | ||
// use `addHelper` to extend the object | ||
return this.addHelper(o); | ||
} | ||
return this; | ||
}.bind(this); | ||
}); | ||
/** | ||
@@ -193,3 +211,3 @@ * Load an object of async helpers. | ||
defineGetter(Helpers.prototype, 'addAsyncHelpers', function () { | ||
defineGetter(HelperCache.prototype, 'addAsyncHelpers', function () { | ||
return function (helpers, thisArg) { | ||
@@ -202,8 +220,13 @@ // when a function is passed, execute it and use the results | ||
// use `addAsyncHelper` to extend the object | ||
return this.addAsyncHelper(helpers, thisArg); | ||
if (typeof helpers === 'object') { | ||
for (var key in helpers) { | ||
if (helpers.hasOwnProperty(key)) { | ||
this.addAsyncHelper(key, helpers[key], thisArg); | ||
} | ||
} | ||
} | ||
return this; | ||
}.bind(this); | ||
}); | ||
/** | ||
@@ -222,8 +245,5 @@ * Get a registered helper. | ||
defineGetter(Helpers.prototype, 'getHelper', function () { | ||
defineGetter(HelperCache.prototype, 'getHelper', function () { | ||
return function(key) { | ||
if (!key) { | ||
return this; | ||
} | ||
return this[key]; | ||
return typeof key === 'string' ? this[key] : this; | ||
}.bind(this); | ||
@@ -247,7 +267,12 @@ }); | ||
get: getter, | ||
set: function() {} | ||
set: function() { | ||
throw new Error(name + ' is a read-only getter.'); | ||
} | ||
}); | ||
} | ||
/** | ||
* Expose `HelperCache` | ||
*/ | ||
module.exports = Helpers; | ||
module.exports = HelperCache; |
{ | ||
"name": "helper-cache", | ||
"description": "Easily register and get helper functions to be passed to any template engine or node.js application. Methods for both sync and async helpers.", | ||
"version": "0.6.0", | ||
"version": "0.7.0", | ||
"homepage": "https://github.com/jonschlinkert/helper-cache", | ||
@@ -23,12 +23,10 @@ "author": { | ||
}, | ||
"licenses": [ | ||
{ | ||
"type": "MIT", | ||
"url": "https://github.com/jonschlinkert/helper-cache/blob/master/LICENSE" | ||
} | ||
], | ||
"main": "index.js", | ||
"license": { | ||
"type": "MIT", | ||
"url": "https://github.com/jonschlinkert/helper-cache/blob/master/LICENSE" | ||
}, | ||
"files": [ | ||
"index.js" | ||
], | ||
"main": "index.js", | ||
"engines": { | ||
@@ -41,37 +39,22 @@ "node": ">=0.10.0" | ||
"dependencies": { | ||
"lodash": "^2.4.1" | ||
"lodash": "^3.7.0" | ||
}, | ||
"devDependencies": { | ||
"extend-shallow": "^0.2.0", | ||
"globby": "^1.1.0", | ||
"extend-shallow": "^1.1.2", | ||
"globby": "^2.0.0", | ||
"kind-of": "^1.1.0", | ||
"loader-cache": "^0.3.1", | ||
"mocha": "*", | ||
"object.reduce": "^0.1.3", | ||
"should": "^4.0.4" | ||
"loader-cache": "^0.4.0", | ||
"mocha": "^2.2.4", | ||
"object.reduce": "^0.1.6", | ||
"should": "^6.0.1" | ||
}, | ||
"keywords": [ | ||
"assemble", | ||
"cache", | ||
"compile", | ||
"consolidate", | ||
"content", | ||
"data", | ||
"delimiters", | ||
"delims", | ||
"docs", | ||
"documentation", | ||
"engine", | ||
"express", | ||
"helper", | ||
"helpers", | ||
"template", | ||
"lo-dash", | ||
"lodash", | ||
"process", | ||
"render", | ||
"templates", | ||
"underscore", | ||
"verb", | ||
"yaml" | ||
"lodash" | ||
] | ||
} |
@@ -1,2 +0,2 @@ | ||
# helper-cache [![NPM version](https://badge.fury.io/js/helper-cache.svg)](http://badge.fury.io/js/helper-cache) | ||
# helper-cache [![NPM version](https://badge.fury.io/js/helper-cache.svg)](http://badge.fury.io/js/helper-cache) [![Build Status](https://travis-ci.org/jonschlinkert/helper-cache.svg)](https://travis-ci.org/jonschlinkert/helper-cache) | ||
@@ -13,5 +13,5 @@ > Easily register and get helper functions to be passed to any template engine or node.js application. Methods for both sync and async helpers. | ||
### [Helpers](index.js#L30) | ||
### [HelperCache](index.js#L27) | ||
Create an instance of `Helpers`, optionally passing default `options`. | ||
Create an instance of `HelperCache`, optionally passing default `options`. | ||
@@ -24,7 +24,7 @@ * `options` **{Object}**: Default options to use. | ||
```js | ||
var Helpers = require('helper-cache'); | ||
var helpers = new Helpers(); | ||
var HelperCache = require('helper-cache'); | ||
var helpers = new HelperCache(); | ||
``` | ||
### [addHelper](index.js#L66) | ||
### [.addHelper](index.js#L53) | ||
@@ -35,2 +35,3 @@ Register a helper. | ||
* `fn` **{Function}**: Helper function. | ||
* `returns` **{Object}**: Return `this` to enable chaining | ||
@@ -43,3 +44,3 @@ ```js | ||
### [addAsyncHelper](index.js#L99) | ||
### [.addAsyncHelper](index.js#L106) | ||
@@ -50,2 +51,3 @@ Register an async helper. | ||
* `fn` **{Function}**: Helper function. | ||
* `returns` **{Object}**: Return `this` to enable chaining | ||
@@ -58,14 +60,11 @@ ```js | ||
### [addHelpers](index.js#L148) | ||
### [.addHelpers](index.js#L165) | ||
Load a glob or object of helpers. | ||
Load an object of helpers. | ||
* `key` **{String}**: The name of the helper. | ||
* `fn` **{Function}**: Helper function. | ||
* `returns` **{Object}**: Return `this` to enable chaining. | ||
```js | ||
// glob patterns | ||
helpers.addHelpers('helpers/*.js'); | ||
helpers.addHelpers(['helpers/a.js', 'helpers/{b,c}.js']); | ||
// object of helper functions | ||
helpers.addHelpers({ | ||
@@ -78,16 +77,11 @@ a: function() {}, | ||
See [load-helpers] for issues, API details and the full range of options. | ||
### [.addAsyncHelpers](index.js#L202) | ||
### [addAsyncHelpers](index.js#L193) | ||
Load an object of async helpers. | ||
Load a glob or object of async helpers. | ||
* `key` **{String}**: The name of the helper. | ||
* `fn` **{Function}**: Helper function. | ||
* `returns` **{Object}**: Return `this` to enable chaining | ||
```js | ||
// glob patterns | ||
helpers.addAsyncHelpers('helpers/*.js'); | ||
helpers.addAsyncHelpers(['helpers/a.js', 'helpers/{b,c}.js']); | ||
// object of helper functions | ||
helpers.addAsyncHelpers({ | ||
@@ -100,6 +94,4 @@ a: function() {}, | ||
See [load-helpers] for issues, API details and the full range of options. | ||
### [.getHelper](index.js#L234) | ||
### [getHelper](index.js#L216) | ||
Get a registered helper. | ||
@@ -114,42 +106,24 @@ | ||
### [getAsyncHelper](index.js#L238) | ||
## Related projects | ||
* [engine-cache](https://github.com/jonschlinkert/engine-cache): express.js inspired template-engine manager. | ||
* [handlebars-helpers](https://github.com/assemble/handlebars-helpers): 120+ Handlebars helpers in ~20 categories, for Assemble, YUI, Ghost… [more](https://github.com/assemble/handlebars-helpers) | ||
* [template-helpers](https://github.com/jonschlinkert/template-helpers): Generic JavaScript helpers that can be used with any template… [more](https://github.com/jonschlinkert/template-helpers) | ||
* [template](https://github.com/jonschlinkert/template): Render templates from any engine. Make custom template types, use… [more](https://github.com/jonschlinkert/template) | ||
Get a registered async helper. | ||
## Running tests | ||
Install dev dependencies: | ||
* `key` **{String}**: The helper to get. | ||
* `returns` **{Object}**: The specified helper. If no `key` is passed, the entire cache is returned. | ||
```js | ||
helpers.getAsyncHelper('foo'); | ||
```bash | ||
npm i -d && npm test | ||
``` | ||
### [resolveHelper](index.js#L265) | ||
## Contributing | ||
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/jonschlinkert/helper-cache/issues) | ||
Getter method to resolve async helper values that were called during the render process. Rendering is done by whatever engine you've registered the helpers with. | ||
* `content` **{String}**: Rendered string containing async ids | ||
* `cb` **{Function}** | ||
```js | ||
helper.resolveHelper(str, function (err, content) { | ||
if (err) return done(err); | ||
// do stuff with `content` | ||
done(); | ||
}); | ||
``` | ||
## Author | ||
**Jon Schlinkert** | ||
+ [github/jonschlinkert](https://github.com/jonschlinkert) | ||
+ [twitter/jonschlinkert](http://twitter.com/jonschlinkert) | ||
**Brian Woodward** | ||
+ [github/doowb](https://github.com/doowb) | ||
+ [twitter/doowb](http://twitter.com/doowb) | ||
+ [twitter/jonschlinkert](http://twitter.com/jonschlinkert) | ||
## License | ||
@@ -161,5 +135,5 @@ Copyright (c) 2014-2015 Jon Schlinkert | ||
_This file was generated by [verb](https://github.com/assemble/verb) on February 14, 2015._ | ||
_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on April 23, 2015._ | ||
[load-helpers]: https://github.com/assemble/load-helpers |
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
12423
238
129
+ Addedlodash@3.10.1(transitive)
- Removedlodash@2.4.2(transitive)
Updatedlodash@^3.7.0