aboutsummaryrefslogtreecommitdiff
path: root/node_modules/use/index.js
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2018-09-20 02:56:13 +0200
committerFlorian Dold <florian.dold@gmail.com>2018-09-20 02:56:13 +0200
commitbbff7403fbf46f9ad92240ac213df8d30ef31b64 (patch)
treec58400ec5124da1c7d56b01aea83309f80a56c3b /node_modules/use/index.js
parent003fb34971cf63466184351b4db5f7c67df4f444 (diff)
update packages
Diffstat (limited to 'node_modules/use/index.js')
-rw-r--r--node_modules/use/index.js92
1 files changed, 63 insertions, 29 deletions
diff --git a/node_modules/use/index.js b/node_modules/use/index.js
index 61cae325f..9a1eb4ee9 100644
--- a/node_modules/use/index.js
+++ b/node_modules/use/index.js
@@ -1,26 +1,21 @@
/*!
* use <https://github.com/jonschlinkert/use>
*
- * Copyright (c) 2015, 2017, Jon Schlinkert.
+ * Copyright (c) 2015-2017, Jon Schlinkert.
* Released under the MIT License.
*/
'use strict';
-var utils = require('./utils');
-
-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 = {};
+module.exports = function base(app, options) {
+ if (!isObject(app) && typeof app !== 'function') {
+ throw new TypeError('expected an object or function');
}
- 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, []);
}
/**
@@ -53,7 +48,7 @@ module.exports = function base(app, opts) {
* @api public
*/
- utils.define(app, 'use', use);
+ define(app, 'use', use);
/**
* Run all plugins on `fns`. Any plugin that returns a function
@@ -69,9 +64,17 @@ module.exports = function base(app, opts) {
* @api public
*/
- 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;
var fns = self[prop];
@@ -89,33 +92,64 @@ module.exports = function base(app, opts) {
* `fns` array to be called by the `run` method.
*/
- function use(fn, options) {
+ 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('.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, offset);
+ 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);
}
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;
+ };
}
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
+ });
+}