Skip to content
Browse files

improve api for iterators with arity 0

  • Loading branch information...
1 parent 0f465c4 commit 8e600442ab9ca8e1f1f5c04f1213b8479ce54fa0 @caolan committed Mar 23, 2011
Showing with 81 additions and 15 deletions.
  1. +3 −3 index.html
  2. +36 −10 nimble.js
  3. +1 −1 nimble.min.js
  4. +41 −1 test/test.js
View
6 index.html
@@ -238,7 +238,7 @@
<span class="download">
<strong>Download: </strong>
<a href="https://github.com/caolan/nimble/raw/master/nimble.js">Development</a> | <a href="https://github.com/caolan/nimble/raw/master/nimble.min.js">Production</a>
- <span class="size">(794 bytes, minified and gzipped)</span>
+ <span class="size">(834 bytes, minified and gzipped)</span>
</span>
<a href="https://github.com/caolan/nimble"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://d3nwyuy0nl342s.cloudfront.net/img/30f550e0d38ceb6ef5b81500c64d970b7fb0f028/687474703a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f6f72616e67655f6666373630302e706e67" alt="Fork me on GitHub"></a>
@@ -517,12 +517,12 @@
<div class="section">
<h2>Nimble is small</h2>
<div id="sizegraph">
- <img src="https://chart.googleapis.com/chart?cht=bhs&chs=790x140&chd=t:3321,794|1859,0&chds=0,120&chco=4d89f9|00B88A,c6d9fd&chbh=60&chds=0,5500&chm=tunderscore,000000,0,0,16|tasync,000000,1,0,16|tnimble,000000,0,1,16" />
+ <img src="https://chart.googleapis.com/chart?cht=bhs&chs=790x140&chd=t:3321,834|1859,0&chds=0,120&chco=4d89f9|00B88A,c6d9fd&chbh=60&chds=0,5500&chm=tunderscore,000000,0,0,16|tasync,000000,1,0,16|tnimble,000000,0,1,16" />
</div>
<p>
By adding only the most frequently used features, and designing the code
with minification and gzip compression in mind, Nimble is able to fit into
- a tiny 794 bytes. Previously, I was including both underscore.js and
+ a tiny 834 bytes. Previously, I was including both underscore.js and
async.js, which adds up to over 5kb.
</p>
</div>
View
46 nimble.js
@@ -46,10 +46,7 @@
}
var completed = 0;
eachSync(obj, function () {
- var args = Array.prototype.slice.call(
- arguments, 0, iterator.length - 1
- );
- args[iterator.length - 1] = function (err) {
+ var cb = function (err) {
if (err) {
callback(err);
callback = function () {};
@@ -60,6 +57,14 @@
}
}
};
+ var args = Array.prototype.slice.call(arguments);
+ if (iterator.length) {
+ var args = args.slice(0, iterator.length - 1);
+ args[iterator.length - 1] = cb;
+ }
+ else {
+ args.push(cb);
+ };
iterator.apply(this, args);
});
};
@@ -104,11 +109,18 @@
return function (obj, iterator, callback) {
var results = [];
eachfn(obj, function (value, i, obj, callback) {
- var args = [value, i, obj].slice(0, iterator.length - 1);
- args[iterator.length - 1] = function (err, v) {
+ var cb = function (err, v) {
results[results.length] = v;
callback(err);
};
+ var args = [value, i, obj];
+ if (iterator.length) {
+ args = args.slice(0, iterator.length - 1);
+ args[iterator.length - 1] = cb;
+ }
+ else {
+ args.push(cb);
+ }
iterator.apply(this, args);
}, function (err) {
callback(err, results);
@@ -129,13 +141,20 @@
var filterParallel = function (obj, iterator, callback) {
var results = [];
eachParallel(obj, function (value, k, obj, callback) {
- var args = [value, k, obj].slice(0, iterator.length - 1);
- args[iterator.length - 1] = function (err, a) {
+ var cb = function (err, a) {
if (a) {
results[results.length] = value;
}
callback(err);
};
+ var args = [value, k, obj];
+ if (iterator.length) {
+ args = args.slice(0, iterator.length - 1);
+ args[iterator.length - 1] = cb;
+ }
+ else {
+ args.push(cb);
+ }
iterator.apply(this, args);
}, function (err) {
callback(err, results);
@@ -151,11 +170,18 @@
var reduceSeries = function (obj, iterator, memo, callback) {
eachSeries(obj, function (value, i, obj, callback) {
- var args = [memo, value, i, obj].slice(0, iterator.length - 1);
- args[iterator.length - 1] = function (err, v) {
+ var cb = function (err, v) {
memo = v;
callback(err);
};
+ var args = [memo, value, i, obj];
+ if (iterator.length) {
+ args = args.slice(0, iterator.length - 1);
+ args[iterator.length - 1] = cb;
+ }
+ else {
+ args.push(cb);
+ }
iterator.apply(this, args);
}, function (err) {
callback(err, memo);
View
2 nimble.min.js
@@ -1 +1 @@
-(function(a){var b=Object.keys||function(a){var b=[];for(var c in a)a.hasOwnProperty(c)&&b.push(c);return b};var c=function(a,b){var c=Array.prototype[a];return function(d,e,f){var g=d?d[a]:0;return g&&g===c?g.call(d,e,f):b(d,e,f)}};var d=c("forEach",function(a,c){var d=a instanceof Object;var e=d?b(a):a||[];for(var f=0,g=e.length;f<g;f++){var h=d?e[f]:f;c(a[h],h,a)}});var e=function(a,c,e){var f=a.length||b(a).length;if(!f)return e();var g=0;d(a,function(){var a=Array.prototype.slice.call(arguments,0,c.length-1);a[c.length-1]=function(a){a?(e(a),e=function(){}):++g===f&&e()},c.apply(this,a)})};var f=function(a,c,d){var e=b(a);if(!e.length)return d();var f=0;var g=function(){var b=e[f];var h=[a[b],b,a].slice(0,c.length-1);h[c.length-1]=function(a){a?(d(a),d=function(){}):++f===e.length?d():g()},c.apply(this,h)};g()};var g=c("map",function(a,b){var c=[];d(a,function(a,d,e){c[c.length]=b(a,d,e)});return c});var h=function(a){return function(b,c,d){var e=[];a(b,function(a,b,d,f){var g=[a,b,d].slice(0,c.length-1);g[c.length-1]=function(a,b){e[e.length]=b,f(a)},c.apply(this,g)},function(a){d(a,e)})}};var i=c("filter",function(a,b,c){var e=[];d(a,function(a,c,d){b(a,c,d)&&(e[e.length]=a)});return e});var j=function(a,b,c){var d=[];e(a,function(a,c,e,f){var g=[a,c,e].slice(0,b.length-1);g[b.length-1]=function(b,c){c&&(d[d.length]=a),f(b)},b.apply(this,g)},function(a){c(a,d)})};var k=c("reduce",function(a,b,c){d(a,function(a,d,e){c=b(c,a,d,e)});return c});var l=function(a,b,c,d){f(a,function(a,d,e,f){var g=[c,a,d,e].slice(0,b.length-1);g[b.length-1]=function(a,b){c=b,f(a)},b.apply(this,g)},function(a){d(a,c)})};a.each=function(a,b,c){return(c?e:d)(a,b,c)},a.map=function(a,b,c){return(c?h(e):g)(a,b,c)},a.filter=function(a,b,c){return(c?j:i)(a,b,c)},a.reduce=function(a,b,c,d){return(d?l:k)(a,b,c,d)},a.parallel=function(a,b){var c=new a.constructor;e(a,function(a,b,d){a(function(a){var e=Array.prototype.slice.call(arguments,1);c[b]=e.length<=1?e[0]:e,d(a)})},function(a){(b||function(){})(a,c)})},a.series=function(a,b){var c=new a.constructor;f(a,function(a,b,d){a(function(a,e){var f=Array.prototype.slice.call(arguments,1);c[b]=f.length<=1?f[0]:f,d(a)})},function(a){(b||function(){})(a,c)})}})(typeof exports==="undefined"?this._={}:exports)
+(function(a){var b=Object.keys||function(a){var b=[];for(var c in a)a.hasOwnProperty(c)&&b.push(c);return b};var c=function(a,b){var c=Array.prototype[a];return function(d,e,f){var g=d?d[a]:0;return g&&g===c?g.call(d,e,f):b(d,e,f)}};var d=c("forEach",function(a,c){var d=a instanceof Object;var e=d?b(a):a||[];for(var f=0,g=e.length;f<g;f++){var h=d?e[f]:f;c(a[h],h,a)}});var e=function(a,c,e){var f=a.length||b(a).length;if(!f)return e();var g=0;d(a,function(){var a=function(a){a?(e(a),e=function(){}):++g===f&&e()};var b=Array.prototype.slice.call(arguments);if(c.length){var b=b.slice(0,c.length-1);b[c.length-1]=a}else b.push(a);c.apply(this,b)})};var f=function(a,c,d){var e=b(a);if(!e.length)return d();var f=0;var g=function(){var b=e[f];var h=[a[b],b,a].slice(0,c.length-1);h[c.length-1]=function(a){a?(d(a),d=function(){}):++f===e.length?d():g()},c.apply(this,h)};g()};var g=c("map",function(a,b){var c=[];d(a,function(a,d,e){c[c.length]=b(a,d,e)});return c});var h=function(a){return function(b,c,d){var e=[];a(b,function(a,b,d,f){var g=function(a,b){e[e.length]=b,f(a)};var h=[a,b,d];c.length?(h=h.slice(0,c.length-1),h[c.length-1]=g):h.push(g),c.apply(this,h)},function(a){d(a,e)})}};var i=c("filter",function(a,b,c){var e=[];d(a,function(a,c,d){b(a,c,d)&&(e[e.length]=a)});return e});var j=function(a,b,c){var d=[];e(a,function(a,c,e,f){var g=function(b,c){c&&(d[d.length]=a),f(b)};var h=[a,c,e];b.length?(h=h.slice(0,b.length-1),h[b.length-1]=g):h.push(g),b.apply(this,h)},function(a){c(a,d)})};var k=c("reduce",function(a,b,c){d(a,function(a,d,e){c=b(c,a,d,e)});return c});var l=function(a,b,c,d){f(a,function(a,d,e,f){var g=function(a,b){c=b,f(a)};var h=[c,a,d,e];b.length?(h=h.slice(0,b.length-1),h[b.length-1]=g):h.push(g),b.apply(this,h)},function(a){d(a,c)})};a.each=function(a,b,c){return(c?e:d)(a,b,c)},a.map=function(a,b,c){return(c?h(e):g)(a,b,c)},a.filter=function(a,b,c){return(c?j:i)(a,b,c)},a.reduce=function(a,b,c,d){return(d?l:k)(a,b,c,d)},a.parallel=function(a,b){var c=new a.constructor;e(a,function(a,b,d){a(function(a){var e=Array.prototype.slice.call(arguments,1);c[b]=e.length<=1?e[0]:e,d(a)})},function(a){(b||function(){})(a,c)})},a.series=function(a,b){var c=new a.constructor;f(a,function(a,b,d){a(function(a,e){var f=Array.prototype.slice.call(arguments,1);c[b]=f.length<=1?f[0]:f,d(a)})},function(a){(b||function(){})(a,c)})}})(typeof exports==="undefined"?this._={}:exports)
View
42 test/test.js
@@ -105,6 +105,16 @@ exports['each - async, reduced arity'] = function (test) {
});
};
+exports['each - async, zero arity'] = function (test) {
+ _.each([1,2,3], function () {
+ // should be passed the full set of arguments
+ test.equal(arguments.length, 4);
+ setTimeout(arguments[3], 0);
+ }, function () {
+ test.done();
+ });
+};
+
exports['each - async, empty array'] = function(test){
_.each([], function(x, callback){
test.ok(false, 'iterator should not be called');
@@ -274,6 +284,16 @@ exports['map - async, reduced arity'] = function (test) {
});
};
+exports['map - async, zero arity'] = function (test) {
+ _.map([1,2,3], function () {
+ // should be passed the full set of arguments
+ test.equal(arguments.length, 4);
+ setTimeout(arguments[3], 0);
+ }, function () {
+ test.done();
+ });
+};
+
exports['map - async, original untouched'] = function(test){
var a = [1,2,3];
_.map(a, function(x, callback){
@@ -440,6 +460,16 @@ exports['filter - async, reduced arity'] = function (test) {
});
};
+exports['filter - async, zero arity'] = function (test) {
+ _.filter([1,2,3], function () {
+ // should be passed the full set of arguments
+ test.equal(arguments.length, 4);
+ setTimeout(arguments[3], 0);
+ }, function () {
+ test.done();
+ });
+};
+
exports['filter - async, original untouched'] = function(test){
var a = [3,1,2];
_.filter(a, function(x, callback){
@@ -570,7 +600,7 @@ exports['reduce - async, object'] = function (test) {
});
};
-exports['reduce - async, reduce arity'] = function (test) {
+exports['reduce - async, reduced arity'] = function (test) {
_.reduce([1,2,3], function (a, value, cb) {
setTimeout(function () {
cb(null, a + value);
@@ -582,6 +612,16 @@ exports['reduce - async, reduce arity'] = function (test) {
});
};
+exports['reduce - async, zero arity'] = function (test) {
+ _.reduce([1,2,3], function () {
+ // should be passed the full set of arguments
+ test.equal(arguments.length, 5);
+ setTimeout(arguments[4], 0);
+ }, 10, function () {
+ test.done();
+ });
+};
+
exports['reduce - async with non-reference memo'] = function(test){
_.reduce([1,3,2], function(a, x, callback){
setTimeout(function(){callback(null, a + x)}, Math.random()*100);

0 comments on commit 8e60044

Please sign in to comment.
Something went wrong with that request. Please try again.