[Pkg-javascript-commits] [node-async] 26/480: Added detect, reject, reduceRight and sortBy functions and added a few common aliases
Jonas Smedegaard
js at moszumanska.debian.org
Fri May 2 08:58:08 UTC 2014
This is an automated email from the git hooks/post-receive script.
js pushed a commit to branch master
in repository node-async.
commit 9ca19c1060ac9b31d2a7a369a3e26a9a968dcc9d
Author: Caolan McMahon <caolan at caolanmcmahon.com>
Date: Wed Jun 16 19:38:20 2010 +0100
Added detect, reject, reduceRight and sortBy functions and added a few common aliases
---
lib/async.js | 75 ++++++++++++++++++++++++---
test/test-async.js | 148 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
2 files changed, 216 insertions(+), 7 deletions(-)
diff --git a/lib/async.js b/lib/async.js
index 58213bb..cccf665 100644
--- a/lib/async.js
+++ b/lib/async.js
@@ -54,9 +54,9 @@ var doSeries = function(fn){
var _map = function(eachfn, arr, iterator, callback){
var results = [];
- for(var i=0; i<arr.length; i++){
- arr[i] = {index: i, value: arr[i]};
- }
+ arr = arr.map(function(x, i){
+ return {index: i, value: x};
+ });
eachfn(arr, function(x, callback){
iterator(x.value, function(err, v){
results[x.index] = v;
@@ -82,13 +82,23 @@ exports.reduce = function(arr, memo, iterator, callback){
callback(err, memo);
});
};
+// inject alias
+exports.inject = exports.reduce;
+// foldl alias
+exports.foldl = exports.reduce;
+exports.reduceRight = function(arr, memo, iterator, callback){
+ var reversed = arr.map(function(x){return x;}).reverse();
+ exports.reduce(reversed, memo, iterator, callback);
+};
+// foldr alias
+exports.foldr = exports.reduceRight;
var _filter = function(eachfn, arr, iterator, callback){
var results = [];
- for(var i=0; i<arr.length; i++){
- arr[i] = {index: i, value: arr[i]};
- }
+ arr = arr.map(function(x, i){
+ return {index: i, value: x};
+ });
eachfn(arr, function(x, callback){
iterator(x.value, function(v){
if(v) results.push(x);
@@ -104,7 +114,42 @@ var _filter = function(eachfn, arr, iterator, callback){
};
exports.filter = doParallel(_filter);
exports.filterSeries = doSeries(_filter);
+// select alias
+exports.select = exports.filter;
+exports.selectSeries = exports.filterSeries;
+var _reject = function(eachfn, arr, iterator, callback){
+ var results = [];
+ arr = arr.map(function(x, i){
+ return {index: i, value: x};
+ });
+ eachfn(arr, function(x, callback){
+ iterator(x.value, function(v){
+ if(!v) results.push(x);
+ callback();
+ });
+ }, function(err){
+ callback(results.sort(function(a,b){
+ return a.index - b.index;
+ }).map(function(x){
+ return x.value;
+ }));
+ });
+};
+exports.reject = doParallel(_reject);
+exports.rejectSeries = doSeries(_reject);
+
+var _detect = function(eachfn, arr, iterator, main_callback){
+ eachfn(arr, function(x, callback){
+ iterator(x, function(result){
+ result ? main_callback(x): callback();
+ });
+ }, function(err){
+ main_callback();
+ });
+};
+exports.detect = doParallel(_detect);
+exports.detectSeries = doSeries(_detect);
exports.some = function(arr, iterator, main_callback){
exports.forEach(arr, function(x, callback){
@@ -119,6 +164,8 @@ exports.some = function(arr, iterator, main_callback){
main_callback(false);
});
};
+// any alias
+exports.any = exports.some;
exports.every = function(arr, iterator, main_callback){
exports.forEach(arr, function(x, callback){
@@ -133,7 +180,23 @@ exports.every = function(arr, iterator, main_callback){
main_callback(true);
});
};
+// all alias
+exports.all = exports.every;
+exports.sortBy = function(arr, iterator, callback){
+ exports.map(arr, function(x, callback){
+ iterator(x, function(err, criteria){
+ if(err) callback(err);
+ else callback(null, {value: x, criteria: criteria});
+ });
+ }, function(err, results){
+ if(err) return callback(err);
+ else callback(null, results.sort(function(left, right){
+ var a = left.criteria, b = right.criteria;
+ return a < b ? -1 : a > b ? 1 : 0;
+ }).map(function(x){return x.value;}));
+ })
+};
exports.auto = function(tasks, callback){
callback = callback || function(){};
diff --git a/test/test-async.js b/test/test-async.js
index 6387fca..dde5773 100644
--- a/test/test-async.js
+++ b/test/test-async.js
@@ -437,6 +437,17 @@ exports['map'] = function(test){
});
};
+exports['map original untouched'] = function(test){
+ var a = [1,2,3];
+ async.map(a, function(x, callback){
+ callback(null, x*2);
+ }, function(err, results){
+ test.same(results, [2,4,6]);
+ test.same(a, [1,2,3]);
+ test.done();
+ });
+};
+
exports['map error'] = function(test){
test.expect(1);
async.map([1,2,3], function(x, callback){
@@ -472,10 +483,13 @@ exports['mapSeries error'] = function(test){
};
exports['reduce'] = function(test){
- async.reduce([1,3,2], 0, function(a, x, callback){
+ var call_order = [];
+ async.reduce([1,2,3], 0, function(a, x, callback){
+ call_order.push(x);
callback(null, a + x);
}, function(err, result){
test.equals(result, 6);
+ test.same(call_order, [1,2,3]);
test.done();
});
};
@@ -499,6 +513,35 @@ exports['reduce error'] = function(test){
setTimeout(test.done, 50);
};
+exports['inject alias'] = function(test){
+ test.equals(async.inject, async.reduce);
+ test.done();
+};
+
+exports['foldl alias'] = function(test){
+ test.equals(async.foldl, async.reduce);
+ test.done();
+};
+
+exports['reduceRight'] = function(test){
+ var call_order = [];
+ var a = [1,2,3];
+ async.reduceRight(a, 0, function(a, x, callback){
+ call_order.push(x);
+ callback(null, a + x);
+ }, function(err, result){
+ test.equals(result, 6);
+ test.same(call_order, [3,2,1]);
+ test.same(a, [1,2,3]);
+ test.done();
+ });
+};
+
+exports['foldr alias'] = function(test){
+ test.equals(async.foldr, async.reduceRight);
+ test.done();
+};
+
exports['filter'] = function(test){
async.filter([3,1,2], function(x, callback){
setTimeout(function(){callback(x % 2);}, x*25);
@@ -508,6 +551,17 @@ exports['filter'] = function(test){
});
};
+exports['filter original untouched'] = function(test){
+ var a = [3,1,2];
+ async.filter(a, function(x, callback){
+ callback(x % 2);
+ }, function(results){
+ test.same(results, [3,1]);
+ test.same(a, [3,1,2]);
+ test.done();
+ });
+};
+
exports['filterSeries'] = function(test){
async.filterSeries([3,1,2], function(x, callback){
setTimeout(function(){callback(x % 2);}, x*25);
@@ -517,6 +571,45 @@ exports['filterSeries'] = function(test){
});
};
+exports['select alias'] = function(test){
+ test.equals(async.select, async.filter);
+ test.done();
+};
+
+exports['selectSeries alias'] = function(test){
+ test.equals(async.selectSeries, async.filterSeries);
+ test.done();
+};
+
+exports['reject'] = function(test){
+ async.reject([3,1,2], function(x, callback){
+ setTimeout(function(){callback(x % 2);}, x*25);
+ }, function(results){
+ test.same(results, [2]);
+ test.done();
+ });
+};
+
+exports['reject original untouched'] = function(test){
+ var a = [3,1,2];
+ async.reject(a, function(x, callback){
+ callback(x % 2);
+ }, function(results){
+ test.same(results, [2]);
+ test.same(a, [3,1,2]);
+ test.done();
+ });
+};
+
+exports['rejectSeries'] = function(test){
+ async.rejectSeries([3,1,2], function(x, callback){
+ setTimeout(function(){callback(x % 2);}, x*25);
+ }, function(results){
+ test.same(results, [2]);
+ test.done();
+ });
+};
+
exports['some true'] = function(test){
async.some([3,1,2], function(x, callback){
process.nextTick(function(){
@@ -555,6 +648,11 @@ exports['some early return'] = function(test){
}, 100);
};
+exports['any alias'] = function(test){
+ test.equals(async.any, async.some);
+ test.done();
+};
+
exports['every true'] = function(test){
async.every([1,2,3], function(x, callback){
process.nextTick(function(){callback(true);});
@@ -588,3 +686,51 @@ exports['every early return'] = function(test){
test.done();
}, 100);
};
+
+exports['all alias'] = function(test){
+ test.equals(async.all, async.every);
+ test.done();
+};
+
+exports['detect'] = function(test){
+ var call_order = [];
+ async.detect([3,2,1], function(x, callback){
+ setTimeout(function(){
+ call_order.push(x);
+ callback(x == 2);
+ }, x*25);
+ }, function(result){
+ call_order.push('callback');
+ test.equals(result, 2);
+ });
+ setTimeout(function(){
+ test.same(call_order, [1,2,'callback',3]);
+ test.done();
+ }, 100);
+};
+
+exports['detectSeries'] = function(test){
+ var call_order = [];
+ async.detectSeries([3,2,1], function(x, callback){
+ setTimeout(function(){
+ call_order.push(x);
+ callback(x == 2);
+ }, x*25);
+ }, function(result){
+ call_order.push('callback');
+ test.equals(result, 2);
+ });
+ setTimeout(function(){
+ test.same(call_order, [3,2,'callback']);
+ test.done();
+ }, 200);
+};
+
+exports['sortBy'] = function(test){
+ async.sortBy([{a:1},{a:15},{a:6}], function(x, callback){
+ process.nextTick(function(){callback(null, x.a);});
+ }, function(err, result){
+ test.same(result, [{a:1},{a:6},{a:15}]);
+ test.done();
+ });
+};
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-async.git
More information about the Pkg-javascript-commits
mailing list