Comparing version 1.0.13 to 1.0.14
{ | ||
"name": "contra", | ||
"description": "Asynchronous flow control with a `_` taste to it", | ||
"version": "1.0.13", | ||
"version": "1.0.14", | ||
"homepage": "https://github.com/bevacqua/contra", | ||
@@ -6,0 +6,0 @@ "author": { |
@@ -121,3 +121,3 @@ (function (Object) { | ||
}); | ||
flow(tasks, finish ? finish(done) : done); | ||
flow(tasks, finish ? finish(collection, done) : done); | ||
}; | ||
@@ -129,3 +129,3 @@ } | ||
return _map(flow, finish); | ||
function finish (done) { | ||
function finish (collection, done) { | ||
return function mask (err) { | ||
@@ -137,2 +137,23 @@ done(err); // only return the error, no more arguments | ||
// dependencies: ['map'] | ||
function _filter (flow) { | ||
return _map(flow, finish); | ||
function finish (collection, done) { | ||
return function filter (err, results) { | ||
function exists (item, key) { | ||
return !!results[key]; | ||
} | ||
function ofilter () { | ||
var filtered = {}; | ||
Object.keys(collection).forEach(function omapper (key) { | ||
if (exists(null, key)) { filtered[key] = collection[key]; } | ||
}); | ||
return filtered; | ||
} | ||
if (err) { done(err); return; } | ||
done(null, a(results) ? collection.filter(exists) : ofilter()); | ||
}; | ||
} | ||
} | ||
// { name: 'emitter', dependencies: ['core'] } | ||
@@ -199,2 +220,3 @@ function _emitter (thing) { | ||
map: _map(_concurrent), | ||
filter: _filter(_concurrent), | ||
queue: _queue, | ||
@@ -206,2 +228,3 @@ emitter: _emitter | ||
$.map.series = _map(_series); | ||
$.filter.series = _filter(_series); | ||
@@ -208,0 +231,0 @@ // cross-platform export |
@@ -1,1 +0,1 @@ | ||
!function(n){"use strict";function t(t){return"[object Array]"===n.prototype.toString.call(t)}function e(n){return Array.prototype.slice.call(n)}function r(n,t,e){n&&h(function(){n.apply(e||null,t||[])})}function u(n){function t(){e||(e=!0,n.apply(null,arguments))}var e;return t.discard=function(){e=!0},t}function i(n,t,e){var u=n.shift();return u?(e&&e.discard(),r(t,[u]),!0):void 0}function o(){var n=e(arguments),t=n.shift();return function(){var r=e(n),u=e(arguments);t.apply(t,r.concat(u))}}function f(n,t){function o(){var f=u(t);return u(function(){var t=e(arguments),u=n.shift();if(u){if(i(t,f))return;t.push(o()),r(u,t)}else r(f,arguments)})}o()()}function c(o,f){function c(){return u(function(){var n=l.shift(),t=e(arguments),u=o[n];if(a){if(i(t,s))return;p[a]=t.shift()}a=n,u?r(u,[c()]):r(s,[null,p])})}var a,s=u(f),l=n.keys(o),p=t(o)?[]:{};c()()}function a(o,f){function c(n){var t=u(function(){var u=e(arguments);i(u,a,t)||(l[n]=u.shift(),++p===h&&r(a,[null,l]))});return t}var a=u(f),s=n.keys(o),l=t(o)?[]:{},p=0,h=s.length;s.forEach(function(n){r(o[n],[c(n)])})}function s(e,r){return function(u,i,o){var f=n.keys(u),c=t(u)?[]:{};f.forEach(function(n){c[n]=function(t){i(u[n],t)}}),e(c,r?r(o):o)}}function l(n){function t(n){return function(t){n(t)}}return s(n,t)}function p(n){var t=this,u={};return n.on=function(n,t){u[n]?u[n].push(t):u[n]=[t]},n.emit=function(){var n=e(arguments),i=n.shift(),o=u[i];if("error"===i&&!o)throw 1===n.length?n[0]:n;o&&o.forEach(function(e){r(e,n,t)})},n}var h,m="function"==typeof setImmediate;h="undefined"!=typeof process&&process.nextTick?m?setImmediate:process.nextTick:m?function(n){setImmediate(n)}:function(n){setTimeout(n,0)};var d=function(e,i){function o(n,e,u){var i=e?"unshift":"push",o=t(n)?n:[n];o.forEach(function(n){s[i]({t:n,done:u})}),r(f)}function f(){if(!(a||l>=p)&&s.length){l++;var n=s.pop();e(n.t,u(c.bind(null,n)))}}function c(n,t){l--,r(n.done,[t]),r(f)}var a,s=[],l=0,p=i||1,h={push:function(n,t){o(n,!1,t)},unshift:function(n,t){o(n,!0,t)},pause:function(){a=!0},resume:function(){a=!1,f()}};return n.defineProperty(h,"length",{get:function(){return s.length}}),h},v={apply:o,concurrent:a,series:c,waterfall:f,each:l(a),map:s(a),queue:d,emitter:p};v.each.series=l(c),v.map.series=s(c),"undefined"!=typeof module&&module.exports?module.exports=v:window.contra=v}(Object); | ||
!function(n){"use strict";function t(t){return"[object Array]"===n.prototype.toString.call(t)}function r(n){return Array.prototype.slice.call(n)}function u(n,t,r){n&&m(function(){n.apply(r||null,t||[])})}function e(n){function t(){r||(r=!0,n.apply(null,arguments))}var r;return t.discard=function(){r=!0},t}function i(n,t,r){var e=n.shift();return e?(r&&r.discard(),u(t,[e]),!0):void 0}function o(){var n=r(arguments),t=n.shift();return function(){var u=r(n),e=r(arguments);t.apply(t,u.concat(e))}}function f(n,t){function o(){var f=e(t);return e(function(){var t=r(arguments),e=n.shift();if(e){if(i(t,f))return;t.push(o()),u(e,t)}else u(f,arguments)})}o()()}function c(o,f){function c(){return e(function(){var n=l.shift(),t=r(arguments),e=o[n];if(a){if(i(t,s))return;p[a]=t.shift()}a=n,e?u(e,[c()]):u(s,[null,p])})}var a,s=e(f),l=n.keys(o),p=t(o)?[]:{};c()()}function a(o,f){function c(n){var t=e(function(){var e=r(arguments);i(e,a,t)||(l[n]=e.shift(),++p===h&&u(a,[null,l]))});return t}var a=e(f),s=n.keys(o),l=t(o)?[]:{},p=0,h=s.length;s.forEach(function(n){u(o[n],[c(n)])})}function s(r,u){return function(e,i,o){var f=n.keys(e),c=t(e)?[]:{};f.forEach(function(n){c[n]=function(t){i(e[n],t)}}),r(c,u?u(e,o):o)}}function l(n){function t(n,t){return function(n){t(n)}}return s(n,t)}function p(r){function u(r,u){return function(e,i){function o(n,t){return!!i[t]}function f(){var t={};return n.keys(r).forEach(function(n){o(null,n)&&(t[n]=r[n])}),t}return e?void u(e):void u(null,t(i)?r.filter(o):f())}}return s(r,u)}function h(n){var t=this,e={};return n.on=function(n,t){e[n]?e[n].push(t):e[n]=[t]},n.emit=function(){var n=r(arguments),i=n.shift(),o=e[i];if("error"===i&&!o)throw 1===n.length?n[0]:n;o&&o.forEach(function(r){u(r,n,t)})},n}var m,v="function"==typeof setImmediate;m="undefined"!=typeof process&&process.nextTick?v?setImmediate:process.nextTick:v?function(n){setImmediate(n)}:function(n){setTimeout(n,0)};var d=function(r,i){function o(n,r,e){var i=r?"unshift":"push",o=t(n)?n:[n];o.forEach(function(n){s[i]({t:n,done:e})}),u(f)}function f(){if(!(a||l>=p)&&s.length){l++;var n=s.pop();r(n.t,e(c.bind(null,n)))}}function c(n,t){l--,u(n.done,[t]),u(f)}var a,s=[],l=0,p=i||1,h={push:function(n,t){o(n,!1,t)},unshift:function(n,t){o(n,!0,t)},pause:function(){a=!0},resume:function(){a=!1,f()}};return n.defineProperty(h,"length",{get:function(){return s.length}}),h},y={apply:o,concurrent:a,series:c,waterfall:f,each:l(a),map:s(a),filter:p(a),queue:d,emitter:h};y.each.series=l(c),y.map.series=s(c),y.filter.series=p(c),"undefined"!=typeof module&&module.exports?module.exports=y:window.contra=y}(Object); |
{ | ||
"name": "contra", | ||
"description": "Asynchronous flow control with a `_` taste to it", | ||
"version": "1.0.13", | ||
"version": "1.0.14", | ||
"homepage": "https://github.com/bevacqua/contra", | ||
@@ -6,0 +6,0 @@ "author": { |
@@ -30,2 +30,4 @@ ![contra.png][logo] | ||
- [`λ.map.series`](#%CE%BBmapseriesitems-iterator-done) | ||
- [`λ.filter`](#%CE%BBfilteritems-iterator-done) | ||
- [`λ.filter.series`](#%CE%BBfilterseriesitems-iterator-done) | ||
@@ -196,3 +198,3 @@ Uncategorized | ||
Applies an iterator to each element in the collection concurrently. Produces an object with the transformation results. Task order is preserved in results. | ||
Applies an iterator to each element in the collection concurrently. Produces an object with the transformation results. Task order is preserved in the results. | ||
@@ -203,11 +205,11 @@ - `items` Collection of items. Can be an array or an object | ||
- `cb` Needs to be called when processing for current item is done | ||
- `done` Optional function with the `(err)` signature | ||
- `done` Optional function with the `(err, results)` signature | ||
```js | ||
λ.each({ thing: 900, another: 23 }, function (item, cb) { | ||
λ.map({ thing: 900, another: 23 }, function (item, cb) { | ||
setTimeout(function () { | ||
cb(null, item * 2); | ||
}, item); | ||
}, function (err, result) { | ||
console.log(result); | ||
}, function (err, results) { | ||
console.log(results); | ||
<- { thing: 1800, another: 46 } | ||
@@ -221,4 +223,33 @@ }); | ||
## `λ.filter(items, iterator[, done])` | ||
Applies an iterator to each element in the collection concurrently. Produces an object with the filtered results. Task order is preserved in results. | ||
- `items` Collection of items. Can be an array or an object | ||
- `iterator(item, cb)` Function to execute on each item | ||
- `item` The current item | ||
- `cb` Needs to be called when processing for current item is done | ||
- `err` An optional error which will short-circuit the filtering process, calling `done` | ||
- `keep` Truthy will keep the item. Falsy will remove it in the results | ||
- `done` Optional function with the `(err, results)` signature | ||
```js | ||
λ.filter({ thing: 900, another: 23, foo: 69 }, function (item, cb) { | ||
setTimeout(function () { | ||
cb(null, item % 23 === 0); | ||
}, item); | ||
}, function (err, results) { | ||
console.log(results); | ||
<- { another: 23, foo: 69 } | ||
}); | ||
``` | ||
## `λ.filter.series(items, iterator[, done])` | ||
Same as `λ.filter(items, iterator[, done])`, but in series instead of concurrently. | ||
## `λ.queue(worker[, concurrency=1])` | ||
Used to create a job queue. | ||
- `worker(job, done)` Function to process jobs in the queue | ||
@@ -229,3 +260,3 @@ - `job` The current job | ||
Returns a queue you can `push` or `unshift` tasks to. You can pause and resume the queue by hand. | ||
Returns a queue you can `push` or `unshift` jobs to. You can pause and resume the queue by hand. | ||
@@ -321,3 +352,3 @@ - `push(job[, done])` Array of jobs or an individual job object. Enqueue those jobs, resume processing. Optional callback to run when each job is completed | ||
---|--- | ||
Aimed at Noders|All ages may use! | ||
Aimed at Noders|Tailored for browsers | ||
Arrays for [some][5], collections for [others][6]|Collections for **everyone**! | ||
@@ -327,3 +358,3 @@ `parallel`|`concurrent` | ||
More _comprehensive_|More _focused_ | ||
`~29.6k (minified, uncompressed)`|`~2.3k (minified, uncompressed)` | ||
`~29.6k (minified, uncompressed)`|`~2.6k (minified, uncompressed)` | ||
@@ -330,0 +361,0 @@ `λ` isn't meant to be a replacement for `async`. It aims to provide a more focused library, and a bit more consistency. |
@@ -121,3 +121,3 @@ (function (Object) { | ||
}); | ||
flow(tasks, finish ? finish(done) : done); | ||
flow(tasks, finish ? finish(collection, done) : done); | ||
}; | ||
@@ -129,3 +129,3 @@ } | ||
return _map(flow, finish); | ||
function finish (done) { | ||
function finish (collection, done) { | ||
return function mask (err) { | ||
@@ -137,2 +137,23 @@ done(err); // only return the error, no more arguments | ||
// dependencies: ['map'] | ||
function _filter (flow) { | ||
return _map(flow, finish); | ||
function finish (collection, done) { | ||
return function filter (err, results) { | ||
function exists (item, key) { | ||
return !!results[key]; | ||
} | ||
function ofilter () { | ||
var filtered = {}; | ||
Object.keys(collection).forEach(function omapper (key) { | ||
if (exists(null, key)) { filtered[key] = collection[key]; } | ||
}); | ||
return filtered; | ||
} | ||
if (err) { done(err); return; } | ||
done(null, a(results) ? collection.filter(exists) : ofilter()); | ||
}; | ||
} | ||
} | ||
// { name: 'emitter', dependencies: ['core'] } | ||
@@ -199,2 +220,3 @@ function _emitter (thing) { | ||
map: _map(_concurrent), | ||
filter: _filter(_concurrent), | ||
queue: _queue, | ||
@@ -206,2 +228,3 @@ emitter: _emitter | ||
$.map.series = _map(_series); | ||
$.filter.series = _filter(_series); | ||
@@ -208,0 +231,0 @@ // cross-platform export |
@@ -403,2 +403,71 @@ 'use strict'; | ||
describe('filter()', function () { | ||
it('should filter array concurrently', function (done) { | ||
function t (i, done) { | ||
done(null, typeof i === 'string'); | ||
} | ||
function d (err, results) { | ||
should(err).be.not.ok; | ||
results.length.should.equal(2); | ||
results.should.eql(['b', 'c']); | ||
done(); | ||
} | ||
λ.filter([1,2,'b',3,'c',5],t,d); | ||
}); | ||
it('should filter object concurrently', function (done) { | ||
function t (i, done) { | ||
done(null, typeof i === 'string'); | ||
} | ||
function d (err, results) { | ||
should(err).be.not.ok; | ||
should(Object.keys(results).length).equal(2); | ||
results.should.eql({ a: 'b', b: 'c' }); | ||
done(); | ||
} | ||
λ.filter({ n: 3, a: 'b', b: 'c', c: 4, d: 5, e: 6 }, t, d); | ||
}); | ||
it('should short-circuit on error', function (done) { | ||
function t (i, done) { | ||
done(i); | ||
} | ||
function d (err, results) { | ||
should(err).be.ok; | ||
should(results).be.not.ok; | ||
done(); | ||
} | ||
λ.filter(['b','c','e'],t,d); | ||
}); | ||
}); | ||
describe('filter.series()', function () { | ||
it('should filter array in a series', function (done) { | ||
function t (i, done) { | ||
done(null, typeof i === 'string'); | ||
} | ||
function d (err, results) { | ||
should(err).be.not.ok; | ||
results.length.should.equal(2); | ||
results.should.eql(['b', 'c']); | ||
done(); | ||
} | ||
λ.filter.series([1,2,'b',3,'c',5],t,d); | ||
}); | ||
it('should filter object in a series', function (done) { | ||
function t (i, done) { | ||
done(null, typeof i === 'string'); | ||
} | ||
function d (err, results) { | ||
should(err).be.not.ok; | ||
should(Object.keys(results).length).equal(2); | ||
results.should.eql({ a: 'b', b: 'c' }); | ||
done(); | ||
} | ||
λ.filter.series({ n: 3, a: 'b', b: 'c', c: 4, d: 5, e: 6 }, t, d); | ||
}); | ||
}); | ||
describe('queue()', function () { | ||
@@ -405,0 +474,0 @@ it('should queue things', function (done) { |
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
47673
1128
389