added unit testing, and started implementing unit tests...phew
This commit is contained in:
2
node_modules/one-time/.npmignore
generated
vendored
Normal file
2
node_modules/one-time/.npmignore
generated
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
node_modules
|
||||
coverage
|
18
node_modules/one-time/.travis.yml
generated
vendored
Normal file
18
node_modules/one-time/.travis.yml
generated
vendored
Normal file
@ -0,0 +1,18 @@
|
||||
language: node_js
|
||||
node_js:
|
||||
- "0.12"
|
||||
- "0.10"
|
||||
- "0.8"
|
||||
- "iojs"
|
||||
before_install:
|
||||
- "npm install -g npm@1.4.x"
|
||||
script:
|
||||
- "npm run test-travis"
|
||||
after_script:
|
||||
- "npm install coveralls@2.11.x && cat coverage/lcov.info | coveralls"
|
||||
notifications:
|
||||
irc:
|
||||
channels:
|
||||
- "irc.freenode.org#unshift"
|
||||
on_success: change
|
||||
on_failure: change
|
22
node_modules/one-time/LICENSE
generated
vendored
Normal file
22
node_modules/one-time/LICENSE
generated
vendored
Normal file
@ -0,0 +1,22 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2015 Unshift.io, Arnout Kazemier, the Contributors.
|
||||
|
||||
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.
|
||||
|
65
node_modules/one-time/README.md
generated
vendored
Normal file
65
node_modules/one-time/README.md
generated
vendored
Normal file
@ -0,0 +1,65 @@
|
||||
# one-time
|
||||
|
||||
[](http://unshift.io)[](http://browsenpm.org/package/one-time)[](https://travis-ci.org/unshiftio/one-time)[](https://david-dm.org/unshiftio/one-time)[](https://coveralls.io/r/unshiftio/one-time?branch=master)[](http://webchat.freenode.net/?channels=unshift)
|
||||
|
||||
Call the supplied function exactly one time. This prevents double callback
|
||||
execution. This module can be used on both node and browsers using browserify.
|
||||
No magical ES5/6 methods used unlike the `once` module does.
|
||||
|
||||
## Installation
|
||||
|
||||
```
|
||||
npm install one-time
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
Simply supply the function with the function that should only be called one
|
||||
time:
|
||||
|
||||
```js
|
||||
var one = require('one-time');
|
||||
|
||||
function load(file, fn) {
|
||||
fn = one(fn);
|
||||
|
||||
eventemitter.once('load', fn);
|
||||
eventemitter.once('error', fn);
|
||||
|
||||
// do stuff
|
||||
eventemitter.emit('error', new Error('Failed to load, but still finished'));
|
||||
eventemitter.emit('load');
|
||||
}
|
||||
|
||||
function example(fn) {
|
||||
fn = one(fn);
|
||||
|
||||
fn();
|
||||
fn('also receives all arguments');
|
||||
fn('it returns the same value') === 'bar';
|
||||
fn('never');
|
||||
fn('gonna');
|
||||
fn('give');
|
||||
fn('you');
|
||||
fn('up');
|
||||
}
|
||||
|
||||
example(function () {
|
||||
return 'bar'
|
||||
});
|
||||
```
|
||||
|
||||
### Why not `once`?
|
||||
|
||||
The main reason is that `once` cannot be used in a browser environment unless it's
|
||||
ES5 compatible. For a module as simple as this I find that unacceptable. In addition
|
||||
to that it super heavy on the dependency side. So it's totally not suitable to be
|
||||
used in client side applications.
|
||||
|
||||
In addition to that we make sure that your code stays easy to debug as returned
|
||||
functions are named in the same way as your supplied functions. Making heap
|
||||
inspection and stacktraces easier to understand.
|
||||
|
||||
## License
|
||||
|
||||
MIT
|
37
node_modules/one-time/index.js
generated
vendored
Normal file
37
node_modules/one-time/index.js
generated
vendored
Normal file
@ -0,0 +1,37 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Wrap callbacks to prevent double execution.
|
||||
*
|
||||
* @param {Function} fn Function that should only be called once.
|
||||
* @returns {Function} A wrapped callback which prevents execution.
|
||||
* @api public
|
||||
*/
|
||||
module.exports = function one(fn) {
|
||||
var called = 0
|
||||
, value;
|
||||
|
||||
/**
|
||||
* The function that prevents double execution.
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
function onetime() {
|
||||
if (called) return value;
|
||||
|
||||
called = 1;
|
||||
value = fn.apply(this, arguments);
|
||||
fn = null;
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
//
|
||||
// To make debugging more easy we want to use the name of the supplied
|
||||
// function. So when you look at the functions that are assigned to event
|
||||
// listeners you don't see a load of `onetime` functions but actually the
|
||||
// names of the functions that this module will call.
|
||||
//
|
||||
onetime.displayName = fn.displayName || fn.name || onetime.displayName || onetime.name;
|
||||
return onetime;
|
||||
};
|
65
node_modules/one-time/package.json
generated
vendored
Normal file
65
node_modules/one-time/package.json
generated
vendored
Normal file
@ -0,0 +1,65 @@
|
||||
{
|
||||
"_from": "one-time@0.0.4",
|
||||
"_id": "one-time@0.0.4",
|
||||
"_inBundle": false,
|
||||
"_integrity": "sha1-+M33eISCb+Tf+T46nMN7HkSAdC4=",
|
||||
"_location": "/one-time",
|
||||
"_phantomChildren": {},
|
||||
"_requested": {
|
||||
"type": "version",
|
||||
"registry": true,
|
||||
"raw": "one-time@0.0.4",
|
||||
"name": "one-time",
|
||||
"escapedName": "one-time",
|
||||
"rawSpec": "0.0.4",
|
||||
"saveSpec": null,
|
||||
"fetchSpec": "0.0.4"
|
||||
},
|
||||
"_requiredBy": [
|
||||
"/winston"
|
||||
],
|
||||
"_resolved": "https://registry.npmjs.org/one-time/-/one-time-0.0.4.tgz",
|
||||
"_shasum": "f8cdf77884826fe4dff93e3a9cc37b1e4480742e",
|
||||
"_spec": "one-time@0.0.4",
|
||||
"_where": "/Users/josh.burman/Projects/braid/node_modules/winston",
|
||||
"author": {
|
||||
"name": "Arnout Kazemier"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/unshiftio/one-time/issues"
|
||||
},
|
||||
"bundleDependencies": false,
|
||||
"deprecated": false,
|
||||
"description": "Run the supplied function exactly one time (once)",
|
||||
"devDependencies": {
|
||||
"assume": "1.2.x",
|
||||
"istanbul": "0.3.x",
|
||||
"mocha": "2.2.x",
|
||||
"pre-commit": "1.0.x"
|
||||
},
|
||||
"homepage": "https://github.com/unshiftio/one-time#readme",
|
||||
"keywords": [
|
||||
"once",
|
||||
"function",
|
||||
"single",
|
||||
"one",
|
||||
"one-time",
|
||||
"execution",
|
||||
"nope"
|
||||
],
|
||||
"license": "MIT",
|
||||
"main": "index.js",
|
||||
"name": "one-time",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/unshiftio/one-time.git"
|
||||
},
|
||||
"scripts": {
|
||||
"100%": "istanbul check-coverage --statements 100 --functions 100 --lines 100 --branches 100",
|
||||
"coverage": "istanbul cover ./node_modules/.bin/_mocha -- test.js",
|
||||
"test": "mocha test.js",
|
||||
"test-travis": "istanbul cover node_modules/.bin/_mocha --report lcovonly -- test.js",
|
||||
"watch": "mocha --watch test.js"
|
||||
},
|
||||
"version": "0.0.4"
|
||||
}
|
45
node_modules/one-time/test.js
generated
vendored
Normal file
45
node_modules/one-time/test.js
generated
vendored
Normal file
@ -0,0 +1,45 @@
|
||||
describe('one-time', function () {
|
||||
'use strict';
|
||||
|
||||
var assume = require('assume')
|
||||
, one = require('./');
|
||||
|
||||
it('is exported as a function', function () {
|
||||
assume(one).is.a('function');
|
||||
});
|
||||
|
||||
it('only calls the supplied function once', function (next) {
|
||||
next = one(next);
|
||||
|
||||
next();
|
||||
next();
|
||||
next();
|
||||
next();
|
||||
});
|
||||
|
||||
it('returns the same value as the called function every single time', function () {
|
||||
var foo = one(function () {
|
||||
return 'bar';
|
||||
});
|
||||
|
||||
assume(foo()).equals('bar');
|
||||
assume(foo()).equals('bar');
|
||||
assume(foo()).equals('bar');
|
||||
assume(foo()).equals('bar');
|
||||
assume(foo()).equals('bar');
|
||||
assume(foo()).equals('bar');
|
||||
assume(foo()).equals('bar');
|
||||
assume(foo()).equals('bar');
|
||||
assume(foo()).equals('bar');
|
||||
assume(foo()).equals('bar');
|
||||
assume(foo()).equals('bar');
|
||||
});
|
||||
|
||||
it('the returned function uses the same displayName as the given fn', function () {
|
||||
var foo = one(function banana() {
|
||||
return 'bar';
|
||||
});
|
||||
|
||||
assume(foo.displayName).equals('banana');
|
||||
});
|
||||
});
|
Reference in New Issue
Block a user