New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

helper-cache

Package Overview
Dependencies
Maintainers
2
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

helper-cache - npm Package Compare versions

Comparing version 0.7.2 to 1.0.0

378

index.js
'use strict';
var lazy = require('lazy-cache')(require);
lazy('extend-shallow', 'extend');
lazy('lodash.bind', 'bind');
var get = require('get-value');
var visit = require('collection-visit');
var define = require('define-property');
var Loader = require('load-helpers');
/**
* Create an instance of `HelperCache`, optionally passing
* default `options`.
* Create an instance of `HelperCache` with the given `options.`
*
* ```js
* var HelperCache = require('helper-cache');
* var helpers = new HelperCache();
* var App = require('helper-cache');
* var app = new App();
* ```
*
* @param {Object} `options` Default options to use.
* @option {Boolean} [options] `bind` Bind functions to `this`. Defaults to `false`.
* @option {Boolean} [options] `thisArg` The context to use.
* @param {Object} `options`
* @api public
*/
function HelperCache(opts) {
function HelperCache(options) {
if (!(this instanceof HelperCache)) {
return new HelperCache(opts);
return new HelperCache(options);
}
defineGetter(this, 'options', function () {
return lazy.extend({bind: false, thisArg: null }, opts);
});
options = options || {};
define(this, 'loader', new Loader(options));
this.cache = this.loader.cache;
if (options.async === true) {
this.async = true;
}
}

@@ -36,226 +37,231 @@

* ```js
* helpers.addHelper('lower', function(str) {
* return str.toLowerCase();
* app.helper('uppercase', function(str) {
* return str.toUpperCase();
* });
* ```
* @param {String} `name`
* @param {Function} `fn`
* @return {Object} Retuns the instance of `HelperCache` for chaining.
* @api public
*/
HelperCache.prototype.addHelper = function(name, fn, options) {
this.loader.addHelper.apply(this.loader, arguments);
return this;
};
/**
* Get a helper.
*
* @name .addHelper
* @param {String} `name` The name of the helper.
* @param {Function} `fn` Helper function.
* @return {Object} Return `this` to enable chaining
* ```js
* app.helper('uppercase', function(str) {
* return str.toUpperCase();
* });
* ```
* @param {String} `name`
* @param {Function} `fn`
* @return {Object} Retuns the instance of `HelperCache` for chaining.
* @api public
*/
defineGetter(HelperCache.prototype, 'addHelper', function () {
return function (name, fn, thisArg) {
thisArg = thisArg || this.options.thisArg;
HelperCache.prototype.getHelper = function(name) {
return get(this.loader.cache, name);
};
// `addHelpers` handles functions
if (typeof name === 'function') {
return this.addHelpers.call(this, arguments);
}
/**
* Register a sync template helper `fn` as `name`.
*
* ```js
* app.helper('uppercase', function(str) {
* return str.toUpperCase();
* });
* ```
* @param {String} `name`
* @param {Function} `fn`
* @return {Object} Retuns the instance of `HelperCache` for chaining.
* @api public
*/
if (typeof name === 'object') {
for (var key in name) {
this.addHelper(key, name[key], thisArg);
}
} else {
// 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)) {
res[prop] = lazy.bind(fn[prop], thisArg);
}
}
this[name] = res;
} else {
this[name] = lazy.bind(fn, thisArg);
}
} else {
this[name] = fn;
}
}
HelperCache.prototype.helper = function(name, fn) {
if (arguments.length === 1 && typeof name === 'string') {
return this.getHelper(name);
}
if (isObject(name)) {
return this.helpers.apply(this, arguments);
}
if (isObject(fn) && typeof fn.async !== 'boolean') {
return this.asyncGroup.apply(this, arguments);
}
if (typeof fn !== 'function') {
throw new TypeError('expected a function');
}
this.addHelper(name, fn);
return this;
};
// chaining
return this;
}.bind(this);
});
/**
* Register an async helper.
* Register multiple sync helpers at once.
*
* ```js
* helpers.addAsyncHelper('foo', function (str, callback) {
* callback(null, str + ' foo');
* app.helpers({
* foo: function() {},
* bar: function() {},
* baz: function() {}
* });
* ```
*
* @name .addAsyncHelper
* @param {String} `key` The name of the helper.
* @param {Function} `fn` Helper function.
* @return {Object} Return `this` to enable chaining
* @param {Object} `helpers` Array of globs, file paths or key-value pair helper objects.
* @return {Object} Retuns the instance of `HelperCache` for chaining.
* @api public
*/
defineGetter(HelperCache.prototype, 'addAsyncHelper', function () {
return function(name, fn, thisArg) {
// `addAsyncHelpers` handles functions
if (typeof name === 'function') {
return this.addAsyncHelpers.call(this, arguments);
}
HelperCache.prototype.helpers = function(helpers) {
this.visit('helper', helpers);
return this;
};
// pass each key/value pair to `addAsyncHelper`
if (typeof name === 'object') {
for (var key in name) {
if (name.hasOwnProperty(key)) {
this.addAsyncHelper(key, name[key], thisArg);
}
}
} else {
// 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] = lazy.bind(val, thisArg);
}
}
this[name] = res;
} else {
fn.async = true;
this[name] = lazy.bind(fn, thisArg);
}
} else {
fn.async = true;
this[name] = fn;
}
}
/**
* Register an async template helper `fn` as `name`.
*
* ```js
* app.asyncHelper('uppercase', function(str) {
* return str.toUpperCase();
* });
* ```
* @param {String} `name`
* @param {Function} `fn`
* @return {Object} Retuns the instance of `HelperCache` for chaining.
* @api public
*/
return this;
}.bind(this);
});
HelperCache.prototype.asyncHelper = function(name, fn) {
if (arguments.length === 1 && typeof name === 'string') {
return this.helper(name);
}
if (isObject(name)) {
return this.asyncHelpers.apply(this, arguments);
}
if (typeof fn !== 'function') {
throw new TypeError('expected a function');
}
this.addHelper(name, toAsync(fn), {async: true});
return this;
};
/**
* Load an object of helpers.
* Register multiple async helpers at once.
*
* ```js
* helpers.addHelpers({
* a: function() {},
* b: function() {},
* c: function() {},
* app.asyncHelpers({
* foo: function() {},
* bar: function() {},
* baz: function() {}
* });
* ```
*
* @name .addHelpers
* @param {String} `key` The name of the helper.
* @param {Function} `fn` Helper function.
* @return {Object} Return `this` to enable chaining.
* @param {Object} `helpers` Array of globs, file paths or key-value pair helper objects.
* @return {Object} Retuns the instance of `HelperCache` for chaining.
* @api public
*/
defineGetter(HelperCache.prototype, 'addHelpers', function () {
return function (helpers, thisArg) {
thisArg = thisArg || this.options.thisArg;
HelperCache.prototype.asyncHelpers = function(helpers) {
return this.visit('asyncHelper', helpers);
};
// when a function is passed, execute it and use the results
if (typeof helpers === 'function') {
return this.addHelpers(helpers(thisArg), thisArg);
}
/**
* Namespace a collection of sync helpers on the given `prop`.
*
* ```js
* app.group('mdu', require('markdown-utils'));
* // Usage: '<%= mdu.heading("My heading") %>'
* ```
* @param {Object|Array} `helpers` Object, array of objects, or glob patterns.
* @api public
*/
// allow binding each helper if enabled
for (var key in helpers) {
if (helpers.hasOwnProperty(key)) {
this.addHelper(key, helpers[key], thisArg);
}
}
return this;
}.bind(this);
});
HelperCache.prototype.group = function(prop, helpers) {
if (!isObject(helpers)) {
throw new TypeError('expected an object');
}
var keys = Object.keys(helpers);
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
this.set([prop, key], helpers[key]);
}
return this;
};
/**
* Load an object of async helpers.
* Namespace a collection of async helpers on the given `prop`.
*
* ```js
* helpers.addAsyncHelpers({
* a: function() {},
* b: function() {},
* c: function() {},
* });
* app.asyncGroup('mdu', require('markdown-utils'));
* // Usage: '<%= mdu.heading("My heading") %>'
* ```
*
* @name .addAsyncHelpers
* @param {String} `key` The name of the helper.
* @param {Function} `fn` Helper function.
* @return {Object} Return `this` to enable chaining
* @param {Object|Array} `helpers` Object, array of objects, or glob patterns.
* @api public
*/
defineGetter(HelperCache.prototype, 'addAsyncHelpers', function () {
return function (helpers, thisArg) {
// when a function is passed, execute it and use the results
if (typeof helpers === 'function') {
thisArg = thisArg || this.options.thisArg;
return this.addAsyncHelpers(helpers(thisArg), thisArg);
}
HelperCache.prototype.asyncGroup = function(name, helpers) {
if (typeof helpers === 'function') {
return this.asyncGroup(helpers(this.options));
}
if (typeof helpers === 'object') {
for (var key in helpers) {
if (helpers.hasOwnProperty(key)) {
this.addAsyncHelper(key, helpers[key], thisArg);
}
}
}
return this;
}.bind(this);
});
if (!isObject(helpers)) {
throw new TypeError('expected an object');
}
var keys = Object.keys(helpers);
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
this.addHelper(name + '.' + key, toAsync(helpers[key]));
}
return this;
};
/**
* Get a registered helper.
* Load helpers.
*
* ```js
* helpers.getHelper('foo');
* app.load({
* foo: function() {},
* bar: function() {},
* baz: function() {}
* });
* ```
*
* @name .getHelper
* @param {String} `key` The helper to get.
* @return {Object} The specified helper. If no `key` is passed, the entire cache is returned.
* @param {Object} `helpers` Array of globs, file paths or key-value pair helper objects.
* @return {Object} Retuns the instance of `HelperCache` for chaining.
* @api public
*/
defineGetter(HelperCache.prototype, 'getHelper', function () {
return function(key) {
return typeof key === 'string' ? this[key] : this;
}.bind(this);
});
HelperCache.prototype.load = function() {
this.loader.load.apply(this.loader, arguments);
return this;
};
HelperCache.prototype.loadGroup = function() {
this.loader.loadGroup.apply(this.loader, arguments);
return this;
};
HelperCache.prototype.visit = function(method, val) {
visit.apply(null, [this].concat([].slice.call(arguments)));
return this;
};
/**
* Utility method to define getters.
*
* @param {Object} `obj`
* @param {String} `name`
* @param {Function} `getter`
* @return {Getter}
* @api private
* Expose `helper-cache`
*/
function defineGetter(obj, name, getter) {
Object.defineProperty(obj, name, {
configurable: false,
enumerable: false,
get: getter,
set: function() {
throw new Error(name + ' is a read-only getter.');
}
});
}
module.exports = HelperCache;
/**
* Expose `HelperCache`
* Return true if a value is an object
*/
module.exports = HelperCache;
function isObject(val) {
return val && typeof val === 'object' && !Array.isArray(val);
}
function toAsync(fn) {
fn.async = true;
return fn;
}
{
"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.7.2",
"homepage": "https://github.com/jonschlinkert/helper-cache",
"repository": "jonschlinkert/helper-cache",
"version": "1.0.0",
"homepage": "https://github.com/helpers/helper-cache",
"author": "Jon Schlinkert (https://github.com/jonschlinkert)",
"maintainers": [
{
"name": "Brian Woodward",
"url": "https://github.com/doowb"
}
"Brian Woodward (https://github.com/doowb)"
],
"repository": "helpers/helper-cache",
"bugs": {
"url": "https://github.com/jonschlinkert/helper-cache/issues"
"url": "https://github.com/helpers/helper-cache/issues"
},

@@ -28,3 +25,16 @@ "license": "MIT",

},
"dependencies": {
"collection-visit": "^1.0.0",
"define-property": "^1.0.0",
"get-value": "^2.0.6",
"load-helpers": "^1.0.1"
},
"devDependencies": {
"gulp-format-md": "^0.1.12",
"mocha": "^3.2.0",
"should": "^11.2.1"
},
"keywords": [
"cache",
"compile",
"consolidate",

@@ -34,19 +44,34 @@ "engine",

"helpers",
"lo-dash",
"lodash",
"render",
"template",
"lo-dash",
"lodash"
"templates"
],
"dependencies": {
"extend-shallow": "^2.0.1",
"lazy-cache": "^0.2.3",
"lodash.bind": "^3.1.0"
},
"devDependencies": {
"globby": "^2.0.0",
"kind-of": "^2.0.0",
"loader-cache": "^0.4.0",
"mocha": "^2.2.5",
"object.reduce": "^0.1.7",
"should": "^6.0.3"
"verb": {
"toc": true,
"layout": "default",
"tasks": [
"readme"
],
"plugins": [
"gulp-format-md"
],
"lint": {
"reflinks": true
},
"related": {
"list": [
"engine-cache",
"handlebars-helpers",
"template",
"template-helpers"
]
},
"reflinks": [
"generate",
"generate-helper",
"helpers"
]
}
}

@@ -1,129 +0,248 @@

# 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)
# helper-cache [![NPM version](https://img.shields.io/npm/v/helper-cache.svg?style=flat)](https://www.npmjs.com/package/helper-cache) [![NPM monthly downloads](https://img.shields.io/npm/dm/helper-cache.svg?style=flat)](https://npmjs.org/package/helper-cache) [![NPM total downloads](https://img.shields.io/npm/dt/helper-cache.svg?style=flat)](https://npmjs.org/package/helper-cache) [![Linux Build Status](https://img.shields.io/travis/helpers/helper-cache.svg?style=flat&label=Travis)](https://travis-ci.org/helpers/helper-cache)
> Easily register and get helper functions to be passed to any template engine or node.js application. Methods for both sync and async helpers.
## Install with [npm](npmjs.org)
## Table of Contents
```bash
npm i helper-cache --save
- [Install](#install)
- [API](#api)
- [About](#about)
_(TOC generated by [verb](https://github.com/verbose/verb) using [markdown-toc](https://github.com/jonschlinkert/markdown-toc))_
## Install
Install with [npm](https://www.npmjs.com/):
```sh
$ npm install --save helper-cache
```
## Usage
Install with [yarn](https://yarnpkg.com):
### [HelperCache](index.js#L27)
```sh
$ yarn add helper-cache
```
Create an instance of `HelperCache`, optionally passing default `options`.
## API
* `options` **{Object}**: Default options to use.
- `bind` **{Boolean}**: Bind functions to `this`. Defaults to `false`.
- `thisArg` **{Boolean}**: The context to use.
### [HelperCache](index.js#L19)
Create an instance of `HelperCache` with the given `options.`
**Params**
* `options` **{Object}**
**Example**
```js
var HelperCache = require('helper-cache');
var helpers = new HelperCache();
var App = require('helper-cache');
var app = new App();
```
### [.addHelper](index.js#L53)
### [.addHelper](index.js#L47)
Register a helper.
* `name` **{String}**: The name of the helper.
* `fn` **{Function}**: Helper function.
* `returns` **{Object}**: Return `this` to enable chaining
**Params**
* `name` **{String}**
* `fn` **{Function}**
* `returns` **{Object}**: Retuns the instance of `HelperCache` for chaining.
**Example**
```js
helpers.addHelper('lower', function(str) {
return str.toLowerCase();
app.helper('uppercase', function(str) {
return str.toUpperCase();
});
```
### [.addAsyncHelper](index.js#L106)
### [.getHelper](index.js#L66)
Register an async helper.
Get a helper.
* `key` **{String}**: The name of the helper.
* `fn` **{Function}**: Helper function.
* `returns` **{Object}**: Return `this` to enable chaining
**Params**
* `name` **{String}**
* `fn` **{Function}**
* `returns` **{Object}**: Retuns the instance of `HelperCache` for chaining.
**Example**
```js
helpers.addAsyncHelper('foo', function (str, callback) {
callback(null, str + ' foo');
app.helper('uppercase', function(str) {
return str.toUpperCase();
});
```
### [.addHelpers](index.js#L165)
### [.helper](index.js#L84)
Load an object of helpers.
Register a sync template helper `fn` as `name`.
* `key` **{String}**: The name of the helper.
* `fn` **{Function}**: Helper function.
* `returns` **{Object}**: Return `this` to enable chaining.
**Params**
* `name` **{String}**
* `fn` **{Function}**
* `returns` **{Object}**: Retuns the instance of `HelperCache` for chaining.
**Example**
```js
helpers.addHelpers({
a: function() {},
b: function() {},
c: function() {},
app.helper('uppercase', function(str) {
return str.toUpperCase();
});
```
### [.addAsyncHelpers](index.js#L202)
### [.helpers](index.js#L116)
Load an object of async helpers.
Register multiple sync helpers at once.
* `key` **{String}**: The name of the helper.
* `fn` **{Function}**: Helper function.
* `returns` **{Object}**: Return `this` to enable chaining
**Params**
* `helpers` **{Object}**: Array of globs, file paths or key-value pair helper objects.
* `returns` **{Object}**: Retuns the instance of `HelperCache` for chaining.
**Example**
```js
helpers.addAsyncHelpers({
a: function() {},
b: function() {},
c: function() {},
app.helpers({
foo: function() {},
bar: function() {},
baz: function() {}
});
```
### [.getHelper](index.js#L234)
### [.asyncHelper](index.js#L135)
Get a registered helper.
Register an async template helper `fn` as `name`.
* `key` **{String}**: The helper to get.
* `returns` **{Object}**: The specified helper. If no `key` is passed, the entire cache is returned.
**Params**
* `name` **{String}**
* `fn` **{Function}**
* `returns` **{Object}**: Retuns the instance of `HelperCache` for chaining.
**Example**
```js
helpers.getHelper('foo');
app.asyncHelper('uppercase', function(str) {
return str.toUpperCase();
});
```
## 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)
### [.asyncHelpers](index.js#L164)
## Running tests
Install dev dependencies:
Register multiple async helpers at once.
```bash
npm i -d && npm test
**Params**
* `helpers` **{Object}**: Array of globs, file paths or key-value pair helper objects.
* `returns` **{Object}**: Retuns the instance of `HelperCache` for chaining.
**Example**
```js
app.asyncHelpers({
foo: function() {},
bar: function() {},
baz: function() {}
});
```
## Contributing
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/jonschlinkert/helper-cache/issues)
### [.group](index.js#L179)
## Author
Namespace a collection of sync helpers on the given `prop`.
**Params**
* `helpers` **{Object|Array}**: Object, array of objects, or glob patterns.
**Example**
```js
app.group('mdu', require('markdown-utils'));
// Usage: '<%= mdu.heading("My heading") %>'
```
### [.asyncGroup](index.js#L202)
Namespace a collection of async helpers on the given `prop`.
**Params**
* `helpers` **{Object|Array}**: Object, array of objects, or glob patterns.
**Example**
```js
app.asyncGroup('mdu', require('markdown-utils'));
// Usage: '<%= mdu.heading("My heading") %>'
```
### [.load](index.js#L234)
Load helpers.
**Params**
* `helpers` **{Object}**: Array of globs, file paths or key-value pair helper objects.
* `returns` **{Object}**: Retuns the instance of `HelperCache` for chaining.
**Example**
```js
app.load({
foo: function() {},
bar: function() {},
baz: function() {}
});
```
## About
### Related projects
* [engine-cache](https://www.npmjs.com/package/engine-cache): express.js inspired template-engine manager. | [homepage](https://github.com/jonschlinkert/engine-cache "express.js inspired template-engine manager.")
* [handlebars-helpers](https://www.npmjs.com/package/handlebars-helpers): More than 130 Handlebars helpers in ~20 categories. Helpers can be used with Assemble, Generate… [more](https://github.com/helpers/handlebars-helpers) | [homepage](https://github.com/helpers/handlebars-helpers "More than 130 Handlebars helpers in ~20 categories. Helpers can be used with Assemble, Generate, Verb, Ghost, gulp-handlebars, grunt-handlebars, consolidate, or any node.js/Handlebars project.")
* [template-helpers](https://www.npmjs.com/package/template-helpers): Generic JavaScript helpers that can be used with any template engine. Handlebars, Lo-Dash, Underscore, or… [more](https://github.com/jonschlinkert/template-helpers) | [homepage](https://github.com/jonschlinkert/template-helpers "Generic JavaScript helpers that can be used with any template engine. Handlebars, Lo-Dash, Underscore, or any engine that supports helper functions.")
* [template](https://www.npmjs.com/package/template): Render templates using any engine. Supports, layouts, pages, partials and custom template types. Use template… [more](https://github.com/jonschlinkert/template) | [homepage](https://github.com/jonschlinkert/template "Render templates using any engine. Supports, layouts, pages, partials and custom template types. Use template helpers, middleware, routes, loaders, and lots more. Powers assemble, verb and other node.js apps.")
### Contributing
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
### Building docs
_(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_
To generate the readme, run the following command:
```sh
$ npm install -g verbose/verb#dev verb-generate-readme && verb
```
### Running tests
Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:
```sh
$ npm install && npm test
```
### Author
**Jon Schlinkert**
+ [github/jonschlinkert](https://github.com/jonschlinkert)
+ [twitter/jonschlinkert](http://twitter.com/jonschlinkert)
* [github/jonschlinkert](https://github.com/jonschlinkert)
* [twitter/jonschlinkert](https://twitter.com/jonschlinkert)
## License
Copyright (c) 2014-2015 Jon Schlinkert
Released under the MIT license
### License
Copyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert).
Released under the [MIT License](LICENSE).
***
_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
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.5.0, on April 20, 2017._

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc