pax_global_header00006660000000000000000000000064127277307620014527gustar00rootroot0000000000000052 comment=583a75ed4fe41761b66416bb6e703ebb1f8963bf asynckit-0.4.0/000077500000000000000000000000001272773076200133555ustar00rootroot00000000000000asynckit-0.4.0/.editorconfig000066400000000000000000000004231272773076200160310ustar00rootroot00000000000000# This file is for unifying the coding style for different editors and IDEs # editorconfig.org root = true [*] end_of_line = lf charset = utf-8 trim_trailing_whitespace = true [**.{css,hbs,js,json,md,scss}] indent_style = space indent_size = 2 insert_final_newline = true asynckit-0.4.0/.eslintignore000066400000000000000000000000321272773076200160530ustar00rootroot00000000000000coverage/* node_modules/* asynckit-0.4.0/.eslintrc000066400000000000000000000020641272773076200152030ustar00rootroot00000000000000{ "rules": { "indent": [2, 2, {"SwitchCase": 1}], "quotes": [2, "single"], "linebreak-style": [2, "unix"], "semi": [2, "always"], "curly": [2, "multi-line"], "handle-callback-err": [2, "^err"], "valid-jsdoc": [2, { "requireReturn": false, "requireReturnDescription": false, "prefer": { "return": "returns" } }], "require-jsdoc": [2, { "require": { "FunctionDeclaration": true } }], "no-redeclare": [2, { "builtinGlobals": true }], "no-shadow": [2, { "builtinGlobals": true, "hoist": "all" }], "no-use-before-define": [2, "nofunc"], "no-sparse-arrays": 2, "no-shadow-restricted-names": 2, "no-extra-semi": 2, "no-unused-vars": 2, "no-undef": 2, "no-irregular-whitespace": 2, "no-console": 2, "key-spacing": 0, "strict": 0, "dot-notation": 0, "eol-last": 0, "no-new": 0, "semi-spacing": 0, "no-multi-spaces": 0, "eqeqeq": 0, "no-mixed-requires": 0 }, "env": { "node": true, "browser": true } } asynckit-0.4.0/.gitignore000066400000000000000000000000641272773076200153450ustar00rootroot00000000000000.DS_Store .nyc_output coverage node_modules *.log asynckit-0.4.0/.istanbul.yml000066400000000000000000000007061272773076200160020ustar00rootroot00000000000000verbose: false instrumentation: root: . extensions: - .js default-excludes: true excludes: ['test.js', '**/node_modules/**'] compact: false reporting: root: ./coverage/tmp print: summary reports: - lcov dir: ./coverage report-config: json: file: coverage.json watermarks: statements: [90, 99] lines: [90, 99] functions: [90, 99] branches: [90, 99] asynckit-0.4.0/.npmignore000066400000000000000000000002341272773076200153530ustar00rootroot00000000000000.DS_Store .editorconfig .eslintignore .eslintrc .gitignore .istanbul.yml .npmignore .travis.yml appveyor.yml .nyc_output coverage node_modules test *.log asynckit-0.4.0/.travis.yml000066400000000000000000000005431272773076200154700ustar00rootroot00000000000000sudo: false language: node_js node_js: - "0.12" - "iojs" - "4" - "5" - "stable" script: - node --version - npm --version # linting - npm run lint # nodejs testing - npm run test # browser testing - npm run browser # coverage report - npm run report after_success: - "cat coverage/lcov.info | ./node_modules/.bin/coveralls" asynckit-0.4.0/LICENSE000066400000000000000000000020661272773076200143660ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2016 Alex Indigo Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. asynckit-0.4.0/README.md000066400000000000000000000167211272773076200146430ustar00rootroot00000000000000# asynckit [![NPM Module](https://img.shields.io/npm/v/asynckit.svg?style=flat)](https://www.npmjs.com/package/asynckit) Minimal async jobs utility library, with streams support. [![PhantomJS Build](https://img.shields.io/travis/alexindigo/asynckit/master.svg?label=browser&style=flat)](https://travis-ci.org/alexindigo/asynckit) [![Linux Build](https://img.shields.io/travis/alexindigo/asynckit/master.svg?label=linux:0.12-6.x&style=flat)](https://travis-ci.org/alexindigo/asynckit) [![Windows Build](https://img.shields.io/appveyor/ci/alexindigo/asynckit/master.svg?label=windows:0.12-6.x&style=flat)](https://ci.appveyor.com/project/alexindigo/asynckit) [![Coverage Status](https://img.shields.io/coveralls/alexindigo/asynckit/master.svg?label=code+coverage&style=flat)](https://coveralls.io/github/alexindigo/asynckit?branch=master) [![Dependency Status](https://img.shields.io/david/alexindigo/asynckit.svg?style=flat)](https://david-dm.org/alexindigo/asynckit) [![bitHound Overall Score](https://www.bithound.io/github/alexindigo/asynckit/badges/score.svg)](https://www.bithound.io/github/alexindigo/asynckit) AsyncKit provides harness for `parallel` and `serial` iterators over list of items represented by arrays or objects. Optionally it accepts abort function (should be synchronously return by iterator for each item), and terminates left over jobs upon an error event. For specific iteration order built-in (`ascending` and `descending`) and custom sort helpers also supported, via `asynckit.serialOrdered` method. It ensures async operations to keep behavior more stable and prevent `Maximum call stack size exceeded` errors, from sync iterators. | compression | size | | :----------------- | -------: | | asynckit.js | 12.34 kB | | asynckit.min.js | 4.11 kB | | asynckit.min.js.gz | 1.47 kB | ## Install ```sh $ npm install --save asynckit ``` ## Examples ### Parallel Jobs Runs iterator over provided array in parallel. Stores output in the `result` array, on the matching positions. In unlikely event of an error from one of the jobs, will terminate rest of the active jobs (if abort function is provided) and return error along with salvaged data to the main callback function. #### Input Array ```javascript var parallel = require('asynckit').parallel , assert = require('assert') ; var source = [ 1, 1, 4, 16, 64, 32, 8, 2 ] , expectedResult = [ 2, 2, 8, 32, 128, 64, 16, 4 ] , expectedTarget = [ 1, 1, 2, 4, 8, 16, 32, 64 ] , target = [] ; parallel(source, asyncJob, function(err, result) { assert.deepEqual(result, expectedResult); assert.deepEqual(target, expectedTarget); }); // async job accepts one element from the array // and a callback function function asyncJob(item, cb) { // different delays (in ms) per item var delay = item * 25; // pretend different jobs take different time to finish // and not in consequential order var timeoutId = setTimeout(function() { target.push(item); cb(null, item * 2); }, delay); // allow to cancel "leftover" jobs upon error // return function, invoking of which will abort this job return clearTimeout.bind(null, timeoutId); } ``` More examples could be found in [test/test-parallel-array.js](test/test-parallel-array.js). #### Input Object Also it supports named jobs, listed via object. ```javascript var parallel = require('asynckit/parallel') , assert = require('assert') ; var source = { first: 1, one: 1, four: 4, sixteen: 16, sixtyFour: 64, thirtyTwo: 32, eight: 8, two: 2 } , expectedResult = { first: 2, one: 2, four: 8, sixteen: 32, sixtyFour: 128, thirtyTwo: 64, eight: 16, two: 4 } , expectedTarget = [ 1, 1, 2, 4, 8, 16, 32, 64 ] , expectedKeys = [ 'first', 'one', 'two', 'four', 'eight', 'sixteen', 'thirtyTwo', 'sixtyFour' ] , target = [] , keys = [] ; parallel(source, asyncJob, function(err, result) { assert.deepEqual(result, expectedResult); assert.deepEqual(target, expectedTarget); assert.deepEqual(keys, expectedKeys); }); // supports full value, key, callback (shortcut) interface function asyncJob(item, key, cb) { // different delays (in ms) per item var delay = item * 25; // pretend different jobs take different time to finish // and not in consequential order var timeoutId = setTimeout(function() { keys.push(key); target.push(item); cb(null, item * 2); }, delay); // allow to cancel "leftover" jobs upon error // return function, invoking of which will abort this job return clearTimeout.bind(null, timeoutId); } ``` More examples could be found in [test/test-parallel-object.js](test/test-parallel-object.js). ### Serial Jobs Runs iterator over provided array sequentially. Stores output in the `result` array, on the matching positions. In unlikely event of an error from one of the jobs, will not proceed to the rest of the items in the list and return error along with salvaged data to the main callback function. #### Input Array ```javascript var serial = require('asynckit/serial') , assert = require('assert') ; var source = [ 1, 1, 4, 16, 64, 32, 8, 2 ] , expectedResult = [ 2, 2, 8, 32, 128, 64, 16, 4 ] , expectedTarget = [ 0, 1, 2, 3, 4, 5, 6, 7 ] , target = [] ; serial(source, asyncJob, function(err, result) { assert.deepEqual(result, expectedResult); assert.deepEqual(target, expectedTarget); }); // extended interface (item, key, callback) // also supported for arrays function asyncJob(item, key, cb) { target.push(key); // it will be automatically made async // even it iterator "returns" in the same event loop cb(null, item * 2); } ``` More examples could be found in [test/test-serial-array.js](test/test-serial-array.js). #### Input Object Also it supports named jobs, listed via object. ```javascript var serial = require('asynckit').serial , assert = require('assert') ; var source = [ 1, 1, 4, 16, 64, 32, 8, 2 ] , expectedResult = [ 2, 2, 8, 32, 128, 64, 16, 4 ] , expectedTarget = [ 0, 1, 2, 3, 4, 5, 6, 7 ] , target = [] ; var source = { first: 1, one: 1, four: 4, sixteen: 16, sixtyFour: 64, thirtyTwo: 32, eight: 8, two: 2 } , expectedResult = { first: 2, one: 2, four: 8, sixteen: 32, sixtyFour: 128, thirtyTwo: 64, eight: 16, two: 4 } , expectedTarget = [ 1, 1, 4, 16, 64, 32, 8, 2 ] , target = [] ; serial(source, asyncJob, function(err, result) { assert.deepEqual(result, expectedResult); assert.deepEqual(target, expectedTarget); }); // shortcut interface (item, callback) // works for object as well as for the arrays function asyncJob(item, cb) { target.push(item); // it will be automatically made async // even it iterator "returns" in the same event loop cb(null, item * 2); } ``` More examples could be found in [test/test-serial-object.js](test/test-serial-object.js). _Note: Since _object_ is an _unordered_ collection of properties, it may produce unexpected results with sequential iterations. Whenever order of the jobs' execution is important please use `serialOrdered` method._ ### Ordered Serial Iterations TBD For example [compare-property](compare-property) package. ### Streaming interface TBD ## Want to Know More? More examples can be found in [test folder](test/). Or open an [issue](https://github.com/alexindigo/asynckit/issues) with questions and/or suggestions. ## License AsyncKit is licensed under the MIT license. asynckit-0.4.0/appveyor.yml000066400000000000000000000006151272773076200157470ustar00rootroot00000000000000environment: matrix: - nodejs_version: '0.12' - nodejs_version: '3' - nodejs_version: '4' - nodejs_version: '5' - nodejs_version: '6' platform: - x86 - x64 install: - ps: Install-Product node $env:nodejs_version $env:platform - npm install test_script: - node --version - npm --version - npm run lint - npm run win-test build: off matrix: fast_finish: true asynckit-0.4.0/bench.js000066400000000000000000000023501272773076200147720ustar00rootroot00000000000000/* eslint no-console: "off" */ var asynckit = require('./') , async = require('async') , assert = require('assert') , expected = 0 ; var Benchmark = require('benchmark'); var suite = new Benchmark.Suite; var source = []; for (var z = 1; z < 100; z++) { source.push(z); expected += z; } suite // add tests .add('async.map', function(deferred) { var total = 0; async.map(source, function(i, cb) { setImmediate(function() { total += i; cb(null, total); }); }, function(err, result) { assert.ifError(err); assert.equal(result[result.length - 1], expected); deferred.resolve(); }); }, {'defer': true}) .add('asynckit.parallel', function(deferred) { var total = 0; asynckit.parallel(source, function(i, cb) { setImmediate(function() { total += i; cb(null, total); }); }, function(err, result) { assert.ifError(err); assert.equal(result[result.length - 1], expected); deferred.resolve(); }); }, {'defer': true}) // add listeners .on('cycle', function(ev) { console.log(String(ev.target)); }) .on('complete', function() { console.log('Fastest is ' + this.filter('fastest').map('name')); }) // run async .run({ 'async': true }); asynckit-0.4.0/index.js000066400000000000000000000002341272773076200150210ustar00rootroot00000000000000module.exports = { parallel : require('./parallel.js'), serial : require('./serial.js'), serialOrdered : require('./serialOrdered.js') }; asynckit-0.4.0/lib/000077500000000000000000000000001272773076200141235ustar00rootroot00000000000000asynckit-0.4.0/lib/abort.js000066400000000000000000000007611272773076200155740ustar00rootroot00000000000000// API module.exports = abort; /** * Aborts leftover active jobs * * @param {object} state - current state object */ function abort(state) { Object.keys(state.jobs).forEach(clean.bind(state)); // reset leftover jobs state.jobs = {}; } /** * Cleans up leftover job by invoking abort function for the provided job id * * @this state * @param {string|number} key - job id to abort */ function clean(key) { if (typeof this.jobs[key] == 'function') { this.jobs[key](); } } asynckit-0.4.0/lib/async.js000066400000000000000000000011271272773076200155770ustar00rootroot00000000000000var defer = require('./defer.js'); // API module.exports = async; /** * Runs provided callback asynchronously * even if callback itself is not * * @param {function} callback - callback to invoke * @returns {function} - augmented callback */ function async(callback) { var isAsync = false; // check if async happened defer(function() { isAsync = true; }); return function async_callback(err, result) { if (isAsync) { callback(err, result); } else { defer(function nextTick_callback() { callback(err, result); }); } }; } asynckit-0.4.0/lib/defer.js000066400000000000000000000006711272773076200155520ustar00rootroot00000000000000module.exports = defer; /** * Runs provided function on next iteration of the event loop * * @param {function} fn - function to run */ function defer(fn) { var nextTick = typeof setImmediate == 'function' ? setImmediate : ( typeof process == 'object' && typeof process.nextTick == 'function' ? process.nextTick : null ); if (nextTick) { nextTick(fn); } else { setTimeout(fn, 0); } } asynckit-0.4.0/lib/iterate.js000066400000000000000000000034021272773076200161150ustar00rootroot00000000000000var async = require('./async.js') , abort = require('./abort.js') ; // API module.exports = iterate; /** * Iterates over each job object * * @param {array|object} list - array or object (named list) to iterate over * @param {function} iterator - iterator to run * @param {object} state - current job status * @param {function} callback - invoked when all elements processed */ function iterate(list, iterator, state, callback) { // store current index var key = state['keyedList'] ? state['keyedList'][state.index] : state.index; state.jobs[key] = runJob(iterator, key, list[key], function(error, output) { // don't repeat yourself // skip secondary callbacks if (!(key in state.jobs)) { return; } // clean up jobs delete state.jobs[key]; if (error) { // don't process rest of the results // stop still active jobs // and reset the list abort(state); } else { state.results[key] = output; } // return salvaged results callback(error, state.results); }); } /** * Runs iterator over provided job element * * @param {function} iterator - iterator to invoke * @param {string|number} key - key/index of the element in the list of jobs * @param {mixed} item - job description * @param {function} callback - invoked after iterator is done with the job * @returns {function|mixed} - job abort function or something else */ function runJob(iterator, key, item, callback) { var aborter; // allow shortcut if iterator expects only two arguments if (iterator.length == 2) { aborter = iterator(item, async(callback)); } // otherwise go with full three arguments else { aborter = iterator(item, key, async(callback)); } return aborter; } asynckit-0.4.0/lib/readable_asynckit.js000066400000000000000000000031131272773076200201230ustar00rootroot00000000000000var streamify = require('./streamify.js') , defer = require('./defer.js') ; // API module.exports = ReadableAsyncKit; /** * Base constructor for all streams * used to hold properties/methods */ function ReadableAsyncKit() { ReadableAsyncKit.super_.apply(this, arguments); // list of active jobs this.jobs = {}; // add stream methods this.destroy = destroy; this._start = _start; this._read = _read; } /** * Destroys readable stream, * by aborting outstanding jobs * * @returns {void} */ function destroy() { if (this.destroyed) { return; } this.destroyed = true; if (typeof this.terminator == 'function') { this.terminator(); } } /** * Starts provided jobs in async manner * * @private */ function _start() { // first argument – runner function var runner = arguments[0] // take away first argument , args = Array.prototype.slice.call(arguments, 1) // second argument - input data , input = args[0] // last argument - result callback , endCb = streamify.callback.call(this, args[args.length - 1]) ; args[args.length - 1] = endCb; // third argument - iterator args[1] = streamify.iterator.call(this, args[1]); // allow time for proper setup defer(function() { if (!this.destroyed) { this.terminator = runner.apply(null, args); } else { endCb(null, Array.isArray(input) ? [] : {}); } }.bind(this)); } /** * Implement _read to comply with Readable streams * Doesn't really make sense for flowing object mode * * @private */ function _read() { } asynckit-0.4.0/lib/readable_parallel.js000066400000000000000000000012411272773076200200720ustar00rootroot00000000000000var parallel = require('../parallel.js'); // API module.exports = ReadableParallel; /** * Streaming wrapper to `asynckit.parallel` * * @param {array|object} list - array or object (named list) to iterate over * @param {function} iterator - iterator to run * @param {function} callback - invoked when all elements processed * @returns {stream.Readable#} */ function ReadableParallel(list, iterator, callback) { if (!(this instanceof ReadableParallel)) { return new ReadableParallel(list, iterator, callback); } // turn on object mode ReadableParallel.super_.call(this, {objectMode: true}); this._start(parallel, list, iterator, callback); } asynckit-0.4.0/lib/readable_serial.js000066400000000000000000000012171272773076200175600ustar00rootroot00000000000000var serial = require('../serial.js'); // API module.exports = ReadableSerial; /** * Streaming wrapper to `asynckit.serial` * * @param {array|object} list - array or object (named list) to iterate over * @param {function} iterator - iterator to run * @param {function} callback - invoked when all elements processed * @returns {stream.Readable#} */ function ReadableSerial(list, iterator, callback) { if (!(this instanceof ReadableSerial)) { return new ReadableSerial(list, iterator, callback); } // turn on object mode ReadableSerial.super_.call(this, {objectMode: true}); this._start(serial, list, iterator, callback); } asynckit-0.4.0/lib/readable_serial_ordered.js000066400000000000000000000016551272773076200212720ustar00rootroot00000000000000var serialOrdered = require('../serialOrdered.js'); // API module.exports = ReadableSerialOrdered; // expose sort helpers module.exports.ascending = serialOrdered.ascending; module.exports.descending = serialOrdered.descending; /** * Streaming wrapper to `asynckit.serialOrdered` * * @param {array|object} list - array or object (named list) to iterate over * @param {function} iterator - iterator to run * @param {function} sortMethod - custom sort function * @param {function} callback - invoked when all elements processed * @returns {stream.Readable#} */ function ReadableSerialOrdered(list, iterator, sortMethod, callback) { if (!(this instanceof ReadableSerialOrdered)) { return new ReadableSerialOrdered(list, iterator, sortMethod, callback); } // turn on object mode ReadableSerialOrdered.super_.call(this, {objectMode: true}); this._start(serialOrdered, list, iterator, sortMethod, callback); } asynckit-0.4.0/lib/state.js000066400000000000000000000016551272773076200156100ustar00rootroot00000000000000// API module.exports = state; /** * Creates initial state object * for iteration over list * * @param {array|object} list - list to iterate over * @param {function|null} sortMethod - function to use for keys sort, * or `null` to keep them as is * @returns {object} - initial state object */ function state(list, sortMethod) { var isNamedList = !Array.isArray(list) , initState = { index : 0, keyedList: isNamedList || sortMethod ? Object.keys(list) : null, jobs : {}, results : isNamedList ? {} : [], size : isNamedList ? Object.keys(list).length : list.length } ; if (sortMethod) { // sort array keys based on it's values // sort object's keys just on own merit initState.keyedList.sort(isNamedList ? sortMethod : function(a, b) { return sortMethod(list[a], list[b]); }); } return initState; } asynckit-0.4.0/lib/streamify.js000066400000000000000000000056241272773076200164730ustar00rootroot00000000000000var async = require('./async.js'); // API module.exports = { iterator: wrapIterator, callback: wrapCallback }; /** * Wraps iterators with long signature * * @this ReadableAsyncKit# * @param {function} iterator - function to wrap * @returns {function} - wrapped function */ function wrapIterator(iterator) { var stream = this; return function(item, key, cb) { var aborter , wrappedCb = async(wrapIteratorCallback.call(stream, cb, key)) ; stream.jobs[key] = wrappedCb; // it's either shortcut (item, cb) if (iterator.length == 2) { aborter = iterator(item, wrappedCb); } // or long format (item, key, cb) else { aborter = iterator(item, key, wrappedCb); } return aborter; }; } /** * Wraps provided callback function * allowing to execute snitch function before * real callback * * @this ReadableAsyncKit# * @param {function} callback - function to wrap * @returns {function} - wrapped function */ function wrapCallback(callback) { var stream = this; var wrapped = function(error, result) { return finisher.call(stream, error, result, callback); }; return wrapped; } /** * Wraps provided iterator callback function * makes sure snitch only called once, * but passes secondary calls to the original callback * * @this ReadableAsyncKit# * @param {function} callback - callback to wrap * @param {number|string} key - iteration key * @returns {function} wrapped callback */ function wrapIteratorCallback(callback, key) { var stream = this; return function(error, output) { // don't repeat yourself if (!(key in stream.jobs)) { callback(error, output); return; } // clean up jobs delete stream.jobs[key]; return streamer.call(stream, error, {key: key, value: output}, callback); }; } /** * Stream wrapper for iterator callback * * @this ReadableAsyncKit# * @param {mixed} error - error response * @param {mixed} output - iterator output * @param {function} callback - callback that expects iterator results */ function streamer(error, output, callback) { if (error && !this.error) { this.error = error; this.pause(); this.emit('error', error); // send back value only, as expected callback(error, output && output.value); return; } // stream stuff this.push(output); // back to original track // send back value only, as expected callback(error, output && output.value); } /** * Stream wrapper for finishing callback * * @this ReadableAsyncKit# * @param {mixed} error - error response * @param {mixed} output - iterator output * @param {function} callback - callback that expects final results */ function finisher(error, output, callback) { // signal end of the stream // only for successfully finished streams if (!error) { this.push(null); } // back to original track callback(error, output); } asynckit-0.4.0/lib/terminator.js000066400000000000000000000010251272773076200166430ustar00rootroot00000000000000var abort = require('./abort.js') , async = require('./async.js') ; // API module.exports = terminator; /** * Terminates jobs in the attached state context * * @this AsyncKitState# * @param {function} callback - final callback to invoke after termination */ function terminator(callback) { if (!Object.keys(this.jobs).length) { return; } // fast forward iteration index this.index = this.size; // abort jobs abort(this); // send back results we have so far async(callback)(null, this.results); } asynckit-0.4.0/package.json000066400000000000000000000031131272773076200156410ustar00rootroot00000000000000{ "name": "asynckit", "version": "0.4.0", "description": "Minimal async jobs utility library, with streams support", "main": "index.js", "scripts": { "clean": "rimraf coverage", "lint": "eslint *.js lib/*.js test/*.js", "test": "istanbul cover --reporter=json tape -- 'test/test-*.js' | tap-spec", "win-test": "tape test/test-*.js", "browser": "browserify -t browserify-istanbul test/lib/browserify_adjustment.js test/test-*.js | obake --coverage | tap-spec", "report": "istanbul report", "size": "browserify index.js | size-table asynckit", "debug": "tape test/test-*.js" }, "pre-commit": [ "clean", "lint", "test", "browser", "report", "size" ], "repository": { "type": "git", "url": "git+https://github.com/alexindigo/asynckit.git" }, "keywords": [ "async", "jobs", "parallel", "serial", "iterator", "array", "object", "stream", "destroy", "terminate", "abort" ], "author": "Alex Indigo ", "license": "MIT", "bugs": { "url": "https://github.com/alexindigo/asynckit/issues" }, "homepage": "https://github.com/alexindigo/asynckit#readme", "devDependencies": { "browserify": "^13.0.0", "browserify-istanbul": "^2.0.0", "coveralls": "^2.11.9", "eslint": "^2.9.0", "istanbul": "^0.4.3", "obake": "^0.1.2", "phantomjs-prebuilt": "^2.1.7", "pre-commit": "^1.1.3", "reamde": "^1.1.0", "rimraf": "^2.5.2", "size-table": "^0.2.0", "tap-spec": "^4.1.1", "tape": "^4.5.1" }, "dependencies": {} } asynckit-0.4.0/parallel.js000066400000000000000000000017711272773076200155150ustar00rootroot00000000000000var iterate = require('./lib/iterate.js') , initState = require('./lib/state.js') , terminator = require('./lib/terminator.js') ; // Public API module.exports = parallel; /** * Runs iterator over provided array elements in parallel * * @param {array|object} list - array or object (named list) to iterate over * @param {function} iterator - iterator to run * @param {function} callback - invoked when all elements processed * @returns {function} - jobs terminator */ function parallel(list, iterator, callback) { var state = initState(list); while (state.index < (state['keyedList'] || list).length) { iterate(list, iterator, state, function(error, result) { if (error) { callback(error, result); return; } // looks like it's the last one if (Object.keys(state.jobs).length === 0) { callback(null, state.results); return; } }); state.index++; } return terminator.bind(state, callback); } asynckit-0.4.0/serial.js000066400000000000000000000007651272773076200152020ustar00rootroot00000000000000var serialOrdered = require('./serialOrdered.js'); // Public API module.exports = serial; /** * Runs iterator over provided array elements in series * * @param {array|object} list - array or object (named list) to iterate over * @param {function} iterator - iterator to run * @param {function} callback - invoked when all elements processed * @returns {function} - jobs terminator */ function serial(list, iterator, callback) { return serialOrdered(list, iterator, null, callback); } asynckit-0.4.0/serialOrdered.js000066400000000000000000000033271272773076200165040ustar00rootroot00000000000000var iterate = require('./lib/iterate.js') , initState = require('./lib/state.js') , terminator = require('./lib/terminator.js') ; // Public API module.exports = serialOrdered; // sorting helpers module.exports.ascending = ascending; module.exports.descending = descending; /** * Runs iterator over provided sorted array elements in series * * @param {array|object} list - array or object (named list) to iterate over * @param {function} iterator - iterator to run * @param {function} sortMethod - custom sort function * @param {function} callback - invoked when all elements processed * @returns {function} - jobs terminator */ function serialOrdered(list, iterator, sortMethod, callback) { var state = initState(list, sortMethod); iterate(list, iterator, state, function iteratorHandler(error, result) { if (error) { callback(error, result); return; } state.index++; // are we there yet? if (state.index < (state['keyedList'] || list).length) { iterate(list, iterator, state, iteratorHandler); return; } // done here callback(null, state.results); }); return terminator.bind(state, callback); } /* * -- Sort methods */ /** * sort helper to sort array elements in ascending order * * @param {mixed} a - an item to compare * @param {mixed} b - an item to compare * @returns {number} - comparison result */ function ascending(a, b) { return a < b ? -1 : a > b ? 1 : 0; } /** * sort helper to sort array elements in descending order * * @param {mixed} a - an item to compare * @param {mixed} b - an item to compare * @returns {number} - comparison result */ function descending(a, b) { return -1 * ascending(a, b); } asynckit-0.4.0/stream.js000066400000000000000000000012771272773076200152150ustar00rootroot00000000000000var inherits = require('util').inherits , Readable = require('stream').Readable , ReadableAsyncKit = require('./lib/readable_asynckit.js') , ReadableParallel = require('./lib/readable_parallel.js') , ReadableSerial = require('./lib/readable_serial.js') , ReadableSerialOrdered = require('./lib/readable_serial_ordered.js') ; // API module.exports = { parallel : ReadableParallel, serial : ReadableSerial, serialOrdered : ReadableSerialOrdered, }; inherits(ReadableAsyncKit, Readable); inherits(ReadableParallel, ReadableAsyncKit); inherits(ReadableSerial, ReadableAsyncKit); inherits(ReadableSerialOrdered, ReadableAsyncKit); asynckit-0.4.0/test/000077500000000000000000000000001272773076200143345ustar00rootroot00000000000000asynckit-0.4.0/test/lib/000077500000000000000000000000001272773076200151025ustar00rootroot00000000000000asynckit-0.4.0/test/lib/browserify_adjustment.js000066400000000000000000000010161272773076200220670ustar00rootroot00000000000000var tape = require('tape') , _end = tape.Test.prototype._end , run = tape.Test.prototype.run , nextTick = process.nextTick ; // hook into tape to clean up stuff we don't need in the browser tape.Test.prototype.run = function() { // don't break streams if (!this.name.match(/^stream: /)) { process.nextTick = undefined; } return run.apply(this, arguments); }; // restore things tape.Test.prototype._end = function() { process.nextTick = nextTick; return _end.apply(this, arguments); }; asynckit-0.4.0/test/lib/stream_assert.js000066400000000000000000000045531272773076200203230ustar00rootroot00000000000000var util = require('util') , Writable = require('stream').Writable ; // API module.exports = { success: streamSuccessfulAssert, failure: streamFailedAssert }; util.inherits(Receiver, Writable); /** * Tests provided successful readable stream * * @param {object} test - test suite object * @param {stream.Readable#} stream - readable stream to test * @param {mixed} fixture - fixture to check against */ function streamSuccessfulAssert(test, stream, fixture) { var receiver = new Receiver(test, fixture); stream.pipe(receiver); } /** * Tests provided failed readable stream * * @param {object} test - test suite object * @param {stream.Readable#} stream - readable stream to test * @param {mixed} fixture - fixture to check against */ function streamFailedAssert(test, stream, fixture) { var receiver = new Receiver(test, fixture); stream.pipe(receiver); stream.on('error', function(error) { if (fixture.error) { test.deepEqual(error, fixture.error, '+ expect error object to match ' + JSON.stringify(fixture.error)); } else { test.fail(error); } }); } /** * Receiver stream constructor * * @param {object} test - test suite object * @param {mixed} fixture - fixture to check against */ function Receiver(test, fixture) { this.test = test; this.fixture = fixture; // turn on object mode Writable.call(this, {objectMode: true}); this.on('finish', function() { if (fixture.error) { test.fail('+ do not expect stream to have normal ending'); } else { test.ok(true, '+ stream finished successfully.'); } }); } /** * Receiver for the written data (underlying writeable stream interface) * * @private * @param {mixed} element - bundle metadata * @param {string} encoding - encoding type for a chunk, * ignored since expecting only objects * @param {function} next - callback function */ Receiver.prototype._write = function(element, encoding, next) { if (typeof this.fixture.compare == 'function') { this.fixture.compare(element.key, element.value); } else { this.test.equal(this.fixture.elements[element.key], element.value, '+ expect element (' + element.value + ') to equal fixture.elements[' + element.key + '] (' + this.fixture.elements[element.key] + ') of ' + JSON.stringify(this.fixture.elements)); } next(); }; asynckit-0.4.0/test/readme.js000066400000000000000000000015341272773076200161320ustar00rootroot00000000000000// TODO: Need to fix reamde first // make it parse markdown properly var fs = require('fs') , path = require('path') , test = require('tape') , reamde = require('reamde') , asynckit = require('../') , parallel = require('../parallel.js') , content = fs.readFileSync(path.join(__dirname, '../README.md'), 'utf-8') , examples ; examples = reamde(content, { runtime: [ 'callback' ], replace: { 'require(\'asynckit\')' : asynckit, 'require(\'asynckit/parallel\')' : parallel, 'assert.deepEqual(' : 'callback(' } }); // Run tests test('readme', function(t) { t.plan(5); examples.forEach(function(ex) { ex(function(actual, expected) { t.deepEqual(actual, expected, 'expecting readme examples to match: ' + actual + ' vs ' + expected + '.'); }); }); }); asynckit-0.4.0/test/test-parallel-array.js000066400000000000000000000173171272773076200205700ustar00rootroot00000000000000/* eslint no-sparse-arrays: "off" */ var test = require('tape').test , parallel = require('../').parallel , defer = require('../lib/defer.js') ; test('parallel: iterates over array', function(t) { var source = [ 1, 2, 3, 4, 3, 2, 1 ] , expected = [ 'A', 'B', 'C', 'D', 'C', 'B', 'A' ] , start = +new Date() ; t.plan(expected.length + 3); parallel(source, function(item, cb) { t.ok(source.indexOf(item) != -1, 'expect item (' + item + ') to exist in the subject array'); setTimeout(cb.bind(null, null, String.fromCharCode(64 + item)), 10 * item); }, function(err, result) { var diff = +new Date() - start; t.ok(diff < 160, 'expect response time (' + diff + 'ms) to be less than sum of delays'); t.error(err, 'expect no errors'); t.deepEqual(result, expected, 'expect result to be an ordered letters array'); }); }); test('parallel: handles sync array iterator asynchronously', function(t) { var source = [ 1, 2, 3, 4, 3, 2, 1 ] , expected = [ 'A', 'B', 'C', 'D', 'C', 'B', 'A' ] , isAsync = false ; t.plan(expected.length + 3); defer(function(){ isAsync = true; }); parallel(source, function(item, cb) { t.ok(source.indexOf(item) != -1, 'expect item (' + item + ') to exist in the subject array'); cb(null, String.fromCharCode(64 + item)); }, function(err, result) { t.ok(isAsync, 'expect async response'); t.error(err, 'expect no errors'); t.deepEqual(result, expected, 'expect result to be an ordered letters array'); }); }); test('parallel: array: longest finishes last', function(t) { var source = [ 1, 1, 4, 16, 64, 32, 8, 2 ] , expected = [ 1, 1, 2, 4, 8, 16, 32, 64 ] , target = [] ; t.plan(expected.length + 3); // supports full value, key, callback (shortcut) interface parallel(source, function(item, key, cb) { setTimeout(function() { // just "hardcode" first element var sum = target.reduce(function(acc, num){ return acc + num; }, 0) || 1; t.equal(sum, item, 'expect sum (' + sum + ') to be equal current number (' + item + ')'); target.push(item); cb(null, item); }, 25 * item); }, function(err, result) { t.error(err, 'expect no errors'); t.deepEqual(result, source, 'expect result to be same as source array'); t.deepEqual(target, expected, 'expect target to contain ordered numbers'); }); }); test('parallel: array: terminates early', function(t) { var source = [ 1, 1, 4, 16, 66, 34, 8, 2 ] , salvaged = [ 1, 1, 4, , , , 8, 2 ] , expected = [ 1, 1, 2, 4, 8 ] , target = [] ; t.plan(expected.length + 3 + 1); parallel(source, function(item, cb) { var id = setTimeout(function() { // just "hardcode" first element var sum = target.reduce(function(acc, num){ return acc + num; }, 0) || 1; t.equal(sum, item, 'expect sum (' + sum + ') to be equal current number (' + item + ')'); if (item < 10) { target.push(item); cb(null, item); } // return error on big numbers else { cb({item: item}); } }, 25 * item); return clearTimeout.bind(null, id); }, function(err, result) { t.equal(err.item, 16, 'expect to error out on 16'); t.deepEqual(result, salvaged, 'expect result to contain salvaged parts of the source array'); t.deepEqual(target, expected, 'expect target to contain passed numbers'); }); }); test('parallel: array: terminated early from outside', function(t) { var source = [ 1, 1, 4, 16, 66, 34, 8, 2 ] , salvaged = [ 1, 1, 4, , , , 8, 2 ] , expected = [ 1, 1, 2, 4, 8 ] , target = [] , limitNum = 15 , terminator ; t.plan(expected.length * 2 + 3); setTimeout(function() { terminator(); }, 25 * limitNum); terminator = parallel(source, function(item, cb) { var id = setTimeout(function() { // just "hardcode" first element var sum = target.reduce(function(acc, num){ return acc + num; }, 0) || 1; t.ok(item < limitNum, 'expect to only process numbers (' + item + ') less than ' + limitNum); t.equal(sum, item, 'expect sum (' + sum + ') to be equal current number (' + item + ')'); target.push(item); cb(null, item); }, 25 * item); return clearTimeout.bind(null, id); }, function(err, result) { t.error(err, 'expect no error response'); t.deepEqual(result, salvaged, 'expect result to contain salvaged parts of the source array'); t.deepEqual(target, expected, 'expect target to contain passed numbers'); }); }); test('parallel: array: terminated prematurely from outside', function(t) { var source = [ 1, 1, 4, 16, 66, 34, 8, 2 ] , salvaged = [ ] , terminator ; t.plan(2); terminator = parallel(source, function(item, cb) { var id = setTimeout(function() { t.fail('do not expect it to come that far'); cb(null, item); }, 25 * item); return clearTimeout.bind(null, id); }, function(err, result) { t.error(err, 'expect no error response'); t.deepEqual(result, salvaged, 'expect result to contain salvaged parts of the source array'); }); terminator(); }); test('parallel: array: terminated too late from outside', function(t) { var source = [ 1, 1, 4, 16, 64, 32, 8, 2 ] , salvaged = [ 1, 1, 4, 16, 64, 32, 8, 2 ] , expected = [ 1, 1, 2, 4, 8, 16, 32, 64 ] , target = [] , terminator ; t.plan(expected.length + 4); terminator = parallel(source, function(item, cb) { var id = setTimeout(function() { // just "hardcode" first element var sum = target.reduce(function(acc, num){ return acc + num; }, 0) || 1; t.equal(sum, item, 'expect sum (' + sum + ') to be equal current number (' + item + ')'); target.push(item); cb(null, item); }, 25 * item); return clearTimeout.bind(null, id); }, function(err, result) { t.error(err, 'expect no error response'); t.deepEqual(result, salvaged, 'expect result to contain salvaged parts of the source array'); t.deepEqual(target, expected, 'expect target to contain passed numbers'); }); setTimeout(function() { terminator(); t.ok(true, 'expect terminator function to be invoked'); }, 25 * 64); }); test('parallel: array: handles non terminable iterations', function(t) { var source = [ 1, 1, 4, 16, 65, 33, 8, 2 ] , expected = [ 1, 1, 2, 4, 8 ] , target = [] , previous = 0 ; t.plan(expected.length + 2); parallel(source, function(item, cb) { var id = setTimeout(function() { if (item < 10) { // expect it to be invoked in order t.ok(item >= previous, 'expect item (' + item + ') to be equal or greater than previous item (' + previous + ')'); previous = item; target.push(item); cb(null, item); } // return error on big numbers else { cb({item: item}); } }, 25 * item); return (item % 2) ? null : clearTimeout.bind(null, id); }, function(err) { t.equal(err.item, 16, 'expect to error out on 16'); t.deepEqual(target, expected, 'expect target to contain passed numbers'); }); }); test('parallel: array: handles unclean callbacks', function(t) { var source = [ 1, 2, 3, 4, 3, 2, 1 ] , expected = [ 2, 4, 6, 8, 6, 4, 2 ] ; t.plan(expected.length + 2); parallel(source, function(item, cb) { setTimeout(function() { t.ok(source.indexOf(item) != -1, 'expect item (' + item + ') to exist in the source array'); cb(null, item * 2); cb(null, item * -2); }, 50 * item); }, function(err, result) { t.error(err, 'expect no errors'); t.deepEqual(result, expected, 'expect result to be an multiplied by two array'); }); }); asynckit-0.4.0/test/test-parallel-object.js000066400000000000000000000225441272773076200207160ustar00rootroot00000000000000var test = require('tape').test , parallel = require('../parallel.js') , defer = require('../lib/defer.js') ; test('parallel: iterates over object', function(t) { var source = { first: 1, second: 2, third: 3, fourth: 4, three: 3, two: 2, one: 1 } , keys = Object.keys(source) , expected = { first: 'A', second: 'B', third: 'C', fourth: 'D', three: 'C', two: 'B', one: 'A' } , start = +new Date() ; t.plan(keys.length * 2 + 3); // supports full value, key, callback (shortcut) interface parallel(source, function(item, key, cb) { t.ok(keys.indexOf(key) != -1, 'expect key (' + key + ') to exist in the keys array'); t.equal(item, source[key], 'expect item (' + item + ') to match in same key (' + key + ') element in the source object'); setTimeout(cb.bind(null, null, String.fromCharCode(64 + item)), 10 * item); }, function(err, result) { var diff = +new Date() - start; t.ok(diff < 160, 'expect response time (' + diff + 'ms) to be less than sum of delays'); t.error(err, 'expect no errors'); t.deepEqual(result, expected, 'expect result to be an ordered letters object'); }); }); test('parallel: handles sync object iterator asynchronously', function(t) { var source = { first: 1, second: 2, third: 3, fourth: 4, three: 3, two: 2, one: 1 } , keys = Object.keys(source) , expected = { first: 'A', second: 'B', third: 'C', fourth: 'D', three: 'C', two: 'B', one: 'A' } , isAsync = false ; t.plan(keys.length * 2 + 3); defer(function(){ isAsync = true; }); // supports full value, key, callback (shortcut) interface parallel(source, function(item, key, cb) { t.ok(keys.indexOf(key) != -1, 'expect key (' + key + ') to exist in the keys array'); t.equal(item, source[key], 'expect item (' + item + ') to match in same key (' + key + ') element in the source object'); cb(null, String.fromCharCode(64 + item)); }, function(err, result) { t.ok(isAsync, 'expect async response'); t.error(err, 'expect no errors'); t.deepEqual(result, expected, 'expect result to be an ordered letters object'); }); }); test('parallel: object: longest finishes last', function(t) { var source = { first: 1, one: 1, four: 4, sixteen: 16, sixtyFour: 64, thirtyTwo: 32, eight: 8, two: 2 } , keys = Object.keys(source) , expected = [ 1, 1, 2, 4, 8, 16, 32, 64 ] , target = [] ; t.plan(keys.length + 3); // supports just value, callback (shortcut) interface parallel(source, function(item, cb) { setTimeout(function() { // just "hardcode" first element var sum = target.reduce(function(acc, num){ return acc + num; }, 0) || 1; t.equal(sum, item, 'expect sum (' + sum + ') to be equal current number (' + item + ')'); target.push(item); cb(null, item); }, 10 * item); }, function(err, result) { t.error(err, 'expect no errors'); t.deepEqual(result, source, 'expect result to be same as source object'); t.deepEqual(target, expected, 'expect target to contain ordered numbers'); }); }); test('parallel: object: terminates early', function(t) { var source = { first: 1, one: 1, four: 4, sixteen: 16, sixtyFour: 64, thirtyTwo: 32, eight: 8, two: 2 } , salvaged = { first: 1, one: 1, two: 2, four: 4, eight: 8} , expected = [ 1, 1, 2, 4, 8 ] , target = [] ; t.plan(Object.keys(salvaged).length + 4 + 1); // supports full value, key, callback (shortcut) interface parallel(source, function(item, key, cb) { var id = setTimeout(function() { // just "hardcode" first element var sum = target.reduce(function(acc, num){ return acc + num; }, 0) || 1; t.equal(sum, item, 'expect sum (' + sum + ') to be equal current item (' + key + ':' + item + ')'); if (item < 10) { target.push(item); cb(null, item); } // return error on big numbers else { cb({key: key, item: item}); } }, 10 * item); return clearTimeout.bind(null, id); }, function(err, result) { t.equal(err.key, 'sixteen', 'expect to error out on key `sixteen`'); t.equal(err.item, 16, 'expect to error out on value `16`'); t.deepEqual(result, salvaged, 'expect result to contain salvaged parts of the source object'); t.deepEqual(target, expected, 'expect target to contain passed numbers'); }); }); test('parallel: object: terminated early from outside', function(t) { var source = { first: 1, one: 1, four: 4, sixteen: 16, sixtyFour: 64, thirtyTwo: 32, eight: 8, two: 2 } , salvaged = { first: 1, one: 1, two: 2, four: 4, eight: 8} , expected = [ 1, 1, 2, 4, 8 ] , target = [] , limitNum = 15 , terminator ; t.plan(expected.length * 2 + 3); setTimeout(function() { terminator(); }, 25 * limitNum); terminator = parallel(source, function(item, key, cb) { var id = setTimeout(function() { // just "hardcode" first element var sum = target.reduce(function(acc, num){ return acc + num; }, 0) || 1; t.ok(item < limitNum, 'expect to only process numbers (' + item + ') less than ' + limitNum); t.equal(sum, item, 'expect sum (' + sum + ') to be equal current item (' + key + ':' + item + ')'); target.push(item); cb(null, item); }, 25 * item); return clearTimeout.bind(null, id); }, function(err, result) { t.error(err, 'expect no error response'); t.deepEqual(result, salvaged, 'expect result to contain salvaged parts of the source object'); t.deepEqual(target, expected, 'expect target to contain passed numbers'); }); }); test('parallel: object: terminated prematurely from outside', function(t) { var source = { first: 1, one: 1, four: 4, sixteen: 16, sixtyFour: 64, thirtyTwo: 32, eight: 8, two: 2 } , salvaged = { } , terminator ; t.plan(2); terminator = parallel(source, function(item, cb) { var id = setTimeout(function() { t.fail('do not expect it to come that far'); cb(null, item); }, 25 * item); return clearTimeout.bind(null, id); }, function(err, result) { t.error(err, 'expect no error response'); t.deepEqual(result, salvaged, 'expect result to contain salvaged parts of the source object'); }); terminator(); }); test('parallel: object: terminated too late from outside', function(t) { var source = { first: 1, one: 1, four: 4, sixteen: 16, sixtyFour: 64, thirtyTwo: 32, eight: 8, two: 2 } , salvaged = { first: 1, one: 1, four: 4, sixteen: 16, sixtyFour: 64, thirtyTwo: 32, eight: 8, two: 2 } , expected = [ 1, 1, 2, 4, 8, 16, 32, 64 ] , target = [] , terminator ; t.plan(expected.length + 4); terminator = parallel(source, function(item, cb) { var id = setTimeout(function() { // just "hardcode" first element var sum = target.reduce(function(acc, num){ return acc + num; }, 0) || 1; t.equal(sum, item, 'expect sum (' + sum + ') to be equal current number (' + item + ')'); target.push(item); cb(null, item); }, 25 * item); return clearTimeout.bind(null, id); }, function(err, result) { t.error(err, 'expect no error response'); t.deepEqual(result, salvaged, 'expect result to contain salvaged parts of the source object'); t.deepEqual(target, expected, 'expect target to contain passed numbers'); }); setTimeout(function() { terminator(); t.ok(true, 'expect terminator function to be invoked'); }, 25 * 64); }); test('parallel: object: handles non terminable iterations', function(t) { var source = { first: 1, one: 1, four: 4, sixteen: 16, sixtyFour: 64, thirtyTwo: 32, eight: 8, two: 2 } , expected = [ 1, 1, 2, 4, 8 ] , target = [] , previous = 0 ; t.plan(expected.length + 2); // supports just value, callback (shortcut) interface parallel(source, function(item, cb) { var id = setTimeout(function() { if (item < 10) { // expect it to be invoked in order t.ok(item >= previous, 'expect item (' + item + ') to be equal or greater than previous item (' + previous + ')'); previous = item; target.push(item); cb(null, item); } // return error on big numbers else { cb({item: item}); } }, 10 * item); return (item % 2) ? null : clearTimeout.bind(null, id); }, function(err) { t.equal(err.item, 16, 'expect to error out on 16'); t.deepEqual(target, expected, 'expect target to contain passed numbers'); }); }); test('parallel: object: handles unclean callbacks', function(t) { var source = { first: 1, second: 2, third: 3, fourth: 4, three: 3, two: 2, one: 1 } , keys = Object.keys(source) , expected = { first: 2, second: 4, third: 6, fourth: 8, three: 6, two: 4, one: 2 } ; t.plan(keys.length * 2 + 2); // supports full value, key, callback (shortcut) interface parallel(source, function(item, key, cb) { setTimeout(function() { t.ok(keys.indexOf(key) != -1, 'expect key (' + key + ') to exist in the keys array'); t.equal(item, source[key], 'expect item (' + item + ') to match in same key (' + key + ') element in the source object'); cb(null, item * 2); cb(null, item * -2); }, 10 * item); }, function(err, result) { t.error(err, 'expect no errors'); t.deepEqual(result, expected, 'expect result to be an multiplied by two object values'); }); }); asynckit-0.4.0/test/test-serial-array.js000066400000000000000000000157661272773076200202610ustar00rootroot00000000000000var test = require('tape').test , serial = require('../').serial , defer = require('../lib/defer.js') ; test('serial: iterates over array', function(t) { var source = [ 1, 2, 3, 4, 3, 2, 1 ] , itemsSum = 16 , expected = [ 'A', 'B', 'C', 'D', 'C', 'B', 'A' ] , start = +new Date() ; t.plan(expected.length + 3); serial(source, function(item, cb) { t.ok(source.indexOf(item) != -1, 'expect item (' + item + ') to exist in the subject array (' + source + ')'); setTimeout(cb.bind(null, null, String.fromCharCode(64 + item)), 10 * item); }, function(err, result) { var diff = +new Date() - start; t.ok(diff > (itemsSum * 10), 'expect response time (' + diff + 'ms) to be more than ' + (itemsSum * 10) + ' ms'); t.error(err, 'expect no errors'); t.deepEqual(result, expected, 'expect result to be an ordered letters array'); }); }); test('serial: handles sync array iterator asynchronously', function(t) { var source = [ 1, 2, 3, 4, 3, 2, 1 ] , expected = [ 'A', 'B', 'C', 'D', 'C', 'B', 'A' ] , isAsync = false ; t.plan(expected.length + 3); defer(function(){ isAsync = true; }); serial(source, function(item, cb) { t.ok(source.indexOf(item) != -1, 'expect item (' + item + ') to exist in the subject array'); cb(null, String.fromCharCode(64 + item)); }, function(err, result) { t.ok(isAsync, 'expect async response'); t.error(err, 'expect no errors'); t.deepEqual(result, expected, 'expect result to be an ordered letters array'); }); }); test('serial: array: longest finishes in order', function(t) { var source = [ 1, 1, 4, 16, 64, 32, 8, 2 ] , notExpected = [ 1, 1, 2, 4, 8, 16, 32, 64 ] , target = [] ; t.plan(4); // supports full value, key, callback (shortcut) interface serial(source, function(item, key, cb) { setTimeout(function() { target.push(item); cb(null, item); }, 5 * item); }, function(err, result) { t.error(err, 'expect no errors'); t.deepEqual(result, source, 'expect result to be same as source array'); t.deepEqual(target, source, 'expect target to be same as source array'); t.notDeepEqual(target, notExpected, 'do not expect target to contain ordered numbers'); }); }); test('serial: array: terminates early', function(t) { var source = [ 1, 1, 4, 16, 66, 34, 8, 2 ] , expected = [ 1, 1, 4 ] , target = [] ; t.plan(expected.length + 3 + 1); serial(source, function(item, cb) { var id = setTimeout(function() { t.ok(item < 5 || item == 16, 'expect only certain numbers being processed'); if (item < 10) { target.push(item); cb(null, item); } // return error on big numbers else { cb({item: item}); } }, 5 * item); return clearTimeout.bind(null, id); }, function(err, result) { t.equal(err.item, 16, 'expect to error out on 16'); t.deepEqual(result, expected, 'expect result to contain processed parts that less than 10 of the source array'); t.deepEqual(target, expected, 'expect target to contain passed numbers'); }); }); test('serial: array: terminated early from outside', function(t) { var source = [ 1, 1, 4, 16, 66, 34, 8, 2 ] , expected = [ 1, 1, 4 ] , target = [] , limitNum = 7 , terminator ; t.plan(expected.length + 3); setTimeout(function() { terminator(); }, 5 * (limitNum + expected.reduce(function(a, b){ return a + b; }))); terminator = serial(source, function(item, cb) { var id = setTimeout(function() { t.ok(item < limitNum, 'expect only numbers (' + item + ') less than (' + limitNum + ') being processed'); target.push(item); cb(null, item); }, 5 * item); return clearTimeout.bind(null, id); }, function(err, result) { t.error(err, 'expect no error response'); t.deepEqual(result, expected, 'expect result to contain processed parts that less than ' + limitNum + ' of the source array'); t.deepEqual(target, expected, 'expect target to contain passed numbers'); }); }); test('serial: array: terminated prematurely from outside', function(t) { var source = [ 1, 1, 4, 16, 66, 34, 8, 2 ] , expected = [ ] , terminator ; t.plan(2); terminator = serial(source, function(item, cb) { var id = setTimeout(function() { t.fail('do not expect it to come that far'); cb(null, item); }, 5 * item); return clearTimeout.bind(null, id); }, function(err, result) { t.error(err, 'expect no error response'); t.deepEqual(result, expected, 'expect result to contain salvaged parts of the source array'); }); terminator(); }); test('serial: array: terminated too late from outside', function(t) { var source = [ 1, 1, 4, 16, 64, 32, 8, 2 ] , expected = [ 1, 1, 4, 16, 64, 32, 8, 2 ] , target = [] , terminator ; t.plan(expected.length + 3); terminator = serial(source, function(item, cb) { var id = setTimeout(function() { t.ok(source.indexOf(item) != -1, 'expect item (' + item + ') to exist in the subject array'); target.push(item); cb(null, item); }, 5 * item); return clearTimeout.bind(null, id); }, function(err, result) { // terminate it after it's done terminator(); t.error(err, 'expect no error response'); t.deepEqual(result, expected, 'expect result to contain salvaged parts of the source array'); t.deepEqual(target, expected, 'expect target to contain passed numbers'); }); }); test('serial: array: handles non terminable iterations', function(t) { var source = [ 1, 1, 4, 16, 65, 33, 8, 2 ] , expected = [ 1, 1, 4 ] , target = [] , previous = 0 ; t.plan(expected.length + 2 + 1); serial(source, function(item, cb) { var id = setTimeout(function() { // expect it to be invoked in order t.ok(item >= previous, 'expect item (' + item + ') to be equal or greater than previous item (' + previous + ')'); previous = item; if (item < 10) { target.push(item); cb(null, item); } // return error on big numbers else { cb({item: item}); } }, 5 * item); return (item % 2) ? null : clearTimeout.bind(null, id); }, function(err) { t.equal(err.item, 16, 'expect to error out on 16'); t.deepEqual(target, expected, 'expect target to contain passed numbers'); }); }); test('serial: array: handles unclean callbacks', function(t) { var source = [ 1, 2, 3, 4, 3, 2, 1 ] , expected = [ 2, 4, 6, 8, 6, 4, 2 ] ; t.plan(expected.length + 2); serial(source, function(item, cb) { setTimeout(function() { t.ok(source.indexOf(item) != -1, 'expect item (' + item + ') to exist in the source array'); cb(null, item * 2); cb(null, item * -2); }, 5 * item); }, function(err, result) { t.error(err, 'expect no errors'); t.deepEqual(result, expected, 'expect result to be an multiplied by two array'); }); }); asynckit-0.4.0/test/test-serial-object.js000066400000000000000000000216761272773076200204060ustar00rootroot00000000000000var test = require('tape').test , serial = require('../serial.js') , defer = require('../lib/defer.js') ; test('serial: iterates over object', function(t) { var source = { first: 1, second: 2, third: 3, fourth: 4, three: 3, two: 2, one: 1 } , itemsSum = 16 , keys = Object.keys(source) , expected = { first: 'A', second: 'B', third: 'C', fourth: 'D', three: 'C', two: 'B', one: 'A' } , start = +new Date() ; t.plan(keys.length * 2 + 3); // supports full value, key, callback interface serial(source, function(item, key, cb) { t.ok(keys.indexOf(key) != -1, 'expect key (' + key + ') to exist in the keys array'); t.equal(item, source[key], 'expect item (' + item + ') to match in same key (' + key + ') element in the source object'); setTimeout(cb.bind(null, null, String.fromCharCode(64 + item)), 10 * item); }, function(err, result) { var diff = +new Date() - start; t.ok(diff > (itemsSum * 10), 'expect response time (' + diff + 'ms) to be more than ' + (itemsSum * 10) + ' ms'); t.error(err, 'expect no errors'); t.deepEqual(result, expected, 'expect result to be an ordered letters object'); }); }); test('serial: handles sync object iterator asynchronously', function(t) { var source = { first: 1, second: 2, third: 3, fourth: 4, three: 3, two: 2, one: 1 } , keys = Object.keys(source) , expected = { first: 'A', second: 'B', third: 'C', fourth: 'D', three: 'C', two: 'B', one: 'A' } , isAsync = false ; t.plan(keys.length * 2 + 3); defer(function(){ isAsync = true; }); // supports full value, key, callback (shortcut) interface serial(source, function(item, key, cb) { t.ok(keys.indexOf(key) != -1, 'expect key (' + key + ') to exist in the keys array'); t.equal(item, source[key], 'expect item (' + item + ') to match in same key (' + key + ') element in the source object'); cb(null, String.fromCharCode(64 + item)); }, function(err, result) { t.ok(isAsync, 'expect async response'); t.error(err, 'expect no errors'); t.deepEqual(result, expected, 'expect result to be an ordered letters object'); }); }); test('serial: object: longest finishes in order', function(t) { var source = { first: 1, one: 1, four: 4, sixteen: 16, sixtyFour: 64, thirtyTwo: 32, eight: 8, two: 2 } , expected = [ 1, 1, 4, 16, 64, 32, 8, 2 ] , target = [] ; t.plan(3); // supports just value, callback (shortcut) interface serial(source, function(item, cb) { setTimeout(function() { target.push(item); cb(null, item); }, 5 * item); }, function(err, result) { t.error(err, 'expect no errors'); t.deepEqual(result, source, 'expect result to be same as source object'); // expect it to be invoked in order // which is not always the case with objects // use `serialOrdered` if order really matters t.deepEqual(target, expected, 'expect target to be same as source object'); }); }); test('serial: object: terminates early', function(t) { var source = { first: 1, one: 1, four: 4, sixteen: 16, sixtyFour: 64, thirtyTwo: 32, eight: 8, two: 2 } , salvaged = { first: 1, one: 1, four: 4 } , expected = [ 1, 1, 4 ] , target = [] ; t.plan(Object.keys(salvaged).length + 4 + 1); // supports full value, key, callback (shortcut) interface serial(source, function(item, key, cb) { var id = setTimeout(function() { // expect it to be invoked in order // which is not always the case with objects // use `serialOrdered` if order really matters t.ok(item < 5 || item == 16, 'expect only certain numbers being processed'); if (item < 10) { target.push(item); cb(null, item); } // return error on big numbers else { cb({key: key, item: item}); } }, 5 * item); return clearTimeout.bind(null, id); }, function(err, result) { t.equal(err.key, 'sixteen', 'expect to error out on key `sixteen`'); t.equal(err.item, 16, 'expect to error out on value `16`'); t.deepEqual(result, salvaged, 'expect result to contain salvaged parts of the source object'); t.deepEqual(target, expected, 'expect target to contain passed numbers'); }); }); test('serial: object: terminated early from outside', function(t) { var source = { first: 1, one: 1, four: 4, sixteen: 16, sixtyFour: 64, thirtyTwo: 32, eight: 8, two: 2 } , salvaged = { first: 1, one: 1, four: 4 } , expected = [ 1, 1, 4 ] , target = [] , limitNum = 7 , terminator ; t.plan(expected.length + 3); setTimeout(function() { terminator(); }, 5 * (limitNum + expected.reduce(function(a, b){ return a + b; }))); terminator = serial(source, function(item, key, cb) { var id = setTimeout(function() { t.ok(item < limitNum, 'expect only numbers (' + key + ':' + item + ') less than (' + limitNum + ') being processed'); target.push(item); cb(null, item); }, 5 * item); return clearTimeout.bind(null, id); }, function(err, result) { t.error(err, 'expect no error response'); t.deepEqual(result, salvaged, 'expect result to contain salvaged parts of the source object'); t.deepEqual(target, expected, 'expect target to contain passed numbers'); }); }); test('serial: object: terminated prematurely from outside', function(t) { var source = { first: 1, one: 1, four: 4, sixteen: 16, sixtyFour: 64, thirtyTwo: 32, eight: 8, two: 2 } , expected = { } , terminator ; t.plan(2); terminator = serial(source, function(item, cb) { var id = setTimeout(function() { t.fail('do not expect it to come that far'); cb(null, item); }, 5 * item); return clearTimeout.bind(null, id); }, function(err, result) { t.error(err, 'expect no error response'); t.deepEqual(result, expected, 'expect result to contain salvaged parts of the source object'); }); terminator(); }); test('serial: object: terminated too late from outside', function(t) { var source = { first: 1, one: 1, four: 4, sixteen: 16, sixtyFour: 64, thirtyTwo: 32, eight: 8, two: 2 } , salvaged = { first: 1, one: 1, four: 4, sixteen: 16, sixtyFour: 64, thirtyTwo: 32, eight: 8, two: 2 } , expected = [ 1, 1, 4, 16, 64, 32, 8, 2 ] , target = [] , terminator ; t.plan(expected.length + 3); terminator = serial(source, function(item, key, cb) { var id = setTimeout(function() { t.equal(source[key], item, 'expect item (' + key + ':' + item + ') to equal ' + source[key]); target.push(item); cb(null, item); }, 5 * item); return clearTimeout.bind(null, id); }, function(err, result) { // terminate it after it's done terminator(); t.error(err, 'expect no error response'); t.deepEqual(result, salvaged, 'expect result to contain salvaged parts of the source oject'); t.deepEqual(target, expected, 'expect target to contain passed numbers'); }); }); test('serial: object: handles non terminable iterations', function(t) { var source = { first: 1, one: 1, four: 4, sixteen: 16, sixtyFour: 64, thirtyTwo: 32, eight: 8, two: 2 } , expected = [ 1, 1, 4 ] , target = [] , previous = 0 ; t.plan(expected.length + 2 + 1); // supports just value, callback (shortcut) interface serial(source, function(item, cb) { var id = setTimeout(function() { // expect it to be invoked in order // which is not always the case with objects // use `serialOrdered` if order really matters t.ok(item >= previous, 'expect item (' + item + ') to be equal or greater than previous item (' + previous + ')'); previous = item; if (item < 10) { target.push(item); cb(null, item); } // return error on big numbers else { cb({item: item}); } }, 5 * item); return (item % 2) ? null : clearTimeout.bind(null, id); }, function(err) { t.equal(err.item, 16, 'expect to error out on 16'); t.deepEqual(target, expected, 'expect target to contain passed numbers'); }); }); test('serial: object: handles unclean callbacks', function(t) { var source = { first: 1, second: 2, third: 3, fourth: 4, three: 3, two: 2, one: 1 } , keys = Object.keys(source) , expected = { first: 2, second: 4, third: 6, fourth: 8, three: 6, two: 4, one: 2 } ; t.plan(keys.length * 2 + 2); // supports full value, key, callback (shortcut) interface serial(source, function(item, key, cb) { setTimeout(function() { t.ok(keys.indexOf(key) != -1, 'expect key (' + key + ') to exist in the keys array'); t.equal(item, source[key], 'expect item (' + item + ') to match in same key (' + key + ') element in the source object'); cb(null, item * 2); cb(null, item * -2); }, 5 * item); }, function(err, result) { t.error(err, 'expect no errors'); t.deepEqual(result, expected, 'expect result to be an multiplied by two object values'); }); }); asynckit-0.4.0/test/test-serialOrdered-array.js000066400000000000000000000213021272773076200215450ustar00rootroot00000000000000/* eslint no-sparse-arrays: "off" */ var test = require('tape').test , serialOrdered = require('../').serialOrdered ; test('serialOrdered: iterates over array with no sortMethod', function(t) { var source = [ 1, 2, 3, 4, 3, 2, 1 ] , expected = [ 'A', 'B', 'C', 'D', 'C', 'B', 'A' ] , index = 0 ; t.plan(expected.length * 2 + 2); serialOrdered(source, function(item, key, cb) { t.equal(index, key, 'expect key (' + key + ') for the iteration to match incremental index (' + index + ')'); t.equal(source[index], item, 'expect item of the iteration to match incremental source element with index position'); setTimeout(cb.bind(null, null, String.fromCharCode(64 + item)), 10 * item); index++; }, null, // force no sortMethod function(err, result) { t.error(err, 'expect no errors'); t.deepEqual(result, expected, 'expect result to be an ordered letters array'); }); }); test('serialOrdered: iterates over array sorted ascending', function(t) { var source = [ 1, 2, 3, 4, 3, 2, 1 ] , expected = [ 'A', 'B', 'C', 'D', 'C', 'B', 'A' ] , prev = Math.min.apply(Math, source) ; t.plan(expected.length * 2 + 2); serialOrdered(source, function(item, key, cb) { t.ok(prev <= item, 'expect item not to decrease on each iteration – ascending sorting'); t.equal(source[key], item, 'expect iteration indices to match original array positions'); cb(null, String.fromCharCode(64 + item)); // this should happen before next invocation of the iterator prev = item; }, serialOrdered.ascending, // sort ascending function(err, result) { t.error(err, 'expect no errors'); t.deepEqual(result, expected, 'expect result to keep order of the original array'); }); }); test('serialOrdered: iterates over array sorted descending', function(t) { var source = [ 1, 2, 3, 4, 3, 2, 1 ] , expected = [ 'A', 'B', 'C', 'D', 'C', 'B', 'A' ] , prev = Math.max.apply(Math, source) ; t.plan(expected.length * 2 + 2); serialOrdered(source, function(item, key, cb) { t.ok(prev >= item, 'expect item not to increase on each iteration – descending sorting'); t.equal(source[key], item, 'expect iteration indices to match original array positions'); setTimeout(cb.bind(null, null, String.fromCharCode(64 + item)), 10 * item); prev = item; }, serialOrdered.descending, // sort descending function(err, result) { t.error(err, 'expect no errors'); t.deepEqual(result, expected, 'expect result to keep order of the original array'); }); }); test('serialOrdered: iterates over array custom sorted', function(t) { var source = [ 1, 2, 3, 4, 3, 2, 1 ] , expected = [ 'A', 'B', 'C', 'D', 'C', 'B', 'A' ] // get smallest even number , prev = Math.min.apply(Math, source.filter(function(n){ return !(n % 2); })) // puts even numbers first , customSort = function(a, b) { var order = a < b ? -1 : a > b ? 1 : 0 , aOdd = a % 2 , bOdd = b % 2 ; return aOdd === bOdd ? order : aOdd ? 1 : -1; } ; t.plan(expected.length * 2 + 2); serialOrdered(source, function(item, key, cb) { var incr = prev <= item , shift = (prev % 2) !== (item % 2) ; t.ok(incr || shift, 'expect item (' + item + ') to increase on each iteration, unless it is switch from even to odd'); t.equal(source[key], item, 'expect iteration indices to match original array positions'); setTimeout(cb.bind(null, null, String.fromCharCode(64 + item)), 10 * item); prev = item; }, customSort, // custom sorting function(err, result) { t.error(err, 'expect no errors'); t.deepEqual(result, expected, 'expect result to keep order of the original array'); }); }); test('serialOrdered: array: terminates early with custom sorting', function(t) { var source = [ 1, 1, 4, 5, 16, 66, 34, 9, 8, 2 ] // even numbers below 10 , expectedResult = [ , , 4, , , , , , 8, 2 ] // ascending even numbers below 10 // and 16 as next even number 16 , expectedTarget = [ 2, 4, 8, 16 ] // puts even numbers first , customSort = function(a, b) { var order = a < b ? -1 : a > b ? 1 : 0 , aOdd = a % 2 , bOdd = b % 2 ; return aOdd === bOdd ? order : aOdd ? 1 : -1; } , target = [] ; t.plan(expectedTarget.length + 3); serialOrdered(source, function(item, cb) { var id = setTimeout(function() { t.ok((item < 10 && item % 2 === 0) || item == 16, 'expect only certain (even) numbers being processed'); target.push(item); if (item < 10) { cb(null, item); } // return error on big numbers else { cb({item: item}); } }, 5 * item); return clearTimeout.bind(null, id); }, customSort, // custom sort function(err, result) { t.equal(err.item, 16, 'expect to error out on 16'); t.deepEqual(result, expectedResult, 'expect result to contain processed parts that less than 10 of the source array'); t.deepEqual(target, expectedTarget, 'expect target to contain passed numbers'); }); }); test('serialOrdered: array: terminated early from outside, with custom sorting', function(t) { var source = [ 1, 1, 4, 5, 16, 66, 34, 9, 8, 2 ] // even numbers below 10 , expectedResult = [ , , 4, , , , , , 8, 2 ] // ascending even numbers below 10 , expectedTarget = [ 2, 4, 8 ] // puts even numbers first , customSort = function(a, b) { var order = a < b ? -1 : a > b ? 1 : 0 , aOdd = a % 2 , bOdd = b % 2 ; return aOdd === bOdd ? order : aOdd ? 1 : -1; } , target = [] , limitNum = 10 , terminator ; t.plan(expectedTarget.length + 3); setTimeout(function() { terminator(); }, 5 * (limitNum + expectedTarget.reduce(function(a, b){ return a + b; }))); terminator = serialOrdered(source, function(item, cb) { var id = setTimeout(function() { t.ok((item < limitNum && item % 2 === 0), 'expect only even numbers (' + item + ') less than (' + limitNum + ') being processed'); target.push(item); cb(null, item); }, 5 * item); return clearTimeout.bind(null, id); }, customSort, // custom sort function(err, result) { t.error(err, 'expect no error response'); t.deepEqual(result, expectedResult, 'expect result to contain processed parts that less than ' + limitNum + ' of the source array'); t.deepEqual(target, expectedTarget, 'expect target to contain passed numbers'); }); }); test('serialOrdered: array: terminated prematurely from outside, with custom sorting', function(t) { var source = [ 1, 1, 4, 5, 16, 66, 34, 9, 8, 2 ] , expected = [ ] // puts even numbers first , customSort = function(a, b) { var order = a < b ? -1 : a > b ? 1 : 0 , aOdd = a % 2 , bOdd = b % 2 ; return aOdd === bOdd ? order : aOdd ? 1 : -1; } , terminator ; t.plan(2); terminator = serialOrdered(source, function(item, cb) { var id = setTimeout(function() { t.fail('do not expect it to come that far'); cb(null, item); }, 5 * item); return clearTimeout.bind(null, id); }, customSort, // custom sort function(err, result) { t.error(err, 'expect no error response'); t.deepEqual(result, expected, 'expect result to contain salvaged parts of the source array'); }); terminator(); }); test('serialOrdered: array: terminated too late from outside, with custom sorting', function(t) { var source = [ 1, 1, 4, 5, 16, 66, 34, 9, 8, 2 ] , expectedResult = [ 1, 1, 4, 5, 16, 66, 34, 9, 8, 2 ] , expectedTarget = [ 2, 4, 8, 16, 34, 66, 1, 1, 5, 9 ] // puts even numbers first , customSort = function(a, b) { var order = a < b ? -1 : a > b ? 1 : 0 , aOdd = a % 2 , bOdd = b % 2 ; return aOdd === bOdd ? order : aOdd ? 1 : -1; } , target = [] , terminator ; t.plan(expectedTarget.length + 3); terminator = serialOrdered(source, function(item, cb) { var id = setTimeout(function() { t.ok(source.indexOf(item) != -1, 'expect item (' + item + ') to exist in the subject array'); target.push(item); cb(null, item); }, 5 * item); return clearTimeout.bind(null, id); }, customSort, // custom sort function(err, result) { // terminate it after it's done terminator(); t.error(err, 'expect no error response'); t.deepEqual(result, expectedResult, 'expect result to contain processed parts of the source array'); t.deepEqual(target, expectedTarget, 'expect target to contain passed numbers'); }); }); asynckit-0.4.0/test/test-serialOrdered-object.js000066400000000000000000000304511272773076200217020ustar00rootroot00000000000000var test = require('tape').test , serialOrdered = require('../serialOrdered.js') ; test('serialOrdered: iterates over object with no sortMethod', function(t) { var source = { first: 1, second: 2, third: 3, fourth: 4, three: 3, two: 2, one: 1 } , keys = Object.keys(source) , expected = { first: 'A', second: 'B', third: 'C', fourth: 'D', three: 'C', two: 'B', one: 'A' } , index = 0 ; t.plan(keys.length * 2 + 2); // supports full value, key, callback interface serialOrdered(source, function(item, key, cb) { t.equal(keys[index], key, 'expect key (' + key + ') for the iteration to match incremental index (' + index + ':' + keys[index] + ')'); t.equal(source[key], item, 'expect item of the iteration to match incremental source element with key position'); setTimeout(cb.bind(null, null, String.fromCharCode(64 + item)), 10 * item); index++; }, null, // force no sortMethod function(err, result) { t.error(err, 'expect no errors'); t.deepEqual(result, expected, 'expect result to be an ordered letters object'); }); }); test('serialOrdered: iterates over object sorted ascending', function(t) { var source = { first: 1, second: 2, third: 3, fourth: 4, three: 3, two: 2, one: 1 } , keys = Object.keys(source) , expected = { first: 'A', second: 'B', third: 'C', fourth: 'D', three: 'C', two: 'B', one: 'A' } , prev = 'a' ; t.plan(keys.length * 2 + 2); serialOrdered(source, function(item, key, cb) { t.ok(prev <= key, 'expect key (' + key + ') not to decrease on each iteration – ascending sorting'); t.equal(source[key], item, 'expect iteration indices to match original object positions'); setTimeout(cb.bind(null, null, String.fromCharCode(64 + item)), 10 * item); prev = key; }, serialOrdered.ascending, // sort ascending function(err, result) { t.error(err, 'expect no errors'); t.deepEqual(result, expected, 'expect result to keep order of the original array'); }); }); test('serialOrdered: iterates over object sorted descending (sync iterator)', function(t) { var source = { first: 1, second: 2, third: 3, fourth: 4, three: 3, two: 2, one: 1 } , keys = Object.keys(source) , expected = { first: 'A', second: 'B', third: 'C', fourth: 'D', three: 'C', two: 'B', one: 'A' } , prev = 'zzz' ; t.plan(keys.length * 2 + 2); serialOrdered(source, function(item, key, cb) { t.ok(prev >= key, 'expect key (' + key + ') not to increase on each iteration – descending sorting'); t.equal(source[key], item, 'expect iteration indices to match original array positions'); cb(null, String.fromCharCode(64 + item)); // this should happen before next invocation of the iterator prev = key; }, serialOrdered.descending, // sort descending function(err, result) { t.error(err, 'expect no errors'); t.deepEqual(result, expected, 'expect result to keep order of the original array'); }); }); test('serialOrdered: iterates over object custom sorted', function(t) { var source = { first: 1, second : 2, third : 3, fourth : 4, three : 3, two : 2, one : 1 } , keys = Object.keys(source) , expected = { first: 'A', second: 'B', third: 'C', fourth: 'D', three: 'C', two: 'B', one: 'A' } // separate vowels from consonants , splitVowels = function(word) { var vowels = ['a', 'e', 'i', 'o', 'u'] , wordVow = word.split('').map(function(x){ return vowels.indexOf(x) == -1 ? '~' : x; }).join('') , wordCon = word.split('').map(function(x){ return vowels.indexOf(x) == -1 ? x : '.'; }).join('') ; return [wordVow, wordCon]; } // sort words based on vowels and their position , customSort = function(wordA, wordB) { wordA = splitVowels(wordA); wordB = splitVowels(wordB); return wordA[0] < wordB[0] ? -1 : ( wordA[0] > wordB[0] ? 1 : ( wordA[1] < wordB[1] ? -1 : wordA[1] > wordB[1] ? 1 : 0 ) ); } // pre-sort list keys , customSortedKeys = keys.sort(customSort) , prev ; t.plan(keys.length * 3 + 2); serialOrdered(source, function(item, key, cb) { t.notEqual(customSortedKeys.indexOf(key), -1, 'expect key to be in the list'); t.ok(customSortedKeys.indexOf(prev) < customSortedKeys.indexOf(key), 'expect key (' + prev + ' -> ' + key + ') index (' + customSortedKeys.indexOf(prev) + ' -> ' + customSortedKeys.indexOf(key) + ') to increase on each iteration'); t.equal(source[key], item, 'expect iteration indices to match original array positions'); setTimeout(cb.bind(null, null, String.fromCharCode(64 + item)), 10 * item); prev = key; }, customSort, // custom sorting function(err, result) { t.error(err, 'expect no errors'); t.deepEqual(result, expected, 'expect result to keep order of the original array'); }); }); test('serialOrdered: iterates over object custom sorted over values', function(t) { var source = { first: 1, second: 2, third: 3, fourth: 4, three: 3, two: 2, one: 1 } , keys = Object.keys(source) , values = keys.map(function(k){ return source[k]; }) , expected = { first: 'A', second: 'B', third: 'C', fourth: 'D', three: 'C', two: 'B', one: 'A' } // get smallest even number , prev = Math.min.apply(Math, values.filter(function(n){ return !(n % 2); })) // puts even numbers first , evenOddSort = function(a, b) { var order = a < b ? -1 : a > b ? 1 : 0 , aOdd = a % 2 , bOdd = b % 2 ; return aOdd === bOdd ? order : aOdd ? 1 : -1; } , customSort = function(keyA, keyB) { return evenOddSort(source[keyA], source[keyB]); } ; t.plan(keys.length * 2 + 2); serialOrdered(source, function(item, key, cb) { var incr = prev <= item , shift = (prev % 2) !== (item % 2) ; t.ok(incr || shift, 'expect item (' + prev + ' -> ' + item + ') to increase on each iteration, unless it is switch from even to odd'); t.equal(source[key], item, 'expect iteration indices to match original array positions'); setTimeout(cb.bind(null, null, String.fromCharCode(64 + item)), 10 * item); prev = item; }, customSort, // custom sorting function(err, result) { t.error(err, 'expect no errors'); t.deepEqual(result, expected, 'expect result to keep order of the original array'); }); }); test('serialOrdered: object: terminates early with custom sorting', function(t) { var source = { first: 1, one : 1, four: 4, five: 5, sixteen: 16, sixtyFour: 64, thirtyTwo: 32, nine: 9, eight: 8, two: 2 } , salvaged = { eight: 8, four: 4, two : 2 } , expected = [ 2, 4, 8, 16 ] , target = [] // puts even numbers first , evenOddSort = function(a, b) { var order = a < b ? -1 : a > b ? 1 : 0 , aOdd = a % 2 , bOdd = b % 2 ; return aOdd === bOdd ? order : aOdd ? 1 : -1; } // sort based on the value of the key // even values go first , customSort = function(keyA, keyB) { return evenOddSort(source[keyA], source[keyB]); } ; t.plan(expected.length + 4); serialOrdered(source, function(item, key, cb) { var id = setTimeout(function() { t.ok((item < 10 && item % 2 === 0) || item == 16, 'expect only certain numbers being processed'); target.push(item); if (item < 10) { cb(null, item); } // return error on big numbers else { cb({key: key, item: item}); } }, 5 * item); return clearTimeout.bind(null, id); }, customSort, // custom sort function(err, result) { t.equal(err.key, 'sixteen', 'expect to error out on key `sixteen`'); t.equal(err.item, 16, 'expect to error out on value `16`'); t.deepEqual(result, salvaged, 'expect result to contain processed parts that less than 10 of the source object'); t.deepEqual(target, expected, 'expect target to contain passed numbers'); }); }); test('serialOrdered: object: terminated early from outside, with custom sorting', function(t) { var source = { first: 1, one : 1, four: 4, five: 5, sixteen: 16, sixtyFour: 64, thirtyTwo: 32, nine: 9, eight: 8, two: 2 } , salvaged = { eight: 8, four: 4, two : 2 } // ascending even numbers below 10 , expected = [ 2, 4, 8 ] // puts even numbers first , evenOddSort = function(a, b) { var order = a < b ? -1 : a > b ? 1 : 0 , aOdd = a % 2 , bOdd = b % 2 ; return aOdd === bOdd ? order : aOdd ? 1 : -1; } // sort based on the value of the key // even values go first , customSort = function(keyA, keyB) { return evenOddSort(source[keyA], source[keyB]); } , target = [] , limitNum = 10 , terminator ; t.plan(expected.length + 3); setTimeout(function() { terminator(); }, 5 * (limitNum + expected.reduce(function(a, b){ return a + b; }))); terminator = serialOrdered(source, function(item, key, cb) { var id = setTimeout(function() { t.ok((item < limitNum && item % 2 === 0), 'expect only even numbers (' + key + ':' + item + ') less than (' + limitNum + ') being processed'); target.push(item); cb(null, item); }, 5 * item); return clearTimeout.bind(null, id); }, customSort, // custom sort function(err, result) { t.error(err, 'expect no error response'); t.deepEqual(result, salvaged, 'expect result to contain processed parts that less than ' + limitNum + ' of the source array'); t.deepEqual(target, expected, 'expect target to contain passed numbers'); }); }); test('serialOrdered: object: terminated prematurely from outside, with custom sorting', function(t) { var source = { first: 1, one : 1, four: 4, five: 5, sixteen: 16, sixtyFour: 64, thirtyTwo: 32, nine: 9, eight: 8, two: 2 } , expected = { } // puts even numbers first , evenOddSort = function(a, b) { var order = a < b ? -1 : a > b ? 1 : 0 , aOdd = a % 2 , bOdd = b % 2 ; return aOdd === bOdd ? order : aOdd ? 1 : -1; } // sort based on the value of the key // even values go first , customSort = function(keyA, keyB) { return evenOddSort(source[keyA], source[keyB]); } , terminator ; t.plan(2); terminator = serialOrdered(source, function(item, cb) { var id = setTimeout(function() { t.fail('do not expect it to come that far'); cb(null, item); }, 5 * item); return clearTimeout.bind(null, id); }, customSort, // custom sort function(err, result) { t.error(err, 'expect no error response'); t.deepEqual(result, expected, 'expect result to contain salvaged parts of the source array'); }); terminator(); }); test('serialOrdered: object: terminated too late from outside, with custom sorting', function(t) { var source = { first: 1, one: 1, four: 4, five: 5, sixteen: 16, sixtyFour: 64, thirtyTwo: 32, nine: 9, eight: 8, two: 2 } , salvaged = { eight: 8, four: 4, two: 2, sixteen: 16, sixtyFour: 64, thirtyTwo: 32, first: 1, one: 1, five: 5, nine: 9 } // ascending even numbers below 10 , expected = [ 2, 4, 8, 16, 32, 64, 1, 1, 5, 9 ] // puts even numbers first , evenOddSort = function(a, b) { var order = a < b ? -1 : a > b ? 1 : 0 , aOdd = a % 2 , bOdd = b % 2 ; return aOdd === bOdd ? order : aOdd ? 1 : -1; } // sort based on the value of the key // even values go first , customSort = function(keyA, keyB) { return evenOddSort(source[keyA], source[keyB]); } , target = [] , terminator ; t.plan(expected.length + 3); terminator = serialOrdered(source, function(item, key, cb) { var id = setTimeout(function() { t.equal(source[key], item, 'expect item (' + key + ':' + item + ') to be equal ' + source[key]); target.push(item); cb(null, item); }, 5 * item); return clearTimeout.bind(null, id); }, customSort, // custom sort function(err, result) { // terminate it after it's done terminator(); t.error(err, 'expect no error response'); t.deepEqual(result, salvaged, 'expect result to contain processed parts of the source array'); t.deepEqual(target, expected, 'expect target to contain passed numbers'); }); }); asynckit-0.4.0/test/test-stream-parallel-array.js000066400000000000000000000160271272773076200220560ustar00rootroot00000000000000/* eslint no-sparse-arrays: "off" */ var test = require('tape').test , streamAssert = require('./lib/stream_assert.js') , asynckitStream = require('../stream.js') ; test('stream: parallel: iterates over array', function(t) { var source = [ 1, 2, 3, 4, 3, 2, 1 ] , expected = [ 'A', 'B', 'C', 'D', 'C', 'B', 'A' ] , start = +new Date() , stream ; t.plan(expected.length * 2 + 4); stream = asynckitStream.parallel(source, function(item, cb) { t.ok(source.indexOf(item) != -1, 'expect item (' + item + ') to exist in the subject array'); setTimeout(cb.bind(null, null, String.fromCharCode(64 + item)), 10 * item); }, function(err, result) { var diff = +new Date() - start; t.ok(diff < 160, 'expect response time (' + diff + 'ms) to be less than sum of delays'); t.error(err, 'expect no errors'); t.deepEqual(result, expected, 'expect result to be an ordered letters array'); }); streamAssert.success(t, stream, {elements: expected}); }); test('stream: parallel: array: terminates early', function(t) { var source = [ 1, 1, 4, 16, 66, 34, 8, 2 ] , salvaged = [ 1, 1, 4, , , , 8, 2 ] , expected = [ 1, 1, 2, 4, 8 ] , expError = {item: 16} , target = [] , stream ; t.plan(expected.length * 2 + 5); stream = asynckitStream.parallel(source, function(item, cb) { var id = setTimeout(function() { // just "hardcode" first element var sum = target.reduce(function(acc, num){ return acc + num; }, 0) || 1; t.equal(sum, item, 'expect sum (' + sum + ') to be equal current number (' + item + ')'); if (item < 10) { target.push(item); cb(null, item); } // return error on big numbers else { cb({item: item}); } }, 25 * item); return clearTimeout.bind(null, id); }, function(err, result) { t.equal(err.item, expError.item, 'expect to error out on 16'); t.deepEqual(result, salvaged, 'expect result to contain salvaged parts of the source array'); t.deepEqual(target, expected, 'expect target to contain passed numbers'); }); streamAssert.failure(t, stream, {elements: salvaged, error: expError}); }); test('stream: parallel: array: handles non terminable iterations', function(t) { var source = [ 1, 1, 4, 16, 65, 33, 8, 2 ] , salvaged = [ 1, 1, 4, , , , 8, 2 ] , expected = [ 1, 1, 2, 4, 8 ] , expError = {item: 16} , target = [] , previous = 0 , stream ; t.plan(expected.length * 2 + 3); stream = asynckitStream.parallel(source, function(item, cb) { var id = setTimeout(function() { if (item < 10) { // expect it to be invoked in order t.ok(item >= previous, 'expect item (' + item + ') to be equal or greater than previous item (' + previous + ')'); previous = item; target.push(item); cb(null, item); } // return error on big numbers else { cb({item: item}); } }, 25 * item); return (item % 2) ? null : clearTimeout.bind(null, id); }, function(err) { t.equal(err.item, expError.item, 'expect to error out on 16'); t.deepEqual(target, expected, 'expect target to contain passed numbers'); }); streamAssert.failure(t, stream, {elements: salvaged, error: expError}); }); test('stream: parallel: array: handles unclean callbacks', function(t) { var source = [ 1, 2, 3, 4, 3, 2, 1 ] , expected = [ 2, 4, 6, 8, 6, 4, 2 ] , stream ; t.plan(expected.length * 2 + 3); stream = asynckitStream.parallel(source, function(item, cb) { setTimeout(function() { t.ok(source.indexOf(item) != -1, 'expect item (' + item + ') to exist in the source array'); cb(null, item * 2); cb(null, item * -2); }, 50 * item); }, function(err, result) { t.error(err, 'expect no errors'); t.deepEqual(result, expected, 'expect result to be an multiplied by two array'); }); streamAssert.success(t, stream, {elements: expected}); }); test('stream: parallel: array: destroyed cleanly', function(t) { var source = [ 1, 1, 4, 16, 66, 34, 8, 2 ] , salvaged = [ 1, 1, 4, , , , 8, 2 ] , expected = [ 1, 1, 2, 4, 8 ] , limitNum = 15 , target = [] , stream ; t.plan(expected.length * 3 + 4); // destroy stream before element 16 is processed setTimeout(function() { stream.destroy(); }, 25 * limitNum); stream = asynckitStream.parallel(source, function(item, cb) { var id = setTimeout(function() { // just "hardcode" first element var sum = target.reduce(function(acc, num){ return acc + num; }, 0) || 1; t.ok(item < limitNum, 'expect only numbers (' + item + ') less than ' + limitNum + ' to be processed.'); t.equal(sum, item, 'expect sum (' + sum + ') to be equal current number (' + item + ')'); target.push(item); cb(null, item); }, 25 * item); return clearTimeout.bind(null, id); }, function(err, result) { t.error(err, 'expect no errors'); t.deepEqual(result, salvaged, 'expect result to contain salvaged parts of the source array'); t.deepEqual(target, expected, 'expect target to contain passed numbers'); }); streamAssert.success(t, stream, {elements: salvaged}); }); test('stream: parallel: array: destroyed cleanly at start', function(t) { var source = [ 1, 1, 4, 16, 66, 34, 8, 2 ] , expected = [ ] , stream ; t.plan(3); stream = asynckitStream.parallel(source, function(item, cb) { var id = setTimeout(function() { t.fail('do not expect it to come that far'); cb(null, item); }, 25 * item); return clearTimeout.bind(null, id); }, function(err, result) { t.error(err, 'expect no errors'); t.deepEqual(result, expected, 'expect result to contain salvaged parts of the source array'); }); streamAssert.success(t, stream, {elements: expected}); // destroy stream before element 16 is processed stream.destroy(); }); test('stream: parallel: array: destroyed after finish', function(t) { var source = [ 1, 1, 4, 16, 64, 32, 8, 2 ] , salvaged = [ 1, 1, 4, 16, 64, 32, 8, 2 ] , expected = [ 1, 1, 2, 4, 8, 16, 32, 64 ] , target = [] , stream ; t.plan(expected.length * 2 + 4); stream = asynckitStream.parallel(source, function(item, cb) { var id = setTimeout(function() { // just "hardcode" first element var sum = target.reduce(function(acc, num){ return acc + num; }, 0) || 1; t.equal(sum, item, 'expect sum (' + sum + ') to be equal current number (' + item + ')'); target.push(item); cb(null, item); }, 25 * item); return clearTimeout.bind(null, id); }, function(err, result) { stream.destroy(); stream.destroy(); // do it couple times to make sure t.error(err, 'expect no errors'); t.deepEqual(result, salvaged, 'expect result to contain salvaged parts of the source array'); t.deepEqual(target, expected, 'expect target to contain passed numbers'); }); streamAssert.success(t, stream, {elements: salvaged}); }); asynckit-0.4.0/test/test-stream-parallel-object.js000066400000000000000000000255071272773076200222110ustar00rootroot00000000000000/* eslint no-sparse-arrays: "off" */ var test = require('tape').test , defer = require('../lib/defer.js') , streamAssert = require('./lib/stream_assert.js') , asynckitStream = require('../stream.js') ; test('stream: parallel: iterates over object', function(t) { var source = { first: 1, second: 2, third: 3, fourth: 4, three: 3, two: 2, one: 1 } , keys = Object.keys(source) , expected = { first: 'A', second: 'B', third: 'C', fourth: 'D', three: 'C', two: 'B', one: 'A' } , start = +new Date() , stream ; t.plan(keys.length * 3 + 4); // supports full value, key, callback (shortcut) interface stream = asynckitStream.parallel(source, function(item, key, cb) { t.ok(keys.indexOf(key) != -1, 'expect key (' + key + ') to exist in the keys array'); t.equal(item, source[key], 'expect item (' + item + ') to match in same key (' + key + ') element in the source object'); setTimeout(cb.bind(null, null, String.fromCharCode(64 + item)), 10 * item); }, function(err, result) { var diff = +new Date() - start; t.ok(diff < 160, 'expect response time (' + diff + 'ms) to be less than sum of delays'); t.error(err, 'expect no errors'); t.deepEqual(result, expected, 'expect result to be an ordered letters object'); }); streamAssert.success(t, stream, {elements: expected}); }); test('stream: parallel: handles sync object iterator asynchronously', function(t) { var source = { first: 1, second: 2, third: 3, fourth: 4, three: 3, two: 2, one: 1 } , keys = Object.keys(source) , expected = { first: 'A', second: 'B', third: 'C', fourth: 'D', three: 'C', two: 'B', one: 'A' } , isAsync = false , stream ; t.plan(keys.length * 3 + 4); defer(function(){ isAsync = true; }); // supports full value, key, callback (shortcut) interface stream = asynckitStream.parallel(source, function(item, key, cb) { t.ok(keys.indexOf(key) != -1, 'expect key (' + key + ') to exist in the keys array'); t.equal(item, source[key], 'expect item (' + item + ') to match in same key (' + key + ') element in the source object'); cb(null, String.fromCharCode(64 + item)); }, function(err, result) { t.ok(isAsync, 'expect async response'); t.error(err, 'expect no errors'); t.deepEqual(result, expected, 'expect result to be an ordered letters object'); }); streamAssert.success(t, stream, {elements: expected}); }); test('stream: parallel: object: longest finishes last', function(t) { var source = { first: 1, one: 1, four: 4, sixteen: 16, sixtyFour: 64, thirtyTwo: 32, eight: 8, two: 2 } , keys = Object.keys(source) , expected = [ 1, 1, 2, 4, 8, 16, 32, 64 ] , testbody = expected.concat() , target = [] , stream ; t.plan(keys.length * 2 + 4); // supports just value, callback (shortcut) interface stream = asynckitStream.parallel(source, function(item, cb) { setTimeout(function() { // just "hardcode" first element var sum = target.reduce(function(acc, num){ return acc + num; }, 0) || 1; t.equal(sum, item, 'expect sum (' + sum + ') to be equal current number (' + item + ')'); target.push(item); cb(null, item); }, 10 * item); }, function(err, result) { t.error(err, 'expect no errors'); t.deepEqual(result, source, 'expect result to be same as source object'); t.deepEqual(target, expected, 'expect target to contain ordered numbers'); }); // expect values "arrive" in the specific order streamAssert.success(t, stream, {compare: function(key, value) { var lineup = JSON.stringify(testbody) , slider = testbody.shift() ; t.equal(value, slider, 'expect (' + value + ') to be first in line in ' + lineup); }}); }); test('stream: parallel: object: terminates early', function(t) { var source = { first: 1, one: 1, four: 4, sixteen: 16, sixtyFour: 64, thirtyTwo: 32, eight: 8, two: 2 } , salvaged = { first: 1, one: 1, two: 2, four: 4, eight: 8} , expected = [ 1, 1, 2, 4, 8 ] , expError = {key: 'sixteen', item: 16} , target = [] , stream ; t.plan(Object.keys(salvaged).length * 2 + 6); // supports full value, key, callback (shortcut) interface stream = asynckitStream.parallel(source, function(item, key, cb) { var id = setTimeout(function() { // just "hardcode" first element var sum = target.reduce(function(acc, num){ return acc + num; }, 0) || 1; t.equal(sum, item, 'expect sum (' + sum + ') to be equal current item (' + key + ':' + item + ')'); if (item < 10) { target.push(item); cb(null, item); } // return error on big numbers else { cb({key: key, item: item}); } }, 10 * item); return clearTimeout.bind(null, id); }, function(err, result) { t.equal(err.key, 'sixteen', 'expect to error out on key `sixteen`'); t.equal(err.item, expError.item, 'expect to error out on value `16`'); t.deepEqual(result, salvaged, 'expect result to contain salvaged parts of the source object'); t.deepEqual(target, expected, 'expect target to contain passed numbers'); }); streamAssert.failure(t, stream, {elements: salvaged, error: expError}); }); test('stream: parallel: object: destroyed cleanly', function(t) { var source = { first: 1, one: 1, four: 4, sixteen: 16, sixtyFour: 64, thirtyTwo: 32, eight: 8, two: 2 } , salvaged = { first: 1, one: 1, two: 2, four: 4, eight: 8} , expected = [ 1, 1, 2, 4, 8 ] , limitNum = 15 , target = [] , stream ; t.plan(expected.length * 3 + 4); // destroy stream before element 16 is processed setTimeout(function() { stream.destroy(); }, 25 * limitNum); // do it couple times to make sure setTimeout(function() { stream.destroy(); }, 25 * (limitNum + 1)); stream = asynckitStream.parallel(source, function(item, key, cb) { var id = setTimeout(function() { // just "hardcode" first element var sum = target.reduce(function(acc, num){ return acc + num; }, 0) || 1; t.ok(item < limitNum, 'expect only numbers (' + item + ') less than ' + limitNum + ' to be processed.'); t.equal(sum, item, 'expect sum (' + sum + ') to be equal current item (' + key + ':' + item + ')'); target.push(item); cb(null, item); }, 25 * item); return clearTimeout.bind(null, id); }, function(err, result) { t.error(err, 'expect no errors'); t.deepEqual(result, salvaged, 'expect result to contain salvaged parts of the source object'); t.deepEqual(target, expected, 'expect target to contain passed numbers'); }); streamAssert.success(t, stream, {elements: salvaged}); }); test('stream: parallel: object: destroyed cleanly at start', function(t) { var source = { first: 1, one: 1, four: 4, sixteen: 16, sixtyFour: 64, thirtyTwo: 32, eight: 8, two: 2 } , expected = { } , stream ; t.plan(3); stream = asynckitStream.parallel(source, function(item, cb) { var id = setTimeout(function() { t.fail('do not expect it to come that far'); cb(null, item); }, 25 * item); return clearTimeout.bind(null, id); }, function(err, result) { t.error(err, 'expect no errors'); t.deepEqual(result, expected, 'expect result to contain salvaged parts of the source object'); }); streamAssert.success(t, stream, {elements: expected}); // destroy stream before element 16 is processed stream.destroy(); }); test('stream: parallel: object: destroyed after finish', function(t) { var source = { first: 1, one: 1, four: 4, sixteen: 16, sixtyFour: 64, thirtyTwo: 32, eight: 8, two: 2 } , salvaged = { first: 1, one: 1, two: 2, four: 4, eight: 8, sixteen: 16, thirtyTwo: 32, sixtyFour: 64 } , expected = [ 1, 1, 2, 4, 8, 16, 32, 64 ] , target = [] , stream ; t.plan(expected.length * 2 + 4); stream = asynckitStream.parallel(source, function(item, cb) { var id = setTimeout(function() { // just "hardcode" first element var sum = target.reduce(function(acc, num){ return acc + num; }, 0) || 1; t.equal(sum, item, 'expect sum (' + sum + ') to be equal current number (' + item + ')'); target.push(item); cb(null, item); }, 25 * item); return clearTimeout.bind(null, id); }, function(err, result) { stream.destroy(); t.error(err, 'expect no errors'); t.deepEqual(result, salvaged, 'expect result to contain salvaged parts of the source object'); t.deepEqual(target, expected, 'expect target to contain passed numbers'); }); streamAssert.success(t, stream, {elements: salvaged}); }); test('stream: parallel: object: handles non terminable iterations', function(t) { var source = { first: 1, one: 1, four: 4, sixteen: 16, sixtyFour: 64, thirtyTwo: 32, eight: 8, two: 2 } , salvaged = { first: 1, one: 1, two: 2, four: 4, eight: 8} , expected = [ 1, 1, 2, 4, 8 ] , expError = {item: 16} , target = [] , previous = 0 , stream ; t.plan(expected.length * 2 + 3); // supports just value, callback (shortcut) interface stream = asynckitStream.parallel(source, function(item, cb) { var id = setTimeout(function() { if (item < 10) { // expect it to be invoked in order t.ok(item >= previous, 'expect item (' + item + ') to be equal or greater than previous item (' + previous + ')'); previous = item; target.push(item); cb(null, item); } // return error on big numbers else { cb({item: item}); } }, 10 * item); return (item % 2) ? null : clearTimeout.bind(null, id); }, function(err) { t.equal(err.item, expError.item, 'expect to error out on 16'); t.deepEqual(target, expected, 'expect target to contain passed numbers'); }); streamAssert.failure(t, stream, {elements: salvaged, error: expError}); }); test('stream: parallel: object: handles unclean callbacks', function(t) { var source = { first: 1, second: 2, third: 3, fourth: 4, three: 3, two: 2, one: 1 } , keys = Object.keys(source) , expected = { first: 2, second: 4, third: 6, fourth: 8, three: 6, two: 4, one: 2 } , stream ; t.plan(keys.length * 3 + 3); // supports full value, key, callback (shortcut) interface stream = asynckitStream.parallel(source, function(item, key, cb) { setTimeout(function() { t.ok(keys.indexOf(key) != -1, 'expect key (' + key + ') to exist in the keys array'); t.equal(item, source[key], 'expect item (' + item + ') to match in same key (' + key + ') element in the source object'); cb(null, item * 2); cb(null, item * -2); }, 10 * item); }, function(err, result) { t.error(err, 'expect no errors'); t.deepEqual(result, expected, 'expect result to be an multiplied by two object values'); }); streamAssert.success(t, stream, {elements: expected}); }); asynckit-0.4.0/test/test-stream-serial-array.js000066400000000000000000000205471272773076200215430ustar00rootroot00000000000000var test = require('tape').test , streamAssert = require('./lib/stream_assert.js') , asynckitStream = require('../stream.js') , defer = require('../lib/defer.js') ; test('stream: serial: iterates over array', function(t) { var source = [ 1, 2, 3, 4, 3, 2, 1 ] , itemsSum = 16 , expected = [ 'A', 'B', 'C', 'D', 'C', 'B', 'A' ] , start = +new Date() , stream ; t.plan(expected.length * 2 + 4); stream = asynckitStream.serial(source, function(item, cb) { t.ok(source.indexOf(item) != -1, 'expect item (' + item + ') to exist in the subject array (' + source + ')'); setTimeout(cb.bind(null, null, String.fromCharCode(64 + item)), 10 * item); }, function(err, result) { var diff = +new Date() - start; t.ok(diff > (itemsSum * 10), 'expect response time (' + diff + 'ms) to be more than ' + (itemsSum * 10) + ' ms'); t.error(err, 'expect no errors'); t.deepEqual(result, expected, 'expect result to be an ordered letters array'); }); streamAssert.success(t, stream, {elements: expected}); }); test('stream: serial: handles sync array iterator asynchronously', function(t) { var source = [ 1, 2, 3, 4, 3, 2, 1 ] , expected = [ 'A', 'B', 'C', 'D', 'C', 'B', 'A' ] , isAsync = false , stream ; t.plan(expected.length * 2 + 4); defer(function(){ isAsync = true; }); stream = asynckitStream.serial(source, function(item, cb) { t.ok(source.indexOf(item) != -1, 'expect item (' + item + ') to exist in the subject array'); cb(null, String.fromCharCode(64 + item)); }, function(err, result) { t.ok(isAsync, 'expect async response'); t.error(err, 'expect no errors'); t.deepEqual(result, expected, 'expect result to be an ordered letters array'); }); streamAssert.success(t, stream, {elements: expected}); }); test('stream: serial: array: longest finishes in order', function(t) { var source = [ 1, 1, 4, 16, 64, 32, 8, 2 ] , notExpected = [ 1, 1, 2, 4, 8, 16, 32, 64 ] , target = [] , slider = 0 , stream ; t.plan(source.length * 2 + 5); // supports full value, key, callback (shortcut) interface stream = asynckitStream.serial(source, function(item, key, cb) { setTimeout(function() { target.push(item); cb(null, item); }, 5 * item); }, function(err, result) { t.error(err, 'expect no errors'); t.deepEqual(result, source, 'expect result to be same as source array'); t.deepEqual(target, source, 'expect target to be same as source array'); t.notDeepEqual(target, notExpected, 'do not expect target to contain ordered numbers'); }); streamAssert.success(t, stream, {compare: function(key, value) { t.equal(slider, key, 'expect key (' + key + ') to be in line with slider (' + slider + ')'); t.equal(source[slider], value, 'expect value (' + value + ') to be in line with slider-index value (' + source[slider] + ')'); slider++; }}); }); test('stream: serial: array: terminates early', function(t) { var source = [ 1, 1, 4, 16, 66, 34, 8, 2 ] , expected = [ 1, 1, 4 ] , expError = {item: 16} , target = [] , stream ; t.plan(expected.length * 2 + 5); stream = asynckitStream.serial(source, function(item, cb) { var id = setTimeout(function() { t.ok(item < 5 || item == 16, 'expect only certain numbers being processed'); if (item < 10) { target.push(item); cb(null, item); } // return error on big numbers else { cb({item: item}); } }, 5 * item); return clearTimeout.bind(null, id); }, function(err, result) { t.equal(err.item, expError.item, 'expect to error out on ' + expError.item); t.deepEqual(result, expected, 'expect result to contain processed parts that less than 10 of the source array'); t.deepEqual(target, expected, 'expect target to contain passed numbers'); }); streamAssert.failure(t, stream, {elements: expected, error: expError}); }); test('stream: serial: array: terminated early from outside', function(t) { var source = [ 1, 1, 4, 16, 66, 34, 8, 2 ] , expected = [ 1, 1, 4 ] , target = [] , limitNum = 7 , stream ; t.plan(expected.length * 2 + 4); setTimeout(function() { stream.destroy(); }, 5 * (limitNum + expected.reduce(function(a, b){ return a + b; }))); stream = asynckitStream.serial(source, function(item, cb) { var id = setTimeout(function() { t.ok(item < limitNum, 'expect only numbers (' + item + ') less than (' + limitNum + ') being processed'); target.push(item); cb(null, item); }, 5 * item); return clearTimeout.bind(null, id); }, function(err, result) { t.error(err, 'expect no error response'); t.deepEqual(result, expected, 'expect result to contain processed parts that less than ' + limitNum + ' of the source array'); t.deepEqual(target, expected, 'expect target to contain passed numbers'); }); streamAssert.success(t, stream, {elements: expected}); }); test('stream: serial: array: terminated prematurely from outside', function(t) { var source = [ 1, 1, 4, 16, 66, 34, 8, 2 ] , expected = [ ] , stream ; t.plan(3); stream = asynckitStream.serial(source, function(item, cb) { var id = setTimeout(function() { t.fail('do not expect it to come that far'); cb(null, item); }, 5 * item); return clearTimeout.bind(null, id); }, function(err, result) { t.error(err, 'expect no error response'); t.deepEqual(result, expected, 'expect result to contain salvaged parts of the source array'); }); streamAssert.success(t, stream, {elements: expected}); stream.destroy(); }); test('stream: serial: array: terminated too late from outside', function(t) { var source = [ 1, 1, 4, 16, 64, 32, 8, 2 ] , expected = [ 1, 1, 4, 16, 64, 32, 8, 2 ] , target = [] , stream ; t.plan(expected.length * 2 + 4); stream = asynckitStream.serial(source, function(item, cb) { var id = setTimeout(function() { t.ok(source.indexOf(item) != -1, 'expect item (' + item + ') to exist in the subject array'); target.push(item); cb(null, item); }, 5 * item); return clearTimeout.bind(null, id); }, function(err, result) { // terminate it after it's done stream.destroy(); t.error(err, 'expect no error response'); t.deepEqual(result, expected, 'expect result to contain salvaged parts of the source array'); t.deepEqual(target, expected, 'expect target to contain passed numbers'); }); streamAssert.success(t, stream, {elements: expected}); }); test('stream: serial: array: handles non terminable iterations', function(t) { var source = [ 1, 1, 4, 16, 65, 33, 8, 2 ] , expected = [ 1, 1, 4 ] , expError = {item: 16} , target = [] , previous = 0 , stream ; t.plan(expected.length * 2 + 4); stream = asynckitStream.serial(source, function(item, cb) { var id = setTimeout(function() { // expect it to be invoked in order t.ok(item >= previous, 'expect item (' + item + ') to be equal or greater than previous item (' + previous + ')'); previous = item; if (item < 10) { target.push(item); cb(null, item); } // return error on big numbers else { cb({item: item}); } }, 5 * item); return (item % 2) ? null : clearTimeout.bind(null, id); }, function(err) { t.equal(err.item, expError.item, 'expect to error out on ' + expError.item); t.deepEqual(target, expected, 'expect target to contain passed numbers'); }); streamAssert.failure(t, stream, {elements: expected, error: expError}); }); test('stream: serial: array: handles unclean callbacks', function(t) { var source = [ 1, 2, 3, 4, 3, 2, 1 ] , expected = [ 2, 4, 6, 8, 6, 4, 2 ] , stream ; t.plan(expected.length * 2 + 3); stream = asynckitStream.serial(source, function(item, cb) { setTimeout(function() { t.ok(source.indexOf(item) != -1, 'expect item (' + item + ') to exist in the source array'); cb(null, item * 2); cb(null, item * -2); }, 5 * item); }, function(err, result) { t.error(err, 'expect no errors'); t.deepEqual(result, expected, 'expect result to be an multiplied by two array'); }); streamAssert.success(t, stream, {elements: expected}); }); asynckit-0.4.0/test/test-stream-serial-object.js000066400000000000000000000250421272773076200216660ustar00rootroot00000000000000var test = require('tape').test , streamAssert = require('./lib/stream_assert.js') , asynckitStream = require('../stream.js') , defer = require('../lib/defer.js') ; test('stream: serial: iterates over object', function(t) { var source = { first: 1, second: 2, third: 3, fourth: 4, three: 3, two: 2, one: 1 } , itemsSum = 16 , keys = Object.keys(source) , expected = { first: 'A', second: 'B', third: 'C', fourth: 'D', three: 'C', two: 'B', one: 'A' } , start = +new Date() , stream ; t.plan(keys.length * 3 + 4); // supports full value, key, callback interface stream = asynckitStream.serial(source, function(item, key, cb) { t.ok(keys.indexOf(key) != -1, 'expect key (' + key + ') to exist in the keys array'); t.equal(item, source[key], 'expect item (' + item + ') to match in same key (' + key + ') element in the source object'); setTimeout(cb.bind(null, null, String.fromCharCode(64 + item)), 10 * item); }, function(err, result) { var diff = +new Date() - start; t.ok(diff > (itemsSum * 10), 'expect response time (' + diff + 'ms) to be more than ' + (itemsSum * 10) + ' ms'); t.error(err, 'expect no errors'); t.deepEqual(result, expected, 'expect result to be an ordered letters object'); }); streamAssert.success(t, stream, {elements: expected}); }); test('stream: serial: handles sync object iterator asynchronously', function(t) { var source = { first: 1, second: 2, third: 3, fourth: 4, three: 3, two: 2, one: 1 } , keys = Object.keys(source) , expected = { first: 'A', second: 'B', third: 'C', fourth: 'D', three: 'C', two: 'B', one: 'A' } , isAsync = false , stream ; t.plan(keys.length * 3 + 4); defer(function(){ isAsync = true; }); // supports full value, key, callback (shortcut) interface stream = asynckitStream.serial(source, function(item, key, cb) { t.ok(keys.indexOf(key) != -1, 'expect key (' + key + ') to exist in the keys array'); t.equal(item, source[key], 'expect item (' + item + ') to match in same key (' + key + ') element in the source object'); cb(null, String.fromCharCode(64 + item)); }, function(err, result) { t.ok(isAsync, 'expect async response'); t.error(err, 'expect no errors'); t.deepEqual(result, expected, 'expect result to be an ordered letters object'); }); streamAssert.success(t, stream, {elements: expected}); }); test('stream: serial: object: longest finishes in order', function(t) { var source = { first: 1, one: 1, four: 4, sixteen: 16, sixtyFour: 64, thirtyTwo: 32, eight: 8, two: 2 } , keys = Object.keys(source) // assume stable order , expected = [ 1, 1, 4, 16, 64, 32, 8, 2 ] , target = [] , slider = 0 , stream ; t.plan(keys.length * 2 + 4); // supports just value, callback (shortcut) interface stream = asynckitStream.serial(source, function(item, cb) { setTimeout(function() { target.push(item); cb(null, item); }, 5 * item); }, function(err, result) { t.error(err, 'expect no errors'); t.deepEqual(result, source, 'expect result to be same as source object'); // expect it to be invoked in order // which is not always the case with objects // use `serialOrdered` if order really matters t.deepEqual(target, expected, 'expect target to be same as source object'); }); streamAssert.success(t, stream, {compare: function(key, value) { t.equal(keys[slider], key, 'expect key (' + key + ') to be in line with slider (' + slider + ':' + keys[slider] + ')'); t.equal(source[keys[slider]], value, 'expect value (' + value + ') to be in line with slider-index value (' + source[keys[slider]] + ')'); slider++; }}); }); test('stream: serial: object: terminates early', function(t) { var source = { first: 1, one: 1, four: 4, sixteen: 16, sixtyFour: 64, thirtyTwo: 32, eight: 8, two: 2 } , salvaged = { first: 1, one: 1, four: 4 } , expected = [ 1, 1, 4 ] , expError = { key: 'sixteen', item: 16 } , target = [] , stream ; t.plan(Object.keys(salvaged).length * 2 + 6); // supports full value, key, callback (shortcut) interface stream = asynckitStream.serial(source, function(item, key, cb) { var id = setTimeout(function() { // expect it to be invoked in order // which is not always the case with objects // use `serialOrdered` if order really matters t.ok(item < 5 || item == expError.item, 'expect only certain numbers being processed'); if (item < 10) { target.push(item); cb(null, item); } // return error on big numbers else { cb({key: key, item: item}); } }, 5 * item); return clearTimeout.bind(null, id); }, function(err, result) { t.equal(err.key, expError.key, 'expect to error out on key `' + expError.key + '`'); t.equal(err.item, expError.item, 'expect to error out on value `' + expError.item + '`'); t.deepEqual(result, salvaged, 'expect result to contain salvaged parts of the source object'); t.deepEqual(target, expected, 'expect target to contain passed numbers'); }); streamAssert.failure(t, stream, {elements: salvaged, error: expError}); }); test('stream: serial: object: terminated early from outside', function(t) { var source = { first: 1, one: 1, four: 4, sixteen: 16, sixtyFour: 64, thirtyTwo: 32, eight: 8, two: 2 } , salvaged = { first: 1, one: 1, four: 4 } , expected = [ 1, 1, 4 ] , target = [] , limitNum = 7 , stream ; t.plan(expected.length * 2 + 4); setTimeout(function() { stream.destroy(); }, 5 * (limitNum + expected.reduce(function(a, b){ return a + b; }))); stream = asynckitStream.serial(source, function(item, key, cb) { var id = setTimeout(function() { t.ok(item < limitNum, 'expect only numbers (' + key + ':' + item + ') less than (' + limitNum + ') being processed'); target.push(item); cb(null, item); }, 5 * item); return clearTimeout.bind(null, id); }, function(err, result) { t.error(err, 'expect no error response'); t.deepEqual(result, salvaged, 'expect result to contain salvaged parts of the source object'); t.deepEqual(target, expected, 'expect target to contain passed numbers'); }); streamAssert.success(t, stream, {elements: salvaged}); }); test('stream: serial: object: terminated prematurely from outside', function(t) { var source = { first: 1, one: 1, four: 4, sixteen: 16, sixtyFour: 64, thirtyTwo: 32, eight: 8, two: 2 } , expected = { } , stream ; t.plan(3); stream = asynckitStream.serial(source, function(item, cb) { var id = setTimeout(function() { t.fail('do not expect it to come that far'); cb(null, item); }, 5 * item); return clearTimeout.bind(null, id); }, function(err, result) { t.error(err, 'expect no error response'); t.deepEqual(result, expected, 'expect result to contain salvaged parts of the source object'); }); streamAssert.success(t, stream, {elements: expected}); // destroy stream before element 16 is processed stream.destroy(); }); test('stream: serial: object: terminated too late from outside', function(t) { var source = { first: 1, one: 1, four: 4, sixteen: 16, sixtyFour: 64, thirtyTwo: 32, eight: 8, two: 2 } , salvaged = { first: 1, one: 1, four: 4, sixteen: 16, sixtyFour: 64, thirtyTwo: 32, eight: 8, two: 2 } , expected = [ 1, 1, 4, 16, 64, 32, 8, 2 ] , target = [] , stream ; t.plan(expected.length * 2 + 4); stream = asynckitStream.serial(source, function(item, key, cb) { var id = setTimeout(function() { t.equal(source[key], item, 'expect item (' + key + ':' + item + ') to equal ' + source[key]); target.push(item); cb(null, item); }, 5 * item); return clearTimeout.bind(null, id); }, function(err, result) { // terminate it after it's done stream.destroy(); // survives two destroy calls stream.destroy(); t.error(err, 'expect no error response'); t.deepEqual(result, salvaged, 'expect result to contain salvaged parts of the source oject'); t.deepEqual(target, expected, 'expect target to contain passed numbers'); }); streamAssert.success(t, stream, {elements: salvaged}); }); test('stream: serial: object: handles non terminable iterations', function(t) { var source = { first: 1, one: 1, four: 4, sixteen: 16, sixtyFour: 64, thirtyTwo: 32, eight: 8, two: 2 } , salvaged = { first: 1, one: 1, four: 4 } , expected = [ 1, 1, 4 ] , expError = {item: 16} , target = [] , previous = 0 , stream ; t.plan(expected.length * 2 + 4); // supports just value, callback (shortcut) interface stream = asynckitStream.serial(source, function(item, cb) { var id = setTimeout(function() { // expect it to be invoked in order // which is not always the case with objects // use `serialOrdered` if order really matters t.ok(item >= previous, 'expect item (' + item + ') to be equal or greater than previous item (' + previous + ')'); previous = item; if (item < 10) { target.push(item); cb(null, item); } // return error on big numbers else { cb({item: item}); } }, 5 * item); return (item % 2) ? null : clearTimeout.bind(null, id); }, function(err) { t.equal(err.item, 16, 'expect to error out on 16'); t.deepEqual(target, expected, 'expect target to contain passed numbers'); }); streamAssert.failure(t, stream, {elements: salvaged, error: expError}); }); test('stream: serial: object: handles unclean callbacks', function(t) { var source = { first: 1, second: 2, third: 3, fourth: 4, three: 3, two: 2, one: 1 } , keys = Object.keys(source) , expected = { first: 2, second: 4, third: 6, fourth: 8, three: 6, two: 4, one: 2 } , stream ; t.plan(keys.length * 3 + 3); // supports full value, key, callback (shortcut) interface stream = asynckitStream.serial(source, function(item, key, cb) { setTimeout(function() { t.ok(keys.indexOf(key) != -1, 'expect key (' + key + ') to exist in the keys array'); t.equal(item, source[key], 'expect item (' + item + ') to match in same key (' + key + ') element in the source object'); cb(null, item * 2); cb(null, item * -2); }, 5 * item); }, function(err, result) { t.error(err, 'expect no errors'); t.deepEqual(result, expected, 'expect result to be an multiplied by two object values'); }); streamAssert.success(t, stream, {elements: expected}); }); asynckit-0.4.0/test/test-stream-serialOrdered-array.js000066400000000000000000000251351272773076200230460ustar00rootroot00000000000000/* eslint no-sparse-arrays: "off" */ var test = require('tape').test , streamAssert = require('./lib/stream_assert.js') , asynckitStream = require('../stream.js') ; test('stream: serialOrdered: iterates over array with no sortMethod', function(t) { var source = [ 1, 2, 3, 4, 3, 2, 1 ] , expected = [ 'A', 'B', 'C', 'D', 'C', 'B', 'A' ] , index = 0 , stream ; t.plan(expected.length * 3 + 3); stream = asynckitStream.serialOrdered(source, function(item, key, cb) { t.equal(index, key, 'expect key (' + key + ') for the iteration to match incremental index (' + index + ')'); t.equal(source[index], item, 'expect item of the iteration to match incremental source element with index position'); setTimeout(cb.bind(null, null, String.fromCharCode(64 + item)), 10 * item); index++; }, null, // force no sortMethod function(err, result) { t.error(err, 'expect no errors'); t.deepEqual(result, expected, 'expect result to be an ordered letters array'); }); streamAssert.success(t, stream, {elements: expected}); }); test('stream: serialOrdered: iterates over array sorted ascending', function(t) { var source = [ 1, 2, 3, 4, 3, 2, 1 ] , expected = [ 'A', 'B', 'C', 'D', 'C', 'B', 'A' ] , prev = Math.min.apply(Math, source) , stream ; t.plan(expected.length * 3 + 3); stream = asynckitStream.serialOrdered(source, function(item, key, cb) { t.ok(prev <= item, 'expect item not to decrease on each iteration – ascending sorting'); t.equal(source[key], item, 'expect iteration indices to match original array positions'); cb(null, String.fromCharCode(64 + item)); // this should happen before next invocation of the iterator prev = item; }, asynckitStream.serialOrdered.ascending, // sort ascending function(err, result) { t.error(err, 'expect no errors'); t.deepEqual(result, expected, 'expect result to keep order of the original array'); }); streamAssert.success(t, stream, {compare: function(key, item) { var prevChar = String.fromCharCode(64 + prev); // stream chunks posted in order t.equal(prevChar, item, '+ expect item (' + item + ') to equal updated prev char (' + prevChar + ') on each iteration – ascending sorting'); }}); }); test('stream: serialOrdered: iterates over array sorted descending', function(t) { var source = [ 1, 2, 3, 4, 3, 2, 1 ] , expected = [ 'A', 'B', 'C', 'D', 'C', 'B', 'A' ] , prev = Math.max.apply(Math, source) , stream ; t.plan(expected.length * 3 + 3); stream = asynckitStream.serialOrdered(source, function(item, key, cb) { t.ok(prev >= item, 'expect item not to increase on each iteration – descending sorting'); t.equal(source[key], item, 'expect iteration indices to match original array positions'); setTimeout(cb.bind(null, null, String.fromCharCode(64 + item)), 10 * item); prev = item; }, asynckitStream.serialOrdered.descending, // sort descending function(err, result) { t.error(err, 'expect no errors'); t.deepEqual(result, expected, 'expect result to keep order of the original array'); }); streamAssert.success(t, stream, {compare: function(key, item) { var prevChar = String.fromCharCode(64 + prev); // stream chunks posted in order t.equal(prevChar, item, '+ expect item (' + item + ') to equal updated prev char (' + prevChar + ') on each iteration – descending sorting'); }}); }); test('stream: serialOrdered: iterates over array custom sorted', function(t) { var source = [ 1, 2, 3, 4, 3, 2, 1 ] , expected = [ 'A', 'B', 'C', 'D', 'C', 'B', 'A' ] // get smallest even number , prev = Math.min.apply(Math, source.filter(function(n){ return !(n % 2); })) // puts even numbers first , customSort = function(a, b) { var order = a < b ? -1 : a > b ? 1 : 0 , aOdd = a % 2 , bOdd = b % 2 ; return aOdd === bOdd ? order : aOdd ? 1 : -1; } , stream ; t.plan(expected.length * 3 + 3); stream = asynckitStream.serialOrdered(source, function(item, key, cb) { var incr = prev <= item , shift = (prev % 2) !== (item % 2) ; t.ok(incr || shift, 'expect item (' + item + ') to increase on each iteration, unless it is switch from even to odd'); t.equal(source[key], item, 'expect iteration indices to match original array positions'); setTimeout(cb.bind(null, null, String.fromCharCode(64 + item)), 10 * item); prev = item; }, customSort, // custom sorting function(err, result) { t.error(err, 'expect no errors'); t.deepEqual(result, expected, 'expect result to keep order of the original array'); }); streamAssert.success(t, stream, {compare: function(key, item) { var prevChar = String.fromCharCode(64 + prev); // stream chunks posted in order t.equal(prevChar, item, '+ expect item (' + item + ') to equal updated prev char (' + prevChar + ') on each iteration – custom sorting'); }}); }); test('stream: serialOrdered: array: terminates early with custom sorting', function(t) { var source = [ 1, 1, 4, 5, 16, 66, 34, 9, 8, 2 ] // even numbers below 10 , expectedResult = [ , , 4, , , , , , 8, 2 ] // ascending even numbers below 10 // and 16 as next even number 16 , expectedTarget = [ 2, 4, 8, 16 ] , expError = { item: 16 } // puts even numbers first , customSort = function(a, b) { var order = a < b ? -1 : a > b ? 1 : 0 , aOdd = a % 2 , bOdd = b % 2 ; return aOdd === bOdd ? order : aOdd ? 1 : -1; } , target = [] , stream ; t.plan(expectedTarget.length * 2 + 4 - 1); // `16` doesn't get posted to the stream stream = asynckitStream.serialOrdered(source, function(item, cb) { var id = setTimeout(function() { t.ok((item < 10 && item % 2 === 0) || item == expError.item, 'expect only certain (even) numbers being processed'); target.push(item); if (item < 10) { cb(null, item); } // return error on big numbers else { cb({item: item}); } }, 5 * item); return clearTimeout.bind(null, id); }, customSort, // custom sort function(err, result) { t.equal(err.item, expError.item, 'expect to error out on ' + expError.item); t.deepEqual(result, expectedResult, 'expect result to contain processed parts that less than 10 of the source array'); t.deepEqual(target, expectedTarget, 'expect target to contain passed numbers'); }); streamAssert.failure(t, stream, {elements: expectedResult, error: expError}); }); test('stream: serialOrdered: array: terminated early from outside, with custom sorting', function(t) { var source = [ 1, 1, 4, 5, 16, 66, 34, 9, 8, 2 ] // even numbers below 10 , expectedResult = [ , , 4, , , , , , 8, 2 ] // ascending even numbers below 10 , expectedTarget = [ 2, 4, 8 ] // puts even numbers first , customSort = function(a, b) { var order = a < b ? -1 : a > b ? 1 : 0 , aOdd = a % 2 , bOdd = b % 2 ; return aOdd === bOdd ? order : aOdd ? 1 : -1; } , target = [] , limitNum = 10 , stream ; t.plan(expectedTarget.length * 2 + 4); setTimeout(function() { stream.destroy(); }, 5 * (limitNum + expectedTarget.reduce(function(a, b){ return a + b; }))); stream = asynckitStream.serialOrdered(source, function(item, cb) { var id = setTimeout(function() { t.ok((item < limitNum && item % 2 === 0), 'expect only even numbers (' + item + ') less than (' + limitNum + ') being processed'); target.push(item); cb(null, item); }, 5 * item); return clearTimeout.bind(null, id); }, customSort, // custom sort function(err, result) { t.error(err, 'expect no error response'); t.deepEqual(result, expectedResult, 'expect result to contain processed parts that less than ' + limitNum + ' of the source array'); t.deepEqual(target, expectedTarget, 'expect target to contain passed numbers'); }); streamAssert.success(t, stream, {elements: expectedResult}); }); test('stream: serialOrdered: array: terminated prematurely from outside, with custom sorting', function(t) { var source = [ 1, 1, 4, 5, 16, 66, 34, 9, 8, 2 ] , expected = [ ] // puts even numbers first , customSort = function(a, b) { var order = a < b ? -1 : a > b ? 1 : 0 , aOdd = a % 2 , bOdd = b % 2 ; return aOdd === bOdd ? order : aOdd ? 1 : -1; } , stream ; t.plan(3); stream = asynckitStream.serialOrdered(source, function(item, cb) { var id = setTimeout(function() { t.fail('do not expect it to come that far'); cb(null, item); }, 5 * item); return clearTimeout.bind(null, id); }, customSort, // custom sort function(err, result) { t.error(err, 'expect no error response'); t.deepEqual(result, expected, 'expect result to contain salvaged parts of the source array'); }); streamAssert.success(t, stream, {elements: expected}); stream.destroy(); }); test('stream: serialOrdered: array: terminated too late from outside, with custom sorting', function(t) { var source = [ 1, 1, 4, 5, 16, 66, 34, 9, 8, 2 ] , expectedResult = [ 1, 1, 4, 5, 16, 66, 34, 9, 8, 2 ] , expectedTarget = [ 2, 4, 8, 16, 34, 66, 1, 1, 5, 9 ] // puts even numbers first , customSort = function(a, b) { var order = a < b ? -1 : a > b ? 1 : 0 , aOdd = a % 2 , bOdd = b % 2 ; return aOdd === bOdd ? order : aOdd ? 1 : -1; } , target = [] , slider = 0 , stream ; t.plan(expectedTarget.length * 2 + 4); stream = asynckitStream.serialOrdered(source, function(item, cb) { var id = setTimeout(function() { t.ok(source.indexOf(item) != -1, 'expect item (' + item + ') to exist in the subject array'); target.push(item); cb(null, item); }, 5 * item); return clearTimeout.bind(null, id); }, customSort, // custom sort function(err, result) { // terminate it after it's done stream.destroy(); t.error(err, 'expect no error response'); t.deepEqual(result, expectedResult, 'expect result to contain processed parts of the source array'); t.deepEqual(target, expectedTarget, 'expect target to contain passed numbers'); }); streamAssert.success(t, stream, {compare: function(key, item) { t.equal(expectedTarget[slider], item, '+ expect items (' + item + ' == ' + expectedTarget[slider] + ') to be posted to the stream in custom order'); slider++; }}); }); asynckit-0.4.0/test/test-stream-serialOrdered-object.js000066400000000000000000000351031272773076200231720ustar00rootroot00000000000000var test = require('tape').test , streamAssert = require('./lib/stream_assert.js') , asynckitStream = require('../stream.js') ; test('stream: serialOrdered: iterates over object with no sortMethod', function(t) { var source = { first: 1, second: 2, third: 3, fourth: 4, three: 3, two: 2, one: 1 } , keys = Object.keys(source) , expected = { first: 'A', second: 'B', third: 'C', fourth: 'D', three: 'C', two: 'B', one: 'A' } , index = 0 , stream ; t.plan(keys.length * 3 + 3); // supports full value, key, callback interface stream = asynckitStream.serialOrdered(source, function(item, key, cb) { t.equal(keys[index], key, 'expect key (' + key + ') for the iteration to match incremental index (' + index + ':' + keys[index] + ')'); t.equal(source[key], item, 'expect item of the iteration to match incremental source element with key position'); setTimeout(cb.bind(null, null, String.fromCharCode(64 + item)), 10 * item); index++; }, null, // force no sortMethod function(err, result) { t.error(err, 'expect no errors'); t.deepEqual(result, expected, 'expect result to be an ordered letters object'); }); streamAssert.success(t, stream, {elements: expected}); }); test('stream: serialOrdered: iterates over object sorted ascending', function(t) { var source = { first: 1, second: 2, third: 3, fourth: 4, three: 3, two: 2, one: 1 } , keys = Object.keys(source) , expected = { first: 'A', second: 'B', third: 'C', fourth: 'D', three: 'C', two: 'B', one: 'A' } , prev = 'a' , stream ; t.plan(keys.length * 3 + 3); stream = asynckitStream.serialOrdered(source, function(item, key, cb) { t.ok(prev <= key, 'expect key (' + item + ') not to decrease on each iteration – ascending sorting'); t.equal(source[key], item, 'expect iteration indices to match original object positions'); setTimeout(cb.bind(null, null, String.fromCharCode(64 + item)), 10 * item); prev = key; }, asynckitStream.serialOrdered.ascending, // sort ascending function(err, result) { t.error(err, 'expect no errors'); t.deepEqual(result, expected, 'expect result to keep order of the original array'); }); streamAssert.success(t, stream, {compare: function(key, item) { // stream chunks posted in order t.equal(expected[prev], item, '+ expect item (' + item + ') to equal updated prev char (' + expected[prev] + ') on each iteration – ascending sorting'); }}); }); test('stream: serialOrdered: iterates over object sorted descending (sync iterator)', function(t) { var source = { first: 1, second: 2, third: 3, fourth: 4, three: 3, two: 2, one: 1 } , keys = Object.keys(source) , expected = { first: 'A', second: 'B', third: 'C', fourth: 'D', three: 'C', two: 'B', one: 'A' } , prev = 'zzz' , stream ; t.plan(keys.length * 3 + 3); stream = asynckitStream.serialOrdered(source, function(item, key, cb) { t.ok(prev >= key, 'expect key (' + key + ') not to increase on each iteration – descending sorting'); t.equal(source[key], item, 'expect iteration indices to match original array positions'); cb(null, String.fromCharCode(64 + item)); // this should happen before next invocation of the iterator prev = key; }, asynckitStream.serialOrdered.descending, // sort descending function(err, result) { t.error(err, 'expect no errors'); t.deepEqual(result, expected, 'expect result to keep order of the original array'); }); streamAssert.success(t, stream, {compare: function(key, item) { // stream chunks posted in order t.equal(expected[prev], item, '+ expect item (' + item + ') to equal updated prev char (' + expected[prev] + ') on each iteration – ascending sorting'); }}); }); test('stream: serialOrdered: iterates over object custom sorted', function(t) { var source = { first: 1, second : 2, third : 3, fourth : 4, three : 3, two : 2, one : 1 } , keys = Object.keys(source) , expected = { first: 'A', second: 'B', third: 'C', fourth: 'D', three: 'C', two: 'B', one: 'A' } // separate vowels from consonants , splitVowels = function(word) { var vowels = ['a', 'e', 'i', 'o', 'u'] , wordVow = word.split('').map(function(x){ return vowels.indexOf(x) == -1 ? '~' : x; }).join('') , wordCon = word.split('').map(function(x){ return vowels.indexOf(x) == -1 ? x : '.'; }).join('') ; return [wordVow, wordCon]; } // sort words based on vowels and their position , customSort = function(wordA, wordB) { wordA = splitVowels(wordA); wordB = splitVowels(wordB); return wordA[0] < wordB[0] ? -1 : ( wordA[0] > wordB[0] ? 1 : ( wordA[1] < wordB[1] ? -1 : wordA[1] > wordB[1] ? 1 : 0 ) ); } // pre-sort list keys , customSortedKeys = keys.sort(customSort) , slider = 0 , prev , stream ; t.plan(keys.length * 5 + 3); stream = asynckitStream.serialOrdered(source, function(item, key, cb) { t.equal(customSortedKeys[slider], key, 'expect keys to be iterated in custom order'); t.ok(customSortedKeys.indexOf(prev) < customSortedKeys.indexOf(key), 'expect key (' + prev + ' -> ' + key + ') index (' + customSortedKeys.indexOf(prev) + ' -> ' + customSortedKeys.indexOf(key) + ') to increase on each iteration'); t.equal(source[key], item, 'expect iteration indices to match original array positions'); setTimeout(cb.bind(null, null, String.fromCharCode(64 + item)), 10 * item); prev = key; }, customSort, // custom sorting function(err, result) { t.error(err, 'expect no errors'); t.deepEqual(result, expected, 'expect result to keep order of the original array'); }); streamAssert.success(t, stream, {compare: function(key, item) { t.equal(customSortedKeys[slider], key, '+ expect keys arraive in custom order'); // stream chunks posted in order t.equal(expected[prev], item, '+ expect item (' + item + ') to equal updated prev char (' + expected[prev] + ') on each iteration – custom sorting'); slider++; }}); }); test('stream: serialOrdered: iterates over object custom sorted over values', function(t) { var source = { first: 1, second: 2, third: 3, fourth: 4, three: 3, two: 2, one: 1 } , keys = Object.keys(source) , values = keys.map(function(k){ return source[k]; }) , expected = { first: 'A', second: 'B', third: 'C', fourth: 'D', three: 'C', two: 'B', one: 'A' } // get smallest even number , prev = Math.min.apply(Math, values.filter(function(n){ return !(n % 2); })) // puts even numbers first , evenOddSort = function(a, b) { var order = a < b ? -1 : a > b ? 1 : 0 , aOdd = a % 2 , bOdd = b % 2 ; return aOdd === bOdd ? order : aOdd ? 1 : -1; } , customSort = function(keyA, keyB) { return evenOddSort(source[keyA], source[keyB]); } , stream ; t.plan(keys.length * 3 + 3); stream = asynckitStream.serialOrdered(source, function(item, key, cb) { var incr = prev <= item , shift = (prev % 2) !== (item % 2) ; t.ok(incr || shift, 'expect item (' + prev + ' -> ' + item + ') to increase on each iteration, unless it is switch from even to odd'); t.equal(source[key], item, 'expect iteration indices to match original array positions'); setTimeout(cb.bind(null, null, String.fromCharCode(64 + item)), 10 * item); prev = item; }, customSort, // custom sorting function(err, result) { t.error(err, 'expect no errors'); t.deepEqual(result, expected, 'expect result to keep order of the original array'); }); streamAssert.success(t, stream, {compare: function(key, item) { var prevChar = String.fromCharCode(64 + prev); // stream chunks posted in order t.equal(prevChar, item, '+ expect item (' + item + ') to equal updated prev char (' + prevChar + ') on each iteration – custom sorting'); }}); }); test('stream: serialOrdered: object: terminates early with custom sorting', function(t) { var source = { first: 1, one : 1, four: 4, five: 5, sixteen: 16, sixtyFour: 64, thirtyTwo: 32, nine: 9, eight: 8, two: 2 } , salvaged = { eight: 8, four: 4, two : 2 } , expected = [ 2, 4, 8, 16 ] , expError = {key: 'sixteen', item: 16} , target = [] // puts even numbers first , evenOddSort = function(a, b) { var order = a < b ? -1 : a > b ? 1 : 0 , aOdd = a % 2 , bOdd = b % 2 ; return aOdd === bOdd ? order : aOdd ? 1 : -1; } // sort based on the value of the key // even values go first , customSort = function(keyA, keyB) { return evenOddSort(source[keyA], source[keyB]); } , stream ; t.plan(expected.length * 2 + 4); // -1 since last item of expected array doesn't go into stream stream = asynckitStream.serialOrdered(source, function(item, key, cb) { var id = setTimeout(function() { t.ok((item < 10 && item % 2 === 0) || item == expError.item, 'expect only certain numbers being processed'); target.push(item); if (item < 10) { cb(null, item); } // return error on big numbers else { cb({key: key, item: item}); } }, 5 * item); return clearTimeout.bind(null, id); }, customSort, // custom sort function(err, result) { t.equal(err.key, expError.key, 'expect to error out on key `' + expError.key + '`'); t.equal(err.item, expError.item, 'expect to error out on value `' + expError.item + '`'); t.deepEqual(result, salvaged, 'expect result to contain processed parts that less than 10 of the source object'); t.deepEqual(target, expected, 'expect target to contain passed numbers'); }); streamAssert.failure(t, stream, {elements: salvaged, error: expError}); }); test('stream: serialOrdered: object: terminated early from outside, with custom sorting', function(t) { var source = { first: 1, one : 1, four: 4, five: 5, sixteen: 16, sixtyFour: 64, thirtyTwo: 32, nine: 9, eight: 8, two: 2 } , salvaged = { eight: 8, four: 4, two : 2 } // ascending even numbers below 10 , expected = [ 2, 4, 8 ] // puts even numbers first , evenOddSort = function(a, b) { var order = a < b ? -1 : a > b ? 1 : 0 , aOdd = a % 2 , bOdd = b % 2 ; return aOdd === bOdd ? order : aOdd ? 1 : -1; } // sort based on the value of the key // even values go first , customSort = function(keyA, keyB) { return evenOddSort(source[keyA], source[keyB]); } , target = [] , limitNum = 10 , stream ; t.plan(expected.length * 2 + 4); setTimeout(function() { stream.destroy(); }, 5 * (limitNum + expected.reduce(function(a, b){ return a + b; }))); stream = asynckitStream.serialOrdered(source, function(item, key, cb) { var id = setTimeout(function() { t.ok((item < limitNum && item % 2 === 0), 'expect only even numbers (' + key + ':' + item + ') less than (' + limitNum + ') being processed'); target.push(item); cb(null, item); }, 5 * item); return clearTimeout.bind(null, id); }, customSort, // custom sort function(err, result) { t.error(err, 'expect no error response'); t.deepEqual(result, salvaged, 'expect result to contain processed parts that less than ' + limitNum + ' of the source array'); t.deepEqual(target, expected, 'expect target to contain passed numbers'); }); streamAssert.success(t, stream, {elements: salvaged}); }); test('stream: serialOrdered: object: terminated prematurely from outside, with custom sorting', function(t) { var source = { first: 1, one : 1, four: 4, five: 5, sixteen: 16, sixtyFour: 64, thirtyTwo: 32, nine: 9, eight: 8, two: 2 } , expected = { } // puts even numbers first , evenOddSort = function(a, b) { var order = a < b ? -1 : a > b ? 1 : 0 , aOdd = a % 2 , bOdd = b % 2 ; return aOdd === bOdd ? order : aOdd ? 1 : -1; } // sort based on the value of the key // even values go first , customSort = function(keyA, keyB) { return evenOddSort(source[keyA], source[keyB]); } , stream ; t.plan(3); stream = asynckitStream.serialOrdered(source, function(item, cb) { var id = setTimeout(function() { t.fail('do not expect it to come that far'); cb(null, item); }, 5 * item); return clearTimeout.bind(null, id); }, customSort, // custom sort function(err, result) { t.error(err, 'expect no error response'); t.deepEqual(result, expected, 'expect result to contain salvaged parts of the source array'); }); streamAssert.success(t, stream, {elements: expected}); stream.destroy(); }); test('stream: serialOrdered: object: terminated too late from outside, with custom sorting', function(t) { var source = { first: 1, one: 1, four: 4, five: 5, sixteen: 16, sixtyFour: 64, thirtyTwo: 32, nine: 9, eight: 8, two: 2 } , salvaged = { eight: 8, four: 4, two: 2, sixteen: 16, sixtyFour: 64, thirtyTwo: 32, first: 1, one: 1, five: 5, nine: 9 } // ascending even numbers below 10 , expected = [ 2, 4, 8, 16, 32, 64, 1, 1, 5, 9 ] // puts even numbers first , evenOddSort = function(a, b) { var order = a < b ? -1 : a > b ? 1 : 0 , aOdd = a % 2 , bOdd = b % 2 ; return aOdd === bOdd ? order : aOdd ? 1 : -1; } // sort based on the value of the key // even values go first , customSort = function(keyA, keyB) { return evenOddSort(source[keyA], source[keyB]); } , target = [] , slider = 0 , stream ; t.plan(expected.length * 2 + 4); stream = asynckitStream.serialOrdered(source, function(item, key, cb) { var id = setTimeout(function() { t.equal(source[key], item, 'expect item (' + key + ':' + item + ') to be equal ' + source[key]); target.push(item); cb(null, item); }, 5 * item); return clearTimeout.bind(null, id); }, customSort, // custom sort function(err, result) { // terminate it after it's done stream.destroy(); t.error(err, 'expect no error response'); t.deepEqual(result, salvaged, 'expect result to contain processed parts of the source array'); t.deepEqual(target, expected, 'expect target to contain passed numbers'); }); streamAssert.success(t, stream, {compare: function(key, item) { t.equal(expected[slider], item, '+ expect items (' + item + ' == ' + expected[slider] + ') to be posted to the stream in custom order'); slider++; }}); });