pax_global_header00006660000000000000000000000064132110452660014512gustar00rootroot0000000000000052 comment=97153b8dc538a46f6fd627f8d901d641b36f4e6c use-3.1.0/000077500000000000000000000000001321104526600123075ustar00rootroot00000000000000use-3.1.0/.editorconfig000066400000000000000000000004411321104526600147630ustar00rootroot00000000000000# 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.0/.eslintrc.json000066400000000000000000000067751321104526600151220ustar00rootroot00000000000000{ "env": { "browser": false, "es6": true, "node": true, "mocha": 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.0/.gitattributes000066400000000000000000000002001321104526600151720ustar00rootroot00000000000000# Enforce Unix newlines * text eol=lf # binaries *.ai binary *.psd binary *.jpg binary *.gif binary *.png binary *.jpeg binary use-3.1.0/.gitignore000066400000000000000000000004461321104526600143030ustar00rootroot00000000000000# always ignore files *.DS_Store .idea *.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.jsonuse-3.1.0/.travis.yml000066400000000000000000000002141321104526600144150ustar00rootroot00000000000000sudo: false os: - linux - osx language: node_js node_js: - node - '9' - '8' - '7' - '6' - '5' - '4' - '0.12' - '0.10' use-3.1.0/.verb.md000066400000000000000000000004111321104526600136410ustar00rootroot00000000000000A 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 var use = require('{%= name %}'); ``` See the [examples folder](./examples) for usage examples. use-3.1.0/LICENSE000066400000000000000000000021001321104526600133050ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2015-2017, 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.0/README.md000066400000000000000000000067541321104526600136020ustar00rootroot00000000000000# 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) > 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 var 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** | | --- | --- | | 33 | [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/in/jonschlinkert](https://linkedin.com/in/jonschlinkert) * [github/jonschlinkert](https://github.com/jonschlinkert) * [twitter/jonschlinkert](https://twitter.com/jonschlinkert) ### License Copyright © 2017, [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 December 03, 2017._use-3.1.0/examples/000077500000000000000000000000001321104526600141255ustar00rootroot00000000000000use-3.1.0/examples/app.js000066400000000000000000000031461321104526600152470ustar00rootroot00000000000000 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.0/examples/layered.js000066400000000000000000000010541321104526600161100ustar00rootroot00000000000000 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.0/gulpfile.js000066400000000000000000000011051321104526600144510ustar00rootroot00000000000000'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.0/index.js000066400000000000000000000066051321104526600137630ustar00rootroot00000000000000/*! * use * * Copyright (c) 2015, 2017, Jon Schlinkert. * Released under the MIT License. */ 'use strict'; var typeOf = require('kind-of'); module.exports = function base(app, options) { if (typeOf(app) !== 'object' && typeof app !== 'function') { throw new TypeError('expected an object or function'); } var opts = typeOf(options) === 'object' ? 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 (typeOf(val) !== 'object') 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 define(obj, key, val) { Object.defineProperty(obj, key, { configurable: true, writable: true, value: val }); } use-3.1.0/package.json000066400000000000000000000026441321104526600146030ustar00rootroot00000000000000{ "name": "use", "description": "Easily add plugin support to your node.js application.", "version": "3.1.0", "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" }, "dependencies": { "kind-of": "^6.0.2" }, "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": { "related": { "list": [ "base", "base-plugins", "ware" ] }, "reflinks": [ "verb", "ware" ], "toc": false, "layout": "default", "tasks": [ "readme" ], "plugins": [ "gulp-format-md" ], "lint": { "reflinks": true } } } use-3.1.0/test.js000066400000000000000000000175511321104526600136350ustar00rootroot00000000000000'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']); }); }); });