pax_global_header 0000666 0000000 0000000 00000000064 13321706111 0014505 g ustar 00root root 0000000 0000000 52 comment=843c0e1b6934cef9570e7a9962501bf84212c3b5
use-3.1.1/ 0000775 0000000 0000000 00000000000 13321706111 0012303 5 ustar 00root root 0000000 0000000 use-3.1.1/.editorconfig 0000664 0000000 0000000 00000000441 13321706111 0014757 0 ustar 00root root 0000000 0000000 # http://editorconfig.org/
root = true
[*]
charset = utf-8
end_of_line = lf
indent_size = 2
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true
[{**/{actual,fixtures,expected,templates}/**,*.md}]
trim_trailing_whitespace = false
insert_final_newline = false
use-3.1.1/.eslintrc.json 0000664 0000000 0000000 00000007325 13321706111 0015106 0 ustar 00root root 0000000 0000000 {
"extends": [
"eslint:recommended"
],
"env": {
"browser": false,
"es6": true,
"node": true,
"mocha": true
},
"parserOptions":{
"ecmaVersion": 9,
"sourceType": "module",
"ecmaFeatures": {
"modules": true,
"experimentalObjectRestSpread": true
}
},
"globals": {
"document": false,
"navigator": false,
"window": false
},
"rules": {
"accessor-pairs": 2,
"arrow-spacing": [2, { "before": true, "after": true }],
"block-spacing": [2, "always"],
"brace-style": [2, "1tbs", { "allowSingleLine": true }],
"comma-dangle": [2, "never"],
"comma-spacing": [2, { "before": false, "after": true }],
"comma-style": [2, "last"],
"constructor-super": 2,
"curly": [2, "multi-line"],
"dot-location": [2, "property"],
"eol-last": 2,
"eqeqeq": [2, "allow-null"],
"generator-star-spacing": [2, { "before": true, "after": true }],
"handle-callback-err": [2, "^(err|error)$" ],
"indent": [2, 2, { "SwitchCase": 1 }],
"key-spacing": [2, { "beforeColon": false, "afterColon": true }],
"keyword-spacing": [2, { "before": true, "after": true }],
"new-cap": [2, { "newIsCap": true, "capIsNew": false }],
"new-parens": 2,
"no-array-constructor": 2,
"no-caller": 2,
"no-class-assign": 2,
"no-cond-assign": 2,
"no-const-assign": 2,
"no-control-regex": 2,
"no-debugger": 2,
"no-delete-var": 2,
"no-dupe-args": 2,
"no-dupe-class-members": 2,
"no-dupe-keys": 2,
"no-duplicate-case": 2,
"no-empty-character-class": 2,
"no-eval": 2,
"no-ex-assign": 2,
"no-extend-native": 2,
"no-extra-bind": 2,
"no-extra-boolean-cast": 2,
"no-extra-parens": [2, "functions"],
"no-fallthrough": 2,
"no-floating-decimal": 2,
"no-func-assign": 2,
"no-implied-eval": 2,
"no-inner-declarations": [2, "functions"],
"no-invalid-regexp": 2,
"no-irregular-whitespace": 2,
"no-iterator": 2,
"no-label-var": 2,
"no-labels": 2,
"no-lone-blocks": 2,
"no-mixed-spaces-and-tabs": 2,
"no-multi-spaces": 2,
"no-multi-str": 2,
"no-multiple-empty-lines": [2, { "max": 1 }],
"no-native-reassign": 0,
"no-negated-in-lhs": 2,
"no-new": 2,
"no-new-func": 2,
"no-new-object": 2,
"no-new-require": 2,
"no-new-wrappers": 2,
"no-obj-calls": 2,
"no-octal": 2,
"no-octal-escape": 2,
"no-proto": 0,
"no-redeclare": 2,
"no-regex-spaces": 2,
"no-return-assign": 2,
"no-self-compare": 2,
"no-sequences": 2,
"no-shadow-restricted-names": 2,
"no-spaced-func": 2,
"no-sparse-arrays": 2,
"no-this-before-super": 2,
"no-throw-literal": 2,
"no-trailing-spaces": 0,
"no-undef": 2,
"no-undef-init": 2,
"no-unexpected-multiline": 2,
"no-unneeded-ternary": [2, { "defaultAssignment": false }],
"no-unreachable": 2,
"no-unused-vars": [2, { "vars": "all", "args": "none" }],
"no-useless-call": 0,
"no-with": 2,
"one-var": [0, { "initialized": "never" }],
"operator-linebreak": [0, "after", { "overrides": { "?": "before", ":": "before" } }],
"padded-blocks": [0, "never"],
"quotes": [2, "single", "avoid-escape"],
"radix": 2,
"semi": [2, "always"],
"semi-spacing": [2, { "before": false, "after": true }],
"space-before-blocks": [2, "always"],
"space-before-function-paren": [2, "never"],
"space-in-parens": [2, "never"],
"space-infix-ops": 2,
"space-unary-ops": [2, { "words": true, "nonwords": false }],
"spaced-comment": [0, "always", { "markers": ["global", "globals", "eslint", "eslint-disable", "*package", "!", ","] }],
"use-isnan": 2,
"valid-typeof": 2,
"wrap-iife": [2, "any"],
"yoda": [2, "never"]
}
}
use-3.1.1/.gitattributes 0000664 0000000 0000000 00000000200 13321706111 0015166 0 ustar 00root root 0000000 0000000 # Enforce Unix newlines
* text eol=lf
# binaries
*.ai binary
*.psd binary
*.jpg binary
*.gif binary
*.png binary
*.jpeg binary
use-3.1.1/.gitignore 0000664 0000000 0000000 00000000456 13321706111 0014300 0 ustar 00root root 0000000 0000000 # always ignore files
*.DS_Store
.idea
.vscode
*.sublime-*
# test related, or directories generated by tests
test/actual
actual
coverage
.nyc*
# npm
node_modules
npm-debug.log
# yarn
yarn.lock
yarn-error.log
# misc
_gh_pages
_draft
_drafts
bower_components
vendor
temp
tmp
TODO.md
package-lock.json use-3.1.1/.travis.yml 0000664 0000000 0000000 00000000225 13321706111 0014413 0 ustar 00root root 0000000 0000000 sudo: false
os:
- linux
- osx
language: node_js
node_js:
- node
- '10'
- '9'
- '8'
- '7'
- '6'
- '5'
- '4'
- '0.12'
- '0.10'
use-3.1.1/.verb.md 0000664 0000000 0000000 00000000413 13321706111 0013637 0 ustar 00root root 0000000 0000000 A different take on plugin handling! This is not a middleware system, if you need something that handles async middleware, [ware][] is great for that.
## Usage
```js
const use = require('{%= name %}');
```
See the [examples folder](./examples) for usage examples.
use-3.1.1/LICENSE 0000664 0000000 0000000 00000002103 13321706111 0013304 0 ustar 00root root 0000000 0000000 The MIT License (MIT)
Copyright (c) 2015-present, Jon Schlinkert.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
use-3.1.1/README.md 0000664 0000000 0000000 00000006725 13321706111 0013574 0 ustar 00root root 0000000 0000000 # use [](https://www.npmjs.com/package/use) [](https://npmjs.org/package/use) [](https://npmjs.org/package/use) [](https://travis-ci.org/jonschlinkert/use)
> Easily add plugin support to your node.js application.
Please consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support.
## Install
Install with [npm](https://www.npmjs.com/):
```sh
$ npm install --save use
```
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
```js
const use = require('use');
```
See the [examples folder](./examples) for usage examples.
## About
Contributing
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
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
```
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
```
### Related projects
You might also be interested in these projects:
* [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, server-side node.js applications, using plugins like building blocks | [homepage](https://github.com/node-base/base "Framework for rapidly creating high quality, server-side 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.")
### Contributors
| **Commits** | **Contributor** |
| --- | --- |
| 37 | [jonschlinkert](https://github.com/jonschlinkert) |
| 7 | [charlike-old](https://github.com/charlike-old) |
| 2 | [doowb](https://github.com/doowb) |
| 2 | [wtgtybhertgeghgtwtg](https://github.com/wtgtybhertgeghgtwtg) |
### Author
**Jon Schlinkert**
* [LinkedIn Profile](https://linkedin.com/in/jonschlinkert)
* [GitHub Profile](https://github.com/jonschlinkert)
* [Twitter Profile](https://twitter.com/jonschlinkert)
### License
Copyright © 2018, [Jon Schlinkert](https://github.com/jonschlinkert).
Released under the [MIT License](LICENSE).
***
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on July 12, 2018._ use-3.1.1/examples/ 0000775 0000000 0000000 00000000000 13321706111 0014121 5 ustar 00root root 0000000 0000000 use-3.1.1/examples/app.js 0000664 0000000 0000000 00000003146 13321706111 0015243 0 ustar 00root root 0000000 0000000
var util = require('util');
var plugins = require('base-plugins');
function Factory(config) {
this.use = function(fn) {
fn.call(this, this);
return this;
};
this.use(plugins());
}
function Multi() {
Factory.call(this);
this.isMulti = true;
this.apps = {};
}
util.inherits(Factory, Multi);
Multi.prototype.app = function(name) {
var app = this.apps[name] = new App();
this.run(app);
return app;
};
function App() {
Factory.call(this);
this.isApp = true;
this.collections = {};
}
util.inherits(Factory, App);
App.prototype.collection = function(name) {
var collection = this.collections[name] = new Collection();
this.run(collection);
return collection;
};
function Collection() {
Factory.call(this);
this.isCollection = true;
this.items = {};
}
util.inherits(Factory, Collection);
Collection.prototype.item = function(name) {
var item = this.items[name] = new Item();
this.run(item);
return item;
};
function Item() {
Factory.call(this);
this.isItem = true;
this.fields = {};
}
util.inherits(Factory, Item);
Item.prototype.field = function(name) {
var field = this.fields[name] = new Field();
this.run(field);
return field;
};
function Field() {
Factory.call(this);
this.isField = true;
}
util.inherits(Factory, Field);
Field.prototype.field = function(key, val) {
this[key] = val;
this.run(val);
return this;
};
var multi = new Multi();
multi.use(function fn(app) {
if (!app.isItem) return fn;
console.log(app)
});
var app = multi.app('b');
var collection = app.collection('c');
var item = collection.item('d');
var field = item.field('e');
use-3.1.1/examples/layered.js 0000664 0000000 0000000 00000001054 13321706111 0016104 0 ustar 00root root 0000000 0000000
var define = require('define-property');
var use = require('..');
function App(name) {
this.name = name;
use(this);
}
App.prototype.create = function(name) {
return new App(name);
};
var a = new App('a');
var b = a.create('b');
var c = b.create('c');
var d = c.create('d');
var e = d.create('e');
var f = e.create('f');
a.children = [];
a.use(function fn(config) {
if (!config.name) return fn;
a.children.push(config.name);
return fn;
});
var config = {};
a.run(b);
b.run(c);
c.run(d);
d.run(e);
e.run(f);
f.run(config);
console.log(a)
use-3.1.1/gulpfile.js 0000664 0000000 0000000 00000001105 13321706111 0014445 0 ustar 00root root 0000000 0000000 'use strict';
var gulp = require('gulp');
var mocha = require('gulp-mocha');
var istanbul = require('gulp-istanbul');
var eslint = require('gulp-eslint');
gulp.task('coverage', function() {
return gulp.src('index.js')
.pipe(istanbul())
.pipe(istanbul.hookRequire());
});
gulp.task('mocha', ['coverage'], function() {
return gulp.src('test.js')
.pipe(mocha({reporter: 'spec'}))
.pipe(istanbul.writeReports());
});
gulp.task('eslint', function() {
return gulp.src(['index.js', 'test.js'])
.pipe(eslint())
});
gulp.task('default', ['mocha', 'eslint']);
use-3.1.1/index.js 0000664 0000000 0000000 00000006637 13321706111 0013764 0 ustar 00root root 0000000 0000000 /*!
* use
*
* Copyright (c) 2015-2017, Jon Schlinkert.
* Released under the MIT License.
*/
'use strict';
module.exports = function base(app, options) {
if (!isObject(app) && typeof app !== 'function') {
throw new TypeError('expected an object or function');
}
var opts = isObject(options) ? options : {};
var prop = typeof opts.prop === 'string' ? opts.prop : 'fns';
if (!Array.isArray(app[prop])) {
define(app, prop, []);
}
/**
* 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.
*
* 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.
*
* ```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);
* ```
* @name .use
* @param {Function} `fn` plugin function to call
* @api public
*/
define(app, 'use', use);
/**
* Run all plugins on `fns`. Any plugin that returns a function
* when called by `use` is pushed onto the `fns` array.
*
* ```js
* var config = {};
* app.run(config);
* ```
* @name .run
* @param {Object} `value` Object to be modified by plugins.
* @return {Object} Returns the object passed to `run`
* @api public
*/
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;
var fns = self[prop];
var len = fns.length;
var idx = -1;
while (++idx < len) {
val.use(fns[idx]);
}
return val;
});
/**
* Call plugin `fn`. If a function is returned push it into the
* `fns` array to be called by the `run` method.
*/
function use(type, fn, options) {
var offset = 1;
if (typeof type === 'string' || Array.isArray(type)) {
fn = wrap(type, fn);
offset++;
} else {
options = fn;
fn = type;
}
if (typeof fn !== 'function') {
throw new TypeError('expected a function');
}
var self = this || app;
var fns = self[prop];
var args = [].slice.call(arguments, offset);
args.unshift(self);
if (typeof opts.hook === 'function') {
opts.hook.apply(self, args);
}
var val = fn.apply(self, args);
if (typeof val === 'function' && fns.indexOf(val) === -1) {
fns.push(val);
}
return self;
}
/**
* 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 wrap(type, fn) {
return function plugin() {
return this.type === type ? fn.apply(this, arguments) : plugin;
};
}
return app;
};
function isObject(val) {
return val && typeof val === 'object' && !Array.isArray(val);
}
function define(obj, key, val) {
Object.defineProperty(obj, key, {
configurable: true,
writable: true,
value: val
});
}
use-3.1.1/package.json 0000664 0000000 0000000 00000002563 13321706111 0014577 0 ustar 00root root 0000000 0000000 {
"name": "use",
"description": "Easily add plugin support to your node.js application.",
"version": "3.1.1",
"homepage": "https://github.com/jonschlinkert/use",
"author": "Jon Schlinkert (https://github.com/jonschlinkert)",
"contributors": [
"Brian Woodward (https://twitter.com/doowb)",
"Jon Schlinkert (http://twitter.com/jonschlinkert)",
"Olsten Larck (https://i.am.charlike.online)",
"(https://github.com/wtgtybhertgeghgtwtg)"
],
"repository": "jonschlinkert/use",
"bugs": {
"url": "https://github.com/jonschlinkert/use/issues"
},
"license": "MIT",
"files": [
"index.js"
],
"main": "index.js",
"engines": {
"node": ">=0.10.0"
},
"scripts": {
"test": "mocha"
},
"devDependencies": {
"base-plugins": "^1.0.0",
"define-property": "^2.0.0",
"extend-shallow": "^3.0.1",
"gulp": "^3.9.1",
"gulp-eslint": "^4.0.0",
"gulp-format-md": "^1.0.0",
"gulp-istanbul": "^1.1.2",
"gulp-mocha": "^3.0.1",
"mocha": "^4.0.1"
},
"keywords": [
"use"
],
"verb": {
"toc": false,
"layout": "default",
"tasks": [
"readme"
],
"plugins": [
"gulp-format-md"
],
"related": {
"list": [
"base",
"base-plugins",
"ware"
]
},
"reflinks": [
"verb",
"ware"
],
"lint": {
"reflinks": true
}
}
}
use-3.1.1/test.js 0000664 0000000 0000000 00000017551 13321706111 0013631 0 ustar 00root root 0000000 0000000 'use strict';
require('mocha');
var assert = require('assert');
var define = require('define-property');
var use = require('./');
var extend = require('extend-shallow');
describe('use', function() {
it('should export a function', function() {
assert.equal(typeof use, 'function');
});
it('should throw TypeError `app` not a function or object', function() {
function fixture() {
use(123);
}
assert.throws(fixture, TypeError);
assert.throws(fixture, /expected an object or function/);
});
it('should throw TypeError if not a function passed to `.use` method', function() {
function fixture() {
use({}).use(123);
}
assert.throws(fixture, TypeError);
assert.throws(fixture, /expected a function/);
});
it('should allow passing `opts.hook` to merge options from each plugin to app options', function() {
var obj = {options: {foo: 'bar'}};
var count = 0;
use(obj, {
hook: function(app, options) {
assert.strictEqual(this.options.foo, 'bar');
this.options = extend(this.options, options);
this.options.qux = 123;
count++;
}
});
obj
.use(function() {
assert.strictEqual(this.options.foo, 'bar');
assert.strictEqual(this.options.xxx, 'yyy');
assert.strictEqual(this.options.qux, 123);
}, { xxx: 'yyy' })
.use(function() {
assert.strictEqual(this.options.foo, 'bar');
assert.strictEqual(this.options.xxx, 'yyy');
assert.strictEqual(this.options.qux, 123);
assert.strictEqual(this.options.ccc, 'ddd');
}, { ccc: 'ddd' });
assert.equal(count, 2);
});
it('should not extend options if `opts.fn` not given (#3)', function() {
var obj = {options: {
foo: 'bar'
}};
use(obj);
obj
.use(function() {
assert.strictEqual(this.options.foo, 'bar');
assert.strictEqual(this.options.xxx, undefined);
assert.strictEqual(this.options.qux, undefined);
}, { xxx: 'yyy' })
.use(function() {
assert.strictEqual(this.options.foo, 'bar');
assert.strictEqual(this.options.xxx, undefined);
assert.strictEqual(this.options.qux, undefined);
assert.strictEqual(this.options.ccc, undefined);
}, { ccc: 'ddd' });
});
it('should decorate "use" onto the given object', function() {
var app = {};
use(app);
assert.equal(typeof app.use, 'function');
});
it('should decorate "fns" onto the given object', function() {
var app = {};
use(app);
assert(Array.isArray(app.fns));
});
it('should not re-add decorate methods onto the given object', function() {
var app = {};
use(app);
assert(Array.isArray(app.fns));
assert(app.fns.length === 0);
app.use(function() {
return function(ctx) {
ctx.foo = 'bar';
};
});
assert(app.fns.length === 1);
use(app);
assert(app.fns.length === 1);
});
it('should allow passing custom property to be used for plugins stack', function() {
var app = {};
use(app, { prop: 'plugins' });
assert.strictEqual(Array.isArray(app.fns), false);
assert.strictEqual(Array.isArray(app.plugins), true);
assert.equal(app.plugins.length, 0);
});
it('should immediately invoke a plugin function', function() {
var app = {};
use(app);
var called = false;
app.use(function(ctx) {
called = true;
});
assert(called);
});
it('should push returned functions onto `fns`', function() {
var app = {};
use(app);
app.use(function(ctx) {
return function() {}
});
app.use(function(ctx) {
return function() {}
});
app.use(function(ctx) {
return function() {}
});
assert(app.fns.length === 3);
});
});
describe('run', function() {
it('should decorate "run" onto the given object', function() {
var app = {};
use(app);
assert.equal(typeof app.run, 'function');
});
it('should return app', function() {
var app = {};
assert.equal(typeof use(app), 'object');
});
it('should run all plugins on "fns"', function() {
var app = {};
use(app);
app.use(function(ctx) {
return function(foo) {
foo.a = 'b';
}
});
app.use(function(ctx) {
return function(foo) {
foo.c = 'd';
}
});
app.use(function(ctx) {
return function(foo) {
foo.e = 'f';
}
});
var foo = {};
app.run(foo);
assert.deepEqual(foo, { a: 'b', c: 'd', e: 'f' });
});
it('should run all plugins on "fns" and decorate ctx', function() {
var app = {};
use(app);
app.use(function(ctx) {
return function(foo) {
foo.a = 'b';
}
});
app.use(function(ctx) {
return function(foo) {
foo.c = 'd';
}
});
app.use(function(ctx) {
return function(foo) {
foo.e = 'f';
}
});
var foo = {};
app.run(foo);
assert.deepEqual(foo, { a: 'b', c: 'd', e: 'f' });
assert.equal(typeof foo.use, 'function');
assert.equal(typeof foo.run, 'function');
assert(Array.isArray(foo.fns));
});
it('should run all plugins on "fns" and decorate ctx when .use is defined but .run is not', function() {
var app = {};
use(app);
app.use(function(ctx) {
return function(foo) {
foo.a = 'b';
}
});
app.use(function(ctx) {
return function(foo) {
foo.c = 'd';
}
});
app.use(function(ctx) {
return function(foo) {
foo.e = 'f';
}
});
var foo = {};
define(foo, 'use', function() {});
app.run(foo);
assert.deepEqual(foo, { a: 'b', c: 'd', e: 'f' });
assert.equal(typeof foo.use, 'function');
assert.equal(typeof foo.run, 'function');
assert(Array.isArray(foo.fns));
});
it('should run all plugins on "fns" and decorate ctx when .run is defined but .use is not', function() {
var app = {};
use(app);
app.use(function(ctx) {
return function(foo) {
foo.a = 'b';
}
});
app.use(function(ctx) {
return function(foo) {
foo.c = 'd';
}
});
app.use(function(ctx) {
return function(foo) {
foo.e = 'f';
}
});
var foo = {};
define(foo, 'run', function() {});
app.run(foo);
assert.deepEqual(foo, { a: 'b', c: 'd', e: 'f' });
assert.equal(typeof foo.use, 'function');
assert.equal(typeof foo.run, 'function');
assert(Array.isArray(foo.fns));
});
it('should run all plugins on "fns" and not decorate ctx when .use and .run are already defined', function() {
var app = {};
use(app);
app.use(function(ctx) {
return function(foo) {
foo.a = 'b';
}
});
app.use(function(ctx) {
return function(foo) {
foo.c = 'd';
}
});
app.use(function(ctx) {
return function(foo) {
foo.e = 'f';
}
});
var foo = {};
define(foo, 'use', function(fn) {
return fn.call(this, this);
});
define(foo, 'run', function() {});
app.run(foo);
assert.deepEqual(foo, { a: 'b', c: 'd', e: 'f' });
assert.equal(typeof foo.use, 'function');
assert.equal(typeof foo.run, 'function');
assert(Array.isArray(foo.fns));
});
describe('named plugins', function() {
it('should register named plugins', function() {
var app = {type: 'app'};
use(app);
var names = [];
app.use('foo', function() {
names.push(this.type);
});
app.use('bar', function() {
names.push(this.type);
});
app.use('baz', function() {
names.push(this.type);
});
var foo = {};
foo.type = 'foo';
foo.parent = app;
app.run(foo);
var bar = {};
bar.type = 'bar';
bar.parent = foo;
foo.run(bar);
var baz = {};
baz.type = 'baz';
baz.parent = bar;
bar.run(baz);
assert.deepEqual(names, ['foo', 'bar', 'baz']);
});
});
});