Comparing version 1.0.0 to 2.0.0
54
index.js
@@ -7,2 +7,3 @@ var noop = function() {} | ||
this.finished = false | ||
this.args = [] | ||
@@ -12,24 +13,51 @@ return this.count.bind(this) | ||
CountableSet.prototype.count = function count(err) { | ||
var args = Array.prototype.slice.call(arguments, 0) | ||
CountableSet.prototype.count = function count(name) { | ||
var self = this | ||
this.num-- | ||
name = name || '' | ||
if (this.finished) { | ||
return false | ||
} | ||
return function(err) { | ||
var args = Array.prototype.slice.call(arguments, 0) | ||
if (err) { | ||
this.finished = true | ||
return this.done.apply(this, args) | ||
} | ||
self.num-- | ||
self.args.push([name].concat(args)) | ||
if (this.num === 0) { | ||
this.finished = true | ||
return this.done.apply(this, args) | ||
if (self.finished) { | ||
return false | ||
} | ||
if (err) { | ||
self.finished = true | ||
return self.done.apply(self, args) | ||
} | ||
if (self.num === 0) { | ||
self.finished = true | ||
return self.done.apply(self, [null, self.format(self.args)]) | ||
} | ||
} | ||
} | ||
CountableSet.prototype.format = function format(args) { | ||
var named = {} | ||
args.forEach(function(arg) { | ||
if (arg.length < 2) { | ||
return named[arg[0]] = undefined | ||
} | ||
return named[arg[0]] = arg.slice(2, 3).pop() | ||
}) | ||
named.all = args.map(function(arg) { | ||
return arg.slice(2) | ||
}) | ||
delete named[''] | ||
return named | ||
} | ||
module.exports = function(num, done) { | ||
return new CountableSet(num, done) | ||
} |
{ | ||
"name": "countn", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"author": { | ||
"name": "Dejan Ranisavljevic", | ||
"email": "dejan.ranisavljevic@gmail.com", | ||
"url": "http://dejan.ranisavljevic.com" | ||
}, | ||
"description": "Simple control flow, for counting callbacks", | ||
@@ -9,2 +14,6 @@ "main": "index.js", | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"tap": "^0.4.12" | ||
}, | ||
"repository": { | ||
@@ -14,3 +23,2 @@ "type": "git", | ||
}, | ||
"author": "Dejan Ranisavljevic <dejan.ranisavljevic@gmail.com>", | ||
"license": "MIT", | ||
@@ -20,6 +28,3 @@ "bugs": { | ||
}, | ||
"homepage": "https://github.com/dejanr/countn", | ||
"devDependencies": { | ||
"tap": "^0.4.12" | ||
} | ||
"homepage": "https://github.com/dejanr/countn" | ||
} |
@@ -11,3 +11,3 @@ # countn | ||
## Usage Example | ||
## Basic Example | ||
@@ -19,13 +19,30 @@ Here is a example of simple async scheduling. Where we have subscribed to execute some code after all concurent tasks | ||
var countn = require('countn') | ||
var cb = countn(5, function() { | ||
console.log('we are done') | ||
var cb = countn(5, function(err, results) { | ||
console.log('results:', results) // results: { all: [ [], [], [], [], [] ] } | ||
}) | ||
setTimeout(cb, 300) | ||
setTimeout(cb, 100) | ||
setTimeout(cb, 500) | ||
setTimeout(cb, 200) | ||
setTimeout(cb, 400) | ||
setTimeout(cb(), 300) | ||
setTimeout(cb(), 100) | ||
setTimeout(cb(), 500) | ||
setTimeout(cb(), 200) | ||
setTimeout(cb(), 400) | ||
``` | ||
## Named Callbacks Example | ||
Here is a example of simple async scheduling with named callbacks, so we could easily reuse results. | ||
```js | ||
var countn = require('countn') | ||
var cb = countn(5, function(err, results) { | ||
console.log('first result: ', results.first) // undefined | ||
}) | ||
setTimeout(cb('first'), 300) | ||
setTimeout(cb('second'), 100) | ||
setTimeout(cb('third'), 500) | ||
setTimeout(cb('fourth'), 200) | ||
setTimeout(cb('fifth'), 400) | ||
``` | ||
## What's it good for? | ||
@@ -38,6 +55,6 @@ | ||
`` | ||
``` | ||
$ npm install | ||
$ npm test | ||
`` | ||
``` | ||
@@ -44,0 +61,0 @@ ## Authors |
38
test.js
@@ -5,4 +5,3 @@ var test = require('tap').test | ||
test('it should initialize and return count callback', function(t) { | ||
test('should initialize and return count callback', function(t) { | ||
t.type(countn(1, noop), 'function', 'callback is function') | ||
@@ -12,3 +11,3 @@ t.end() | ||
test('it should call done when all callbacks are counted', function(t) { | ||
test('should call done when all callbacks are counted', function(t) { | ||
var cb = countn(2, function(err) { | ||
@@ -19,7 +18,7 @@ t.ok(true, 'all callbacks counted') | ||
cb() | ||
cb() | ||
cb()() | ||
cb()() | ||
}) | ||
test('it should return error on first new Error', function(t) { | ||
test('should return error on first error', function(t) { | ||
var cb = countn(3, function(err) { | ||
@@ -30,4 +29,27 @@ t.equal(err.message, 'Some error occured') | ||
cb(new Error('Some error occured')) | ||
cb(new Error('Some other error')) | ||
cb()(new Error('Some error occured')) | ||
cb()(new Error('Some other error')) | ||
}) | ||
test('done cb should pass named results and single value', function(t) { | ||
var cb = countn(2, function(err, results) { | ||
t.equal(results.first, true, 'first should be true') | ||
t.equal(results.second, false, 'second should be false') | ||
t.ok(true, 'all callbacks counted') | ||
t.end() | ||
}) | ||
cb('first')(null, true) | ||
cb('second')(null, false) | ||
}) | ||
test('done cb should pass all args', function(t) { | ||
var cb = countn(2, function(err, results) { | ||
t.deepEqual(results.all, [[true, false], [false, false]], 'all arguments should be merged') | ||
t.ok(true, 'all callbacks should be counted') | ||
t.end() | ||
}) | ||
cb('first')(null, true, false) | ||
cb('second')(null, false, false) | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9741
11
173
65