base-helpers
Advanced tools
Comparing version 0.2.0 to 1.0.0
119
index.js
/*! | ||
* base-helpers (https://github.com/node-base/base-helpers) | ||
* base-helpers <https://github.com/node-base/base-helpers> | ||
* | ||
* Copyright (c) 2016, Jon Schlinkert. | ||
* Licensed under the MIT License. | ||
* Copyright (c) 2016-2017, Jon Schlinkert. | ||
* Released under the MIT License. | ||
*/ | ||
@@ -10,16 +10,17 @@ | ||
var debug = require('debug')('base:helpers'); | ||
var utils = require('./utils'); | ||
var isObject = require('isobject'); | ||
var HelperCache = require('helper-cache'); | ||
var isValidApp = require('is-valid-app'); | ||
var debug = require('debug')('base-helpers'); | ||
module.exports = function() { | ||
return function(app) { | ||
if (!utils.isValid(app)) return; | ||
if (!isValid(app)) return; | ||
app._ = app._ || {}; | ||
if (typeof app._ === 'undefined') { | ||
utils.define(app, '_', {}); | ||
} | ||
/** | ||
* Prime the helpers cache on "app" | ||
*/ | ||
if (typeof app._.helpers === 'undefined') { | ||
app._.helpers = {async: {}, sync: {}}; | ||
} | ||
var helpers = app._.helpers || (app._.helpers = {async: {}, sync: {}}); | ||
@@ -30,5 +31,8 @@ /** | ||
var async = utils.loader(app._.helpers.async, {async: true}); | ||
var sync = utils.loader(app._.helpers.sync); | ||
var async = new HelperCache({cache: helpers.async, async: true}); | ||
var sync = new HelperCache({cache: helpers.sync}); | ||
app._.helpers.async = async.cache; | ||
app._.helpers.sync = sync.cache; | ||
/** | ||
@@ -50,3 +54,3 @@ * Register a template helper. | ||
debug('registering sync helper "%s"', name); | ||
sync.apply(sync, arguments); | ||
sync.load.apply(sync, arguments); | ||
return this; | ||
@@ -71,6 +75,6 @@ }); | ||
app.define('helpers', function(name, helpers) { | ||
if (typeof name === 'string' && utils.isHelperGroup(helpers)) { | ||
if (typeof name === 'string' && isHelperGroup(helpers)) { | ||
return this.helperGroup.apply(this, arguments); | ||
} | ||
sync.apply(sync, arguments); | ||
sync.load.apply(sync, arguments); | ||
return this; | ||
@@ -93,5 +97,8 @@ }); | ||
app.define('asyncHelper', function(name) { | ||
app.define('asyncHelper', function(name, fn) { | ||
debug('registering async helper "%s"', name); | ||
async.apply(async, arguments); | ||
if (typeof fn === 'function') { | ||
fn.async = true; | ||
} | ||
async.load.apply(async, arguments); | ||
return this; | ||
@@ -116,6 +123,6 @@ }); | ||
app.define('asyncHelpers', function(name, helpers) { | ||
if (typeof name === 'string' && utils.isHelperGroup(helpers)) { | ||
if (typeof name === 'string' && isHelperGroup(helpers)) { | ||
return this.helperGroup.apply(this, arguments); | ||
} | ||
async.apply(async, arguments); | ||
async.load.apply(async, arguments); | ||
return this; | ||
@@ -138,3 +145,3 @@ }); | ||
debug('getting sync helper "%s"', name); | ||
return this.get(['_.helpers.sync', name]); | ||
return sync.getHelper(name); | ||
}); | ||
@@ -156,3 +163,3 @@ | ||
debug('getting async helper "%s"', name); | ||
return this.get(['_.helpers.async', name]); | ||
return async.getHelper(name); | ||
}); | ||
@@ -175,3 +182,3 @@ | ||
app.define('hasHelper', function(name) { | ||
return typeof this.getHelper(name) === 'function'; | ||
return typeof sync.getHelper(name) === 'function'; | ||
}); | ||
@@ -194,3 +201,3 @@ | ||
app.define('hasAsyncHelper', function(name) { | ||
return typeof this.getAsyncHelper(name) === 'function'; | ||
return typeof async.getHelper(name) === 'function'; | ||
}); | ||
@@ -219,24 +226,7 @@ | ||
debug('registering helper group "%s"', name); | ||
var type = isAsync ? 'async' : 'sync'; | ||
var group = this._.helpers[type][name] || (this._.helpers[type][name] = {}); | ||
if (typeof helpers === 'function' && utils.isHelperGroup(helpers)) { | ||
Object.defineProperty(helpers, 'isGroup', { | ||
enumerable: false, | ||
configurable: false, | ||
value: true | ||
}); | ||
if (isAsync === true) { | ||
decorateHelpers(helpers, helpers, isAsync); | ||
} | ||
decorateHelpers(helpers, group, isAsync, true); | ||
this._.helpers[type][name] = helpers; | ||
return this; | ||
if (isAsync) { | ||
async.loadGroup(name, helpers); | ||
} else { | ||
sync.loadGroup(name, helpers); | ||
} | ||
helpers = utils.arrayify(helpers); | ||
var loader = utils.loader(group, {async: isAsync}); | ||
loader.call(loader, helpers); | ||
return this; | ||
@@ -247,15 +237,30 @@ }); | ||
function decorateHelpers(oldHelpers, newHelpers, isAsync, override) { | ||
for (let key in newHelpers) { | ||
if (newHelpers.hasOwnProperty(key)) { | ||
// "newHelpers" is the helpers being passed by the user | ||
if (override === true && oldHelpers[key]) continue; | ||
let fn = newHelpers[key]; | ||
if (typeof fn === 'function') { | ||
fn.async = isAsync; | ||
} | ||
oldHelpers[key] = fn; | ||
} | ||
/** | ||
* Return false if `app` is not a valid instance of `Base`, or | ||
* the `base-helpers` plugin is alread registered. | ||
*/ | ||
function isValid(app) { | ||
if (isValidApp(app, 'base-helpers', ['app', 'views', 'collection'])) { | ||
debug('initializing <%s>, from <%s>', __filename, module.parent.id); | ||
return true; | ||
} | ||
return false; | ||
} | ||
/** | ||
* Return true if the given value is a helper "group" | ||
*/ | ||
function isHelperGroup(helpers) { | ||
if (!helpers) return false; | ||
if (typeof helpers === 'function' || isObject(helpers)) { | ||
var len = Object.keys(helpers).length; | ||
var min = helpers.async ? 1 : 0; | ||
return helpers.isGroup === true || len > min; | ||
} | ||
if (Array.isArray(helpers)) { | ||
return helpers.isGroup === true; | ||
} | ||
return false; | ||
} |
{ | ||
"name": "base-helpers", | ||
"description": "Adds support for managing template helpers to your base application.", | ||
"version": "0.2.0", | ||
"version": "1.0.0", | ||
"homepage": "https://github.com/node-base/base-helpers", | ||
@@ -13,4 +13,3 @@ "author": "Jon Schlinkert (https://github.com/jonschlinkert)", | ||
"files": [ | ||
"index.js", | ||
"utils.js" | ||
"index.js" | ||
], | ||
@@ -25,12 +24,10 @@ "main": "index.js", | ||
"dependencies": { | ||
"debug": "^2.6.0", | ||
"define-property": "^0.2.5", | ||
"is-valid-app": "^0.2.1", | ||
"isobject": "^3.0.0", | ||
"lazy-cache": "^2.0.2", | ||
"load-helpers": "^0.3.1" | ||
"debug": "^2.6.3", | ||
"helper-cache": "^1.0.0", | ||
"is-valid-app": "^0.3.0", | ||
"isobject": "^3.0.0" | ||
}, | ||
"devDependencies": { | ||
"base": "^0.11.1", | ||
"gulp-format-md": "^0.1.11", | ||
"base": "^0.13.0", | ||
"gulp-format-md": "^0.1.12", | ||
"mocha": "^3.2.0" | ||
@@ -44,2 +41,3 @@ }, | ||
"base", | ||
"base-plugin", | ||
"baseplugin", | ||
@@ -80,4 +78,4 @@ "building-blocks", | ||
"base", | ||
"verb", | ||
"verb-readme-generator" | ||
"helper-cache", | ||
"load-helpers" | ||
], | ||
@@ -84,0 +82,0 @@ "lint": { |
@@ -13,2 +13,8 @@ # base-helpers [![NPM version](https://img.shields.io/npm/v/base-helpers.svg?style=flat)](https://www.npmjs.com/package/base-helpers) [![NPM monthly downloads](https://img.shields.io/npm/dm/base-helpers.svg?style=flat)](https://npmjs.org/package/base-helpers) [![NPM total downloads](https://img.shields.io/npm/dt/base-helpers.svg?style=flat)](https://npmjs.org/package/base-helpers) [![Linux Build Status](https://img.shields.io/travis/node-base/base-helpers.svg?style=flat&label=Travis)](https://travis-ci.org/node-base/base-helpers) | ||
Install with [yarn](https://yarnpkg.com): | ||
```sh | ||
$ yarn add base-helpers | ||
``` | ||
## Usage | ||
@@ -26,3 +32,3 @@ | ||
### [.helper](index.js#L46) | ||
### [.helper](index.js#L50) | ||
@@ -44,3 +50,3 @@ Register a template helper. | ||
### [.helpers](index.js#L67) | ||
### [.helpers](index.js#L71) | ||
@@ -63,3 +69,3 @@ Register multiple template helpers. | ||
### [.asyncHelper](index.js#L89) | ||
### [.asyncHelper](index.js#L93) | ||
@@ -81,3 +87,3 @@ Register an async helper. | ||
### [.asyncHelpers](index.js#L110) | ||
### [.asyncHelpers](index.js#L117) | ||
@@ -100,3 +106,3 @@ Register multiple async template helpers. | ||
### [.getHelper](index.js#L130) | ||
### [.getHelper](index.js#L137) | ||
@@ -116,3 +122,3 @@ Get a previously registered helper. | ||
### [.getAsyncHelper](index.js#L147) | ||
### [.getAsyncHelper](index.js#L154) | ||
@@ -132,3 +138,3 @@ Get a previously registered async helper. | ||
### [.hasHelper](index.js#L166) | ||
### [.hasHelper](index.js#L173) | ||
@@ -150,3 +156,3 @@ Return true if sync helper `name` is registered. | ||
### [.hasAsyncHelper](index.js#L184) | ||
### [.hasAsyncHelper](index.js#L191) | ||
@@ -168,3 +174,3 @@ Return true if async helper `name` is registered. | ||
### [.helperGroup](index.js#L207) | ||
### [.helperGroup](index.js#L214) | ||
@@ -193,2 +199,6 @@ Register a namespaced helper group. | ||
### v1.0.0 | ||
* upgrades dependencies to take advantage of improvements to [load-helpers](https://github.com/jonschlinkert/load-helpers) and [helper-cache](https://github.com/jonschlinkert/helper-cache). There shouldn't be any breaking changes here, so if you experience regressions please do not hesitate to [create an issue](../../new). | ||
### v0.2.0 | ||
@@ -205,3 +215,3 @@ | ||
* [base-task](https://www.npmjs.com/package/base-task): base plugin that provides a very thin wrapper around [https://github.com/doowb/composer](https://github.com/doowb/composer) for adding task methods to… [more](https://github.com/node-base/base-task) | [homepage](https://github.com/node-base/base-task "base plugin that provides a very thin wrapper around <https://github.com/doowb/composer> for adding task methods to your application.") | ||
* [base](https://www.npmjs.com/package/base): base is the foundation for creating modular, unit testable and highly pluggable node.js applications, starting… [more](https://github.com/node-base/base) | [homepage](https://github.com/node-base/base "base is the foundation for creating modular, unit testable and highly pluggable node.js applications, starting with a handful of common methods, like `set`, `get`, `del` and `use`.") | ||
* [base](https://www.npmjs.com/package/base): Framework for rapidly creating high quality node.js applications, using plugins like building blocks | [homepage](https://github.com/node-base/base "Framework for rapidly creating high quality node.js applications, using plugins like building blocks") | ||
@@ -214,8 +224,8 @@ ### Contributing | ||
_(This document was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme) (a [verb](https://github.com/verbose/verb) generator), please don't edit the readme directly. Any changes to the readme must be made in [.verb.md](.verb.md).)_ | ||
_(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 and API documentation with [verb](https://github.com/verbose/verb): | ||
To generate the readme, run the following command: | ||
```sh | ||
$ npm install -g verb verb-generate-readme && verb | ||
$ npm install -g verbose/verb#dev verb-generate-readme && verb | ||
``` | ||
@@ -225,6 +235,6 @@ | ||
Install dev dependencies: | ||
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 -d && npm test | ||
$ npm install && npm test | ||
``` | ||
@@ -242,6 +252,6 @@ | ||
Copyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert). | ||
Released under the [MIT license](LICENSE). | ||
Released under the [MIT License](LICENSE). | ||
*** | ||
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.4.1, on January 17, 2017._ | ||
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.5.0, on April 20, 2017._ |
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
4
0
242
1
15366
4
224
+ Addedhelper-cache@^1.0.0
+ Addedcollection-visit@1.0.0(transitive)
+ Addeddefine-property@1.0.0(transitive)
+ Addedget-value@2.0.6(transitive)
+ Addedhelper-cache@1.0.0(transitive)
+ Addedis-descriptor@1.0.3(transitive)
+ Addedis-extglob@2.1.1(transitive)
+ Addedis-glob@3.1.0(transitive)
+ Addedis-plain-object@2.0.4(transitive)
+ Addedis-valid-app@0.3.0(transitive)
+ Addedis-valid-instance@0.3.0(transitive)
+ Addedload-helpers@1.0.1(transitive)
+ Addedmap-visit@1.0.0(transitive)
+ Addedobject-visit@1.0.1(transitive)
+ Addedset-value@0.4.3(transitive)
- Removeddefine-property@^0.2.5
- Removedlazy-cache@^2.0.2
- Removedload-helpers@^0.3.1
- Removedis-valid-app@0.2.1(transitive)
- Removedis-valid-instance@0.2.0(transitive)
- Removedload-helpers@0.3.1(transitive)
Updateddebug@^2.6.3
Updatedis-valid-app@^0.3.0