one-time
Advanced tools
Comparing version 0.0.3 to 0.0.4
22
index.js
@@ -11,9 +11,14 @@ 'use strict'; | ||
module.exports = function one(fn) { | ||
var called = false | ||
var called = 0 | ||
, value; | ||
return function time() { | ||
/** | ||
* The function that prevents double execution. | ||
* | ||
* @api private | ||
*/ | ||
function onetime() { | ||
if (called) return value; | ||
called = true; | ||
called = 1; | ||
value = fn.apply(this, arguments); | ||
@@ -23,3 +28,12 @@ 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; | ||
}; |
{ | ||
"name": "one-time", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "Run the supplied function exactly one time (once)", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "node test.js", | ||
"coverage": "istanbul cover test.js", | ||
"test-travis": "istanbul cover test.js --report lcovonly" | ||
"100%": "istanbul check-coverage --statements 100 --functions 100 --lines 100 --branches 100", | ||
"test": "mocha test.js", | ||
"watch": "mocha --watch test.js", | ||
"coverage": "istanbul cover ./node_modules/.bin/_mocha -- test.js", | ||
"test-travis": "istanbul cover node_modules/.bin/_mocha --report lcovonly -- test.js" | ||
}, | ||
@@ -27,5 +29,7 @@ "repository": { | ||
"devDependencies": { | ||
"assume": "1.2.x", | ||
"istanbul": "0.3.x", | ||
"pre-commit": "0.0.x" | ||
"mocha": "2.2.x", | ||
"pre-commit": "1.0.x" | ||
} | ||
} |
@@ -56,7 +56,11 @@ # one-time | ||
ES5 compatible. For a module as simple as this I find that unacceptable. In addition | ||
to that it super heavy on the dependency cite. So it's totally not suitable to be | ||
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 |
54
test.js
@@ -1,21 +0,45 @@ | ||
'use strict'; | ||
describe('one-time', function () { | ||
'use strict'; | ||
var one = require('./') | ||
, called = 0; | ||
var assume = require('assume') | ||
, one = require('./'); | ||
/* istanbul ignore next */ | ||
(function () { | ||
var callme = one(function maybe(bar) { | ||
called++; | ||
it('is exported as a function', function () { | ||
assume(one).is.a('function'); | ||
}); | ||
if (bar !== 'bar') throw new Error('Invalid argument received'); | ||
return 'foo'; | ||
it('only calls the supplied function once', function (next) { | ||
next = one(next); | ||
next(); | ||
next(); | ||
next(); | ||
next(); | ||
}); | ||
if (callme('bar') !== 'foo') throw new Error('Invalid returned state'); | ||
if (callme('bar') !== 'foo') throw new Error('Invalid returned state'); | ||
if (callme('bar') !== 'foo') throw new Error('Invalid returned state'); | ||
if (callme('bar') !== 'foo') throw new Error('Invalid returned state'); | ||
it('returns the same value as the called function every single time', function () { | ||
var foo = one(function () { | ||
return 'bar'; | ||
}); | ||
if (called !== 1) throw new Error('Called multiple times'); | ||
}()); | ||
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'); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
6588
7
69
66
4
1