helper-cache
Advanced tools
Comparing version 0.5.0 to 0.5.1
61
index.js
@@ -53,5 +53,12 @@ /*! | ||
* | ||
* ```js | ||
* helpers.add('foo', function (name) { | ||
* return 'foo-' + name; | ||
* }); | ||
* ``` | ||
* | ||
* @name addHelper | ||
* @param {String} `key` The name of the helper. | ||
* @param {Function} `fn` Helper function. | ||
* @return {Object} Return `this` to enable chaining | ||
* @api public | ||
@@ -63,2 +70,4 @@ */ | ||
thisArg = thisArg || this.options.thisArg; | ||
// `addHelpers` handles functions | ||
if (typeof key === 'function') { | ||
@@ -68,5 +77,8 @@ return this.addHelpers.call(this, arguments); | ||
// just extend an object | ||
if (typeof key !== 'string') { | ||
_.extend(this, key); | ||
} else { | ||
// when `thisArg` and binding is turned on | ||
if (thisArg && this.options.bind) { | ||
@@ -78,2 +90,4 @@ this[key] = _.bind(fn, thisArg); | ||
} | ||
// chaining | ||
return this; | ||
@@ -87,5 +101,12 @@ }.bind(this); | ||
* | ||
* ```js | ||
* helpers.addAsyncHelper('foo', function (name, next) { | ||
* next(null, 'foo-' + name); | ||
* }); | ||
* ``` | ||
* | ||
* @name addAsyncHelper | ||
* @param {String} `key` The name of the helper. | ||
* @param {Function} `fn` Helper function. | ||
* @return {Object} Return `this` to enable chaining | ||
* @api public | ||
@@ -97,2 +118,4 @@ */ | ||
thisArg = thisArg || this.options.thisArg; | ||
// `addAsyncHelpers` handles functions | ||
if (typeof key === 'function') { | ||
@@ -102,2 +125,3 @@ return this.addAsyncHelpers.call(this, arguments); | ||
// pass each key/value pair to `addAsyncHelper` | ||
if (typeof key !== 'string') { | ||
@@ -109,2 +133,3 @@ _.forOwn(key, function (value, k) { | ||
var self = this; | ||
// keep a reference to the original async helper | ||
if (thisArg && this.options.bind) { | ||
@@ -115,2 +140,3 @@ this._.asyncHelpers[key] = _.bind(fn, thisArg); | ||
} | ||
// create a new sync helper that is used in the first pass | ||
this.addHelper(key, function () { | ||
@@ -131,5 +157,16 @@ var id = '__async_helper_id__' + randomize('Aa0', 42) + '__'; | ||
* | ||
* ```js | ||
* helpers.addHelpers({ | ||
* foo: function (name) { | ||
* return 'foo-' + name; | ||
* }, | ||
* bar: function (name) { | ||
* return 'bar-' + name; | ||
* } | ||
* }); | ||
* | ||
* @name addHelpers | ||
* @param {String} `key` The name of the helper. | ||
* @param {Function} `fn` Helper function. | ||
* @return {Object} Return `this` to enable chaining. | ||
* @api public | ||
@@ -141,6 +178,9 @@ */ | ||
thisArg = thisArg || this.options.thisArg; | ||
// when a function is passed, execute it and use the results | ||
if (typeof helpers === 'function') { | ||
helpers = helpers(this.options, thisArg); | ||
return this.addHelpers(helpers(thisArg), thisArg); | ||
} | ||
// allow binding each helper if enabled | ||
var o = {}; | ||
@@ -154,2 +194,4 @@ _.forIn(helpers, function (value, key) { | ||
}, this); | ||
// use `addHelper` to extend the object | ||
return this.addHelper(o); | ||
@@ -163,3 +205,11 @@ }.bind(this); | ||
* | ||
* See [load-helpers] for issues, API details and the full range of options. | ||
* ```js | ||
* helpers.addAsyncHelpers({ | ||
* foo: function (name, next) { | ||
* next(null, 'foo-' + name); | ||
* }, | ||
* bar: function (name, next) { | ||
* next(null, 'bar-' + name); | ||
* } | ||
* }); | ||
* | ||
@@ -169,2 +219,3 @@ * @name addAsyncHelpers | ||
* @param {Function} `fn` Helper function. | ||
* @return {Object} Return `this` to enable chaining | ||
* @api public | ||
@@ -176,5 +227,9 @@ */ | ||
thisArg = thisArg || this.options.thisArg; | ||
// when a function is passed, execute it and use the results | ||
if (typeof helpers === 'function') { | ||
helpers = helpers(this.options, thisArg); | ||
return this.addAsyncHelpers(helpers(thisArg), thisArg); | ||
} | ||
// use `addAsyncHelper` to extend the object | ||
return this.addAsyncHelper(helpers, thisArg); | ||
@@ -181,0 +236,0 @@ }.bind(this); |
{ | ||
"name": "helper-cache", | ||
"description": "Easily get and set helper functions to pass to any application or template engine.", | ||
"version": "0.5.0", | ||
"version": "0.5.1", | ||
"homepage": "https://github.com/jonschlinkert/helper-cache", | ||
@@ -6,0 +6,0 @@ "author": { |
@@ -14,3 +14,3 @@ # helper-cache [![NPM version](https://badge.fury.io/js/helper-cache.svg)](http://badge.fury.io/js/helper-cache) | ||
### [Helpers](index.js#L30) | ||
### [Helpers](index.js#L29) | ||
@@ -29,17 +29,31 @@ Create an instance of `Helpers`, optionally passing default `options`. | ||
### [addHelper](index.js#L60) | ||
### [addHelper](index.js#L66) | ||
Set helpers on the cache. | ||
* `key` **{String}**: The name of the helper. | ||
* `fn` **{Function}**: Helper function. | ||
* `returns` **{Object}**: Return `this` to enable chaining | ||
Set helpers on the cache. | ||
```js | ||
helpers.add('foo', function (name) { | ||
return 'foo-' + name; | ||
}); | ||
``` | ||
### [addAsyncHelper](index.js#L87) | ||
### [addAsyncHelper](index.js#L110) | ||
Set async helpers on the cache. | ||
* `key` **{String}**: The name of the helper. | ||
* `fn` **{Function}**: Helper function. | ||
* `returns` **{Object}**: Return `this` to enable chaining | ||
Set async helpers on the cache. | ||
```js | ||
helpers.addAsyncHelper('foo', function (name, next) { | ||
next(null, 'foo-' + name); | ||
}); | ||
``` | ||
### [addHelpers](index.js#L124) | ||
### [addHelpers](index.js#L165) | ||
@@ -50,6 +64,15 @@ Add an object of helpers to the cache. | ||
* `fn` **{Function}**: Helper function. | ||
* `returns` **{Object}**: Return `this` to enable chaining. | ||
See [load-helpers] for issues, API details and the full range of options. | ||
```js | ||
helpers.addHelpers({ | ||
foo: function (name) { | ||
return 'foo-' + name; | ||
}, | ||
bar: function (name) { | ||
return 'bar-' + name; | ||
} | ||
}); | ||
### [addAsyncHelpers](index.js#L157) | ||
### [addAsyncHelpers](index.js#L210) | ||
@@ -60,6 +83,15 @@ Add an object of async helpers to the cache. | ||
* `fn` **{Function}**: Helper function. | ||
* `returns` **{Object}**: Return `this` to enable chaining | ||
See [load-helpers] for issues, API details and the full range of options. | ||
```js | ||
helpers.addAsyncHelpers({ | ||
foo: function (name, next) { | ||
next(null, 'foo-' + name); | ||
}, | ||
bar: function (name, next) { | ||
next(null, 'bar-' + name); | ||
} | ||
}); | ||
### [getHelper](index.js#L176) | ||
### [getHelper](index.js#L234) | ||
@@ -71,3 +103,3 @@ * `key` **{String}**: The helper to get. | ||
### [getAsyncHelper](index.js#L194) | ||
### [getAsyncHelper](index.js#L252) | ||
@@ -79,3 +111,3 @@ * `key` **{String}**: The helper to get. | ||
### [resolve](index.js#L213) | ||
### [resolve](index.js#L271) | ||
@@ -82,0 +114,0 @@ * `content` **{String}**: Rendered string containing async ids |
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
45713
1293
137