[Pkg-javascript-commits] [node-use] 01/06: Import Upstream version 2.0.0
Sruthi Chandran
srud-guest at moszumanska.debian.org
Tue Oct 25 17:38:06 UTC 2016
This is an automated email from the git hooks/post-receive script.
srud-guest pushed a commit to branch master
in repository node-use.
commit 2025ddcad3e45d0bf837e5741dc6fe78ac86992d
Author: Sruthi <srud at disroot.org>
Date: Tue Oct 25 22:48:49 2016 +0530
Import Upstream version 2.0.0
---
.editorconfig | 22 ++++
.eslintrc.json | 275 ++++++++++++++++++++++++++++++++++++++++++++++++++
.gitattributes | 10 ++
.gitignore | 15 +++
.travis.yml | 12 +++
.verb.md | 9 ++
LICENSE | 21 ++++
README.md | 70 +++++++++++++
examples/app.js | 93 +++++++++++++++++
examples/layered.js | 37 +++++++
gulpfile.js | 25 +++++
index.js | 121 ++++++++++++++++++++++
package.json | 65 ++++++++++++
test.js | 282 ++++++++++++++++++++++++++++++++++++++++++++++++++++
utils.js | 24 +++++
15 files changed, 1081 insertions(+)
diff --git a/.editorconfig b/.editorconfig
new file mode 100644
index 0000000..991900b
--- /dev/null
+++ b/.editorconfig
@@ -0,0 +1,22 @@
+# http://editorconfig.org
+root = true
+
+[*]
+indent_style = space
+end_of_line = lf
+charset = utf-8
+indent_size = 2
+trim_trailing_whitespace = true
+insert_final_newline = true
+
+[*.md]
+trim_trailing_whitespace = false
+insert_final_newline = false
+
+[test/**]
+trim_trailing_whitespace = false
+insert_final_newline = false
+
+[templates/**]
+trim_trailing_whitespace = false
+insert_final_newline = false
diff --git a/.eslintrc.json b/.eslintrc.json
new file mode 100644
index 0000000..5b78706
--- /dev/null
+++ b/.eslintrc.json
@@ -0,0 +1,275 @@
+{
+ "ecmaFeatures": {
+ "modules": true,
+ "experimentalObjectRestSpread": true
+ },
+ "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"
+ ]
+ }
+}
diff --git a/.gitattributes b/.gitattributes
new file mode 100644
index 0000000..660957e
--- /dev/null
+++ b/.gitattributes
@@ -0,0 +1,10 @@
+# Enforce Unix newlines
+* text eol=lf
+
+# binaries
+*.ai binary
+*.psd binary
+*.jpg binary
+*.gif binary
+*.png binary
+*.jpeg binary
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..80a228c
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,15 @@
+*.DS_Store
+*.sublime-*
+_gh_pages
+bower_components
+node_modules
+npm-debug.log
+actual
+test/actual
+temp
+tmp
+TODO.md
+vendor
+.idea
+benchmark
+coverage
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..cb1d261
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,12 @@
+sudo: false
+language: node_js
+node_js:
+ - '5'
+ - '4'
+ - '0.12'
+ - '0.10'
+matrix:
+ fast_finish: true
+ allow_failures:
+ - node_js: '0.10'
+ - node_js: '0.12'
diff --git a/.verb.md b/.verb.md
new file mode 100644
index 0000000..9feec3e
--- /dev/null
+++ b/.verb.md
@@ -0,0 +1,9 @@
+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
+var use = require('{%= name %}');
+```
+
+See the [examples folder](./examples) for usage examples.
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..1e49edf
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,21 @@
+The MIT License (MIT)
+
+Copyright (c) 2015-2016, 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.
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..13ddb9d
--- /dev/null
+++ b/README.md
@@ -0,0 +1,70 @@
+# 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.
+
+## Install
+
+Install with [npm](https://www.npmjs.com/):
+
+```sh
+$ 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
+
+```js
+var use = require('use');
+```
+
+See the [examples folder](./examples) for usage examples.
+
+## Related projects
+
+You might also be interested in these projects:
+
+* [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)
+
+## Contributing
+
+Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/jonschlinkert/use/issues/new).
+
+## Building docs
+
+Generate readme and API documentation with [verb](https://github.com/verbose/verb):
+
+```sh
+$ npm install verb && npm run docs
+```
+
+Or, if [verb](https://github.com/verbose/verb) is installed globally:
+
+```sh
+$ verb
+```
+
+## Running tests
+
+Install dev dependencies:
+
+```sh
+$ npm install -d && npm test
+```
+
+## Author
+
+**Jon Schlinkert**
+
+* [github/jonschlinkert](https://github.com/jonschlinkert)
+* [twitter/jonschlinkert](http://twitter.com/jonschlinkert)
+
+## 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](https://github.com/verbose/verb), v0.9.0, on May 11, 2016._
\ No newline at end of file
diff --git a/examples/app.js b/examples/app.js
new file mode 100644
index 0000000..cd1112e
--- /dev/null
+++ b/examples/app.js
@@ -0,0 +1,93 @@
+
+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');
+
diff --git a/examples/layered.js b/examples/layered.js
new file mode 100644
index 0000000..8b7558e
--- /dev/null
+++ b/examples/layered.js
@@ -0,0 +1,37 @@
+
+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)
diff --git a/gulpfile.js b/gulpfile.js
new file mode 100644
index 0000000..7c0cfb6
--- /dev/null
+++ b/gulpfile.js
@@ -0,0 +1,25 @@
+'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']);
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..f707906
--- /dev/null
+++ b/index.js
@@ -0,0 +1,121 @@
+/*!
+ * use <https://github.com/jonschlinkert/use>
+ *
+ * Copyright (c) 2015, Jon Schlinkert.
+ * Licensed 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 = {};
+ }
+
+ var prop = utils.isString(opts.prop) ? opts.prop : 'fns';
+ if (!utils.isArray(app[prop])) {
+ utils.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
+ */
+
+ utils.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
+ */
+
+ utils.define(app, 'run', function(val) {
+ if (!utils.isObject(val)) return;
+ decorate(val);
+
+ 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(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') {
+ var fns = self[prop];
+ fns.push(plugin);
+ }
+ return self;
+ }
+
+ /**
+ * Ensure the `.use` method exists on `val`
+ */
+
+ function decorate(val) {
+ if (!val.use || !val.run) {
+ base(val);
+ }
+ }
+
+ return app;
+};
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..a2c68c3
--- /dev/null
+++ b/package.json
@@ -0,0 +1,65 @@
+{
+ "name": "use",
+ "description": "Easily add plugin support to your node.js application.",
+ "version": "2.0.0",
+ "homepage": "https://github.com/jonschlinkert/use",
+ "author": "Jon Schlinkert (https://github.com/jonschlinkert)",
+ "repository": "jonschlinkert/use",
+ "bugs": {
+ "url": "https://github.com/jonschlinkert/use/issues"
+ },
+ "license": "MIT",
+ "files": [
+ "index.js",
+ "utils.js"
+ ],
+ "main": "index.js",
+ "engines": {
+ "node": ">=0.10.0"
+ },
+ "scripts": {
+ "test": "mocha"
+ },
+ "dependencies": {
+ "define-property": "^0.2.5",
+ "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": "^2.4.5"
+ },
+ "keywords": [
+ "use"
+ ],
+ "verb": {
+ "related": {
+ "list": [
+ "base-methods",
+ "ware"
+ ]
+ },
+ "reflinks": [
+ "ware",
+ "verb"
+ ],
+ "toc": false,
+ "layout": "default",
+ "tasks": [
+ "readme"
+ ],
+ "plugins": [
+ "gulp-format-md"
+ ],
+ "lint": {
+ "reflinks": true
+ }
+ }
+}
diff --git a/test.js b/test.js
new file mode 100644
index 0000000..4249402
--- /dev/null
+++ b/test.js
@@ -0,0 +1,282 @@
+'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, /expect `app` be 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, /use expects `fn` be a function/);
+ });
+
+ it('should allow passing `opts.fn` to merge options from each plugin to app options', function() {
+ var limon = {options: {
+ foo: 'bar'
+ }};
+ use(limon, {
+ fn: function(app, options) {
+ assert.strictEqual(this.options.foo, 'bar');
+ this.options = extend(this.options, options);
+ this.options.qux = 123;
+ }
+ });
+
+ limon
+ .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' });
+ });
+
+ it('should not extend options if `opts.fn` not given (#3)', function() {
+ var limon = {options: {
+ foo: 'bar'
+ }};
+ use(limon);
+
+ limon
+ .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(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.equal(typeof foo.fns, 'undefined');
+ });
+});
diff --git a/utils.js b/utils.js
new file mode 100644
index 0000000..18cd0ce
--- /dev/null
+++ b/utils.js
@@ -0,0 +1,24 @@
+'use strict';
+
+var utils = require('lazy-cache')(require);
+var fn = require;
+require = utils; // eslint-disable-line
+
+/**
+ * Lazily required module dependencies
+ */
+
+require('define-property', 'define');
+require('isarray', 'isArray');
+require('isobject', 'isObject');
+require = fn; // eslint-disable-line
+
+utils.isString = function(val) {
+ return val && typeof val === 'string';
+};
+
+/**
+ * Expose `utils` modules
+ */
+
+module.exports = utils;
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-use.git
More information about the Pkg-javascript-commits
mailing list