{"name":"contra","description":"Asynchronous flow control with a functional taste to it","version":"1.3.2","homepage":"https://github.com/bevacqua/contra","author":{"name":"Nicolas Bevacqua","email":"ng@bevacqua.io","url":"http://bevacqua.io"},"license":"MIT","repository":{"type":"git","url":"git://github.com/bevacqua/contra.git"},"keywords":["async","a","asynchronous","flow","control","promises","q","generator"],"scripts":{"test":"mocha --reporter tap && jshint --reporter node_modules/jshint-tap/jshint-tap.js test/*"},"testling":{"harness":"mocha","files":["src/contra.shim.js","test/*.js"],"browsers":{"chrome":[5,15,20,25,30,"canary"],"firefox":[3.6,10,15,20,25,"nightly"],"opera":[15,16,17,"next"],"safari":[4,5.1,6],"ie":[6,7,8,9,10],"iphone":[6],"ipad":[6],"android-browser":[4.2]}},"dependencies":{},"devDependencies":{"assert":"~1.1.0","gulp":"~3.4.0","gulp-bump":"~0.1.0","gulp-clean":"~0.2.3","gulp-concat":"~2.1.7","gulp-git":"~0.2.0","gulp-jshint":"~1.3.4","gulp-mocha":"~0.3.0","gulp-rename":"~0.2.2","gulp-size":"~0.1.1","gulp-uglify":"~0.1.0","jshint":"~2.4.1","jshint-stylish":"~0.1.5","jshint-tap":"0.0.1","mocha":"~1.17.0"},"bugs":{"url":"https://github.com/bevacqua/contra/issues"},"_id":"contra@1.3.2","dist":{"shasum":"4c57d27cfe14b9ba7444486c0eb41a269af9efbc","tarball":"http://registry-npm.ywwl.com/contra/download/contra-1.3.2.tgz","integrity":"sha512-zd3ZwYj7uE6IUIGkbDSYP9uKuQGSqGBRDwjrZRyVwbeDEW06iSm+J7zXUzfaFVCWtCsDFKP+mKkVEq9H3Uf5kQ==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDulyf6XLctCu0vY68KC6B44RD4XzoXffjmm//XXvjhmwIgQAcGGplZV4DBK05K2KrW5JnBOOFDF8KpIBM1cydZv6Q="}],"size":12956,"noattachment":false,"key":"/contra/-/contra-1.3.2.tgz"},"_from":".","_npmVersion":"1.3.22","_npmUser":{"name":"bevacqua","email":"nicolasbevacqua@gmail.com"},"maintainers":[{"name":"bevacqua","email":"nicolasbevacqua@gmail.com"}],"directories":{},"readme":"![contra.png][logo]\n\n[![badge](https://travis-ci.org/bevacqua/contra.png?branch=master)](https://travis-ci.org/bevacqua/contra) [![badge](https://badge.fury.io/js/contra.png)](http://badge.fury.io/js/contra) [![badge](https://badge.fury.io/bo/contra.png)](http://badge.fury.io/bo/contra) [![help me on gittip](http://gbindex.ssokolow.com/img/gittip-43x20.png)](https://www.gittip.com/bevacqua/) [![flattr.png](https://api.flattr.com/button/flattr-badge-large.png)](https://flattr.com/submit/auto?user_id=nzgb&url=https%3A%2F%2Fgithub.com%2Fbevacqua%2Fcontra)\n\n> Asynchronous flow control with a functional taste to it\n\n`λ` aims to stay small and simple, while powerful. Inspired by [async][1] and [lodash][2]. Methods are implemented individually and not as part of a whole. That design helps when considering to export functions individually. If you need all the methods in `async`, then stick with it. Otherwise, you might want to check `λ` out!\n\nFeature requests will be considered on a case-by-case basis.\n\n#### Quick Links\n\n- [CHANGELOG](CHANGELOG.md)\n- [Comparison with `async`](#comparison-with-async)\n- [Browser Support](#browser-support)\n- [License](#License)\n\n#### API\n\nFlow Control\n\n- [`λ.waterfall`](#%CE%BBwaterfalltasks-done)\n- [`λ.series`](#%CE%BBseriestasks-done)\n- [`λ.concurrent`](#%CE%BBconcurrenttasks-cap-done)\n\nFunctional\n\n- [`λ.each`](#%CE%BBeachitems-cap-iterator-done)\n- [`λ.each.series`](#%CE%BBeachseriesitems-iterator-done)\n- [`λ.map`](#%CE%BBmapitems-cap-iterator-done)\n- [`λ.map.series`](#%CE%BBmapseriesitems-iterator-done)\n- [`λ.filter`](#%CE%BBfilteritems-cap-iterator-done)\n- [`λ.filter.series`](#%CE%BBfilterseriesitems-iterator-done)\n\nUncategorized\n\n- [`λ.queue`](#%CE%BBqueueworker-cap1)\n- [`λ.emitter`](#%CE%BBemitterthing-options)\n- [`λ.curry`](#%CE%BBcurryfn-arguments)\n\n# Install\n\nInstall using `npm` or `bower`. Or get the [source code][3] and embed that in a `<script>` tag.\n\n```shell\nnpm i contra --save\n```\n\n```shell\nbower i contra --save\n```\n\nYou can use it as a Common.JS module, or embed it directly in your HTML.\n\n```js\nvar λ = require('contra');\n```\n\n```html\n<script src='contra.js'></script>\n<script>\nvar λ = contra;\n</script>\n```\n\n<sub>The only reason `contra` isn't published as `λ` directly is to make it easier for you to type.</sub>\n\n<sub>[_Back to top_](#quick-links)</sub>\n\n# API\n\nThese are the asynchronous flow control methods provided by `λ`.\n\n## `λ.waterfall(tasks, done?)`\n\nExecutes tasks in series. Each step receives the arguments from the previous step.\n\n- `tasks` Array of functions with the `(...results, next)` signature\n- `done` Optional function with the `(err, ...results)` signature\n\n```js\nλ.waterfall([\n  function (next) {\n    next(null, 'params for', 'next', 'step');\n  },\n  function (a, b, c, next) {\n    console.log(b);\n    // <- 'next'\n    next(null, 'ok', 'done');\n  }\n], function (err, ok, result) {\n  console.log(result);\n  // <- 'done'\n});\n```\n\n<sub>[_Back to top_](#quick-links)</sub>\n\n## `λ.concurrent(tasks, cap?, done?)`\n\nExecutes tasks concurrently. Results get passed as an array or hash to an optional `done` callback. Task order is preserved in results. You can set a concurrency cap, and it's uncapped by default.\n\n- `tasks` Collection of functions with the `(cb)` signature. Can be an array or an object\n- `cap` Optional concurrency level, used by the internal [queue](#%CE%BBqueueworker-cap1)\n- `done` Optional function with the `(err, results)` signature\n\n```js\nλ.concurrent([\n  function (cb) {\n    setTimeout(function () {\n      cb(null, 'boom');\n    }, 1000);\n  },\n  function (cb) {\n    cb(null, 'foo');\n  }\n], function (err, results) {\n  console.log(results);\n  // <- ['boom', 'foo']\n});\n```\n\nUsing objects\n\n```js\nλ.concurrent({\n  first: function (cb) {\n    setTimeout(function () {\n      cb(null, 'boom');\n    }, 1000);\n  },\n  second: function (cb) {\n    cb(null, 'foo');\n  }\n}, function (err, results) {\n  console.log(results);\n  // <- { first: 'boom', second: 'foo' }\n});\n```\n\n<sub>[_Back to top_](#quick-links)</sub>\n\n## `λ.series(tasks, done?)`\n\n**Effectively an alias for `λ.concurrent(tasks, 1, done?)`.**\n\nExecutes tasks in series. `done` gets all the results. Results get passed as an array or hash to an optional `done` callback. Task order is preserved in results.\n\n- `tasks` Collection of functions with the `(next)` signature. Can be an array or an object\n- `done` Optional function with the `(err, results)` signature\n\n```js\nλ.series([\n  function (next) {\n    setTimeout(function () {\n      next(null, 'boom');\n    }, 1000);\n  },\n  function (next) {\n    next(null, 'foo');\n  }\n], function (err, results) {\n  console.log(results);\n  // <- ['boom', 'foo']\n});\n```\n\nUsing objects\n\n```js\nλ.series({\n  first: function (next) {\n    setTimeout(function () {\n      next(null, 'boom');\n    }, 1000);\n  },\n  second: function (next) {\n    next(null, 'foo');\n  }\n}, function (err, results) {\n  console.log(results);\n  // <- { first: 'boom', second: 'foo' }\n});\n```\n\n<sub>[_Back to top_](#quick-links)</sub>\n\n## `λ.each(items, cap?, iterator, done?)`\n\nApplies an iterator to each element in the collection concurrently.\n\n- `items` Collection of items. Can be an array or an object\n- `cap` Optional concurrency level, used by the internal [queue](#%CE%BBqueueworker-cap1)\n- `iterator(item, key?, cb)` Function to execute on each item\n  - `item` The current item\n  - `key` Optional, array/object key of the current item\n  - `cb` Needs to be called when processing for current item is done\n- `done` Optional function with the `(err)` signature\n\n```js\nλ.each({ thing: 900, another: 23 }, function (item, cb) {\n  setTimeout(function () {\n    console.log(item);\n    cb();\n  }, item);\n});\n// <- 23\n// <- 900\n```\n\n<sub>[_Back to top_](#quick-links)</sub>\n\n## `λ.each.series(items, iterator, done?)`\n\nEffectively an alias for `λ.each(items, 1, iterator, done?)`.\n\n<sub>[_Back to top_](#quick-links)</sub>\n\n## `λ.map(items, cap?, iterator, done?)`\n\nApplies an iterator to each element in the collection concurrently. Produces an object with the transformation results. Task order is preserved in the results.\n\n- `items` Collection of items. Can be an array or an object\n- `cap` Optional concurrency level, used by the internal [queue](#%CE%BBqueueworker-cap1)\n- `iterator(item, key?, cb)` Function to execute on each item\n  - `item` The current item\n  - `key` Optional, array/object key of the current item\n  - `cb` Needs to be called when processing for current item is done\n- `done` Optional function with the `(err, results)` signature\n\n```js\nλ.map({ thing: 900, another: 23 }, function (item, cb) {\n  setTimeout(function () {\n    cb(null, item * 2);\n  }, item);\n}, function (err, results) {\n  console.log(results);\n  <- { thing: 1800, another: 46 }\n});\n```\n\n<sub>[_Back to top_](#quick-links)</sub>\n\n## `λ.map.series(items, iterator, done?)`\n\nEffectively an alias for `λ.map(items, 1, iterator, done?)`.\n\n<sub>[_Back to top_](#quick-links)</sub>\n\n## `λ.filter(items, cap?, iterator, done?)`\n\nApplies an iterator to each element in the collection concurrently. Produces an object with the filtered results. Task order is preserved in results.\n\n- `items` Collection of items. Can be an array or an object\n- `cap` Optional concurrency level, used by the internal [queue](#%CE%BBqueueworker-cap1)\n- `iterator(item, key?, cb)` Function to execute on each item\n  - `item` The current item\n  - `key` Optional, array/object key of the current item\n  - `cb` Needs to be called when processing for current item is done\n    - `err` An optional error which will short-circuit the filtering process, calling `done`\n    - `keep` Truthy will keep the item. Falsy will remove it in the results\n- `done` Optional function with the `(err, results)` signature\n\n```js\nλ.filter({ thing: 900, another: 23, foo: 69 }, function (item, cb) {\n  setTimeout(function () {\n    cb(null, item % 23 === 0);\n  }, item);\n}, function (err, results) {\n  console.log(results);\n  <- { another: 23, foo: 69 }\n});\n```\n\n<sub>[_Back to top_](#quick-links)</sub>\n\n## `λ.filter.series(items, iterator, done?)`\n\nEffectively an alias for `λ.filter(items, 1, iterator, done?)`.\n\n<sub>[_Back to top_](#quick-links)</sub>\n\n## `λ.queue(worker, cap=1)`\n\nUsed to create a job queue.\n\n- `worker(job, done)` Function to process jobs in the queue\n  - `job` The current job\n  - `done` Needs to be called when processing for current job is done\n- `cap` Optional concurrency level, defaults to `1` (serial)\n\nReturns a queue you can `push` or `unshift` jobs to. You can pause and resume the queue by hand.\n\n- `push(job, done?)` Array of jobs or an individual job object. Enqueue those jobs, continue processing **(unless paused)**. Optional callback to run when each job is completed\n- `unshift(job, done?)` Array of jobs or an individual job object. Add jobs to the top of the queue, continue processing **(unless paused)**. Optional callback to run when each job is completed\n- `pending` Property. Jobs that haven't started processing yet\n- `length` Short-hand for `pending.length`, only works if getters can be defined\n- `pause()` Stop processing jobs. Those already being processed will run to completion\n- `resume()` Start processing jobs again, after a `pause()`\n- `on('drain', fn)` Execute `fn` whenever there's no more pending _(or running)_ jobs and processing is requested. Processing can be requested using `resume`, `push`, or `unshift`\n\n```js\nvar q = λ.queue(worker);\n\nfunction worker (job, done) {\n  console.log(job);\n  done(null);\n}\n\nq.push('job', function () {\n  console.log('this job is done!');\n});\n\nq.push(['some', 'more'], function () {\n  console.log('one of these jobs is done!');\n});\n\nq.on('drain', function () {\n  console.log('all done!');\n  // if you enqueue more tasks now, then drain\n  // will fire again when pending.length reaches 0\n});\n\n// <- 'this job is done!'\n// <- 'one of these jobs is done!'\n// <- 'one of these jobs is done!'\n// <- 'all done!'\n```\n\n<sub>[_Back to top_](#quick-links)</sub>\n\n## `λ.emitter(thing={}, options={})`\n\nAugments `thing` with the event emitter methods listed below. If `thing` isn't provided, an event emitter is created for you. Emitter methods return the `thing` for chaining.\n\n- `thing` Optional. Writable JavaScript object\n- `emit(type, ...arguments)` Emits an event of type `type`, passing any `...arguments`\n- `emitterSnapshot(type)` Returns a function you can call, passing any `...arguments`\n- `on(type, fn)` Registers an event listener `fn` for `type` events\n- `once(type, fn)` Same as `on`, but the listener is discarded after one callback\n- `off(type, fn)` Unregisters an event listener `fn` from `type` events\n- `off(type)` Unregisters all event listeners from `type` events\n- `off()` Unregisters all event listeners\n\nThe `emitterSnapshot(type)` method lets you remove all event listeners before emitting an event that might add more event listeners which shouldn't be removed. In the example below, `thing` removes all events and then emits a `'destroy'` event, resulting in a `'create'` event handler being attached. If we just used `thing.off()` after emitting the destroy event, the `'create'` event handler would be wiped out too _(or the consumer would have to know implementation details as to avoid this issue)_.\n\n```js\nvar thing = λ.emitter();\n\nthing.on('foo', foo);\nthing.on('bar', bar);\nthing.on('destroy', function () {\n  thing.on('create', reinitialize);\n});\n\nvar destroy = thing.emitterSnapshot('destroy');\nthing.off();\ndestroy();\n```\n\nThe emitter can be configured with the following options, too.\n\n- `async` Debounce listeners asynchronously. By default they're executed in sequence.\n- `throws` Throw an exception if an `error` event is emitted and no listeners are defined. Defaults to `true`.\n\n```js\nvar thing = λ.emitter(); // also, λ.emitter({ foo: 'bar' })\n\nthing.once('something', function (level) {\n  console.log('something FIRST TROLL');\n});\n\nthing.on('something', function (level) {\n  console.log('something level ' + level);\n});\n\nthing.emit('something', 4);\nthing.emit('something', 5);\n// <- 'something FIRST TROLL'\n// <- 'something level 4'\n// <- 'something level 5'\n```\n\nReturns `thing`.\n\nEvents of type `error` have a special behavior. `λ.emitter` will throw if there are no `error` listeners when an error event is emitted. This behavior can be turned off setting `throws: false` in the options.\n\n```js\nvar thing = { foo: 'bar' };\n\nλ.emitter(thing);\n\nthing.emit('error', 'foo');\n<- throws 'foo'\n```\n\nIf an `'error'` listener is registered, then it'll work just like any other event type.\n\n```js\nvar thing = { foo: 'bar' };\n\nλ.emitter(thing);\n\nthing.on('error', function (err) {\n  console.log(err);\n});\n\nthing.emit('error', 'foo');\n<- 'foo'\n```\n\n<sub>[_Back to top_](#quick-links)</sub>\n\n## `λ.curry(fn, ...arguments)`\n\nReturns a function bound with some arguments and a `next` callback.\n\n```js\nλ.curry(fn, 1, 3, 5);\n// <- function (next) { fn(1, 3, 5, next); }\n```\n\n<sub>[_Back to top_](#quick-links)</sub>\n\n# Comparison with `async`\n\n[`async`][1]|`λ`\n---|---\nAimed at Noders|Tailored for browsers\nArrays for [some][5], collections for [others][6]|Collections for **everyone**!\n`apply`|`curry`\n`parallel`|`concurrent`\n`parallelLimit`|`concurrent`\n`mapSeries`|`map.series`\nMore _comprehensive_|More _focused_\n`~29.6k (minified, uncompressed)`|`~2.7k (minified, uncompressed)`\n\n`λ` isn't meant to be a replacement for `async`. It aims to provide a more focused library, and a bit more consistency.\n\n<sub>[_Back to top_](#quick-links)</sub>\n\n# Browser Support\n\n[![Browser Support](https://ci.testling.com/bevacqua/contra.png)](https://ci.testling.com/bevacqua/contra)\n\nIf you need support for one of the legacy browsers listed below, you'll need `contra.shim.js`.\n\n- IE < 10\n- Safari < 6\n- Opera < 16\n\n```js\nrequire('contra/shim');\nvar λ = require('contra');\n```\n\n```html\n<script src='contra.shim.js'></script>\n<script src='contra.js'></script>\n<script>\nvar λ = contra;\n</script>\n```\n\nThe shim currently clocks around `~1.2k` minified, uncompressed.\n\n<sub>[_Back to top_](#quick-links)</sub>\n\n# License\n\nMIT\n\n<sub>[_Back to top_](#quick-links)</sub>\n\n  [logo]: https://raw.github.com/bevacqua/contra/master/resources/contra.png\n  [1]: https://github.com/caolan/async\n  [2]: https://github.com/lodash/lodash\n  [3]: https://github.com/bevacqua/contra/tree/master/src/contra.js\n  [4]: https://github.com/bevacqua\n  [5]: https://github.com/caolan/async#maparr-iterator-callback\n  [6]: https://github.com/caolan/async#paralleltasks-callback\n","publish_time":1391612300329,"_cnpm_publish_time":1391612300329,"dist-tags":{"latest":"1.9.4"}}