use
Advanced tools
Comparing version 1.1.2 to 2.0.0
54
index.js
@@ -10,10 +10,18 @@ /*! | ||
var define = require('define-property'); | ||
var isObject = require('isobject'); | ||
var utils = require('./utils'); | ||
module.exports = function base(app) { | ||
if (!app.fns) { | ||
define(app, 'fns', []); | ||
module.exports = function base(app, opts) { | ||
if (!utils.isObject(app) && typeof app !== 'function') { | ||
throw new TypeError('use: expect `app` be an object or function'); | ||
} | ||
if (!utils.isObject(opts)) { | ||
opts = {}; | ||
} | ||
var prop = utils.isString(opts.prop) ? opts.prop : 'fns'; | ||
if (!utils.isArray(app[prop])) { | ||
utils.define(app, prop, []); | ||
} | ||
/** | ||
@@ -49,3 +57,3 @@ * Define a plugin function to be passed to use. The only | ||
define(app, 'use', use); | ||
utils.define(app, 'use', use); | ||
@@ -66,6 +74,14 @@ /** | ||
define(app, 'run', function (val) { | ||
utils.define(app, 'run', function(val) { | ||
if (!utils.isObject(val)) return; | ||
decorate(val); | ||
var len = this.fns.length, i = -1; | ||
while (++i < len) val.use(this.fns[i]); | ||
var self = this || app; | ||
var fns = self[prop]; | ||
var len = fns.length; | ||
var idx = -1; | ||
while (++idx < len) { | ||
val.use(fns[idx]); | ||
} | ||
return val; | ||
@@ -79,8 +95,18 @@ }); | ||
function use(fn) { | ||
var plugin = fn.call(this, this); | ||
function use(fn, options) { | ||
if (typeof fn !== 'function') { | ||
throw new TypeError('.use expects `fn` be a function'); | ||
} | ||
var self = this || app; | ||
if (typeof opts.fn === 'function') { | ||
opts.fn.call(self, self, options); | ||
} | ||
var plugin = fn.call(self, self); | ||
if (typeof plugin === 'function') { | ||
this.fns.push(plugin); | ||
var fns = self[prop]; | ||
fns.push(plugin); | ||
} | ||
return this; | ||
return self; | ||
} | ||
@@ -93,3 +119,3 @@ | ||
function decorate(val) { | ||
if (isObject(val) && (!val.use || !val.run)) { | ||
if (!val.use || !val.run) { | ||
base(val); | ||
@@ -96,0 +122,0 @@ } |
{ | ||
"name": "use", | ||
"description": "Easily add plugin support to your node.js application.", | ||
"version": "1.1.2", | ||
"version": "2.0.0", | ||
"homepage": "https://github.com/jonschlinkert/use", | ||
@@ -13,3 +13,4 @@ "author": "Jon Schlinkert (https://github.com/jonschlinkert)", | ||
"files": [ | ||
"index.js" | ||
"index.js", | ||
"utils.js" | ||
], | ||
@@ -25,12 +26,19 @@ "main": "index.js", | ||
"define-property": "^0.2.5", | ||
"isobject": "^2.0.0" | ||
"isarray": "^1.0.0", | ||
"isobject": "^2.1.0", | ||
"lazy-cache": "^2.0.1" | ||
}, | ||
"devDependencies": { | ||
"base-plugins": "^0.4.1", | ||
"extend-shallow": "^2.0.1", | ||
"gulp": "^3.9.0", | ||
"gulp-eslint": "^1.1.0", | ||
"gulp-format-md": "^0.1.9", | ||
"gulp-istanbul": "^0.10.2", | ||
"gulp-mocha": "^2.1.3", | ||
"mocha": "*" | ||
"mocha": "^2.4.5" | ||
}, | ||
"keywords": [ | ||
"use" | ||
], | ||
"verb": { | ||
@@ -42,4 +50,19 @@ "related": { | ||
] | ||
}, | ||
"reflinks": [ | ||
"ware", | ||
"verb" | ||
], | ||
"toc": false, | ||
"layout": "default", | ||
"tasks": [ | ||
"readme" | ||
], | ||
"plugins": [ | ||
"gulp-format-md" | ||
], | ||
"lint": { | ||
"reflinks": true | ||
} | ||
} | ||
} |
@@ -1,15 +0,15 @@ | ||
# use [![NPM version](https://badge.fury.io/js/use.svg)](http://badge.fury.io/js/use) [![Build Status](https://travis-ci.org/jonschlinkert/use.svg)](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 downloads](https://img.shields.io/npm/dm/use.svg?style=flat)](https://npmjs.org/package/use) [![Build Status](https://img.shields.io/travis/jonschlinkert/use.svg?style=flat)](https://travis-ci.org/jonschlinkert/use) | ||
> Easily add plugin support to your node.js application. | ||
Easily add plugin support to your node.js application. | ||
A different take on plugin handling! This is not a middleware system, if you need something that handles async middleware, [ware](https://github.com/segmentio/ware) is great for that. | ||
## Install | ||
Install with [npm](https://www.npmjs.com/) | ||
Install with [npm](https://www.npmjs.com/): | ||
```sh | ||
$ npm i use --save | ||
$ npm install use --save | ||
``` | ||
A different take on plugin handling! This is not a middleware system, if you need something that handles async middleware, [ware](https://github.com/segmentio/ware) is great for that. | ||
## Usage | ||
@@ -23,56 +23,27 @@ | ||
## API | ||
## Related projects | ||
### [.use](index.js#L48) | ||
You might also be interested in these projects: | ||
Define a plugin function to be passed to use. The only parameter exposed to the plugin is `app`, the object or function. passed to `use(app)`. `app` is also exposed as `this` in plugins. | ||
* [base-methods](https://www.npmjs.com/package/base-methods): base-methods is the foundation for creating modular, unit testable and highly pluggable node.js applications, starting… [more](https://www.npmjs.com/package/base-methods) | [homepage](https://github.com/jonschlinkert/base-methods) | ||
* [ware](https://www.npmjs.com/package/ware): Easily create your own middleware layer. | [homepage](https://github.com/segmentio/ware) | ||
Additionally, **if a plugin returns a function, the function will | ||
be pushed onto the `fns` array**, allowing the plugin to be | ||
called at a later point by the `run` method. | ||
## Contributing | ||
**Params** | ||
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/jonschlinkert/use/issues/new). | ||
* `fn` **{Function}**: plugin function to call | ||
## Building docs | ||
**Example** | ||
Generate readme and API documentation with [verb](https://github.com/verbose/verb): | ||
```js | ||
var use = require('use'); | ||
// define a plugin | ||
function foo(app) { | ||
// do stuff | ||
} | ||
var app = function(){}; | ||
use(app); | ||
// register plugins | ||
app.use(foo); | ||
app.use(bar); | ||
app.use(baz); | ||
```sh | ||
$ npm install verb && npm run docs | ||
``` | ||
### [.run](index.js#L64) | ||
Or, if [verb](https://github.com/verbose/verb) is installed globally: | ||
Run all plugins on `fns`. Any plugin that returns a function when called by `use` is pushed onto the `fns` array. | ||
**Params** | ||
* `value` **{Object}**: Object to be modified by plugins. | ||
* `returns` **{Object}**: Returns the object passed to `run` | ||
**Example** | ||
```js | ||
var config = {}; | ||
app.run(config); | ||
```sh | ||
$ verb | ||
``` | ||
## Similar projects | ||
* [base-methods](https://www.npmjs.com/package/base-methods): Starter for creating a node.js application with a handful of common methods, like `set`, `get`,… [more](https://www.npmjs.com/package/base-methods) | [homepage](https://github.com/jonschlinkert/base-methods) | ||
* [ware](https://www.npmjs.com/package/ware): Easily create your own middleware layer. | [homepage](https://github.com/segmentio/ware) | ||
## Running tests | ||
@@ -83,9 +54,5 @@ | ||
```sh | ||
$ npm i -d && npm test | ||
$ npm install -d && npm test | ||
``` | ||
## Contributing | ||
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/jonschlinkert/use/issues/new). | ||
## Author | ||
@@ -95,12 +62,12 @@ | ||
+ [github/jonschlinkert](https://github.com/jonschlinkert) | ||
+ [twitter/jonschlinkert](http://twitter.com/jonschlinkert) | ||
* [github/jonschlinkert](https://github.com/jonschlinkert) | ||
* [twitter/jonschlinkert](http://twitter.com/jonschlinkert) | ||
## License | ||
Copyright © 2015 Jon Schlinkert | ||
Released under the MIT license. | ||
Copyright © 2016, [Jon Schlinkert](https://github.com/jonschlinkert). | ||
Released under the [MIT license](https://github.com/jonschlinkert/use/blob/master/LICENSE). | ||
*** | ||
_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on November 10, 2015._ | ||
_This file was generated by [verb](https://github.com/verbose/verb), v0.9.0, on May 11, 2016._ |
Sorry, the diff of this file is not supported yet
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
7671
5
121
4
8
70
2
+ Addedisarray@^1.0.0
+ Addedlazy-cache@^2.0.1
+ Addedis-buffer@1.1.6(transitive)
+ Addedkind-of@3.2.2(transitive)
+ Addedlazy-cache@2.0.2(transitive)
+ Addedset-getter@0.1.1(transitive)
+ Addedto-object-path@0.3.0(transitive)
Updatedisobject@^2.1.0