use
Advanced tools
Comparing version 2.0.2 to 3.0.0
79
index.js
@@ -10,16 +10,14 @@ /*! | ||
var utils = require('./utils'); | ||
var isObject = require('isobject'); | ||
var define = require('define-property'); | ||
module.exports = function base(app, opts) { | ||
if (!utils.isObject(app) && typeof app !== 'function') { | ||
throw new TypeError('use: expect `app` be an object or function'); | ||
module.exports = function base(app, options) { | ||
if (!isObject(app) && typeof app !== 'function') { | ||
throw new TypeError('expected an object or function'); | ||
} | ||
if (!utils.isObject(opts)) { | ||
opts = {}; | ||
} | ||
var prop = utils.isString(opts.prop) ? opts.prop : 'fns'; | ||
var opts = isObject(options) ? options : {}; | ||
var prop = typeof opts.prop === 'string' ? opts.prop : 'fns'; | ||
if (!Array.isArray(app[prop])) { | ||
utils.define(app, prop, []); | ||
define(app, prop, []); | ||
} | ||
@@ -57,3 +55,3 @@ | ||
utils.define(app, 'use', use); | ||
define(app, 'use', use); | ||
@@ -74,6 +72,14 @@ /** | ||
utils.define(app, 'run', function(val) { | ||
if (!utils.isObject(val)) return; | ||
decorate(val); | ||
define(app, 'run', function(val) { | ||
if (!isObject(val)) return; | ||
if (!val.use || !val.run) { | ||
define(val, prop, val[prop] || []); | ||
define(val, 'use', use); | ||
} | ||
if (!val[prop] || val[prop].indexOf(base) === -1) { | ||
val.use(base); | ||
} | ||
var self = this || app; | ||
@@ -95,16 +101,30 @@ var fns = self[prop]; | ||
function use(fn, options) { | ||
function use(type, fn, options) { | ||
var idx = 1; | ||
if (typeof type === 'string' || Array.isArray(type)) { | ||
fn = wrap(type, fn); | ||
idx++; | ||
} else { | ||
options = fn; | ||
fn = type; | ||
} | ||
if (typeof fn !== 'function') { | ||
throw new TypeError('.use expects `fn` be a function'); | ||
throw new TypeError('expected a function'); | ||
} | ||
var self = this || app; | ||
if (typeof opts.fn === 'function') { | ||
opts.fn.call(self, self, options); | ||
var fns = self[prop]; | ||
var args = [].slice.call(arguments, idx); | ||
args.unshift(self); | ||
if (typeof opts.hook === 'function') { | ||
opts.hook.apply(self, args); | ||
} | ||
var plugin = fn.call(self, self); | ||
if (typeof plugin === 'function') { | ||
var fns = self[prop]; | ||
fns.push(plugin); | ||
var val = fn.apply(self, args); | ||
if (typeof val === 'function' && fns.indexOf(val) === -1) { | ||
fns.push(val); | ||
} | ||
@@ -115,9 +135,14 @@ return self; | ||
/** | ||
* Ensure the `.use` method exists on `val` | ||
* Wrap a named plugin function so that it's only called on objects of the | ||
* given `type` | ||
* | ||
* @param {String} `type` | ||
* @param {Function} `fn` Plugin function | ||
* @return {Function} | ||
*/ | ||
function decorate(val) { | ||
if (!val.use || !val.run) { | ||
base(val); | ||
} | ||
function wrap(type, fn) { | ||
return function plugin() { | ||
return this.type === type ? fn.apply(this, arguments) : plugin; | ||
}; | ||
} | ||
@@ -124,0 +149,0 @@ |
{ | ||
"name": "use", | ||
"description": "Easily add plugin support to your node.js application.", | ||
"version": "2.0.2", | ||
"version": "3.0.0", | ||
"homepage": "https://github.com/jonschlinkert/use", | ||
@@ -9,3 +9,3 @@ "author": "Jon Schlinkert (https://github.com/jonschlinkert)", | ||
"<wtgtybhertgeghgtwtg@gmail.com> (https://github.com/wtgtybhertgeghgtwtg)", | ||
"Brian Woodward <brian.woodward@gmail.com> (https://github.com/doowb)", | ||
"Brian Woodward <brian.woodward@gmail.com> (https://twitter.com/doowb)", | ||
"Charlike Mike Reagent (https://i.am.charlike.online)", | ||
@@ -20,4 +20,3 @@ "Jon Schlinkert <jon.schlinkert@sellside.com> (http://twitter.com/jonschlinkert)" | ||
"files": [ | ||
"index.js", | ||
"utils.js" | ||
"index.js" | ||
], | ||
@@ -33,13 +32,12 @@ "main": "index.js", | ||
"define-property": "^0.2.5", | ||
"isobject": "^3.0.0", | ||
"lazy-cache": "^2.0.2" | ||
"isobject": "^3.0.0" | ||
}, | ||
"devDependencies": { | ||
"base-plugins": "^0.4.13", | ||
"base-plugins": "^1.0.0", | ||
"extend-shallow": "^2.0.1", | ||
"gulp": "^3.9.1", | ||
"gulp-eslint": "^3.0.1", | ||
"gulp-format-md": "^0.1.11", | ||
"gulp-format-md": "^0.1.12", | ||
"gulp-istanbul": "^1.1.1", | ||
"gulp-mocha": "^4.0.1", | ||
"gulp-mocha": "^3.0.0", | ||
"mocha": "^3.2.0" | ||
@@ -46,0 +44,0 @@ }, |
@@ -1,2 +0,2 @@ | ||
# use [![NPM version](https://img.shields.io/npm/v/use.svg?style=flat)](https://www.npmjs.com/package/use) [![NPM monthly downloads](https://img.shields.io/npm/dm/use.svg?style=flat)](https://npmjs.org/package/use) [![NPM total downloads](https://img.shields.io/npm/dt/use.svg?style=flat)](https://npmjs.org/package/use) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/use.svg?style=flat&label=Travis)](https://travis-ci.org/jonschlinkert/use) | ||
# use [![NPM version](https://img.shields.io/npm/v/use.svg?style=flat)](https://www.npmjs.com/package/use) [![NPM monthly downloads](https://img.shields.io/npm/dm/use.svg?style=flat)](https://npmjs.org/package/use) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/use.svg?style=flat&label=Travis)](https://travis-ci.org/jonschlinkert/use) | ||
@@ -27,4 +27,4 @@ > Easily add plugin support to your node.js application. | ||
* [base-plugins](https://www.npmjs.com/package/base-plugins): Upgrade's plugin support in base applications to allow plugins to be called any time after… [more](https://github.com/node-base/base-plugins) | [homepage](https://github.com/node-base/base-plugins "Upgrade's plugin support in base applications to allow plugins to be called any time after init.") | ||
* [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-plugins](https://www.npmjs.com/package/base-plugins): Adds 'smart plugin' support to your base application. | [homepage](https://github.com/node-base/base-plugins "Adds 'smart plugin' support to your base application.") | ||
* [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") | ||
* [ware](https://www.npmjs.com/package/ware): Easily create your own middleware layer. | [homepage](https://github.com/segmentio/ware "Easily create your own middleware layer.") | ||
@@ -40,6 +40,6 @@ | ||
| --- | --- | | ||
| 22 | [jonschlinkert](https://github.com/jonschlinkert) | | ||
| 27 | [jonschlinkert](https://github.com/jonschlinkert) | | ||
| 7 | [tunnckoCore](https://github.com/tunnckoCore) | | ||
| 2 | [doowb](https://github.com/doowb) | | ||
| 1 | [wtgtybhertgeghgtwtg](https://github.com/wtgtybhertgeghgtwtg) | | ||
| 2 | [wtgtybhertgeghgtwtg](https://github.com/wtgtybhertgeghgtwtg) | | ||
@@ -78,2 +78,2 @@ ### Building docs | ||
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.4.2, on February 25, 2017._ | ||
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.5.0, on April 12, 2017._ |
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
2
123
1
8936
4
- Removedlazy-cache@^2.0.2
- Removeddefine-property@0.2.5(transitive)
- Removedfunction-bind@1.1.2(transitive)
- Removedhasown@2.0.2(transitive)
- Removedis-accessor-descriptor@1.0.1(transitive)
- Removedis-buffer@1.1.6(transitive)
- Removedis-data-descriptor@1.0.1(transitive)
- Removedis-descriptor@0.1.7(transitive)
- Removedisobject@3.0.1(transitive)
- Removedkind-of@3.2.2(transitive)
- Removedlazy-cache@2.0.2(transitive)
- Removedset-getter@0.1.1(transitive)
- Removedto-object-path@0.3.0(transitive)