Best JavaScript code snippet using backstopjs
async.js
Source:async.js
...2206 function arrayEachSync(array, iterator) {2207 var index = -1;2208 var size = array.length;2209 while (++index < size) {2210 iterator(array[index], index);2211 }2212 return array;2213 }2214 /**2215 * @private2216 * @param {Object} object - The object to iterate over.2217 * @param {Function} iterator - The function invoked per iteration.2218 * @param {Array} keys2219 */2220 function baseEachSync(object, iterator, keys) {2221 var index = -1;2222 var size = keys.length;2223 while (++index < size) {2224 var key = keys[index];2225 iterator(object[key], key);2226 }2227 return object;2228 }2229 /**2230 * @private2231 * @param {number} n2232 * @param {Function} iterator2233 */2234 function timesSync(n, iterator) {2235 var index = -1;2236 while (++index < n) {2237 iterator(index);2238 }2239 }2240 /**2241 * @private2242 * @param {Array} array2243 * @param {number[]} criteria2244 */2245 function sortByCriteria(array, criteria) {2246 var l = array.length;2247 var indices = Array(l);2248 var i;2249 for (i = 0; i < l; i++) {2250 indices[i] = i;2251 }2252 quickSort(criteria, 0, l - 1, indices);2253 var result = Array(l);2254 for (var n = 0; n < l; n++) {2255 i = indices[n];2256 result[n] = i === undefined ? array[n] : array[i];2257 }2258 return result;2259 }2260 function partition(array, i, j, mid, indices) {2261 var l = i;2262 var r = j;2263 while (l <= r) {2264 i = l;2265 while (l < r && array[l] < mid) {2266 l++;2267 }2268 while (r >= i && array[r] >= mid) {2269 r--;2270 }2271 if (l > r) {2272 break;2273 }2274 swap(array, indices, l++, r--);2275 }2276 return l;2277 }2278 function swap(array, indices, l, r) {2279 var n = array[l];2280 array[l] = array[r];2281 array[r] = n;2282 var i = indices[l];2283 indices[l] = indices[r];2284 indices[r] = i;2285 }2286 function quickSort(array, i, j, indices) {2287 if (i === j) {2288 return;2289 }2290 var k = i;2291 while (++k <= j && array[i] === array[k]) {2292 var l = k - 1;2293 if (indices[l] > indices[k]) {2294 var index = indices[l];2295 indices[l] = indices[k];2296 indices[k] = index;2297 }2298 }2299 if (k > j) {2300 return;2301 }2302 var p = array[i] > array[k] ? i : k;2303 k = partition(array, i, j, array[p], indices);2304 quickSort(array, i, k - 1, indices);2305 quickSort(array, k, j, indices);2306 }2307 /**2308 * @Private2309 */2310 function makeConcatResult(array) {2311 var result = [];2312 arrayEachSync(array, function(value) {2313 if (value === noop) {2314 return;2315 }2316 if (isArray(value)) {2317 nativePush.apply(result, value);2318 } else {2319 result.push(value);2320 }2321 });2322 return result;2323 }2324 /* async functions */2325 /**2326 * @private2327 */2328 function arrayEach(array, iterator, callback) {2329 var index = -1;2330 var size = array.length;2331 if (iterator.length === 3) {2332 while (++index < size) {2333 iterator(array[index], index, onlyOnce(callback));2334 }2335 } else {2336 while (++index < size) {2337 iterator(array[index], onlyOnce(callback));2338 }2339 }2340 }2341 /**2342 * @private2343 */2344 function baseEach(object, iterator, callback, keys) {2345 var key;2346 var index = -1;2347 var size = keys.length;2348 if (iterator.length === 3) {2349 while (++index < size) {2350 key = keys[index];2351 iterator(object[key], key, onlyOnce(callback));2352 }2353 } else {2354 while (++index < size) {2355 iterator(object[keys[index]], onlyOnce(callback));2356 }2357 }2358 }2359 /**2360 * @private2361 */2362 function symbolEach(collection, iterator, callback) {2363 var iter = collection[iteratorSymbol]();2364 var index = 0;2365 var item;2366 if (iterator.length === 3) {2367 while ((item = iter.next()).done === false) {2368 iterator(item.value, index++, onlyOnce(callback));2369 }2370 } else {2371 while ((item = iter.next()).done === false) {2372 index++;2373 iterator(item.value, onlyOnce(callback));2374 }2375 }2376 return index;2377 }2378 /**2379 * @private2380 */2381 function arrayEachResult(array, result, iterator, callback) {2382 var index = -1;2383 var size = array.length;2384 if (iterator.length === 4) {2385 while (++index < size) {2386 iterator(result, array[index], index, onlyOnce(callback));2387 }2388 } else {2389 while (++index < size) {2390 iterator(result, array[index], onlyOnce(callback));2391 }2392 }2393 }2394 /**2395 * @private2396 */2397 function baseEachResult(object, result, iterator, callback, keys) {2398 var key;2399 var index = -1;2400 var size = keys.length;2401 if (iterator.length === 4) {2402 while (++index < size) {2403 key = keys[index];2404 iterator(result, object[key], key, onlyOnce(callback));2405 }2406 } else {2407 while (++index < size) {2408 iterator(result, object[keys[index]], onlyOnce(callback));2409 }2410 }2411 }2412 /**2413 * @private2414 */2415 function symbolEachResult(collection, result, iterator, callback) {2416 var item;2417 var index = 0;2418 var iter = collection[iteratorSymbol]();2419 if (iterator.length === 4) {2420 while ((item = iter.next()).done === false) {2421 iterator(result, item.value, index++, onlyOnce(callback));2422 }2423 } else {2424 while ((item = iter.next()).done === false) {2425 index++;2426 iterator(result, item.value, onlyOnce(callback));2427 }2428 }2429 return index;2430 }2431 /**2432 * @private2433 */2434 function arrayEachFunc(array, createCallback) {2435 var index = -1;2436 var size = array.length;2437 while (++index < size) {2438 array[index](createCallback(index));2439 }2440 }2441 /**2442 * @private2443 */2444 function baseEachFunc(object, createCallback, keys) {2445 var key;2446 var index = -1;2447 var size = keys.length;2448 while (++index < size) {2449 key = keys[index];2450 object[key](createCallback(key));2451 }2452 }2453 /**2454 * @private2455 */2456 function arrayEachIndex(array, iterator, createCallback) {2457 var index = -1;2458 var size = array.length;2459 if (iterator.length === 3) {2460 while (++index < size) {2461 iterator(array[index], index, createCallback(index));2462 }2463 } else {2464 while (++index < size) {2465 iterator(array[index], createCallback(index));2466 }2467 }2468 }2469 /**2470 * @private2471 */2472 function baseEachIndex(object, iterator, createCallback, keys) {2473 var key;2474 var index = -1;2475 var size = keys.length;2476 if (iterator.length === 3) {2477 while (++index < size) {2478 key = keys[index];2479 iterator(object[key], key, createCallback(index));2480 }2481 } else {2482 while (++index < size) {2483 iterator(object[keys[index]], createCallback(index));2484 }2485 }2486 }2487 /**2488 * @private2489 */2490 function symbolEachIndex(collection, iterator, createCallback) {2491 var item;2492 var index = 0;2493 var iter = collection[iteratorSymbol]();2494 if (iterator.length === 3) {2495 while ((item = iter.next()).done === false) {2496 iterator(item.value, index, createCallback(index++));2497 }2498 } else {2499 while ((item = iter.next()).done === false) {2500 iterator(item.value, createCallback(index++));2501 }2502 }2503 return index;2504 }2505 /**2506 * @private2507 */2508 function baseEachKey(object, iterator, createCallback, keys) {2509 var key;2510 var index = -1;2511 var size = keys.length;2512 if (iterator.length === 3) {2513 while (++index < size) {2514 key = keys[index];2515 iterator(object[key], key, createCallback(key));2516 }2517 } else {2518 while (++index < size) {2519 key = keys[index];2520 iterator(object[key], createCallback(key));2521 }2522 }2523 }2524 /**2525 * @private2526 */2527 function symbolEachKey(collection, iterator, createCallback) {2528 var item;2529 var index = 0;2530 var iter = collection[iteratorSymbol]();2531 if (iterator.length === 3) {2532 while ((item = iter.next()).done === false) {2533 iterator(item.value, index, createCallback(index++));2534 }2535 } else {2536 while ((item = iter.next()).done === false) {2537 iterator(item.value, createCallback(index++));2538 }2539 }2540 return index;2541 }2542 /**2543 * @private2544 */2545 function arrayEachValue(array, iterator, createCallback) {2546 var value;2547 var index = -1;2548 var size = array.length;2549 if (iterator.length === 3) {2550 while (++index < size) {2551 value = array[index];2552 iterator(value, index, createCallback(value));2553 }2554 } else {2555 while (++index < size) {2556 value = array[index];2557 iterator(value, createCallback(value));2558 }2559 }2560 }2561 /**2562 * @private2563 */2564 function baseEachValue(object, iterator, createCallback, keys) {2565 var key, value;2566 var index = -1;2567 var size = keys.length;2568 if (iterator.length === 3) {2569 while (++index < size) {2570 key = keys[index];2571 value = object[key];2572 iterator(value, key, createCallback(value));2573 }2574 } else {2575 while (++index < size) {2576 value = object[keys[index]];2577 iterator(value, createCallback(value));2578 }2579 }2580 }2581 /**2582 * @private2583 */2584 function symbolEachValue(collection, iterator, createCallback) {2585 var value, item;2586 var index = 0;2587 var iter = collection[iteratorSymbol]();2588 if (iterator.length === 3) {2589 while ((item = iter.next()).done === false) {2590 value = item.value;2591 iterator(value, index++, createCallback(value));2592 }2593 } else {2594 while ((item = iter.next()).done === false) {2595 index++;2596 value = item.value;2597 iterator(value, createCallback(value));2598 }2599 }2600 return index;2601 }2602 /**2603 * @private2604 */2605 function arrayEachIndexValue(array, iterator, createCallback) {2606 var value;2607 var index = -1;2608 var size = array.length;2609 if (iterator.length === 3) {2610 while (++index < size) {2611 value = array[index];2612 iterator(value, index, createCallback(index, value));2613 }2614 } else {2615 while (++index < size) {2616 value = array[index];2617 iterator(value, createCallback(index, value));2618 }2619 }2620 }2621 /**2622 * @private2623 */2624 function baseEachIndexValue(object, iterator, createCallback, keys) {2625 var key, value;2626 var index = -1;2627 var size = keys.length;2628 if (iterator.length === 3) {2629 while (++index < size) {2630 key = keys[index];2631 value = object[key];2632 iterator(value, key, createCallback(index, value));2633 }2634 } else {2635 while (++index < size) {2636 value = object[keys[index]];2637 iterator(value, createCallback(index, value));2638 }2639 }2640 }2641 /**2642 * @private2643 */2644 function symbolEachIndexValue(collection, iterator, createCallback) {2645 var value, item;2646 var index = 0;2647 var iter = collection[iteratorSymbol]();2648 if (iterator.length === 3) {2649 while ((item = iter.next()).done === false) {2650 value = item.value;2651 iterator(value, index, createCallback(index++, value));2652 }2653 } else {2654 while ((item = iter.next()).done === false) {2655 value = item.value;2656 iterator(value, createCallback(index++, value));2657 }2658 }2659 return index;2660 }2661 /**2662 * @private2663 */2664 function baseEachKeyValue(object, iterator, createCallback, keys) {2665 var key, value;2666 var index = -1;2667 var size = keys.length;2668 if (iterator.length === 3) {2669 while (++index < size) {2670 key = keys[index];2671 value = object[key];2672 iterator(value, key, createCallback(key, value));2673 }2674 } else {2675 while (++index < size) {2676 key = keys[index];2677 value = object[key];2678 iterator(value, createCallback(key, value));2679 }2680 }2681 }2682 /**2683 * @private2684 */2685 function symbolEachKeyValue(collection, iterator, createCallback) {2686 var value, item;2687 var index = 0;2688 var iter = collection[iteratorSymbol]();2689 if (iterator.length === 3) {2690 while ((item = iter.next()).done === false) {2691 value = item.value;2692 iterator(value, index, createCallback(index++, value));2693 }2694 } else {2695 while ((item = iter.next()).done === false) {2696 value = item.value;2697 iterator(value, createCallback(index++, value));2698 }2699 }2700 return index;2701 }2702 /**2703 * @private2704 * @param {Function} func2705 */2706 function onlyOnce(func) {2707 return function(err, res) {2708 var fn = func;2709 func = throwError;2710 fn(err, res);2711 };2712 }2713 /**2714 * @private2715 * @param {Function} func2716 */2717 function once(func) {2718 return function(err, res) {2719 var fn = func;2720 func = noop;2721 fn(err, res);2722 };2723 }2724 /**2725 * @private2726 * @param {Function} arrayEach2727 * @param {Function} baseEach2728 */2729 function createEach(arrayEach, baseEach, symbolEach) {2730 return function each(collection, iterator, callback) {2731 callback = once(callback || noop);2732 var size, keys;2733 var completed = 0;2734 if (isArray(collection)) {2735 size = collection.length;2736 arrayEach(collection, iterator, done);2737 } else if (!collection) {2738 } else if (iteratorSymbol && collection[iteratorSymbol]) {2739 size = symbolEach(collection, iterator, done);2740 size && size === completed && callback(null);2741 } else if (typeof collection === obj) {2742 keys = nativeKeys(collection);2743 size = keys.length;2744 baseEach(collection, iterator, done, keys);2745 }2746 if (!size) {2747 callback(null);2748 }2749 function done(err, bool) {2750 if (err) {2751 callback = once(callback);2752 callback(err);2753 } else if (++completed === size) {2754 callback(null);2755 } else if (bool === false) {2756 callback = once(callback);2757 callback(null);2758 }2759 }2760 };2761 }2762 /**2763 * @private2764 * @param {Function} arrayEach2765 * @param {Function} baseEach2766 * @param {Function} symbolEach2767 */2768 function createMap(arrayEach, baseEach, symbolEach, useArray) {2769 var init, clone;2770 if (useArray) {2771 init = Array;2772 clone = createArray;2773 } else {2774 init = function() {2775 return {};2776 };2777 clone = objectClone;2778 }2779 return function(collection, iterator, callback) {2780 callback = callback || noop;2781 var size, keys, result;2782 var completed = 0;2783 if (isArray(collection)) {2784 size = collection.length;2785 result = init(size);2786 arrayEach(collection, iterator, createCallback);2787 } else if (!collection) {2788 } else if (iteratorSymbol && collection[iteratorSymbol]) {2789 // TODO: size could be changed2790 result = init(0);2791 size = symbolEach(collection, iterator, createCallback);2792 size && size === completed && callback(null, result);2793 } else if (typeof collection === obj) {2794 keys = nativeKeys(collection);2795 size = keys.length;2796 result = init(size);2797 baseEach(collection, iterator, createCallback, keys);2798 }2799 if (!size) {2800 callback(null, init());2801 }2802 function createCallback(key) {2803 return function done(err, res) {2804 if (key === null) {2805 throwError();2806 }2807 if (err) {2808 key = null;2809 callback = once(callback);2810 callback(err, clone(result));2811 return;2812 }2813 result[key] = res;2814 key = null;2815 if (++completed === size) {2816 callback(null, result);2817 }2818 };2819 }2820 };2821 }2822 /**2823 * @private2824 * @param {Function} arrayEach2825 * @param {Function} baseEach2826 * @param {Function} symbolEach2827 * @param {boolean} bool2828 */2829 function createFilter(arrayEach, baseEach, symbolEach, bool) {2830 return function(collection, iterator, callback) {2831 callback = callback || noop;2832 var size, keys, result;2833 var completed = 0;2834 if (isArray(collection)) {2835 size = collection.length;2836 result = Array(size);2837 arrayEach(collection, iterator, createCallback);2838 } else if (!collection) {2839 } else if (iteratorSymbol && collection[iteratorSymbol]) {2840 result = [];2841 size = symbolEach(collection, iterator, createCallback);2842 size && size === completed && callback(null, compact(result));2843 } else if (typeof collection === obj) {2844 keys = nativeKeys(collection);2845 size = keys.length;2846 result = Array(size);2847 baseEach(collection, iterator, createCallback, keys);2848 }2849 if (!size) {2850 return callback(null, []);2851 }2852 function createCallback(index, value) {2853 return function done(err, res) {2854 if (index === null) {2855 throwError();2856 }2857 if (err) {2858 index = null;2859 callback = once(callback);2860 callback(err);2861 return;2862 }2863 if (!!res === bool) {2864 result[index] = value;2865 }2866 index = null;2867 if (++completed === size) {2868 callback(null, compact(result));2869 }2870 };2871 }2872 };2873 }2874 /**2875 * @private2876 * @param {boolean} bool2877 */2878 function createFilterSeries(bool) {2879 return function(collection, iterator, callback) {2880 callback = onlyOnce(callback || noop);2881 var size, key, value, keys, iter, item, iterate;2882 var sync = false;2883 var completed = 0;2884 var result = [];2885 if (isArray(collection)) {2886 size = collection.length;2887 iterate = iterator.length === 3 ? arrayIteratorWithIndex : arrayIterator;2888 } else if (!collection) {2889 } else if (iteratorSymbol && collection[iteratorSymbol]) {2890 size = Infinity;2891 iter = collection[iteratorSymbol]();2892 iterate = iterator.length === 3 ? symbolIteratorWithKey : symbolIterator;2893 } else if (typeof collection === obj) {2894 keys = nativeKeys(collection);2895 size = keys.length;2896 iterate = iterator.length === 3 ? objectIteratorWithKey : objectIterator;2897 }2898 if (!size) {2899 return callback(null, []);2900 }2901 iterate();2902 function arrayIterator() {2903 value = collection[completed];2904 iterator(value, done);2905 }2906 function arrayIteratorWithIndex() {2907 value = collection[completed];2908 iterator(value, completed, done);2909 }2910 function symbolIterator() {2911 item = iter.next();2912 value = item.value;2913 item.done ? callback(null, result) : iterator(value, done);2914 }2915 function symbolIteratorWithKey() {2916 item = iter.next();2917 value = item.value;2918 item.done ? callback(null, result) : iterator(value, completed, done);2919 }2920 function objectIterator() {2921 key = keys[completed];2922 value = collection[key];2923 iterator(value, done);2924 }2925 function objectIteratorWithKey() {2926 key = keys[completed];2927 value = collection[key];2928 iterator(value, key, done);2929 }2930 function done(err, res) {2931 if (err) {2932 callback(err);2933 return;2934 }2935 if (!!res === bool) {2936 result[result.length] = value;2937 }2938 if (++completed === size) {2939 iterate = throwError;2940 callback(null, result);2941 } else if (sync) {2942 nextTick(iterate);2943 } else {2944 sync = true;2945 iterate();2946 }2947 sync = false;2948 }2949 };2950 }2951 /**2952 * @private2953 * @param {boolean} bool2954 */2955 function createFilterLimit(bool) {2956 return function(collection, limit, iterator, callback) {2957 callback = callback || noop;2958 var size, index, key, value, keys, iter, item, iterate, result;2959 var sync = false;2960 var started = 0;2961 var completed = 0;2962 if (isArray(collection)) {2963 size = collection.length;2964 iterate = iterator.length === 3 ? arrayIteratorWithIndex : arrayIterator;2965 } else if (!collection) {2966 } else if (iteratorSymbol && collection[iteratorSymbol]) {2967 size = Infinity;2968 result = [];2969 iter = collection[iteratorSymbol]();2970 iterate = iterator.length === 3 ? symbolIteratorWithKey : symbolIterator;2971 } else if (typeof collection === obj) {2972 keys = nativeKeys(collection);2973 size = keys.length;2974 iterate = iterator.length === 3 ? objectIteratorWithKey : objectIterator;2975 }2976 if (!size || isNaN(limit) || limit < 1) {2977 return callback(null, []);2978 }2979 result = result || Array(size);2980 timesSync(limit > size ? size : limit, iterate);2981 function arrayIterator() {2982 index = started++;2983 if (index < size) {2984 value = collection[index];2985 iterator(value, createCallback(value, index));2986 }2987 }2988 function arrayIteratorWithIndex() {2989 index = started++;2990 if (index < size) {2991 value = collection[index];2992 iterator(value, index, createCallback(value, index));2993 }2994 }2995 function symbolIterator() {2996 item = iter.next();2997 if (item.done === false) {2998 value = item.value;2999 iterator(value, createCallback(value, started++));3000 } else if (completed === started && iterator !== noop) {3001 iterator = noop;3002 callback(null, compact(result));3003 }3004 }3005 function symbolIteratorWithKey() {3006 item = iter.next();3007 if (item.done === false) {3008 value = item.value;3009 iterator(value, started, createCallback(value, started++));3010 } else if (completed === started && iterator !== noop) {3011 iterator = noop;3012 callback(null, compact(result));3013 }3014 }3015 function objectIterator() {3016 index = started++;3017 if (index < size) {3018 value = collection[keys[index]];3019 iterator(value, createCallback(value, index));3020 }3021 }3022 function objectIteratorWithKey() {3023 index = started++;3024 if (index < size) {3025 key = keys[index];3026 value = collection[key];3027 iterator(value, key, createCallback(value, index));3028 }3029 }3030 function createCallback(value, index) {3031 return function(err, res) {3032 if (index === null) {3033 throwError();3034 }3035 if (err) {3036 index = null;3037 iterate = noop;3038 callback = once(callback);3039 callback(err);3040 return;3041 }3042 if (!!res === bool) {3043 result[index] = value;3044 }3045 index = null;3046 if (++completed === size) {3047 callback = onlyOnce(callback);3048 callback(null, compact(result));3049 } else if (sync) {3050 nextTick(iterate);3051 } else {3052 sync = true;3053 iterate();3054 }3055 sync = false;3056 };3057 }3058 };3059 }3060 /**3061 * @memberof async3062 * @namespace eachSeries3063 * @param {Array|Object} collection3064 * @param {Function} iterator3065 * @param {Function} callback3066 * @example3067 *3068 * // array3069 * var order = [];3070 * var array = [1, 3, 2];3071 * var iterator = function(num, done) {3072 * setTimeout(function() {3073 * order.push(num);3074 * done();3075 * }, num * 10);3076 * };3077 * async.eachSeries(array, iterator, function(err, res) {3078 * console.log(res); // undefined3079 * console.log(order); // [1, 3, 2]3080 * });3081 *3082 * @example3083 *3084 * // array with index3085 * var order = [];3086 * var array = [1, 3, 2];3087 * var iterator = function(num, index, done) {3088 * setTimeout(function() {3089 * order.push([num, index]);3090 * done();3091 * }, num * 10);3092 * };3093 * async.eachSeries(array, iterator, function(err, res) {3094 * console.log(res); // undefined3095 * console.log(order); // [[1, 0], [3, 1], [2, 2]]3096 * });3097 *3098 * @example3099 *3100 * // object3101 * var order = [];3102 * var object = { a: 1, b: 3, c: 2 };3103 * var iterator = function(num, done) {3104 * setTimeout(function() {3105 * order.push(num);3106 * done();3107 * }, num * 10);3108 * };3109 * async.eachSeries(object, iterator, function(err, res) {3110 * console.log(res); // undefined3111 * console.log(order); // [1, 3, 2]3112 * });3113 *3114 * @example3115 *3116 * // object with key3117 * var order = [];3118 * var object = { a: 1, b: 3, c: 2 };3119 * var iterator = function(num, key, done) {3120 * setTimeout(function() {3121 * order.push([num, key]);3122 * done();3123 * }, num * 10);3124 * };3125 * async.eachSeries(object, iterator, function(err, res) {3126 * console.log(res); // undefined3127 * console.log(order); // [[1, 'a'], [3, 'b'], [2, 'b']]3128 * });3129 *3130 * @example3131 *3132 * // break3133 * var order = [];3134 * var array = [1, 3, 2];3135 * var iterator = function(num, done) {3136 * setTimeout(function() {3137 * order.push(num);3138 * done(null, num !== 3);3139 * }, num * 10);3140 * };3141 * async.eachSeries(array, iterator, function(err, res) {3142 * console.log(res); // undefined3143 * console.log(order); // [1, 3]3144 * });3145 */3146 function eachSeries(collection, iterator, callback) {3147 callback = onlyOnce(callback || noop);3148 var size, key, keys, iter, item, iterate;3149 var sync = false;3150 var completed = 0;3151 if (isArray(collection)) {3152 size = collection.length;3153 iterate = iterator.length === 3 ? arrayIteratorWithIndex : arrayIterator;3154 } else if (!collection) {3155 } else if (iteratorSymbol && collection[iteratorSymbol]) {3156 size = Infinity;3157 iter = collection[iteratorSymbol]();3158 iterate = iterator.length === 3 ? symbolIteratorWithKey : symbolIterator;3159 } else if (typeof collection === obj) {3160 keys = nativeKeys(collection);3161 size = keys.length;3162 iterate = iterator.length === 3 ? objectIteratorWithKey : objectIterator;3163 }3164 if (!size) {3165 return callback(null);3166 }3167 iterate();3168 function arrayIterator() {3169 iterator(collection[completed], done);3170 }3171 function arrayIteratorWithIndex() {3172 iterator(collection[completed], completed, done);3173 }3174 function symbolIterator() {3175 item = iter.next();3176 item.done ? callback(null) : iterator(item.value, done);3177 }3178 function symbolIteratorWithKey() {3179 item = iter.next();3180 item.done ? callback(null) : iterator(item.value, completed, done);3181 }3182 function objectIterator() {3183 iterator(collection[keys[completed]], done);3184 }3185 function objectIteratorWithKey() {3186 key = keys[completed];3187 iterator(collection[key], key, done);3188 }3189 function done(err, bool) {3190 if (err) {3191 callback(err);3192 } else if (++completed === size || bool === false) {3193 iterate = throwError;3194 callback(null);3195 } else if (sync) {3196 nextTick(iterate);3197 } else {3198 sync = true;3199 iterate();3200 }3201 sync = false;3202 }3203 }3204 /**3205 * @memberof async3206 * @namespace eachLimit3207 * @param {Array|Object} collection3208 * @param {number} limit - limit >= 13209 * @param {Function} iterator3210 * @param {Function} callback3211 * @example3212 *3213 * // array3214 * var order = [];3215 * var array = [1, 5, 3, 4, 2];3216 * var iterator = function(num, done) {3217 * setTimeout(function() {3218 * order.push(num);3219 * done();3220 * }, num * 10);3221 * };3222 * async.eachLimit(array, 2, iterator, function(err, res) {3223 * console.log(res); // undefined3224 * console.log(order); // [1, 3, 5, 2, 4]3225 * });3226 *3227 * @example3228 *3229 * // array with index3230 * var order = [];3231 * var array = [1, 5, 3, 4, 2];3232 * var iterator = function(num, index, done) {3233 * setTimeout(function() {3234 * order.push([num, index]);3235 * done();3236 * }, num * 10);3237 * };3238 * async.eachLimit(array, 2, iterator, function(err, res) {3239 * console.log(res); // undefined3240 * console.log(order); // [[1, 0], [3, 2], [5, 1], [2, 4], [4, 3]]3241 * });3242 *3243 * @example3244 *3245 * // object3246 * var order = [];3247 * var object = { a: 1, b: 5, c: 3, d: 4, e: 2 };3248 * var iterator = function(num, done) {3249 * setTimeout(function() {3250 * order.push(num);3251 * done();3252 * }, num * 10);3253 * };3254 * async.eachLimit(object, 2, iterator, function(err, res) {3255 * console.log(res); // undefined3256 * console.log(order); // [1, 3, 5, 2, 4]3257 * });3258 *3259 * @example3260 *3261 * // object with key3262 * var order = [];3263 * var object = { a: 1, b: 5, c: 3, d: 4, e: 2 };3264 * var iterator = function(num, key, done) {3265 * setTimeout(function() {3266 * order.push([num, key]);3267 * done();3268 * }, num * 10);3269 * };3270 * async.eachLimit(object, 2, iterator, function(err, res) {3271 * console.log(res); // undefined3272 * console.log(order); // [[1, 'a'], [3, 'c'], [5, 'b'], [2, 'e'], [4, 'd']]3273 * });3274 *3275 * @example3276 *3277 * // break3278 * var order = [];3279 * var array = [1, 5, 3, 4, 2];3280 * var iterator = function(num, done) {3281 * setTimeout(function() {3282 * order.push(num);3283 * done(null, num !== 5);3284 * }, num * 10);3285 * };3286 * async.eachLimit(array, 2, iterator, function(err, res) {3287 * console.log(res); // undefined3288 * console.log(order); // [1, 3, 5]3289 * });3290 *3291 */3292 function eachLimit(collection, limit, iterator, callback) {3293 callback = callback || noop;3294 var size, index, key, keys, iter, item, iterate;3295 var sync = false;3296 var started = 0;3297 var completed = 0;3298 if (isArray(collection)) {3299 size = collection.length;3300 iterate = iterator.length === 3 ? arrayIteratorWithIndex : arrayIterator;3301 } else if (!collection) {3302 } else if (iteratorSymbol && collection[iteratorSymbol]) {3303 size = Infinity;3304 iter = collection[iteratorSymbol]();3305 iterate = iterator.length === 3 ? symbolIteratorWithKey : symbolIterator;3306 } else if (typeof collection === obj) {3307 keys = nativeKeys(collection);3308 size = keys.length;3309 iterate = iterator.length === 3 ? objectIteratorWithKey : objectIterator;3310 } else {3311 return callback(null);3312 }3313 if (!size || isNaN(limit) || limit < 1) {3314 return callback(null);3315 }3316 timesSync(limit > size ? size : limit, iterate);3317 function arrayIterator() {3318 if (started < size) {3319 iterator(collection[started++], done);3320 }3321 }3322 function arrayIteratorWithIndex() {3323 index = started++;3324 if (index < size) {3325 iterator(collection[index], index, done);3326 }3327 }3328 function symbolIterator() {3329 item = iter.next();3330 if (item.done === false) {3331 started++;3332 iterator(item.value, done);3333 } else if (completed === started && iterator !== noop) {3334 iterator = noop;3335 callback(null);3336 }3337 }3338 function symbolIteratorWithKey() {3339 item = iter.next();3340 if (item.done === false) {3341 iterator(item.value, started++, done);3342 } else if (completed === started && iterator !== noop) {3343 iterator = noop;3344 callback(null);3345 }3346 }3347 function objectIterator() {3348 if (started < size) {3349 iterator(collection[keys[started++]], done);3350 }3351 }3352 function objectIteratorWithKey() {3353 index = started++;3354 if (index < size) {3355 key = keys[index];3356 iterator(collection[key], key, done);3357 }3358 }3359 function done(err, bool) {3360 if (err || bool === false) {3361 iterate = noop;3362 callback = once(callback);3363 callback(err);3364 } else if (++completed === size) {3365 iterator = noop;3366 iterate = throwError;3367 callback = onlyOnce(callback);3368 callback(null);3369 } else if (sync) {3370 nextTick(iterate);3371 } else {3372 sync = true;3373 iterate();3374 }3375 sync = false;3376 }3377 }3378 /**3379 * @memberof async3380 * @namespace mapSeries3381 * @param {Array|Object} collection3382 * @param {Function} iterator3383 * @param {Function} callback3384 * @example3385 *3386 * // array3387 * var order = [];3388 * var array = [1, 3, 2];3389 * var iterator = function(num, done) {3390 * setTimeout(function() {3391 * order.push(num);3392 * done(null, num);3393 * }, num * 10);3394 * };3395 * async.mapSeries(array, iterator, function(err, res) {3396 * console.log(res); // [1, 3, 2];3397 * console.log(order); // [1, 3, 2]3398 * });3399 *3400 * @example3401 *3402 * // array with index3403 * var order = [];3404 * var array = [1, 3, 2];3405 * var iterator = function(num, index, done) {3406 * setTimeout(function() {3407 * order.push([num, index]);3408 * done(null, num);3409 * }, num * 10);3410 * };3411 * async.mapSeries(array, iterator, function(err, res) {3412 * console.log(res); // [1, 3, 2]3413 * console.log(order); // [[1, 0], [3, 1], [2, 2]]3414 * });3415 *3416 * @example3417 *3418 * // object3419 * var order = [];3420 * var object = { a: 1, b: 3, c: 2 };3421 * var iterator = function(num, done) {3422 * setTimeout(function() {3423 * order.push(num);3424 * done(null, num);3425 * }, num * 10);3426 * };3427 * async.mapSeries(object, iterator, function(err, res) {3428 * console.log(res); // [1, 3, 2]3429 * console.log(order); // [1, 3, 2]3430 * });3431 *3432 * @example3433 *3434 * // object with key3435 * var order = [];3436 * var object = { a: 1, b: 3, c: 2 };3437 * var iterator = function(num, key, done) {3438 * setTimeout(function() {3439 * order.push([num, key]);3440 * done(null, num);3441 * }, num * 10);3442 * };3443 * async.mapSeries(object, iterator, function(err, res) {3444 * console.log(res); // [1, 3, 2]3445 * console.log(order); // [[1, 'a'], [3, 'b'], [2, 'c']]3446 * });3447 *3448 */3449 function mapSeries(collection, iterator, callback) {3450 callback = callback || noop;3451 var size, key, keys, iter, item, result, iterate;3452 var sync = false;3453 var completed = 0;3454 if (isArray(collection)) {3455 size = collection.length;3456 iterate = iterator.length === 3 ? arrayIteratorWithIndex : arrayIterator;3457 } else if (!collection) {3458 } else if (iteratorSymbol && collection[iteratorSymbol]) {3459 size = Infinity;3460 result = [];3461 iter = collection[iteratorSymbol]();3462 iterate = iterator.length === 3 ? symbolIteratorWithKey : symbolIterator;3463 } else if (typeof collection === obj) {3464 keys = nativeKeys(collection);3465 size = keys.length;3466 iterate = iterator.length === 3 ? objectIteratorWithKey : objectIterator;3467 }3468 if (!size) {3469 return callback(null, []);3470 }3471 result = result || Array(size);3472 iterate();3473 function arrayIterator() {3474 iterator(collection[completed], done);3475 }3476 function arrayIteratorWithIndex() {3477 iterator(collection[completed], completed, done);3478 }3479 function symbolIterator() {3480 item = iter.next();3481 item.done ? callback(null, result) : iterator(item.value, done);3482 }3483 function symbolIteratorWithKey() {3484 item = iter.next();3485 item.done ? callback(null, result) : iterator(item.value, completed, done);3486 }3487 function objectIterator() {3488 iterator(collection[keys[completed]], done);3489 }3490 function objectIteratorWithKey() {3491 key = keys[completed];3492 iterator(collection[key], key, done);3493 }3494 function done(err, res) {3495 if (err) {3496 iterate = throwError;3497 callback = onlyOnce(callback);3498 callback(err, createArray(result));3499 return;3500 }3501 result[completed] = res;3502 if (++completed === size) {3503 iterate = throwError;3504 callback(null, result);3505 callback = throwError;3506 } else if (sync) {3507 nextTick(iterate);3508 } else {3509 sync = true;3510 iterate();3511 }3512 sync = false;3513 }3514 }3515 /**3516 * @memberof async3517 * @namespace mapLimit3518 * @param {Array|Object} collection3519 * @param {number} limit - limit >= 13520 * @param {Function} iterator3521 * @param {Function} callback3522 * @example3523 *3524 * // array3525 * var order = [];3526 * var array = [1, 5, 3, 4, 2];3527 * var iterator = function(num, done) {3528 * setTimeout(function() {3529 * order.push(num);3530 * done(null, num);3531 * }, num * 10);3532 * };3533 * async.mapLimit(array, 2, iterator, function(err, res) {3534 * console.log(res); // [1, 5, 3, 4, 2]3535 * console.log(order); // [1, 3, 5, 2, 4]3536 * });3537 *3538 * @example3539 *3540 * // array with index3541 * var order = [];3542 * var array = [1, 5, 3, 4, 2];3543 * var iterator = function(num, index, done) {3544 * setTimeout(function() {3545 * order.push([num, index]);3546 * done(null, num);3547 * }, num * 10);3548 * };3549 * async.mapLimit(array, 2, iterator, function(err, res) {3550 * console.log(res); // [1, 5, 3, 4, 2]3551 * console.log(order); // [[1, 0], [3, 2], [5, 1], [2, 4], [4, 3]]3552 * });3553 *3554 * @example3555 *3556 * // object3557 * var order = [];3558 * var object = { a: 1, b: 5, c: 3, d: 4, e: 2 };3559 * var iterator = function(num, done) {3560 * setTimeout(function() {3561 * order.push(num);3562 * done(null, num);3563 * }, num * 10);3564 * };3565 * async.mapLimit(object, 2, iterator, function(err, res) {3566 * console.log(res); // [1, 5, 3, 4, 2]3567 * console.log(order); // [1, 3, 5, 2, 4]3568 * });3569 *3570 * @example3571 *3572 * // object with key3573 * var order = [];3574 * var object = { a: 1, b: 5, c: 3, d: 4, e: 2 };3575 * var iterator = function(num, key, done) {3576 * setTimeout(function() {3577 * order.push([num, key]);3578 * done(null, num);3579 * }, num * 10);3580 * };3581 * async.mapLimit(object, 2, iterator, function(err, res) {3582 * console.log(res); // [1, 5, 3, 4, 2]3583 * console.log(order); // [[1, 'a'], [3, 'c'], [5, 'b'], [2, 'e'], [4, 'd']]3584 * });3585 *3586 */3587 function mapLimit(collection, limit, iterator, callback) {3588 callback = callback || noop;3589 var size, index, key, keys, iter, item, result, iterate;3590 var sync = false;3591 var started = 0;3592 var completed = 0;3593 if (isArray(collection)) {3594 size = collection.length;3595 iterate = iterator.length === 3 ? arrayIteratorWithIndex : arrayIterator;3596 } else if (!collection) {3597 } else if (iteratorSymbol && collection[iteratorSymbol]) {3598 size = Infinity;3599 result = [];3600 iter = collection[iteratorSymbol]();3601 iterate = iterator.length === 3 ? symbolIteratorWithKey : symbolIterator;3602 } else if (typeof collection === obj) {3603 keys = nativeKeys(collection);3604 size = keys.length;3605 iterate = iterator.length === 3 ? objectIteratorWithKey : objectIterator;3606 }3607 if (!size || isNaN(limit) || limit < 1) {3608 return callback(null, []);3609 }3610 result = result || Array(size);3611 timesSync(limit > size ? size : limit, iterate);3612 function arrayIterator() {3613 index = started++;3614 if (index < size) {3615 iterator(collection[index], createCallback(index));3616 }3617 }3618 function arrayIteratorWithIndex() {3619 index = started++;3620 if (index < size) {3621 iterator(collection[index], index, createCallback(index));3622 }3623 }3624 function symbolIterator() {3625 item = iter.next();3626 if (item.done === false) {3627 iterator(item.value, createCallback(started++));3628 } else if (completed === started && iterator !== noop) {3629 iterator = noop;3630 callback(null, result);3631 }3632 }3633 function symbolIteratorWithKey() {3634 item = iter.next();3635 if (item.done === false) {3636 iterator(item.value, started, createCallback(started++));3637 } else if (completed === started && iterator !== noop) {3638 iterator = noop;3639 callback(null, result);3640 }3641 }3642 function objectIterator() {3643 index = started++;3644 if (index < size) {3645 iterator(collection[keys[index]], createCallback(index));3646 }3647 }3648 function objectIteratorWithKey() {3649 index = started++;3650 if (index < size) {3651 key = keys[index];3652 iterator(collection[key], key, createCallback(index));3653 }3654 }3655 function createCallback(index) {3656 return function(err, res) {3657 if (index === null) {3658 throwError();3659 }3660 if (err) {3661 index = null;3662 iterate = noop;3663 callback = once(callback);3664 callback(err, createArray(result));3665 return;3666 }3667 result[index] = res;3668 index = null;3669 if (++completed === size) {3670 iterate = throwError;3671 callback(null, result);3672 callback = throwError;3673 } else if (sync) {3674 nextTick(iterate);3675 } else {3676 sync = true;3677 iterate();3678 }3679 sync = false;3680 };3681 }3682 }3683 /**3684 * @memberof async3685 * @namespace mapValuesSeries3686 * @param {Array|Object} collection3687 * @param {Function} iterator3688 * @param {Function} callback3689 * @example3690 *3691 * // array3692 * var order = [];3693 * var array = [1, 3, 2];3694 * var iterator = function(num, done) {3695 * setTimeout(function() {3696 * order.push(num);3697 * done(null, num);3698 * }, num * 10);3699 * };3700 * async.mapValuesSeries(array, iterator, function(err, res) {3701 * console.log(res); // { '0': 1, '1': 3, '2': 2 }3702 * console.log(order); // [1, 3, 2]3703 * });3704 *3705 * @example3706 *3707 * // array with index3708 * var order = [];3709 * var array = [1, 3, 2];3710 * var iterator = function(num, index, done) {3711 * setTimeout(function() {3712 * order.push([num, index]);3713 * done(null, num);3714 * }, num * 10);3715 * };3716 * async.mapValuesSeries(array, iterator, function(err, res) {3717 * console.log(res); // { '0': 1, '1': 3, '2': 2 }3718 * console.log(order); // [[1, 0], [3, 1], [2, 2]]3719 * });3720 *3721 * @example3722 *3723 * // object3724 * var order = [];3725 * var object = { a: 1, b: 3, c: 2 };3726 * var iterator = function(num, done) {3727 * setTimeout(function() {3728 * order.push(num);3729 * done(null, num);3730 * }, num * 10);3731 * };3732 * async.mapValuesSeries(object, iterator, function(err, res) {3733 * console.log(res); // { a: 1, b: 3, c: 2 }3734 * console.log(order); // [1, 3, 2]3735 * });3736 *3737 * @example3738 *3739 * // object with key3740 * var order = [];3741 * var object = { a: 1, b: 3, c: 2 };3742 * var iterator = function(num, key, done) {3743 * setTimeout(function() {3744 * order.push([num, key]);3745 * done(null, num);3746 * }, num * 10);3747 * };3748 * async.mapValuesSeries(object, iterator, function(err, res) {3749 * console.log(res); // { a: 1, b: 3, c: 2 }3750 * console.log(order); // [[1, 'a'], [3, 'b'], [2, 'c']]3751 * });3752 *3753 */3754 function mapValuesSeries(collection, iterator, callback) {3755 callback = callback || noop;3756 var size, key, keys, iter, item, iterate;3757 var sync = false;3758 var result = {};3759 var completed = 0;3760 if (isArray(collection)) {3761 size = collection.length;3762 iterate = iterator.length === 3 ? arrayIteratorWithIndex : arrayIterator;3763 } else if (!collection) {3764 } else if (iteratorSymbol && collection[iteratorSymbol]) {3765 size = Infinity;3766 iter = collection[iteratorSymbol]();3767 iterate = iterator.length === 3 ? symbolIteratorWithKey : symbolIterator;3768 } else if (typeof collection === obj) {3769 keys = nativeKeys(collection);3770 size = keys.length;3771 iterate = iterator.length === 3 ? objectIteratorWithKey : objectIterator;3772 }3773 if (!size) {3774 return callback(null, result);3775 }3776 iterate();3777 function arrayIterator() {3778 key = completed;3779 iterator(collection[completed], done);3780 }3781 function arrayIteratorWithIndex() {3782 key = completed;3783 iterator(collection[completed], completed, done);3784 }3785 function symbolIterator() {3786 key = completed;3787 item = iter.next();3788 item.done ? callback(null, result) : iterator(item.value, done);3789 }3790 function symbolIteratorWithKey() {3791 key = completed;3792 item = iter.next();3793 item.done ? callback(null, result) : iterator(item.value, completed, done);3794 }3795 function objectIterator() {3796 key = keys[completed];3797 iterator(collection[key], done);3798 }3799 function objectIteratorWithKey() {3800 key = keys[completed];3801 iterator(collection[key], key, done);3802 }3803 function done(err, res) {3804 if (err) {3805 iterate = throwError;3806 callback = onlyOnce(callback);3807 callback(err, objectClone(result));3808 return;3809 }3810 result[key] = res;3811 if (++completed === size) {3812 iterate = throwError;3813 callback(null, result);3814 callback = throwError;3815 } else if (sync) {3816 nextTick(iterate);3817 } else {3818 sync = true;3819 iterate();3820 }3821 sync = false;3822 }3823 }3824 /**3825 * @memberof async3826 * @namespace mapValuesLimit3827 * @param {Array|Object} collection3828 * @param {number} limit - limit >= 13829 * @param {Function} iterator3830 * @param {Function} callback3831 * @example3832 *3833 * // array3834 * var order = [];3835 * var array = [1, 5, 3, 4, 2];3836 * var iterator = function(num, done) {3837 * setTimeout(function() {3838 * order.push(num);3839 * done(null, num);3840 * }, num * 10);3841 * };3842 * async.mapValuesLimit(array, 2, iterator, function(err, res) {3843 * console.log(res); // { '0': 1, '1': 5, '2': 3, '3': 4, '4': 2 }3844 * console.log(order); // [1, 3, 5, 2, 4]3845 * });3846 *3847 * @example3848 *3849 * // array with index3850 * var order = [];3851 * var array = [1, 5, 3, 4, 2];3852 * var iterator = function(num, index, done) {3853 * setTimeout(function() {3854 * order.push([num, index]);3855 * done(null, num);3856 * }, num * 10);3857 * };3858 * async.mapValuesLimit(array, 2, iterator, function(err, res) {3859 * console.log(res); // { '0': 1, '1': 5, '2': 3, '3': 4, '4': 2 }3860 * console.log(order); // [[1, 0], [3, 2], [5, 1], [2, 4], [4, 3]]3861 * });3862 *3863 * @example3864 *3865 * // object3866 * var order = [];3867 * var object = { a: 1, b: 5, c: 3, d: 4, e: 2 };3868 * var iterator = function(num, done) {3869 * setTimeout(function() {3870 * order.push(num);3871 * done(null, num);3872 * }, num * 10);3873 * };3874 * async.mapValuesLimit(object, 2, iterator, function(err, res) {3875 * console.log(res); // { a: 1, b: 5, c: 3, d: 4, e: 2 }3876 * console.log(order); // [1, 3, 5, 2, 4]3877 * });3878 *3879 * @example3880 *3881 * // object with key3882 * var order = [];3883 * var object = { a: 1, b: 5, c: 3, d: 4, e: 2 };3884 * var iterator = function(num, key, done) {3885 * setTimeout(function() {3886 * order.push([num, key]);3887 * done(null, num);3888 * }, num * 10);3889 * };3890 * async.mapValuesLimit(object, 2, iterator, function(err, res) {3891 * console.log(res); // { a: 1, b: 5, c: 3, d: 4, e: 2 }3892 * console.log(order); // [[1, 'a'], [3, 'c'], [5, 'b'], [2, 'e'], [4, 'd']]3893 * });3894 *3895 */3896 function mapValuesLimit(collection, limit, iterator, callback) {3897 callback = callback || noop;3898 var size, index, key, keys, iter, item, iterate;3899 var sync = false;3900 var result = {};3901 var started = 0;3902 var completed = 0;3903 if (isArray(collection)) {3904 size = collection.length;3905 iterate = iterator.length === 3 ? arrayIteratorWithIndex : arrayIterator;3906 } else if (!collection) {3907 } else if (iteratorSymbol && collection[iteratorSymbol]) {3908 size = Infinity;3909 iter = collection[iteratorSymbol]();3910 iterate = iterator.length === 3 ? symbolIteratorWithKey : symbolIterator;3911 } else if (typeof collection === obj) {3912 keys = nativeKeys(collection);3913 size = keys.length;3914 iterate = iterator.length === 3 ? objectIteratorWithKey : objectIterator;3915 }3916 if (!size || isNaN(limit) || limit < 1) {3917 return callback(null, result);3918 }3919 timesSync(limit > size ? size : limit, iterate);3920 function arrayIterator() {3921 index = started++;3922 if (index < size) {3923 iterator(collection[index], createCallback(index));3924 }3925 }3926 function arrayIteratorWithIndex() {3927 index = started++;3928 if (index < size) {3929 iterator(collection[index], index, createCallback(index));3930 }3931 }3932 function symbolIterator() {3933 item = iter.next();3934 if (item.done === false) {3935 iterator(item.value, createCallback(started++));3936 } else if (completed === started && iterator !== noop) {3937 iterator = noop;3938 callback(null, result);3939 }3940 }3941 function symbolIteratorWithKey() {3942 item = iter.next();3943 if (item.done === false) {3944 iterator(item.value, started, createCallback(started++));3945 } else if (completed === started && iterator !== noop) {3946 iterator = noop;3947 callback(null, result);3948 }3949 }3950 function objectIterator() {3951 index = started++;3952 if (index < size) {3953 key = keys[index];3954 iterator(collection[key], createCallback(key));3955 }3956 }3957 function objectIteratorWithKey() {3958 index = started++;3959 if (index < size) {3960 key = keys[index];3961 iterator(collection[key], key, createCallback(key));3962 }3963 }3964 function createCallback(key) {3965 return function(err, res) {3966 if (key === null) {3967 throwError();3968 }3969 if (err) {3970 key = null;3971 iterate = noop;3972 callback = once(callback);3973 callback(err, objectClone(result));3974 return;3975 }3976 result[key] = res;3977 key = null;3978 if (++completed === size) {3979 callback(null, result);3980 } else if (sync) {3981 nextTick(iterate);3982 } else {3983 sync = true;3984 iterate();3985 }3986 sync = false;3987 };3988 }3989 }3990 /**3991 * @private3992 * @param {Function} arrayEach3993 * @param {Function} baseEach3994 * @param {Function} symbolEach3995 * @param {boolean} bool3996 */3997 function createDetect(arrayEach, baseEach, symbolEach, bool) {3998 return function(collection, iterator, callback) {3999 callback = callback || noop;4000 var size, keys;4001 var completed = 0;4002 if (isArray(collection)) {4003 size = collection.length;4004 arrayEach(collection, iterator, createCallback);4005 } else if (!collection) {4006 } else if (iteratorSymbol && collection[iteratorSymbol]) {4007 size = symbolEach(collection, iterator, createCallback);4008 size && size === completed && callback(null);4009 } else if (typeof collection === obj) {4010 keys = nativeKeys(collection);4011 size = keys.length;4012 baseEach(collection, iterator, createCallback, keys);4013 }4014 if (!size) {4015 callback(null);4016 }4017 function createCallback(value) {4018 var called = false;4019 return function done(err, res) {4020 if (called) {4021 throwError();4022 }4023 called = true;4024 if (err) {4025 callback = once(callback);4026 callback(err);4027 } else if (!!res === bool) {4028 callback = once(callback);4029 callback(null, value);4030 } else if (++completed === size) {4031 callback(null);4032 }4033 };4034 }4035 };4036 }4037 /**4038 * @private4039 * @param {boolean} bool4040 */4041 function createDetectSeries(bool) {4042 return function(collection, iterator, callback) {4043 callback = onlyOnce(callback || noop);4044 var size, key, value, keys, iter, item, iterate;4045 var sync = false;4046 var completed = 0;4047 if (isArray(collection)) {4048 size = collection.length;4049 iterate = iterator.length === 3 ? arrayIteratorWithIndex : arrayIterator;4050 } else if (!collection) {4051 } else if (iteratorSymbol && collection[iteratorSymbol]) {4052 size = Infinity;4053 iter = collection[iteratorSymbol]();4054 iterate = iterator.length === 3 ? symbolIteratorWithKey : symbolIterator;4055 } else if (typeof collection === obj) {4056 keys = nativeKeys(collection);4057 size = keys.length;4058 iterate = iterator.length === 3 ? objectIteratorWithKey : objectIterator;4059 }4060 if (!size) {4061 return callback(null);4062 }4063 iterate();4064 function arrayIterator() {4065 value = collection[completed];4066 iterator(value, done);4067 }4068 function arrayIteratorWithIndex() {4069 value = collection[completed];4070 iterator(value, completed, done);4071 }4072 function symbolIterator() {4073 item = iter.next();4074 value = item.value;4075 item.done ? callback(null) : iterator(value, done);4076 }4077 function symbolIteratorWithKey() {4078 item = iter.next();4079 value = item.value;4080 item.done ? callback(null) : iterator(value, completed, done);4081 }4082 function objectIterator() {4083 value = collection[keys[completed]];4084 iterator(value, done);4085 }4086 function objectIteratorWithKey() {4087 key = keys[completed];4088 value = collection[key];4089 iterator(value, key, done);4090 }4091 function done(err, res) {4092 if (err) {4093 callback(err);4094 } else if (!!res === bool) {4095 iterate = throwError;4096 callback(null, value);4097 } else if (++completed === size) {4098 iterate = throwError;4099 callback(null);4100 } else if (sync) {4101 nextTick(iterate);4102 } else {4103 sync = true;4104 iterate();4105 }4106 sync = false;4107 }4108 };4109 }4110 /**4111 * @private4112 * @param {boolean} bool4113 */4114 function createDetectLimit(bool) {4115 return function(collection, limit, iterator, callback) {4116 callback = callback || noop;4117 var size, index, key, value, keys, iter, item, iterate;4118 var sync = false;4119 var started = 0;4120 var completed = 0;4121 if (isArray(collection)) {4122 size = collection.length;4123 iterate = iterator.length === 3 ? arrayIteratorWithIndex : arrayIterator;4124 } else if (!collection) {4125 } else if (iteratorSymbol && collection[iteratorSymbol]) {4126 size = Infinity;4127 iter = collection[iteratorSymbol]();4128 iterate = iterator.length === 3 ? symbolIteratorWithKey : symbolIterator;4129 } else if (typeof collection === obj) {4130 keys = nativeKeys(collection);4131 size = keys.length;4132 iterate = iterator.length === 3 ? objectIteratorWithKey : objectIterator;4133 }4134 if (!size || isNaN(limit) || limit < 1) {4135 return callback(null);4136 }4137 timesSync(limit > size ? size : limit, iterate);4138 function arrayIterator() {4139 index = started++;4140 if (index < size) {4141 value = collection[index];4142 iterator(value, createCallback(value));4143 }4144 }4145 function arrayIteratorWithIndex() {4146 index = started++;4147 if (index < size) {4148 value = collection[index];4149 iterator(value, index, createCallback(value));4150 }4151 }4152 function symbolIterator() {4153 item = iter.next();4154 if (item.done === false) {4155 started++;4156 value = item.value;4157 iterator(value, createCallback(value));4158 } else if (completed === started && iterator !== noop) {4159 iterator = noop;4160 callback(null);4161 }4162 }4163 function symbolIteratorWithKey() {4164 item = iter.next();4165 if (item.done === false) {4166 value = item.value;4167 iterator(value, started++, createCallback(value));4168 } else if (completed === started && iterator !== noop) {4169 iterator = noop;4170 callback(null);4171 }4172 }4173 function objectIterator() {4174 index = started++;4175 if (index < size) {4176 value = collection[keys[index]];4177 iterator(value, createCallback(value));4178 }4179 }4180 function objectIteratorWithKey() {4181 if (started < size) {4182 key = keys[started++];4183 value = collection[key];4184 iterator(value, key, createCallback(value));4185 }4186 }4187 function createCallback(value) {4188 var called = false;4189 return function(err, res) {4190 if (called) {4191 throwError();4192 }4193 called = true;4194 if (err) {4195 iterate = noop;4196 callback = once(callback);4197 callback(err);4198 } else if (!!res === bool) {4199 iterate = noop;4200 callback = once(callback);4201 callback(null, value);4202 } else if (++completed === size) {4203 callback(null);4204 } else if (sync) {4205 nextTick(iterate);4206 } else {4207 sync = true;4208 iterate();4209 }4210 sync = false;4211 };4212 }4213 };4214 }4215 /**4216 * @private4217 * @param {Function} arrayEach4218 * @param {Function} baseEach4219 * @param {Function} symbolEach4220 * @param {boolean} bool4221 */4222 function createPick(arrayEach, baseEach, symbolEach, bool) {4223 return function(collection, iterator, callback) {4224 callback = callback || noop;4225 var size, keys;4226 var completed = 0;4227 var result = {};4228 if (isArray(collection)) {4229 size = collection.length;4230 arrayEach(collection, iterator, createCallback);4231 } else if (!collection) {4232 } else if (iteratorSymbol && collection[iteratorSymbol]) {4233 size = symbolEach(collection, iterator, createCallback);4234 size && size === completed && callback(null, result);4235 } else if (typeof collection === obj) {4236 keys = nativeKeys(collection);4237 size = keys.length;4238 baseEach(collection, iterator, createCallback, keys);4239 }4240 if (!size) {4241 return callback(null, {});4242 }4243 function createCallback(key, value) {4244 return function done(err, res) {4245 if (key === null) {4246 throwError();4247 }4248 if (err) {4249 key = null;4250 callback = once(callback);4251 callback(err, objectClone(result));4252 return;4253 }4254 if (!!res === bool) {4255 result[key] = value;4256 }4257 key = null;4258 if (++completed === size) {4259 callback(null, result);4260 }4261 };4262 }4263 };4264 }4265 /**4266 * @private4267 * @param {boolean} bool4268 */4269 function createPickSeries(bool) {4270 return function(collection, iterator, callback) {4271 callback = onlyOnce(callback || noop);4272 var size, key, value, keys, iter, item, iterate;4273 var sync = false;4274 var result = {};4275 var completed = 0;4276 if (isArray(collection)) {4277 size = collection.length;4278 iterate = iterator.length === 3 ? arrayIteratorWithIndex : arrayIterator;4279 } else if (!collection) {4280 } else if (iteratorSymbol && collection[iteratorSymbol]) {4281 size = Infinity;4282 iter = collection[iteratorSymbol]();4283 iterate = iterator.length === 3 ? symbolIteratorWithKey : symbolIterator;4284 } else if (typeof collection === obj) {4285 keys = nativeKeys(collection);4286 size = keys.length;4287 iterate = iterator.length === 3 ? objectIteratorWithKey : objectIterator;4288 }4289 if (!size) {4290 return callback(null, {});4291 }4292 iterate();4293 function arrayIterator() {4294 key = completed;4295 value = collection[completed];4296 iterator(value, done);4297 }4298 function arrayIteratorWithIndex() {4299 key = completed;4300 value = collection[completed];4301 iterator(value, completed, done);4302 }4303 function symbolIterator() {4304 key = completed;4305 item = iter.next();4306 value = item.value;4307 item.done ? callback(null, result) : iterator(value, done);4308 }4309 function symbolIteratorWithKey() {4310 key = completed;4311 item = iter.next();4312 value = item.value;4313 item.done ? callback(null, result) : iterator(value, key, done);4314 }4315 function objectIterator() {4316 key = keys[completed];4317 value = collection[key];4318 iterator(value, done);4319 }4320 function objectIteratorWithKey() {4321 key = keys[completed];4322 value = collection[key];4323 iterator(value, key, done);4324 }4325 function done(err, res) {4326 if (err) {4327 callback(err, result);4328 return;4329 }4330 if (!!res === bool) {4331 result[key] = value;4332 }4333 if (++completed === size) {4334 iterate = throwError;4335 callback(null, result);4336 } else if (sync) {4337 nextTick(iterate);4338 } else {4339 sync = true;4340 iterate();4341 }4342 sync = false;4343 }4344 };4345 }4346 /**4347 * @private4348 * @param {boolean} bool4349 */4350 function createPickLimit(bool) {4351 return function(collection, limit, iterator, callback) {4352 callback = callback || noop;4353 var size, index, key, value, keys, iter, item, iterate;4354 var sync = false;4355 var result = {};4356 var started = 0;4357 var completed = 0;4358 if (isArray(collection)) {4359 size = collection.length;4360 iterate = iterator.length === 3 ? arrayIteratorWithIndex : arrayIterator;4361 } else if (!collection) {4362 } else if (iteratorSymbol && collection[iteratorSymbol]) {4363 size = Infinity;4364 iter = collection[iteratorSymbol]();4365 iterate = iterator.length === 3 ? symbolIteratorWithKey : symbolIterator;4366 } else if (typeof collection === obj) {4367 keys = nativeKeys(collection);4368 size = keys.length;4369 iterate = iterator.length === 3 ? objectIteratorWithKey : objectIterator;4370 }4371 if (!size || isNaN(limit) || limit < 1) {4372 return callback(null, {});4373 }4374 timesSync(limit > size ? size : limit, iterate);4375 function arrayIterator() {4376 index = started++;4377 if (index < size) {4378 value = collection[index];4379 iterator(value, createCallback(value, index));4380 }4381 }4382 function arrayIteratorWithIndex() {4383 index = started++;4384 if (index < size) {4385 value = collection[index];4386 iterator(value, index, createCallback(value, index));4387 }4388 }4389 function symbolIterator() {4390 item = iter.next();4391 if (item.done === false) {4392 value = item.value;4393 iterator(value, createCallback(value, started++));4394 } else if (completed === started && iterator !== noop) {4395 iterator = noop;4396 callback(null, result);4397 }4398 }4399 function symbolIteratorWithKey() {4400 item = iter.next();4401 if (item.done === false) {4402 value = item.value;4403 iterator(value, started, createCallback(value, started++));4404 } else if (completed === started && iterator !== noop) {4405 iterator = noop;4406 callback(null, result);4407 }4408 }4409 function objectIterator() {4410 if (started < size) {4411 key = keys[started++];4412 value = collection[key];4413 iterator(value, createCallback(value, key));4414 }4415 }4416 function objectIteratorWithKey() {4417 if (started < size) {4418 key = keys[started++];4419 value = collection[key];4420 iterator(value, key, createCallback(value, key));4421 }4422 }4423 function createCallback(value, key) {4424 return function(err, res) {4425 if (key === null) {4426 throwError();4427 }4428 if (err) {4429 key = null;4430 iterate = noop;4431 callback = once(callback);4432 callback(err, objectClone(result));4433 return;4434 }4435 if (!!res === bool) {4436 result[key] = value;4437 }4438 key = null;4439 if (++completed === size) {4440 iterate = throwError;4441 callback = onlyOnce(callback);4442 callback(null, result);4443 } else if (sync) {4444 nextTick(iterate);4445 } else {4446 sync = true;4447 iterate();4448 }4449 sync = false;4450 };4451 }4452 };4453 }4454 /**4455 * @memberof async4456 * @namespace reduce4457 * @param {Array|Object} collection4458 * @param {*} result4459 * @param {Function} iterator4460 * @param {Function} callback4461 * @example4462 *4463 * // array4464 * var order = [];4465 * var collection = [1, 3, 2, 4];4466 * var iterator = function(result, num, done) {4467 * setTimeout(function() {4468 * order.push(num);4469 * done(null, result + num);4470 * }, num * 10);4471 * };4472 * async.reduce(collection, 0, iterator, function(err, res) {4473 * console.log(res); // 104474 * console.log(order); // [1, 3, 2, 4]4475 * });4476 *4477 * @example4478 *4479 * // array with index4480 * var order = [];4481 * var collection = [1, 3, 2, 4];4482 * var iterator = function(result, num, index, done) {4483 * setTimeout(function() {4484 * order.push([num, index]);4485 * done(null, result + num);4486 * }, num * 10);4487 * };4488 * async.reduce(collection, '', iterator, function(err, res) {4489 * console.log(res); // '1324'4490 * console.log(order); // [[1, 0], [3, 1], [2, 2], [4, 3]]4491 * });4492 *4493 * @example4494 *4495 * // object4496 * var order = [];4497 * var object = { a: 1, b: 3, c: 2, d: 4 };4498 * var iterator = function(result, num, done) {4499 * setTimeout(function() {4500 * order.push(num);4501 * done(null, result + num);4502 * }, num * 10);4503 * };4504 * async.reduce(collection, '', iterator, function(err, res) {4505 * console.log(res); // '1324'4506 * console.log(order); // [1, 3, 2, 4]4507 * });4508 *4509 * @example4510 *4511 * // object with key4512 * var order = [];4513 * var object = { a: 1, b: 3, c: 2, d: 4 };4514 * var iterator = function(result, num, key, done) {4515 * setTimeout(function() {4516 * order.push([num, key]);4517 * done(null, result + num);4518 * }, num * 10);4519 * };4520 * async.reduce(collection, 0, iterator, function(err, res) {4521 * console.log(res); // 104522 * console.log(order); // [[1, 'a'], [3, 'b'], [2, 'b'], [4, 'd']]4523 * });4524 *4525 */4526 function reduce(collection, result, iterator, callback) {4527 callback = onlyOnce(callback || noop);4528 var size, key, keys, iter, item, iterate;4529 var sync = false;4530 var completed = 0;4531 if (isArray(collection)) {4532 size = collection.length;4533 iterate = iterator.length === 4 ? arrayIteratorWithIndex : arrayIterator;4534 } else if (!collection) {4535 } else if (iteratorSymbol && collection[iteratorSymbol]) {4536 size = Infinity;4537 iter = collection[iteratorSymbol]();4538 iterate = iterator.length === 4 ? symbolIteratorWithKey : symbolIterator;4539 } else if (typeof collection === obj) {4540 keys = nativeKeys(collection);4541 size = keys.length;4542 iterate = iterator.length === 4 ? objectIteratorWithKey : objectIterator;4543 }4544 if (!size) {4545 return callback(null, result);4546 }4547 iterate(result);4548 function arrayIterator(result) {4549 iterator(result, collection[completed], done);4550 }4551 function arrayIteratorWithIndex(result) {4552 iterator(result, collection[completed], completed, done);4553 }4554 function symbolIterator(result) {4555 item = iter.next();4556 item.done ? callback(null, result) : iterator(result, item.value, done);4557 }4558 function symbolIteratorWithKey(result) {4559 item = iter.next();4560 item.done ? callback(null, result) : iterator(result, item.value, completed, done);4561 }4562 function objectIterator(result) {4563 iterator(result, collection[keys[completed]], done);4564 }4565 function objectIteratorWithKey(result) {4566 key = keys[completed];4567 iterator(result, collection[key], key, done);4568 }4569 function done(err, result) {4570 if (err) {4571 callback(err, result);4572 } else if (++completed === size) {4573 iterator = throwError;4574 callback(null, result);4575 } else if (sync) {4576 nextTick(function() {4577 iterate(result);4578 });4579 } else {4580 sync = true;4581 iterate(result);4582 }4583 sync = false;4584 }4585 }4586 /**4587 * @memberof async4588 * @namespace reduceRight4589 * @param {Array|Object} collection4590 * @param {*} result4591 * @param {Function} iterator4592 * @param {Function} callback4593 * @example4594 *4595 * // array4596 * var order = [];4597 * var collection = [1, 3, 2, 4];4598 * var iterator = function(result, num, done) {4599 * setTimeout(function() {4600 * order.push(num);4601 * done(null, result + num);4602 * }, num * 10);4603 * };4604 * async.reduceRight(collection, 0, iterator, function(err, res) {4605 * console.log(res); // 104606 * console.log(order); // [4, 2, 3, 1]4607 * });4608 *4609 * @example4610 *4611 * // array with index4612 * var order = [];4613 * var collection = [1, 3, 2, 4];4614 * var iterator = function(result, num, index, done) {4615 * setTimeout(function() {4616 * order.push([num, index]);4617 * done(null, result + num);4618 * }, num * 10);4619 * };4620 * async.reduceRight(collection, '', iterator, function(err, res) {4621 * console.log(res); // '4231'4622 * console.log(order); // [[4, 3], [2, 2], [3, 1], [1, 0]]4623 * });4624 *4625 * @example4626 *4627 * // object4628 * var order = [];4629 * var object = { a: 1, b: 3, c: 2, d: 4 };4630 * var iterator = function(result, num, done) {4631 * setTimeout(function() {4632 * order.push(num);4633 * done(null, result + num);4634 * }, num * 10);4635 * };4636 * async.reduceRight(collection, '', iterator, function(err, res) {4637 * console.log(res); // '4231'4638 * console.log(order); // [4, 2, 3, 1]4639 * });4640 *4641 * @example4642 *4643 * // object with key4644 * var order = [];4645 * var object = { a: 1, b: 3, c: 2, d: 4 };4646 * var iterator = function(result, num, key, done) {4647 * setTimeout(function() {4648 * order.push([num, key]);4649 * done(null, result + num);4650 * }, num * 10);4651 * };4652 * async.reduceRight(collection, 0, iterator, function(err, res) {4653 * console.log(res); // 104654 * console.log(order); // [[4, 3], [2, 2], [3, 1], [1, 0]]4655 * });4656 *4657 */4658 function reduceRight(collection, result, iterator, callback) {4659 callback = onlyOnce(callback || noop);4660 var resIndex, index, key, keys, iter, item, col, iterate;4661 var sync = false;4662 if (isArray(collection)) {4663 resIndex = collection.length;4664 iterate = iterator.length === 4 ? arrayIteratorWithIndex : arrayIterator;4665 } else if (!collection) {4666 } else if (iteratorSymbol && collection[iteratorSymbol]) {4667 col = [];4668 iter = collection[iteratorSymbol]();4669 index = -1;4670 while ((item = iter.next()).done === false) {4671 col[++index] = item.value;4672 }4673 collection = col;4674 resIndex = col.length;4675 iterate = iterator.length === 4 ? arrayIteratorWithIndex : arrayIterator;4676 } else if (typeof collection === obj) {4677 keys = nativeKeys(collection);4678 resIndex = keys.length;4679 iterate = iterator.length === 4 ? objectIteratorWithKey : objectIterator;4680 }4681 if (!resIndex) {4682 return callback(null, result);4683 }4684 iterate(result);4685 function arrayIterator(result) {4686 iterator(result, collection[--resIndex], done);4687 }4688 function arrayIteratorWithIndex(result) {4689 iterator(result, collection[--resIndex], resIndex, done);4690 }4691 function objectIterator(result) {4692 iterator(result, collection[keys[--resIndex]], done);4693 }4694 function objectIteratorWithKey(result) {4695 key = keys[--resIndex];4696 iterator(result, collection[key], key, done);4697 }4698 function done(err, result) {4699 if (err) {4700 callback(err, result);4701 } else if (resIndex === 0) {4702 iterate = throwError;4703 callback(null, result);4704 } else if (sync) {4705 nextTick(function() {4706 iterate(result);4707 });4708 } else {4709 sync = true;4710 iterate(result);4711 }4712 sync = false;4713 }4714 }4715 /**4716 * @private4717 * @param {Function} arrayEach4718 * @param {Function} baseEach4719 * @param {Function} symbolEach4720 */4721 function createTransform(arrayEach, baseEach, symbolEach) {4722 return function transform(collection, accumulator, iterator, callback) {4723 if (arguments.length === 3) {4724 callback = iterator;4725 iterator = accumulator;4726 accumulator = undefined;4727 }4728 callback = callback || noop;4729 var size, keys, result;4730 var completed = 0;4731 if (isArray(collection)) {4732 size = collection.length;4733 result = accumulator !== undefined ? accumulator : [];4734 arrayEach(collection, result, iterator, done);4735 } else if (!collection) {4736 } else if (iteratorSymbol && collection[iteratorSymbol]) {4737 result = accumulator !== undefined ? accumulator : {};4738 size = symbolEach(collection, result, iterator, done);4739 size && size === completed && callback(null, result);4740 } else if (typeof collection === obj) {4741 keys = nativeKeys(collection);4742 size = keys.length;4743 result = accumulator !== undefined ? accumulator : {};4744 baseEach(collection, result, iterator, done, keys);4745 }4746 if (!size) {4747 callback(null, accumulator !== undefined ? accumulator : result || {});4748 }4749 function done(err, bool) {4750 if (err) {4751 callback = once(callback);4752 callback(err, isArray(result) ? createArray(result) : objectClone(result));4753 } else if (++completed === size) {4754 callback(null, result);4755 } else if (bool === false) {4756 callback = once(callback);4757 callback(null, isArray(result) ? createArray(result) : objectClone(result));4758 }4759 }4760 };4761 }4762 /**4763 * @memberof async4764 * @namespace transformSeries4765 * @param {Array|Object} collection4766 * @param {Array|Object|Function} [accumulator]4767 * @param {Function} [iterator]4768 * @param {Function} [callback]4769 * @example4770 *4771 * // array4772 * var order = [];4773 * var collection = [1, 3, 2, 4];4774 * var iterator = function(result, num, done) {4775 * setTimeout(function() {4776 * order.push(num);4777 * result.push(num)4778 * done();4779 * }, num * 10);4780 * };4781 * async.transformSeries(collection, iterator, function(err, res) {4782 * console.log(res); // [1, 3, 2, 4]4783 * console.log(order); // [1, 3, 2, 4]4784 * });4785 *4786 * @example4787 *4788 * // array with index and accumulator4789 * var order = [];4790 * var collection = [1, 3, 2, 4];4791 * var iterator = function(result, num, index, done) {4792 * setTimeout(function() {4793 * order.push([num, index]);4794 * result[index] = num;4795 * done();4796 * }, num * 10);4797 * };4798 * async.transformSeries(collection, {}, iterator, function(err, res) {4799 * console.log(res); // { '0': 1, '1': 3, '2': 2, '3': 4 }4800 * console.log(order); // [[1, 0], [3, 1], [2, 2], [4, 3]]4801 * });4802 *4803 * @example4804 *4805 * // object with accumulator4806 * var order = [];4807 * var object = { a: 1, b: 3, c: 2, d: 4 };4808 * var iterator = function(result, num, done) {4809 * setTimeout(function() {4810 * order.push(num);4811 * result.push(num);4812 * done();4813 * }, num * 10);4814 * };4815 * async.transformSeries(collection, [], iterator, function(err, res) {4816 * console.log(res); // [1, 3, 2, 4]4817 * console.log(order); // [1, 3, 2, 4]4818 * });4819 *4820 * @example4821 *4822 * // object with key4823 * var order = [];4824 * var object = { a: 1, b: 3, c: 2, d: 4 };4825 * var iterator = function(result, num, key, done) {4826 * setTimeout(function() {4827 * order.push([num, key]);4828 * result[key] = num;4829 * done();4830 * }, num * 10);4831 * };4832 * async.transformSeries(collection, iterator, function(err, res) {4833 * console.log(res); // { a: 1, b: 3, c: 2, d: 4 }4834 * console.log(order); // [[1, 'a'], [3, 'b'], [2, 'b'], [4, 'd']]4835 * });4836 *4837 */4838 function transformSeries(collection, accumulator, iterator, callback) {4839 if (arguments.length === 3) {4840 callback = iterator;4841 iterator = accumulator;4842 accumulator = undefined;4843 }4844 callback = onlyOnce(callback || noop);4845 var size, key, keys, iter, item, iterate, result;4846 var sync = false;4847 var completed = 0;4848 if (isArray(collection)) {4849 size = collection.length;4850 result = accumulator !== undefined ? accumulator : [];4851 iterate = iterator.length === 4 ? arrayIteratorWithIndex : arrayIterator;4852 } else if (!collection) {4853 } else if (iteratorSymbol && collection[iteratorSymbol]) {4854 size = Infinity;4855 iter = collection[iteratorSymbol]();4856 result = accumulator !== undefined ? accumulator : {};4857 iterate = iterator.length === 4 ? symbolIteratorWithKey : symbolIterator;4858 } else if (typeof collection === obj) {4859 keys = nativeKeys(collection);4860 size = keys.length;4861 result = accumulator !== undefined ? accumulator : {};4862 iterate = iterator.length === 4 ? objectIteratorWithKey : objectIterator;4863 }4864 if (!size) {4865 return callback(null, accumulator !== undefined ? accumulator : result || {});4866 }4867 iterate();4868 function arrayIterator() {4869 iterator(result, collection[completed], done);4870 }4871 function arrayIteratorWithIndex() {4872 iterator(result, collection[completed], completed, done);4873 }4874 function symbolIterator() {4875 item = iter.next();4876 item.done ? callback(null, result) : iterator(result, item.value, done);4877 }4878 function symbolIteratorWithKey() {4879 item = iter.next();4880 item.done ? callback(null, result) : iterator(result, item.value, completed, done);4881 }4882 function objectIterator() {4883 iterator(result, collection[keys[completed]], done);4884 }4885 function objectIteratorWithKey() {4886 key = keys[completed];4887 iterator(result, collection[key], key, done);4888 }4889 function done(err, bool) {4890 if (err) {4891 callback(err, result);4892 } else if (++completed === size || bool === false) {4893 iterate = throwError;4894 callback(null, result);4895 } else if (sync) {4896 nextTick(iterate);4897 } else {4898 sync = true;4899 iterate();4900 }4901 sync = false;4902 }4903 }4904 /**4905 * @memberof async4906 * @namespace transformLimit4907 * @param {Array|Object} collection4908 * @param {number} limit - limit >= 14909 * @param {Array|Object|Function} [accumulator]4910 * @param {Function} [iterator]4911 * @param {Function} [callback]4912 * @example4913 *4914 * // array4915 * var order = [];4916 * var array = [1, 5, 3, 4, 2];4917 * var iterator = function(result, num, done) {4918 * setTimeout(function() {4919 * order.push(num);4920 * result.push(num);4921 * done();4922 * }, num * 10);4923 * };4924 * async.transformLimit(array, 2, iterator, function(err, res) {4925 * console.log(res); // [1, 3, 5, 2, 4]4926 * console.log(order); // [1, 3, 5, 2, 4]4927 * });4928 *4929 * @example4930 *4931 * // array with index and accumulator4932 * var order = [];4933 * var array = [1, 5, 3, 4, 2];4934 * var iterator = function(result, num, index, done) {4935 * setTimeout(function() {4936 * order.push([num, index]);4937 * result[index] = key;4938 * done();4939 * }, num * 10);4940 * };4941 * async.transformLimit(array, 2, {}, iterator, function(err, res) {4942 * console.log(res); // { '0': 1, '1': 5, '2': 3, '3': 4, '4': 2 }4943 * console.log(order); // [[1, 0], [3, 2], [5, 1], [2, 4], [4, 3]]4944 * });4945 *4946 * @example4947 *4948 * // object with accumulator4949 * var order = [];4950 * var object = { a: 1, b: 5, c: 3, d: 4, e: 2 };4951 * var iterator = function(result, num, done) {4952 * setTimeout(function() {4953 * order.push(num);4954 * result.push(num);4955 * done();4956 * }, num * 10);4957 * };4958 * async.transformLimit(object, 2, [], iterator, function(err, res) {4959 * console.log(res); // [1, 3, 5, 2, 4]4960 * console.log(order); // [1, 3, 5, 2, 4]4961 * });4962 *4963 * @example4964 *4965 * // object with key4966 * var order = [];4967 * var object = { a: 1, b: 5, c: 3, d: 4, e: 2 };4968 * var iterator = function(result, num, key, done) {4969 * setTimeout(function() {4970 * order.push([num, key]);4971 * result[key] = num;4972 * done();4973 * }, num * 10);4974 * };4975 * async.transformLimit(object, 2, iterator, function(err, res) {4976 * console.log(res); // { a: 1, b: 5, c: 3, d: 4, e: 2 };4977 * console.log(order); // [[1, 'a'], [3, 'c'], [5, 'b'], [2, 'e'], [4, 'd']]4978 * });4979 *4980 */4981 function transformLimit(collection, limit, accumulator, iterator, callback) {4982 if (arguments.length === 4) {4983 callback = iterator;4984 iterator = accumulator;4985 accumulator = undefined;4986 }4987 callback = callback || noop;4988 var size, index, key, keys, iter, item, iterate, result;4989 var sync = false;4990 var started = 0;4991 var completed = 0;4992 if (isArray(collection)) {4993 size = collection.length;4994 result = accumulator !== undefined ? accumulator : [];4995 iterate = iterator.length === 4 ? arrayIteratorWithIndex : arrayIterator;4996 } else if (!collection) {4997 } else if (iteratorSymbol && collection[iteratorSymbol]) {4998 size = Infinity;4999 iter = collection[iteratorSymbol]();5000 result = accumulator !== undefined ? accumulator : {};5001 iterate = iterator.length === 4 ? symbolIteratorWithKey : symbolIterator;5002 } else if (typeof collection === obj) {5003 keys = nativeKeys(collection);5004 size = keys.length;5005 result = accumulator !== undefined ? accumulator : {};5006 iterate = iterator.length === 4 ? objectIteratorWithKey : objectIterator;5007 }5008 if (!size || isNaN(limit) || limit < 1) {5009 return callback(null, accumulator !== undefined ? accumulator : result || {});5010 }5011 timesSync(limit > size ? size : limit, iterate);5012 function arrayIterator() {5013 index = started++;5014 if (index < size) {5015 iterator(result, collection[index], onlyOnce(done));5016 }5017 }5018 function arrayIteratorWithIndex() {5019 index = started++;5020 if (index < size) {5021 iterator(result, collection[index], index, onlyOnce(done));5022 }5023 }5024 function symbolIterator() {5025 item = iter.next();5026 if (item.done === false) {5027 started++;5028 iterator(result, item.value, onlyOnce(done));5029 } else if (completed === started && iterator !== noop) {5030 iterator = noop;5031 callback(null, result);5032 }5033 }5034 function symbolIteratorWithKey() {5035 item = iter.next();5036 if (item.done === false) {5037 iterator(result, item.value, started++, onlyOnce(done));5038 } else if (completed === started && iterator !== noop) {5039 iterator = noop;5040 callback(null, result);5041 }5042 }5043 function objectIterator() {5044 index = started++;5045 if (index < size) {5046 iterator(result, collection[keys[index]], onlyOnce(done));5047 }5048 }5049 function objectIteratorWithKey() {5050 index = started++;5051 if (index < size) {5052 key = keys[index];5053 iterator(result, collection[key], key, onlyOnce(done));5054 }5055 }5056 function done(err, bool) {5057 if (err || bool === false) {5058 iterate = noop;5059 callback(err || null, isArray(result) ? createArray(result) : objectClone(result));5060 callback = noop;5061 } else if (++completed === size) {5062 iterator = noop;5063 callback(null, result);5064 } else if (sync) {5065 nextTick(iterate);5066 } else {5067 sync = true;5068 iterate();5069 }5070 sync = false;5071 }5072 }5073 /**5074 * @private5075 * @param {function} arrayEach5076 * @param {function} baseEach5077 * @param {function} symbolEach5078 */5079 function createSortBy(arrayEach, baseEach, symbolEach) {5080 return function sortBy(collection, iterator, callback) {5081 callback = callback || noop;5082 var size, array, criteria;5083 var completed = 0;5084 if (isArray(collection)) {5085 size = collection.length;5086 array = Array(size);5087 criteria = Array(size);5088 arrayEach(collection, iterator, createCallback);5089 } else if (!collection) {5090 } else if (iteratorSymbol && collection[iteratorSymbol]) {5091 array = [];5092 criteria = [];5093 size = symbolEach(collection, iterator, createCallback);5094 size && size === completed && callback(null, sortByCriteria(array, criteria));5095 } else if (typeof collection === obj) {5096 var keys = nativeKeys(collection);5097 size = keys.length;5098 array = Array(size);5099 criteria = Array(size);5100 baseEach(collection, iterator, createCallback, keys);5101 }5102 if (!size) {5103 callback(null, []);5104 }5105 function createCallback(index, value) {5106 var called = false;5107 array[index] = value;5108 return function done(err, criterion) {5109 if (called) {5110 throwError();5111 }5112 called = true;5113 criteria[index] = criterion;5114 if (err) {5115 callback = once(callback);5116 callback(err);5117 } else if (++completed === size) {5118 callback(null, sortByCriteria(array, criteria));5119 }5120 };5121 }5122 };5123 }5124 /**5125 * @memberof async5126 * @namespace sortBySeries5127 * @param {Array|Object} collection5128 * @param {Function} iterator5129 * @param {Function} callback5130 * @example5131 *5132 * // array5133 * var order = [];5134 * var array = [1, 3, 2];5135 * var iterator = function(num, done) {5136 * setTimeout(function() {5137 * order.push(num);5138 * done(null, num);5139 * }, num * 10);5140 * };5141 * async.sortBySeries(array, iterator, function(err, res) {5142 * console.log(res); // [1, 2, 3];5143 * console.log(order); // [1, 3, 2]5144 * });5145 *5146 * @example5147 *5148 * // array with index5149 * var order = [];5150 * var array = [1, 3, 2];5151 * var iterator = function(num, index, done) {5152 * setTimeout(function() {5153 * order.push([num, index]);5154 * done(null, num);5155 * }, num * 10);5156 * };5157 * async.sortBySeries(array, iterator, function(err, res) {5158 * console.log(res); // [1, 2, 3]5159 * console.log(order); // [[1, 0], [3, 1], [2, 2]]5160 * });5161 *5162 * @example5163 *5164 * // object5165 * var order = [];5166 * var object = { a: 1, b: 3, c: 2 };5167 * var iterator = function(num, done) {5168 * setTimeout(function() {5169 * order.push(num);5170 * done(null, num);5171 * }, num * 10);5172 * };5173 * async.sortBySeries(object, iterator, function(err, res) {5174 * console.log(res); // [1, 2, 3]5175 * console.log(order); // [1, 3, 2]5176 * });5177 *5178 * @example5179 *5180 * // object with key5181 * var order = [];5182 * var object = { a: 1, b: 3, c: 2 };5183 * var iterator = function(num, key, done) {5184 * setTimeout(function() {5185 * order.push([num, key]);5186 * done(null, num);5187 * }, num * 10);5188 * };5189 * async.sortBySeries(object, iterator, function(err, res) {5190 * console.log(res); // [1, 2, 3]5191 * console.log(order); // [[1, 'a'], [3, 'b'], [2, 'c']]5192 * });5193 *5194 */5195 function sortBySeries(collection, iterator, callback) {5196 callback = onlyOnce(callback || noop);5197 var size, key, value, keys, iter, item, array, criteria, iterate;5198 var sync = false;5199 var completed = 0;5200 if (isArray(collection)) {5201 size = collection.length;5202 array = collection;5203 criteria = Array(size);5204 iterate = iterator.length === 3 ? arrayIteratorWithIndex : arrayIterator;5205 } else if (!collection) {5206 } else if (iteratorSymbol && collection[iteratorSymbol]) {5207 size = Infinity;5208 array = [];5209 criteria = [];5210 iter = collection[iteratorSymbol]();5211 iterate = iterator.length === 3 ? symbolIteratorWithKey : symbolIterator;5212 } else if (typeof collection === obj) {5213 keys = nativeKeys(collection);5214 size = keys.length;5215 array = Array(size);5216 criteria = Array(size);5217 iterate = iterator.length === 3 ? objectIteratorWithKey : objectIterator;5218 }5219 if (!size) {5220 return callback(null, []);5221 }5222 iterate();5223 function arrayIterator() {5224 value = collection[completed];5225 iterator(value, done);5226 }5227 function arrayIteratorWithIndex() {5228 value = collection[completed];5229 iterator(value, completed, done);5230 }5231 function symbolIterator() {5232 item = iter.next();5233 if (item.done) {5234 return callback(null, sortByCriteria(array, criteria));5235 }5236 value = item.value;5237 array[completed] = value;5238 iterator(value, done);5239 }5240 function symbolIteratorWithKey() {5241 item = iter.next();5242 if (item.done) {5243 return callback(null, sortByCriteria(array, criteria));5244 }5245 value = item.value;5246 array[completed] = value;5247 iterator(value, completed, done);5248 }5249 function objectIterator() {5250 value = collection[keys[completed]];5251 array[completed] = value;5252 iterator(value, done);5253 }5254 function objectIteratorWithKey() {5255 key = keys[completed];5256 value = collection[key];5257 array[completed] = value;5258 iterator(value, key, done);5259 }5260 function done(err, criterion) {5261 criteria[completed] = criterion;5262 if (err) {5263 callback(err);5264 } else if (++completed === size) {5265 iterate = throwError;5266 callback(null, sortByCriteria(array, criteria));5267 } else if (sync) {5268 nextTick(iterate);5269 } else {5270 sync = true;5271 iterate();5272 }5273 sync = false;5274 }5275 }5276 /**5277 * @memberof async5278 * @namespace sortByLimit5279 * @param {Array|Object} collection5280 * @param {number} limit - limit >= 15281 * @param {Function} iterator5282 * @param {Function} callback5283 * @example5284 *5285 * // array5286 * var order = [];5287 * var array = [1, 5, 3, 4, 2];5288 * var iterator = function(num, done) {5289 * setTimeout(function() {5290 * order.push(num);5291 * done(null, num);5292 * }, num * 10);5293 * };5294 * async.sortByLimit(array, 2, iterator, function(err, res) {5295 * console.log(res); // [1, 2, 3, 4, 5]5296 * console.log(order); // [1, 3, 5, 2, 4]5297 * });5298 *5299 * @example5300 *5301 * // array with index5302 * var order = [];5303 * var array = [1, 5, 3, 4, 2];5304 * var iterator = function(num, index, done) {5305 * setTimeout(function() {5306 * order.push([num, index]);5307 * done(null, num);5308 * }, num * 10);5309 * };5310 * async.sortByLimit(array, 2, iterator, function(err, res) {5311 * console.log(res); // [1, 2, 3, 4, 5]5312 * console.log(order); // [[1, 0], [3, 2], [5, 1], [2, 4], [4, 3]]5313 * });5314 *5315 * @example5316 *5317 * // object5318 * var order = [];5319 * var object = { a: 1, b: 5, c: 3, d: 4, e: 2 };5320 * var iterator = function(num, done) {5321 * setTimeout(function() {5322 * order.push(num);5323 * done(null, num);5324 * }, num * 10);5325 * };5326 * async.sortByLimit(object, 2, iterator, function(err, res) {5327 * console.log(res); // [1, 2, 3, 4, 5]5328 * console.log(order); // [1, 3, 5, 2, 4]5329 * });5330 *5331 * @example5332 *5333 * // object with key5334 * var order = [];5335 * var object = { a: 1, b: 5, c: 3, d: 4, e: 2 };5336 * var iterator = function(num, key, done) {5337 * setTimeout(function() {5338 * order.push([num, key]);5339 * done(null, num);5340 * }, num * 10);5341 * };5342 * async.sortByLimit(object, 2, iterator, function(err, res) {5343 * console.log(res); // [1, 2, 3, 4, 5]5344 * console.log(order); // [[1, 'a'], [3, 'c'], [5, 'b'], [2, 'e'], [4, 'd']]5345 * });5346 *5347 */5348 function sortByLimit(collection, limit, iterator, callback) {5349 callback = callback || noop;5350 var size, index, key, value, array, keys, iter, item, criteria, iterate;5351 var sync = false;5352 var started = 0;5353 var completed = 0;5354 if (isArray(collection)) {5355 size = collection.length;5356 array = collection;5357 iterate = iterator.length === 3 ? arrayIteratorWithIndex : arrayIterator;5358 } else if (!collection) {5359 } else if (iteratorSymbol && collection[iteratorSymbol]) {5360 size = Infinity;5361 iter = collection[iteratorSymbol]();5362 array = [];5363 criteria = [];5364 iterate = iterator.length === 3 ? symbolIteratorWithKey : symbolIterator;5365 } else if (typeof collection === obj) {5366 keys = nativeKeys(collection);5367 size = keys.length;5368 array = Array(size);5369 iterate = iterator.length === 3 ? objectIteratorWithKey : objectIterator;5370 }5371 if (!size || isNaN(limit) || limit < 1) {5372 return callback(null, []);5373 }5374 criteria = criteria || Array(size);5375 timesSync(limit > size ? size : limit, iterate);5376 function arrayIterator() {5377 if (started < size) {5378 value = collection[started];5379 iterator(value, createCallback(value, started++));5380 }5381 }5382 function arrayIteratorWithIndex() {5383 index = started++;5384 if (index < size) {5385 value = collection[index];5386 iterator(value, index, createCallback(value, index));5387 }5388 }5389 function symbolIterator() {5390 item = iter.next();5391 if (item.done === false) {5392 value = item.value;5393 array[started] = value;5394 iterator(value, createCallback(value, started++));5395 } else if (completed === started && iterator !== noop) {5396 iterator = noop;5397 callback(null, sortByCriteria(array, criteria));5398 }5399 }5400 function symbolIteratorWithKey() {5401 item = iter.next();5402 if (item.done === false) {5403 value = item.value;5404 array[started] = value;5405 iterator(value, started, createCallback(value, started++));5406 } else if (completed === started && iterator !== noop) {5407 iterator = noop;5408 callback(null, sortByCriteria(array, criteria));5409 }5410 }5411 function objectIterator() {5412 if (started < size) {5413 value = collection[keys[started]];5414 array[started] = value;5415 iterator(value, createCallback(value, started++));5416 }5417 }5418 function objectIteratorWithKey() {5419 if (started < size) {5420 key = keys[started];5421 value = collection[key];5422 array[started] = value;5423 iterator(value, key, createCallback(value, started++));5424 }5425 }5426 function createCallback(value, index) {5427 var called = false;5428 return function(err, criterion) {5429 if (called) {5430 throwError();5431 }5432 called = true;5433 criteria[index] = criterion;5434 if (err) {5435 iterate = noop;5436 callback(err);5437 callback = noop;5438 } else if (++completed === size) {5439 callback(null, sortByCriteria(array, criteria));5440 } else if (sync) {5441 nextTick(iterate);5442 } else {5443 sync = true;5444 iterate();5445 }5446 sync = false;5447 };5448 }5449 }5450 /**5451 * @memberof async5452 * @namespace some5453 * @param {Array|Object} collection5454 * @param {Function} iterator5455 * @param {Function} callback5456 * @example5457 *5458 * // array5459 * var order = [];5460 * var array = [1, 3, 2];5461 * var iterator = function(num, done) {5462 * setTimeout(function() {5463 * order.push(num);5464 * done(null, num % 2);5465 * }, num * 10);5466 * };5467 * async.some(array, iterator, function(err, res) {5468 * console.log(res); // true5469 * console.log(order); // [1]5470 * });5471 *5472 * @example5473 *5474 * // array with index5475 * var order = [];5476 * var array = [1, 3, 2];5477 * var iterator = function(num, index, done) {5478 * setTimeout(function() {5479 * order.push([num, index]);5480 * done(null, num % 2);5481 * }, num * 10);5482 * };5483 * async.some(array, iterator, function(err, res) {5484 * console.log(res); // true5485 * console.log(order); // [[1, 0]]5486 * });5487 *5488 * @example5489 *5490 * // object5491 * var order = [];5492 * var object = { a: 1, b: 3, c: 2 };5493 * var iterator = function(num, done) {5494 * setTimeout(function() {5495 * order.push(num);5496 * done(null, num % 2);5497 * }, num * 10);5498 * };5499 * async.some(object, iterator, function(err, res) {5500 * console.log(res); // true5501 * console.log(order); // [1]5502 * });5503 *5504 * @example5505 *5506 * // object with key5507 * var order = [];5508 * var object = { a: 1, b: 3, c: 2 };5509 * var iterator = function(num, key, done) {5510 * setTimeout(function() {5511 * order.push([num, key]);5512 * done(null, num % 2);5513 * }, num * 10);5514 * };5515 * async.some(object, iterator, function(err, res) {5516 * console.log(res); // true5517 * console.log(order); // [[1, 'a']]5518 * });5519 *5520 */5521 function some(collection, iterator, callback) {5522 callback = callback || noop;5523 detect(collection, iterator, done);5524 function done(err, res) {5525 if (err) {5526 return callback(err);5527 }5528 callback(null, !!res);5529 }5530 }5531 /**5532 * @memberof async5533 * @namespace someSeries5534 * @param {Array|Object} collection5535 * @param {Function} iterator5536 * @param {Function} callback5537 * @example5538 *5539 * // array5540 * var order = [];5541 * var array = [1, 3, 2];5542 * var iterator = function(num, done) {5543 * setTimeout(function() {5544 * order.push(num);5545 * done(null, num % 2);5546 * }, num * 10);5547 * };5548 * async.someSeries(array, iterator, function(err, res) {5549 * console.log(res); // true5550 * console.log(order); // [1]5551 * });5552 *5553 * @example5554 *5555 * // array with index5556 * var order = [];5557 * var array = [1, 3, 2];5558 * var iterator = function(num, index, done) {5559 * setTimeout(function() {5560 * order.push([num, index]);5561 * done(null, num % 2);5562 * }, num * 10);5563 * };5564 * async.someSeries(array, iterator, function(err, res) {5565 * console.log(res); // true5566 * console.log(order); // [[1, 0]]5567 * });5568 *5569 * @example5570 *5571 * // object5572 * var order = [];5573 * var object = { a: 1, b: 3, c: 2 };5574 * var iterator = function(num, done) {5575 * setTimeout(function() {5576 * order.push(num);5577 * done(null, num % 2);5578 * }, num * 10);5579 * };5580 * async.someSeries(object, iterator, function(err, res) {5581 * console.log(res); // true5582 * console.log(order); // [1]5583 * });5584 *5585 * @example5586 *5587 * // object with key5588 * var order = [];5589 * var object = { a: 1, b: 3, c: 2 };5590 * var iterator = function(num, key, done) {5591 * setTimeout(function() {5592 * order.push([num, key]);5593 * done(null, num % 2);5594 * }, num * 10);5595 * };5596 * async.someSeries(object, iterator, function(err, res) {5597 * console.log(res); // true5598 * console.log(order); // [[1, 'a']]5599 * });5600 *5601 */5602 function someSeries(collection, iterator, callback) {5603 callback = callback || noop;5604 detectSeries(collection, iterator, done);5605 function done(err, res) {5606 if (err) {5607 return callback(err);5608 }5609 callback(null, !!res);5610 }5611 }5612 /**5613 * @memberof async5614 * @namespace someLimit5615 * @param {Array|Object} collection5616 * @param {number} limit - limit >= 15617 * @param {Function} iterator5618 * @param {Function} callback5619 * @example5620 *5621 * // array5622 * var order = [];5623 * var array = [1, 5, 3, 4, 2];5624 * var iterator = function(num, done) {5625 * setTimeout(function() {5626 * order.push(num);5627 * done(null, num % 2);5628 * }, num * 10);5629 * };5630 * async.someLimit(array, 2, iterator, function(err, res) {5631 * console.log(res); // true5632 * console.log(order); // [1]5633 * });5634 *5635 * @example5636 *5637 * // array with index5638 * var order = [];5639 * var array = [1, 5, 3, 4, 2];5640 * var iterator = function(num, index, done) {5641 * setTimeout(function() {5642 * order.push([num, index]);5643 * done(null, num % 2);5644 * }, num * 10);5645 * };5646 * async.someLimit(array, 2, iterator, function(err, res) {5647 * console.log(res); // true5648 * console.log(order); // [[1, 0]]5649 * });5650 *5651 * @example5652 *5653 * // object5654 * var order = [];5655 * var object = { a: 1, b: 5, c: 3, d: 4, e: 2 };5656 * var iterator = function(num, done) {5657 * setTimeout(function() {5658 * order.push(num);5659 * done(null, num % 2);5660 * }, num * 10);5661 * };5662 * async.someLimit(object, 2, iterator, function(err, res) {5663 * console.log(res); // true5664 * console.log(order); // [1]5665 * });5666 *5667 * @example5668 *5669 * // object with key5670 * var order = [];5671 * var object = { a: 1, b: 5, c: 3, d: 4, e: 2 };5672 * var iterator = function(num, key, done) {5673 * setTimeout(function() {5674 * order.push([num, key]);5675 * done(null, num % 2);5676 * }, num * 10);5677 * };5678 * async.someLimit(object, 2, iterator, function(err, res) {5679 * console.log(res); // true5680 * console.log(order); // [[1, 'a']]5681 * });5682 *5683 */5684 function someLimit(collection, limit, iterator, callback) {5685 callback = callback || noop;5686 detectLimit(collection, limit, iterator, done);5687 function done(err, res) {5688 if (err) {5689 return callback(err);5690 }5691 callback(null, !!res);5692 }5693 }5694 /**5695 * @private5696 * @param {Function} arrayEach5697 * @param {Function} baseEach5698 * @param {Function} symbolEach5699 */5700 function createEvery(arrayEach, baseEach, symbolEach) {5701 var deny = createDetect(arrayEach, baseEach, symbolEach, false);5702 return function every(collection, iterator, callback) {5703 callback = callback || noop;5704 deny(collection, iterator, done);5705 function done(err, res) {5706 if (err) {5707 return callback(err);5708 }5709 callback(null, !res);5710 }5711 };5712 }5713 /**5714 * @private5715 */5716 function createEverySeries() {5717 var denySeries = createDetectSeries(false);5718 return function everySeries(collection, iterator, callback) {5719 callback = callback || noop;5720 denySeries(collection, iterator, done);5721 function done(err, res) {5722 if (err) {5723 return callback(err);5724 }5725 callback(null, !res);5726 }5727 };5728 }5729 /**5730 * @private5731 */5732 function createEveryLimit() {5733 var denyLimit = createDetectLimit(false);5734 return function everyLimit(collection, limit, iterator, callback) {5735 callback = callback || noop;5736 denyLimit(collection, limit, iterator, done);5737 function done(err, res) {5738 if (err) {5739 return callback(err);5740 }5741 callback(null, !res);5742 }5743 };5744 }5745 /**5746 * @private5747 * @param {Function} arrayEach5748 * @param {Function} baseEach5749 * @param {Function} symbolEach5750 */5751 function createConcat(arrayEach, baseEach, symbolEach) {5752 return function concat(collection, iterator, callback) {5753 callback = callback || noop;5754 var size, result;5755 var completed = 0;5756 if (isArray(collection)) {5757 size = collection.length;5758 result = Array(size);5759 arrayEach(collection, iterator, createCallback);5760 } else if (!collection) {5761 } else if (iteratorSymbol && collection[iteratorSymbol]) {5762 result = [];5763 size = symbolEach(collection, iterator, createCallback);5764 size && size === completed && callback(null, result);5765 } else if (typeof collection === obj) {5766 var keys = nativeKeys(collection);5767 size = keys.length;5768 result = Array(size);5769 baseEach(collection, iterator, createCallback, keys);5770 }5771 if (!size) {5772 callback(null, []);5773 }5774 function createCallback(index) {5775 return function done(err, res) {5776 if (index === null) {5777 throwError();5778 }5779 if (err) {5780 index = null;5781 callback = once(callback);5782 arrayEachSync(result, function(array, index) {5783 if (array === undefined) {5784 result[index] = noop;5785 }5786 });5787 callback(err, makeConcatResult(result));5788 return;5789 }5790 switch (arguments.length) {5791 case 0:5792 case 1:5793 result[index] = noop;5794 break;5795 case 2:5796 result[index] = res;5797 break;5798 default:5799 result[index] = slice(arguments, 1);5800 break;5801 }5802 index = null;5803 if (++completed === size) {5804 callback(null, makeConcatResult(result));5805 }5806 };5807 }5808 };5809 }5810 /**5811 * @memberof async5812 * @namespace concatSeries5813 * @param {Array|Object} collection5814 * @param {Function} iterator5815 * @param {Function} callback5816 * @example5817 *5818 * // array5819 * var order = [];5820 * var array = [1, 3, 2];5821 * var iterator = function(num, done) {5822 * setTimeout(function() {5823 * order.push(num);5824 * done(null, [num]);5825 * }, num * 10);5826 * };5827 * async.concatSeries(array, iterator, function(err, res) {5828 * console.log(res); // [1, 3, 2];5829 * console.log(order); // [1, 3, 2]5830 * });5831 *5832 * @example5833 *5834 * // array with index5835 * var order = [];5836 * var array = [1, 3, 2];5837 * var iterator = function(num, index, done) {5838 * setTimeout(function() {5839 * order.push([num, index]);5840 * done(null, [num]);5841 * }, num * 10);5842 * };5843 * async.concatSeries(array, iterator, function(err, res) {5844 * console.log(res); // [1, 3, 2]5845 * console.log(order); // [[1, 0], [3, 1], [2, 2]]5846 * });5847 *5848 * @example5849 *5850 * // object5851 * var order = [];5852 * var object = { a: 1, b: 3, c: 2 };5853 * var iterator = function(num, done) {5854 * setTimeout(function() {5855 * order.push(num);5856 * done(null, [num]);5857 * }, num * 10);5858 * };5859 * async.concatSeries(object, iterator, function(err, res) {5860 * console.log(res); // [1, 3, 2]5861 * console.log(order); // [1, 3, 2]5862 * });5863 *5864 * @example5865 *5866 * // object with key5867 * var order = [];5868 * var object = { a: 1, b: 3, c: 2 };5869 * var iterator = function(num, key, done) {5870 * setTimeout(function() {5871 * order.push([num, key]);5872 * done(null, [num]);5873 * }, num * 10);5874 * };5875 * async.concatSeries(object, iterator, function(err, res) {5876 * console.log(res); // [1, 3, 2]5877 * console.log(order); // [[1, 'a'], [3, 'b'], [2, 'c']]5878 * });5879 *5880 */5881 function concatSeries(collection, iterator, callback) {5882 callback = onlyOnce(callback || noop);5883 var size, key, keys, iter, item, iterate;5884 var sync = false;5885 var result = [];5886 var completed = 0;5887 if (isArray(collection)) {5888 size = collection.length;5889 iterate = iterator.length === 3 ? arrayIteratorWithIndex : arrayIterator;5890 } else if (!collection) {5891 } else if (iteratorSymbol && collection[iteratorSymbol]) {5892 size = Infinity;5893 iter = collection[iteratorSymbol]();5894 iterate = iterator.length === 3 ? symbolIteratorWithKey : symbolIterator;5895 } else if (typeof collection === obj) {5896 keys = nativeKeys(collection);5897 size = keys.length;5898 iterate = iterator.length === 3 ? objectIteratorWithKey : objectIterator;5899 }5900 if (!size) {5901 return callback(null, result);5902 }5903 iterate();5904 function arrayIterator() {5905 iterator(collection[completed], done);5906 }5907 function arrayIteratorWithIndex() {5908 iterator(collection[completed], completed, done);5909 }5910 function symbolIterator() {5911 item = iter.next();5912 item.done ? callback(null, result) : iterator(item.value, done);5913 }5914 function symbolIteratorWithKey() {5915 item = iter.next();5916 item.done ? callback(null, result) : iterator(item.value, completed, done);5917 }5918 function objectIterator() {5919 iterator(collection[keys[completed]], done);5920 }5921 function objectIteratorWithKey() {5922 key = keys[completed];5923 iterator(collection[key], key, done);5924 }5925 function done(err, array) {5926 if (isArray(array)) {5927 nativePush.apply(result, array);5928 } else if (arguments.length >= 2) {5929 nativePush.apply(result, slice(arguments, 1));5930 }5931 if (err) {5932 callback(err, result);5933 } else if (++completed === size) {5934 iterate = throwError;5935 callback(null, result);5936 } else if (sync) {5937 nextTick(iterate);5938 } else {5939 sync = true;5940 iterate();5941 }5942 sync = false;5943 }5944 }5945 /**5946 * @memberof async5947 * @namespace concatLimit5948 * @param {Array|Object} collection5949 * @param {number} limit - limit >= 15950 * @param {Function} iterator5951 * @param {Function} callback5952 * @example5953 *5954 * // array5955 * var order = [];5956 * var array = [1, 5, 3, 4, 2];5957 * var iterator = function(num, done) {5958 * setTimeout(function() {5959 * order.push(num);5960 * done(null, [num]);5961 * }, num * 10);5962 * };5963 * async.concatLimit(array, 2, iterator, function(err, res) {5964 * console.log(res); // [1, 3, 5, 2, 4]5965 * console.log(order); // [1, 3, 5, 2, 4]5966 * });5967 *5968 * @example5969 *5970 * // array with index5971 * var order = [];5972 * var array = [1, 5, 3, 4, 2];5973 * var iterator = function(num, index, done) {5974 * setTimeout(function() {5975 * order.push([num, index]);5976 * done(null, [num]);5977 * }, num * 10);5978 * };5979 * async.cocnatLimit(array, 2, iterator, function(err, res) {5980 * console.log(res); // [1, 3, 5, 2, 4]5981 * console.log(order); // [[1, 0], [3, 2], [5, 1], [2, 4], [4, 3]]5982 * });5983 *5984 * @example5985 *5986 * // object5987 * var order = [];5988 * var object = { a: 1, b: 5, c: 3, d: 4, e: 2 };5989 * var iterator = function(num, done) {5990 * setTimeout(function() {5991 * order.push(num);5992 * done(null, [num]);5993 * }, num * 10);5994 * };5995 * async.concatLimit(object, 2, iterator, function(err, res) {5996 * console.log(res); // [1, 3, 5, 2, 4]5997 * console.log(order); // [1, 3, 5, 2, 4]5998 * });5999 *6000 * @example6001 *6002 * // object with key6003 * var order = [];6004 * var object = { a: 1, b: 5, c: 3, d: 4, e: 2 };6005 * var iterator = function(num, key, done) {6006 * setTimeout(function() {6007 * order.push([num, key]);6008 * done(null, num);6009 * }, num * 10);6010 * };6011 * async.cocnatLimit(object, 2, iterator, function(err, res) {6012 * console.log(res); // [1, 3, 5, 2, 4]6013 * console.log(order); // [[1, 'a'], [3, 'c'], [5, 'b'], [2, 'e'], [4, 'd']]6014 * });6015 *6016 */6017 function concatLimit(collection, limit, iterator, callback) {6018 callback = callback || noop;6019 var size, key, iter, item, iterate, result;6020 var sync = false;6021 var started = 0;6022 var completed = 0;6023 if (isArray(collection)) {6024 size = collection.length;6025 iterate = iterator.length === 3 ? arrayIteratorWithIndex : arrayIterator;6026 } else if (!collection) {6027 } else if (iteratorSymbol && collection[iteratorSymbol]) {6028 size = Infinity;6029 result = [];6030 iter = collection[iteratorSymbol]();6031 iterate = iterator.length === 3 ? symbolIteratorWithKey : symbolIterator;6032 } else if (typeof collection === obj) {6033 var keys = nativeKeys(collection);6034 size = keys.length;6035 iterate = iterator.length === 3 ? objectIteratorWithKey : objectIterator;6036 }6037 if (!size || isNaN(limit) || limit < 1) {6038 return callback(null, []);6039 }6040 result = result || Array(size);6041 timesSync(limit > size ? size : limit, iterate);6042 function arrayIterator() {6043 if (started < size) {6044 iterator(collection[started], createCallback(started++));6045 }6046 }6047 function arrayIteratorWithIndex() {6048 if (started < size) {6049 iterator(collection[started], started, createCallback(started++));6050 }6051 }6052 function symbolIterator() {6053 item = iter.next();6054 if (item.done === false) {6055 iterator(item.value, createCallback(started++));6056 } else if (completed === started && iterator !== noop) {6057 iterator = noop;6058 callback(null, makeConcatResult(result));6059 }6060 }6061 function symbolIteratorWithKey() {6062 item = iter.next();6063 if (item.done === false) {6064 iterator(item.value, started, createCallback(started++));6065 } else if (completed === started && iterator !== noop) {6066 iterator = noop;6067 callback(null, makeConcatResult(result));6068 }6069 }6070 function objectIterator() {6071 if (started < size) {6072 iterator(collection[keys[started]], createCallback(started++));6073 }6074 }6075 function objectIteratorWithKey() {6076 if (started < size) {6077 key = keys[started];6078 iterator(collection[key], key, createCallback(started++));6079 }6080 }6081 function createCallback(index) {6082 return function(err, res) {6083 if (index === null) {6084 throwError();6085 }6086 if (err) {6087 index = null;6088 iterate = noop;6089 callback = once(callback);6090 arrayEachSync(result, function(array, index) {6091 if (array === undefined) {6092 result[index] = noop;6093 }6094 });6095 callback(err, makeConcatResult(result));6096 return;6097 }6098 switch (arguments.length) {6099 case 0:6100 case 1:6101 result[index] = noop;6102 break;6103 case 2:6104 result[index] = res;6105 break;6106 default:6107 result[index] = slice(arguments, 1);6108 break;6109 }6110 index = null;6111 if (++completed === size) {6112 iterate = throwError;6113 callback(null, makeConcatResult(result));6114 callback = throwError;6115 } else if (sync) {6116 nextTick(iterate);6117 } else {6118 sync = true;6119 iterate();6120 }6121 sync = false;6122 };6123 }6124 }6125 /**6126 * @private6127 * @param {Function} arrayEach6128 * @param {Function} baseEach6129 * @param {Function} symbolEach6130 */6131 function createGroupBy(arrayEach, baseEach, symbolEach) {6132 return function groupBy(collection, iterator, callback) {6133 callback = callback || noop;6134 var size;6135 var completed = 0;6136 var result = {};6137 if (isArray(collection)) {6138 size = collection.length;6139 arrayEach(collection, iterator, createCallback);6140 } else if (!collection) {6141 } else if (iteratorSymbol && collection[iteratorSymbol]) {6142 size = symbolEach(collection, iterator, createCallback);6143 size && size === completed && callback(null, result);6144 } else if (typeof collection === obj) {6145 var keys = nativeKeys(collection);6146 size = keys.length;6147 baseEach(collection, iterator, createCallback, keys);6148 }6149 if (!size) {6150 callback(null, {});6151 }6152 function createCallback(value) {6153 var called = false;6154 return function done(err, key) {6155 if (called) {6156 throwError();6157 }6158 called = true;6159 if (err) {6160 callback = once(callback);6161 callback(err, objectClone(result));6162 return;6163 }6164 var array = result[key];6165 if (!array) {6166 result[key] = [value];6167 } else {6168 array.push(value);6169 }6170 if (++completed === size) {6171 callback(null, result);6172 }6173 };6174 }6175 };6176 }6177 /**6178 * @memberof async6179 * @namespace groupBySeries6180 * @param {Array|Object} collection6181 * @param {Function} iterator6182 * @param {Function} callback6183 * @example6184 *6185 * // array6186 * var order = [];6187 * var array = [4.2, 6.4, 6.1];6188 * var iterator = function(num, done) {6189 * setTimeout(function() {6190 * order.push(num);6191 * done(null, Math.floor(num));6192 * }, num * 10);6193 * };6194 * async.groupBySeries(array, iterator, function(err, res) {6195 * console.log(res); // { '4': [4.2], '6': [6.4, 6.1] }6196 * console.log(order); // [4.2, 6.4, 6.1]6197 * });6198 *6199 * @example6200 *6201 * // array with index6202 * var order = [];6203 * var array = [4.2, 6.4, 6.1];6204 * var iterator = function(num, index, done) {6205 * setTimeout(function() {6206 * order.push([num, index]);6207 * done(null, Math.floor(num));6208 * }, num * 10);6209 * };6210 * async.groupBySeries(array, iterator, function(err, res) {6211 * console.log(res); // { '4': [4.2], '6': [6.4, 6.1] }6212 * console.log(order); // [[4.2, 0], [6.4, 1], [6.1, 2]]6213 * });6214 *6215 * @example6216 *6217 * // object6218 * var order = [];6219 * var object = { a: 4.2, b: 6.4, c: 6.1 };6220 * var iterator = function(num, done) {6221 * setTimeout(function() {6222 * order.push(num);6223 * done(null, Math.floor(num));6224 * }, num * 10);6225 * };6226 * async.groupBySeries(object, iterator, function(err, res) {6227 * console.log(res); // { '4': [4.2], '6': [6.4, 6.1] }6228 * console.log(order); // [4.2, 6.4, 6.1]6229 * });6230 *6231 * @example6232 *6233 * // object with key6234 * var order = [];6235 * var object = { a: 4.2, b: 6.4, c: 6.1 };6236 * var iterator = function(num, key, done) {6237 * setTimeout(function() {6238 * order.push([num, key]);6239 * done(null, Math.floor(num));6240 * }, num * 10);6241 * };6242 * async.groupBySeries(object, iterator, function(err, res) {6243 * console.log(res); // { '4': [4.2], '6': [6.4, 6.1] }6244 * console.log(order); // [[4.2, 'a'], [6.4, 'b'], [6.1, 'c']]6245 * });6246 *6247 */6248 function groupBySeries(collection, iterator, callback) {6249 callback = onlyOnce(callback || noop);6250 var size, key, value, keys, iter, item, iterate;6251 var sync = false;6252 var completed = 0;6253 var result = {};6254 if (isArray(collection)) {6255 size = collection.length;6256 iterate = iterator.length === 3 ? arrayIteratorWithIndex : arrayIterator;6257 } else if (!collection) {6258 } else if (iteratorSymbol && collection[iteratorSymbol]) {6259 size = Infinity;6260 iter = collection[iteratorSymbol]();6261 iterate = iterator.length === 3 ? symbolIteratorWithKey : symbolIterator;6262 } else if (typeof collection === obj) {6263 keys = nativeKeys(collection);6264 size = keys.length;6265 iterate = iterator.length === 3 ? objectIteratorWithKey : objectIterator;6266 }6267 if (!size) {6268 return callback(null, result);6269 }6270 iterate();6271 function arrayIterator() {6272 value = collection[completed];6273 iterator(value, done);6274 }6275 function arrayIteratorWithIndex() {6276 value = collection[completed];6277 iterator(value, completed, done);6278 }6279 function symbolIterator() {6280 item = iter.next();6281 value = item.value;6282 item.done ? callback(null, result) : iterator(value, done);6283 }6284 function symbolIteratorWithKey() {6285 item = iter.next();6286 value = item.value;6287 item.done ? callback(null, result) : iterator(value, completed, done);6288 }6289 function objectIterator() {6290 value = collection[keys[completed]];6291 iterator(value, done);6292 }6293 function objectIteratorWithKey() {6294 key = keys[completed];6295 value = collection[key];6296 iterator(value, key, done);6297 }6298 function done(err, key) {6299 if (err) {6300 iterate = throwError;6301 callback = onlyOnce(callback);6302 callback(err, objectClone(result));6303 return;6304 }6305 var array = result[key];6306 if (!array) {6307 result[key] = [value];6308 } else {6309 array.push(value);6310 }6311 if (++completed === size) {6312 iterate = throwError;6313 callback(null, result);6314 } else if (sync) {6315 nextTick(iterate);6316 } else {6317 sync = true;6318 iterate();6319 }6320 sync = false;6321 }6322 }6323 /**6324 * @memberof async6325 * @namespace groupByLimit6326 * @param {Array|Object} collection6327 * @param {Function} iterator6328 * @param {Function} callback6329 * @example6330 *6331 * // array6332 * var order = [];6333 * var array = [1.1, 5.9, 3.2, 3.9, 2.1];6334 * var iterator = function(num, done) {6335 * setTimeout(function() {6336 * order.push(num);6337 * done(null, Math.floor(num));6338 * }, num * 10);6339 * };6340 * async.groupByLimit(array, 2, iterator, function(err, res) {6341 * console.log(res); // { '1': [1.1], '3': [3.2, 3.9], '5': [5.9], '2': [2.1] }6342 * console.log(order); // [1.1, 3.2, 5.9, 2.1, 3.9]6343 * });6344 *6345 * @example6346 *6347 * // array with index6348 * var order = [];6349 * var array = [1.1, 5.9, 3.2, 3.9, 2.1];6350 * var iterator = function(num, index, done) {6351 * setTimeout(function() {6352 * order.push([num, index]);6353 * done(null, Math.floor(num));6354 * }, num * 10);6355 * };6356 * async.groupByLimit(array, 2, iterator, function(err, res) {6357 * console.log(res); // { '1': [1.1], '3': [3.2, 3.9], '5': [5.9], '2': [2.1] }6358 * console.log(order); // [[1.1, 0], [3.2, 2], [5.9, 1], [2.1, 4], [3.9, 3]]6359 * });6360 *6361 * @example6362 *6363 * // object6364 * var order = [];6365 * var object = { a: 1.1, b: 5.9, c: 3.2, d: 3.9, e: 2.1 }6366 * var iterator = function(num, done) {6367 * setTimeout(function() {6368 * order.push(num);6369 * done(null, Math.floor(num));6370 * }, num * 10);6371 * };6372 * async.groupByLimit(object, 2, iterator, function(err, res) {6373 * console.log(res); // { '1': [1.1], '3': [3.2, 3.9], '5': [5.9], '2': [2.1] }6374 * console.log(order); // [1.1, 3.2, 5.9, 2.1, 3.9]6375 * });6376 *6377 * @example6378 *6379 * // object with key6380 * var order = [];6381 * var object = { a: 1.1, b: 5.9, c: 3.2, d: 3.9, e: 2.1 }6382 * var iterator = function(num, key, done) {6383 * setTimeout(function() {6384 * order.push([num, key]);6385 * done(null, Math.floor(num));6386 * }, num * 10);6387 * };6388 * async.groupByLimit(object, 2, iterator, function(err, res) {6389 * console.log(res); // { '1': [1.1], '3': [3.2, 3.9], '5': [5.9], '2': [2.1] }6390 * console.log(order); // [[1.1, 'a'], [3.2, 'c'], [5.9, 'b'], [2.1, 'e'], [3.9, 'd']]6391 * });6392 *6393 */6394 function groupByLimit(collection, limit, iterator, callback) {6395 callback = callback || noop;6396 var size, index, key, value, keys, iter, item, iterate;6397 var sync = false;6398 var started = 0;6399 var completed = 0;6400 var result = {};6401 if (isArray(collection)) {6402 size = collection.length;6403 iterate = iterator.length === 3 ? arrayIteratorWithIndex : arrayIterator;6404 } else if (!collection) {6405 } else if (iteratorSymbol && collection[iteratorSymbol]) {6406 size = Infinity;6407 iter = collection[iteratorSymbol]();6408 iterate = iterator.length === 3 ? symbolIteratorWithKey : symbolIterator;6409 } else if (typeof collection === obj) {6410 keys = nativeKeys(collection);6411 size = keys.length;6412 iterate = iterator.length === 3 ? objectIteratorWithKey : objectIterator;6413 }6414 if (!size || isNaN(limit) || limit < 1) {6415 return callback(null, result);6416 }6417 timesSync(limit > size ? size : limit, iterate);6418 function arrayIterator() {6419 if (started < size) {6420 value = collection[started++];6421 iterator(value, createCallback(value));6422 }6423 }6424 function arrayIteratorWithIndex() {6425 index = started++;6426 if (index < size) {6427 value = collection[index];6428 iterator(value, index, createCallback(value));6429 }6430 }6431 function symbolIterator() {6432 item = iter.next();6433 if (item.done === false) {6434 started++;6435 value = item.value;6436 iterator(value, createCallback(value));6437 } else if (completed === started && iterator !== noop) {6438 iterator = noop;6439 callback(null, result);6440 }6441 }6442 function symbolIteratorWithKey() {6443 item = iter.next();6444 if (item.done === false) {6445 value = item.value;6446 iterator(value, started++, createCallback(value));6447 } else if (completed === started && iterator !== noop) {6448 iterator = noop;6449 callback(null, result);6450 }6451 }6452 function objectIterator() {6453 if (started < size) {6454 value = collection[keys[started++]];6455 iterator(value, createCallback(value));6456 }6457 }6458 function objectIteratorWithKey() {6459 if (started < size) {6460 key = keys[started++];6461 value = collection[key];6462 iterator(value, key, createCallback(value));6463 }6464 }6465 function createCallback(value) {6466 var called = false;6467 return function(err, key) {6468 if (called) {6469 throwError();6470 }6471 called = true;6472 if (err) {6473 iterate = noop;6474 callback = once(callback);6475 callback(err, objectClone(result));6476 return;6477 }6478 var array = result[key];6479 if (!array) {6480 result[key] = [value];6481 } else {6482 array.push(value);6483 }6484 if (++completed === size) {6485 callback(null, result);6486 } else if (sync) {6487 nextTick(iterate);6488 } else {6489 sync = true;6490 iterate();6491 }6492 sync = false;6493 };6494 }6495 }6496 /**6497 * @private6498 * @param {Function} arrayEach6499 * @param {Function} baseEach6500 */6501 function createParallel(arrayEach, baseEach) {6502 return function parallel(tasks, callback) {6503 callback = callback || noop;6504 var size, keys, result;6505 var completed = 0;6506 if (isArray(tasks)) {6507 size = tasks.length;6508 result = Array(size);6509 arrayEach(tasks, createCallback);6510 } else if (tasks && typeof tasks === obj) {6511 keys = nativeKeys(tasks);6512 size = keys.length;6513 result = {};6514 baseEach(tasks, createCallback, keys);6515 }6516 if (!size) {6517 callback(null, result);6518 }6519 function createCallback(key) {6520 return function(err, res) {6521 if (key === null) {6522 throwError();6523 }6524 if (err) {6525 key = null;6526 callback = once(callback);6527 callback(err, result);6528 return;6529 }6530 result[key] = arguments.length <= 2 ? res : slice(arguments, 1);6531 key = null;6532 if (++completed === size) {6533 callback(null, result);6534 }6535 };6536 }6537 };6538 }6539 /**6540 * @memberof async6541 * @namespace series6542 * @param {Array|Object} tasks - functions6543 * @param {Function} callback6544 * @example6545 *6546 * var order = [];6547 * var tasks = [6548 * function(done) {6549 * setTimeout(function() {6550 * order.push(1);6551 * done(null, 1);6552 * }, 10);6553 * },6554 * function(done) {6555 * setTimeout(function() {6556 * order.push(2);6557 * done(null, 2);6558 * }, 30);6559 * },6560 * function(done) {6561 * setTimeout(function() {6562 * order.push(3);6563 * done(null, 3);6564 * }, 40);6565 * },6566 * function(done) {6567 * setTimeout(function() {6568 * order.push(4);6569 * done(null, 4);6570 * }, 20);6571 * }6572 * ];6573 * async.series(tasks, function(err, res) {6574 * console.log(res); // [1, 2, 3, 4];6575 * console.log(order); // [1, 2, 3, 4]6576 * });6577 *6578 * @example6579 *6580 * var order = [];6581 * var tasks = {6582 * 'a': function(done) {6583 * setTimeout(function() {6584 * order.push(1);6585 * done(null, 1);6586 * }, 10);6587 * },6588 * 'b': function(done) {6589 * setTimeout(function() {6590 * order.push(2);6591 * done(null, 2);6592 * }, 30);6593 * },6594 * 'c': function(done) {6595 * setTimeout(function() {6596 * order.push(3);6597 * done(null, 3);6598 * }, 40);6599 * },6600 * 'd': function(done) {6601 * setTimeout(function() {6602 * order.push(4);6603 * done(null, 4);6604 * }, 20);6605 * }6606 * };6607 * async.series(tasks, function(err, res) {6608 * console.log(res); // { a: 1, b: 2, c: 3, d:4 }6609 * console.log(order); // [1, 4, 2, 3]6610 * });6611 *6612 */6613 function series(tasks, callback) {6614 callback = callback || noop;6615 var size, key, keys, result, iterate;6616 var sync = false;6617 var completed = 0;6618 if (isArray(tasks)) {6619 size = tasks.length;6620 result = Array(size);6621 iterate = arrayIterator;6622 } else if (tasks && typeof tasks === obj) {6623 keys = nativeKeys(tasks);6624 size = keys.length;6625 result = {};6626 iterate = objectIterator;6627 } else {6628 return callback(null);6629 }6630 if (!size) {6631 return callback(null, result);6632 }6633 iterate();6634 function arrayIterator() {6635 key = completed;6636 tasks[completed](done);6637 }6638 function objectIterator() {6639 key = keys[completed];6640 tasks[key](done);6641 }6642 function done(err, res) {6643 if (err) {6644 iterate = throwError;6645 callback = onlyOnce(callback);6646 callback(err, result);6647 return;6648 }6649 result[key] = arguments.length <= 2 ? res : slice(arguments, 1);6650 if (++completed === size) {6651 iterate = throwError;6652 callback(null, result);6653 } else if (sync) {6654 nextTick(iterate);6655 } else {6656 sync = true;6657 iterate();6658 }6659 sync = false;6660 }6661 }6662 /**6663 * @memberof async6664 * @namespace parallelLimit6665 * @param {Array|Object} tasks - functions6666 * @param {number} limit - limit >= 16667 * @param {Function} callback6668 * @example6669 *6670 * var order = [];6671 * var tasks = [6672 * function(done) {6673 * setTimeout(function() {6674 * order.push(1);6675 * done(null, 1);6676 * }, 10);6677 * },6678 * function(done) {6679 * setTimeout(function() {6680 * order.push(2);6681 * done(null, 2);6682 * }, 50);6683 * },6684 * function(done) {6685 * setTimeout(function() {6686 * order.push(3);6687 * done(null, 3);6688 * }, 30);6689 * },6690 * function(done) {6691 * setTimeout(function() {6692 * order.push(4);6693 * done(null, 4);6694 * }, 40);6695 * }6696 * ];6697 * async.parallelLimit(tasks, 2, function(err, res) {6698 * console.log(res); // [1, 2, 3, 4];6699 * console.log(order); // [1, 3, 2, 4]6700 * });6701 *6702 * @example6703 *6704 * var order = [];6705 * var tasks = {6706 * 'a': function(done) {6707 * setTimeout(function() {6708 * order.push(1);6709 * done(null, 1);6710 * }, 10);6711 * },6712 * 'b': function(done) {6713 * setTimeout(function() {6714 * order.push(2);6715 * done(null, 2);6716 * }, 50);6717 * },6718 * 'c': function(done) {6719 * setTimeout(function() {6720 * order.push(3);6721 * done(null, 3);6722 * }, 20);6723 * },6724 * 'd': function(done) {6725 * setTimeout(function() {6726 * order.push(4);6727 * done(null, 4);6728 * }, 40);6729 * }6730 * };6731 * async.parallelLimit(tasks, 2, function(err, res) {6732 * console.log(res); // { a: 1, b: 2, c: 3, d:4 }6733 * console.log(order); // [1, 3, 2, 4]6734 * });6735 *6736 */6737 function parallelLimit(tasks, limit, callback) {6738 callback = callback || noop;6739 var size, index, key, keys, result, iterate;6740 var sync = false;6741 var started = 0;6742 var completed = 0;6743 if (isArray(tasks)) {6744 size = tasks.length;6745 result = Array(size);6746 iterate = arrayIterator;6747 } else if (tasks && typeof tasks === obj) {6748 keys = nativeKeys(tasks);6749 size = keys.length;6750 result = {};6751 iterate = objectIterator;6752 }6753 if (!size || isNaN(limit) || limit < 1) {6754 return callback(null, result);6755 }6756 timesSync(limit > size ? size : limit, iterate);6757 function arrayIterator() {6758 index = started++;6759 if (index < size) {6760 tasks[index](createCallback(index));6761 }6762 }6763 function objectIterator() {6764 if (started < size) {6765 key = keys[started++];6766 tasks[key](createCallback(key));6767 }6768 }6769 function createCallback(key) {6770 return function(err, res) {6771 if (key === null) {6772 throwError();6773 }6774 if (err) {6775 key = null;6776 iterate = noop;6777 callback = once(callback);6778 callback(err, result);6779 return;6780 }6781 result[key] = arguments.length <= 2 ? res : slice(arguments, 1);6782 key = null;6783 if (++completed === size) {6784 callback(null, result);6785 } else if (sync) {6786 nextTick(iterate);6787 } else {6788 sync = true;6789 iterate();6790 }6791 sync = false;6792 };6793 }6794 }6795 /**6796 * @memberof async6797 * @namespace tryEach6798 * @param {Array|Object} tasks - functions6799 * @param {Function} callback6800 * @example6801 *6802 * var tasks = [6803 * function(done) {6804 * setTimeout(function() {6805 * done(new Error('error'));6806 * }, 10);6807 * },6808 * function(done) {6809 * setTimeout(function() {6810 * done(null, 2);6811 * }, 10);6812 * }6813 * ];6814 * async.tryEach(tasks, function(err, res) {6815 * console.log(res); // 26816 * });6817 *6818 * @example6819 *6820 * var tasks = [6821 * function(done) {6822 * setTimeout(function() {6823 * done(new Error('error1'));6824 * }, 10);6825 * },6826 * function(done) {6827 * setTimeout(function() {6828 * done(new Error('error2');6829 * }, 10);6830 * }6831 * ];6832 * async.tryEach(tasks, function(err, res) {6833 * console.log(err); // error26834 * console.log(res); // undefined6835 * });6836 *6837 */6838 function tryEach(tasks, callback) {6839 callback = callback || noop;6840 var size, keys, iterate;6841 var sync = false;6842 var completed = 0;6843 if (isArray(tasks)) {6844 size = tasks.length;6845 iterate = arrayIterator;6846 } else if (tasks && typeof tasks === obj) {6847 keys = nativeKeys(tasks);6848 size = keys.length;6849 iterate = objectIterator;6850 }6851 if (!size) {6852 return callback(null);6853 }6854 iterate();6855 function arrayIterator() {6856 tasks[completed](done);6857 }6858 function objectIterator() {6859 tasks[keys[completed]](done);6860 }6861 function done(err, res) {6862 if (!err) {6863 if (arguments.length <= 2) {6864 callback(null, res);6865 } else {6866 callback(null, slice(arguments, 1));6867 }6868 } else if (++completed === size) {6869 callback(err);6870 } else {6871 sync = true;6872 iterate();6873 }6874 sync = false;6875 }6876 }6877 /**6878 * check for waterfall tasks6879 * @private6880 * @param {Array} tasks6881 * @param {Function} callback6882 * @return {boolean}6883 */6884 function checkWaterfallTasks(tasks, callback) {6885 if (!isArray(tasks)) {6886 callback(new Error('First argument to waterfall must be an array of functions'));6887 return false;6888 }6889 if (tasks.length === 0) {6890 callback(null);6891 return false;6892 }6893 return true;6894 }6895 /**6896 * check for waterfall tasks6897 * @private6898 * @param {function} func6899 * @param {Array|Object} args - arguments6900 * @return {function} next6901 */6902 function waterfallIterator(func, args, next) {6903 switch (args.length) {6904 case 0:6905 case 1:6906 return func(next);6907 case 2:6908 return func(args[1], next);6909 case 3:6910 return func(args[1], args[2], next);6911 case 4:6912 return func(args[1], args[2], args[3], next);6913 case 5:6914 return func(args[1], args[2], args[3], args[4], next);6915 case 6:6916 return func(args[1], args[2], args[3], args[4], args[5], next);6917 default:6918 args = slice(args, 1);6919 args.push(next);6920 return func.apply(null, args);6921 }6922 }6923 /**6924 * @memberof async6925 * @namespace waterfall6926 * @param {Array} tasks - functions6927 * @param {Function} callback6928 * @example6929 *6930 * var order = [];6931 * var tasks = [6932 * function(next) {6933 * setTimeout(function() {6934 * order.push(1);6935 * next(null, 1);6936 * }, 10);6937 * },6938 * function(arg1, next) {6939 * setTimeout(function() {6940 * order.push(2);6941 * next(null, 1, 2);6942 * }, 30);6943 * },6944 * function(arg1, arg2, next) {6945 * setTimeout(function() {6946 * order.push(3);6947 * next(null, 3);6948 * }, 20);6949 * },6950 * function(arg1, next) {6951 * setTimeout(function() {6952 * order.push(4);6953 * next(null, 1, 2, 3, 4);6954 * }, 40);6955 * }6956 * ];6957 * async.waterfall(tasks, function(err, arg1, arg2, arg3, arg4) {6958 * console.log(arg1, arg2, arg3, arg4); // 1 2 3 46959 * });6960 *6961 */6962 function waterfall(tasks, callback) {6963 callback = callback || noop;6964 if (!checkWaterfallTasks(tasks, callback)) {6965 return;6966 }6967 var func, args, done, sync;6968 var completed = 0;6969 var size = tasks.length;6970 waterfallIterator(tasks[0], [], createCallback(0));6971 function iterate() {6972 waterfallIterator(func, args, createCallback(func));6973 }6974 function createCallback(index) {6975 return function next(err, res) {6976 if (index === undefined) {6977 callback = noop;6978 throwError();6979 }6980 index = undefined;6981 if (err) {6982 done = callback;6983 callback = throwError;6984 done(err);6985 return;6986 }6987 if (++completed === size) {6988 done = callback;6989 callback = throwError;6990 if (arguments.length <= 2) {6991 done(err, res);6992 } else {6993 done.apply(null, createArray(arguments));6994 }6995 return;6996 }6997 if (sync) {6998 args = arguments;6999 func = tasks[completed] || throwError;7000 nextTick(iterate);7001 } else {7002 sync = true;7003 waterfallIterator(tasks[completed] || throwError, arguments, createCallback(completed));7004 }7005 sync = false;7006 };7007 }7008 }7009 /**7010 * `angelFall` is like `waterfall` and inject callback to last argument of next task.7011 *7012 * @memberof async7013 * @namespace angelFall7014 * @param {Array} tasks - functions7015 * @param {Function} callback7016 * @example7017 *7018 * var order = [];7019 * var tasks = [7020 * function(next) {7021 * setTimeout(function() {7022 * order.push(1);7023 * next(null, 1);7024 * }, 10);7025 * },7026 * function(arg1, empty, next) {7027 * setTimeout(function() {7028 * order.push(2);7029 * next(null, 1, 2);7030 * }, 30);7031 * },7032 * function(next) {7033 * setTimeout(function() {7034 * order.push(3);7035 * next(null, 3);7036 * }, 20);7037 * },7038 * function(arg1, empty1, empty2, empty3, next) {7039 * setTimeout(function() {7040 * order.push(4);7041 * next(null, 1, 2, 3, 4);7042 * }, 40);7043 * }7044 * ];7045 * async.angelFall(tasks, function(err, arg1, arg2, arg3, arg4) {7046 * console.log(arg1, arg2, arg3, arg4); // 1 2 3 47047 * });7048 *7049 */7050 function angelFall(tasks, callback) {7051 callback = callback || noop;7052 if (!checkWaterfallTasks(tasks, callback)) {7053 return;7054 }7055 var completed = 0;7056 var sync = false;7057 var size = tasks.length;7058 var func = tasks[completed];7059 var args = [];7060 var iterate = function() {7061 switch (func.length) {7062 case 0:7063 try {7064 next(null, func());7065 } catch (e) {7066 next(e);7067 }7068 return;7069 case 1:7070 return func(next);7071 case 2:7072 return func(args[1], next);7073 case 3:7074 return func(args[1], args[2], next);7075 case 4:7076 return func(args[1], args[2], args[3], next);7077 case 5:7078 return func(args[1], args[2], args[3], args[4], next);7079 default:7080 args = slice(args, 1);7081 args[func.length - 1] = next;7082 return func.apply(null, args);7083 }7084 };7085 iterate();7086 function next(err, res) {7087 if (err) {7088 iterate = throwError;7089 callback = onlyOnce(callback);7090 callback(err);7091 return;7092 }7093 if (++completed === size) {7094 iterate = throwError;7095 var done = callback;7096 callback = throwError;7097 if (arguments.length === 2) {7098 done(err, res);7099 } else {7100 done.apply(null, createArray(arguments));7101 }7102 return;7103 }7104 func = tasks[completed];7105 args = arguments;7106 if (sync) {7107 nextTick(iterate);7108 } else {7109 sync = true;7110 iterate();7111 }7112 sync = false;7113 }7114 }7115 /**7116 * @memberof async7117 * @namespace whilst7118 * @param {Function} test7119 * @param {Function} iterator7120 * @param {Function} callback7121 */7122 function whilst(test, iterator, callback) {7123 callback = callback || noop;7124 var sync = false;7125 if (test()) {7126 iterate();7127 } else {7128 callback(null);7129 }7130 function iterate() {7131 if (sync) {7132 nextTick(next);7133 } else {7134 sync = true;7135 iterator(done);7136 }7137 sync = false;7138 }7139 function next() {7140 iterator(done);7141 }7142 function done(err, arg) {7143 if (err) {7144 return callback(err);7145 }7146 if (arguments.length <= 2) {7147 if (test(arg)) {7148 iterate();7149 } else {7150 callback(null, arg);7151 }7152 return;7153 }7154 arg = slice(arguments, 1);7155 if (test.apply(null, arg)) {7156 iterate();7157 } else {7158 callback.apply(null, [null].concat(arg));7159 }7160 }7161 }7162 /**7163 * @memberof async7164 * @namespace doWhilst7165 * @param {Function} iterator7166 * @param {Function} test7167 * @param {Function} callback7168 */7169 function doWhilst(iterator, test, callback) {7170 callback = callback || noop;7171 var sync = false;7172 next();7173 function iterate() {7174 if (sync) {7175 nextTick(next);7176 } else {7177 sync = true;7178 iterator(done);7179 }7180 sync = false;7181 }7182 function next() {7183 iterator(done);7184 }7185 function done(err, arg) {7186 if (err) {7187 return callback(err);7188 }7189 if (arguments.length <= 2) {7190 if (test(arg)) {7191 iterate();7192 } else {7193 callback(null, arg);7194 }7195 return;7196 }7197 arg = slice(arguments, 1);7198 if (test.apply(null, arg)) {7199 iterate();7200 } else {7201 callback.apply(null, [null].concat(arg));7202 }7203 }7204 }7205 /**7206 * @memberof async7207 * @namespace until7208 * @param {Function} test7209 * @param {Function} iterator7210 * @param {Function} callback7211 */7212 function until(test, iterator, callback) {7213 callback = callback || noop;7214 var sync = false;7215 if (!test()) {7216 iterate();7217 } else {7218 callback(null);7219 }7220 function iterate() {7221 if (sync) {7222 nextTick(next);7223 } else {7224 sync = true;7225 iterator(done);7226 }7227 sync = false;7228 }7229 function next() {7230 iterator(done);7231 }7232 function done(err, arg) {7233 if (err) {7234 return callback(err);7235 }7236 if (arguments.length <= 2) {7237 if (!test(arg)) {7238 iterate();7239 } else {7240 callback(null, arg);7241 }7242 return;7243 }7244 arg = slice(arguments, 1);7245 if (!test.apply(null, arg)) {7246 iterate();7247 } else {7248 callback.apply(null, [null].concat(arg));7249 }7250 }7251 }7252 /**7253 * @memberof async7254 * @namespace doUntil7255 * @param {Function} iterator7256 * @param {Function} test7257 * @param {Function} callback7258 */7259 function doUntil(iterator, test, callback) {7260 callback = callback || noop;7261 var sync = false;7262 next();7263 function iterate() {7264 if (sync) {7265 nextTick(next);7266 } else {7267 sync = true;7268 iterator(done);7269 }7270 sync = false;7271 }7272 function next() {7273 iterator(done);7274 }7275 function done(err, arg) {7276 if (err) {7277 return callback(err);7278 }7279 if (arguments.length <= 2) {7280 if (!test(arg)) {7281 iterate();7282 } else {7283 callback(null, arg);7284 }7285 return;7286 }7287 arg = slice(arguments, 1);7288 if (!test.apply(null, arg)) {7289 iterate();7290 } else {7291 callback.apply(null, [null].concat(arg));7292 }7293 }7294 }7295 /**7296 * @memberof async7297 * @namespace during7298 * @param {Function} test7299 * @param {Function} iterator7300 * @param {Function} callback7301 */7302 function during(test, iterator, callback) {7303 callback = callback || noop;7304 _test();7305 function _test() {7306 test(iterate);7307 }7308 function iterate(err, truth) {7309 if (err) {7310 return callback(err);7311 }7312 if (truth) {7313 iterator(done);7314 } else {7315 callback(null);7316 }7317 }7318 function done(err) {7319 if (err) {7320 return callback(err);7321 }7322 _test();7323 }7324 }7325 /**7326 * @memberof async7327 * @namespace doDuring7328 * @param {Function} test7329 * @param {Function} iterator7330 * @param {Function} callback7331 */7332 function doDuring(iterator, test, callback) {7333 callback = callback || noop;7334 iterate(null, true);7335 function iterate(err, truth) {7336 if (err) {7337 return callback(err);7338 }7339 if (truth) {7340 iterator(done);7341 } else {7342 callback(null);7343 }7344 }7345 function done(err, res) {7346 if (err) {7347 return callback(err);7348 }7349 switch (arguments.length) {7350 case 0:7351 case 1:7352 test(iterate);7353 break;7354 case 2:7355 test(res, iterate);7356 break;7357 default:7358 var args = slice(arguments, 1);7359 args.push(iterate);7360 test.apply(null, args);7361 break;7362 }7363 }7364 }7365 /**7366 * @memberof async7367 * @namespace forever7368 */7369 function forever(iterator, callback) {7370 var sync = false;7371 iterate();7372 function iterate() {7373 iterator(next);7374 }7375 function next(err) {7376 if (err) {7377 if (callback) {7378 return callback(err);7379 }7380 throw err;7381 }7382 if (sync) {7383 nextTick(iterate);7384 } else {7385 sync = true;7386 iterate();7387 }7388 sync = false;7389 }7390 }7391 /**7392 * @memberof async7393 * @namespace compose7394 */7395 function compose() {7396 return seq.apply(null, reverse(arguments));7397 }7398 /**7399 * @memberof async7400 * @namespace seq7401 */7402 function seq(/* functions... */) {7403 var fns = createArray(arguments);7404 return function() {7405 var self = this;7406 var args = createArray(arguments);7407 var callback = args[args.length - 1];7408 if (typeof callback === func) {7409 args.pop();7410 } else {7411 callback = noop;7412 }7413 reduce(fns, args, iterator, done);7414 function iterator(newargs, fn, callback) {7415 var func = function(err) {7416 var nextargs = slice(arguments, 1);7417 callback(err, nextargs);7418 };7419 newargs.push(func);7420 fn.apply(self, newargs);7421 }7422 function done(err, res) {7423 res = isArray(res) ? res : [res];7424 res.unshift(err);7425 callback.apply(self, res);7426 }7427 };7428 }7429 function createApplyEach(func) {7430 return function applyEach(fns /* arguments */) {7431 var go = function() {7432 var self = this;7433 var args = createArray(arguments);7434 var callback = args.pop() || noop;7435 return func(fns, iterator, callback);7436 function iterator(fn, done) {7437 fn.apply(self, args.concat([done]));7438 }7439 };7440 if (arguments.length > 1) {7441 var args = slice(arguments, 1);7442 return go.apply(this, args);7443 } else {7444 return go;7445 }7446 };7447 }7448 /**7449 * @see https://github.com/caolan/async/blob/master/lib/internal/DoublyLinkedList.js7450 */7451 function DLL() {7452 this.head = null;7453 this.tail = null;7454 this.length = 0;7455 }7456 DLL.prototype._removeLink = function(node) {7457 var prev = node.prev;7458 var next = node.next;7459 if (prev) {7460 prev.next = next;7461 } else {7462 this.head = next;7463 }7464 if (next) {7465 next.prev = prev;7466 } else {7467 this.tail = prev;7468 }7469 node.prev = null;7470 node.next = null;7471 this.length--;7472 return node;7473 };7474 DLL.prototype.empty = DLL;7475 DLL.prototype._setInitial = function(node) {7476 this.length = 1;7477 this.head = this.tail = node;7478 };7479 DLL.prototype.insertBefore = function(node, newNode) {7480 newNode.prev = node.prev;7481 newNode.next = node;7482 if (node.prev) {7483 node.prev.next = newNode;7484 } else {7485 this.head = newNode;7486 }7487 node.prev = newNode;7488 this.length++;7489 };7490 DLL.prototype.unshift = function(node) {7491 if (this.head) {7492 this.insertBefore(this.head, node);7493 } else {7494 this._setInitial(node);7495 }7496 };7497 DLL.prototype.push = function(node) {7498 var tail = this.tail;7499 if (tail) {7500 node.prev = tail;7501 node.next = tail.next;7502 this.tail = node;7503 tail.next = node;7504 this.length++;7505 } else {7506 this._setInitial(node);7507 }7508 };7509 DLL.prototype.shift = function() {7510 return this.head && this._removeLink(this.head);7511 };7512 DLL.prototype.splice = function(end) {7513 var task;7514 var tasks = [];7515 while (end-- && (task = this.shift())) {7516 tasks.push(task);7517 }7518 return tasks;7519 };7520 DLL.prototype.remove = function(test) {7521 var node = this.head;7522 while (node) {7523 if (test(node)) {7524 this._removeLink(node);7525 }7526 node = node.next;7527 }7528 return this;7529 };7530 /**7531 * @private7532 */7533 function baseQueue(isQueue, worker, concurrency, payload) {7534 if (concurrency === undefined) {7535 concurrency = 1;7536 } else if (isNaN(concurrency) || concurrency < 1) {7537 throw new Error('Concurrency must not be zero');7538 }7539 var workers = 0;7540 var workersList = [];7541 var _callback, _unshift;7542 var q = {7543 _tasks: new DLL(),7544 concurrency: concurrency,7545 payload: payload,7546 saturated: noop,7547 unsaturated: noop,7548 buffer: concurrency / 4,7549 empty: noop,7550 drain: noop,7551 error: noop,7552 started: false,7553 paused: false,7554 push: push,7555 kill: kill,7556 unshift: unshift,7557 remove: remove,7558 process: isQueue ? runQueue : runCargo,7559 length: getLength,7560 running: running,7561 workersList: getWorkersList,7562 idle: idle,7563 pause: pause,7564 resume: resume,7565 _worker: worker7566 };7567 return q;7568 function push(tasks, callback) {7569 _insert(tasks, callback);7570 }7571 function unshift(tasks, callback) {7572 _insert(tasks, callback, true);7573 }7574 function _exec(task) {7575 var item = {7576 data: task,7577 callback: _callback7578 };7579 if (_unshift) {7580 q._tasks.unshift(item);7581 } else {7582 q._tasks.push(item);7583 }7584 nextTick(q.process);7585 }7586 function _insert(tasks, callback, unshift) {7587 if (callback == null) {7588 callback = noop;7589 } else if (typeof callback !== 'function') {7590 throw new Error('task callback must be a function');7591 }7592 q.started = true;7593 var _tasks = isArray(tasks) ? tasks : [tasks];7594 if (tasks === undefined || !_tasks.length) {7595 if (q.idle()) {7596 nextTick(q.drain);7597 }7598 return;7599 }7600 _unshift = unshift;7601 _callback = callback;7602 arrayEachSync(_tasks, _exec);7603 // Avoid leaking the callback7604 _callback = undefined;7605 }7606 function kill() {7607 q.drain = noop;7608 q._tasks.empty();7609 }7610 function _next(q, tasks) {7611 var called = false;7612 return function done(err, res) {7613 if (called) {7614 throwError();7615 }7616 called = true;7617 workers--;7618 var task;7619 var index = -1;7620 var size = workersList.length;7621 var taskIndex = -1;7622 var taskSize = tasks.length;7623 var useApply = arguments.length > 2;7624 var args = useApply && createArray(arguments);7625 while (++taskIndex < taskSize) {7626 task = tasks[taskIndex];7627 while (++index < size) {7628 if (workersList[index] === task) {7629 if (index === 0) {7630 workersList.shift();7631 } else {7632 workersList.splice(index, 1);7633 }7634 index = size;7635 size--;7636 }7637 }7638 index = -1;7639 if (useApply) {7640 task.callback.apply(task, args);7641 } else {7642 task.callback(err, res);7643 }7644 if (err) {7645 q.error(err, task.data);7646 }7647 }7648 if (workers <= q.concurrency - q.buffer) {7649 q.unsaturated();7650 }7651 if (q._tasks.length + workers === 0) {7652 q.drain();7653 }7654 q.process();7655 };7656 }7657 function runQueue() {7658 while (!q.paused && workers < q.concurrency && q._tasks.length) {7659 var task = q._tasks.shift();7660 workers++;7661 workersList.push(task);7662 if (q._tasks.length === 0) {7663 q.empty();7664 }7665 if (workers === q.concurrency) {7666 q.saturated();7667 }7668 var done = _next(q, [task]);7669 worker(task.data, done);7670 }7671 }7672 function runCargo() {7673 while (!q.paused && workers < q.concurrency && q._tasks.length) {7674 var tasks = q._tasks.splice(q.payload || q._tasks.length);7675 var index = -1;7676 var size = tasks.length;7677 var data = Array(size);7678 while (++index < size) {7679 data[index] = tasks[index].data;7680 }7681 workers++;7682 nativePush.apply(workersList, tasks);7683 if (q._tasks.length === 0) {7684 q.empty();7685 }7686 if (workers === q.concurrency) {7687 q.saturated();7688 }7689 var done = _next(q, tasks);7690 worker(data, done);7691 }7692 }7693 function getLength() {7694 return q._tasks.length;7695 }7696 function running() {7697 return workers;7698 }7699 function getWorkersList() {7700 return workersList;7701 }7702 function idle() {7703 return q.length() + workers === 0;7704 }7705 function pause() {7706 q.paused = true;7707 }7708 function _resume() {7709 nextTick(q.process);7710 }7711 function resume() {7712 if (q.paused === false) {7713 return;7714 }7715 q.paused = false;7716 var count = q.concurrency < q._tasks.length ? q.concurrency : q._tasks.length;7717 timesSync(count, _resume);7718 }7719 /**7720 * @param {Function} test7721 */7722 function remove(test) {7723 q._tasks.remove(test);7724 }7725 }7726 /**7727 * @memberof async7728 * @namespace queue7729 */7730 function queue(worker, concurrency) {7731 return baseQueue(true, worker, concurrency);7732 }7733 /**7734 * @memberof async7735 * @namespace priorityQueue7736 */7737 function priorityQueue(worker, concurrency) {7738 var q = baseQueue(true, worker, concurrency);7739 q.push = push;7740 delete q.unshift;7741 return q;7742 function push(tasks, priority, callback) {7743 q.started = true;7744 priority = priority || 0;7745 var _tasks = isArray(tasks) ? tasks : [tasks];7746 var taskSize = _tasks.length;7747 if (tasks === undefined || taskSize === 0) {7748 if (q.idle()) {7749 nextTick(q.drain);7750 }7751 return;7752 }7753 callback = typeof callback === func ? callback : noop;7754 var nextNode = q._tasks.head;7755 while (nextNode && priority >= nextNode.priority) {7756 nextNode = nextNode.next;7757 }7758 while (taskSize--) {7759 var item = {7760 data: _tasks[taskSize],7761 priority: priority,7762 callback: callback7763 };7764 if (nextNode) {7765 q._tasks.insertBefore(nextNode, item);7766 } else {7767 q._tasks.push(item);7768 }7769 nextTick(q.process);7770 }7771 }7772 }7773 /**7774 * @memberof async7775 * @namespace cargo7776 */7777 function cargo(worker, payload) {7778 return baseQueue(false, worker, 1, payload);7779 }7780 /**7781 * @memberof async7782 * @namespace auto7783 * @param {Object} tasks7784 * @param {number} [concurrency]7785 * @param {Function} [callback]7786 */7787 function auto(tasks, concurrency, callback) {7788 if (typeof concurrency === func) {7789 callback = concurrency;7790 concurrency = null;7791 }7792 var keys = nativeKeys(tasks);7793 var rest = keys.length;7794 var results = {};7795 if (rest === 0) {7796 return callback(null, results);7797 }7798 var runningTasks = 0;7799 var readyTasks = new DLL();7800 var listeners = Object.create(null);7801 callback = onlyOnce(callback || noop);7802 concurrency = concurrency || rest;7803 baseEachSync(tasks, iterator, keys);7804 proceedQueue();7805 function iterator(task, key) {7806 // no dependencies7807 var _task, _taskSize;7808 if (!isArray(task)) {7809 _task = task;7810 _taskSize = 0;7811 readyTasks.push([_task, _taskSize, done]);7812 return;7813 }7814 var dependencySize = task.length - 1;7815 _task = task[dependencySize];7816 _taskSize = dependencySize;7817 if (dependencySize === 0) {7818 readyTasks.push([_task, _taskSize, done]);7819 return;7820 }7821 // dependencies7822 var index = -1;7823 while (++index < dependencySize) {7824 var dependencyName = task[index];7825 if (notInclude(keys, dependencyName)) {7826 var msg =7827 'async.auto task `' +7828 key +7829 '` has non-existent dependency `' +7830 dependencyName +7831 '` in ' +7832 task.join(', ');7833 throw new Error(msg);7834 }7835 var taskListeners = listeners[dependencyName];7836 if (!taskListeners) {7837 taskListeners = listeners[dependencyName] = [];7838 }7839 taskListeners.push(taskListener);7840 }7841 function done(err, arg) {7842 if (key === null) {7843 throwError();7844 }7845 arg = arguments.length <= 2 ? arg : slice(arguments, 1);7846 if (err) {7847 rest = 0;7848 runningTasks = 0;7849 readyTasks.length = 0;7850 var safeResults = objectClone(results);7851 safeResults[key] = arg;7852 key = null;7853 var _callback = callback;7854 callback = noop;7855 _callback(err, safeResults);7856 return;7857 }7858 runningTasks--;7859 rest--;7860 results[key] = arg;7861 taskComplete(key);7862 key = null;7863 }7864 function taskListener() {7865 if (--dependencySize === 0) {7866 readyTasks.push([_task, _taskSize, done]);7867 }7868 }7869 }7870 function proceedQueue() {7871 if (readyTasks.length === 0 && runningTasks === 0) {7872 if (rest !== 0) {7873 throw new Error('async.auto task has cyclic dependencies');7874 }7875 return callback(null, results);7876 }7877 while (readyTasks.length && runningTasks < concurrency && callback !== noop) {7878 runningTasks++;7879 var array = readyTasks.shift();7880 if (array[1] === 0) {7881 array[0](array[2]);7882 } else {7883 array[0](results, array[2]);7884 }7885 }7886 }7887 function taskComplete(key) {7888 var taskListeners = listeners[key] || [];7889 arrayEachSync(taskListeners, function(task) {7890 task();7891 });7892 proceedQueue();7893 }7894 }7895 var FN_ARGS = /^(function)?\s*[^\(]*\(\s*([^\)]*)\)/m;7896 var FN_ARG_SPLIT = /,/;7897 var FN_ARG = /(=.+)?(\s*)$/;7898 var STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/gm;7899 /**7900 * parse function arguments for `autoInject`7901 *7902 * @private7903 */7904 function parseParams(func) {7905 func = func.toString().replace(STRIP_COMMENTS, '');7906 func = func.match(FN_ARGS)[2].replace(' ', '');7907 func = func ? func.split(FN_ARG_SPLIT) : [];7908 func = func.map(function(arg) {7909 return arg.replace(FN_ARG, '').trim();7910 });7911 return func;7912 }7913 /**7914 * @memberof async7915 * @namespace autoInject7916 * @param {Object} tasks7917 * @param {number} [concurrency]7918 * @param {Function} [callback]7919 */7920 function autoInject(tasks, concurrency, callback) {7921 var newTasks = {};7922 baseEachSync(tasks, iterator, nativeKeys(tasks));7923 auto(newTasks, concurrency, callback);7924 function iterator(task, key) {7925 var params;7926 var taskLength = task.length;7927 if (isArray(task)) {7928 if (taskLength === 0) {7929 throw new Error('autoInject task functions require explicit parameters.');7930 }7931 params = createArray(task);7932 taskLength = params.length - 1;7933 task = params[taskLength];7934 if (taskLength === 0) {7935 newTasks[key] = task;7936 return;7937 }7938 } else if (taskLength === 1) {7939 newTasks[key] = task;7940 return;7941 } else {7942 params = parseParams(task);7943 if (taskLength === 0 && params.length === 0) {7944 throw new Error('autoInject task functions require explicit parameters.');7945 }7946 taskLength = params.length - 1;7947 }7948 params[taskLength] = newTask;7949 newTasks[key] = params;7950 function newTask(results, done) {7951 switch (taskLength) {7952 case 1:7953 task(results[params[0]], done);7954 break;7955 case 2:7956 task(results[params[0]], results[params[1]], done);7957 break;7958 case 3:7959 task(results[params[0]], results[params[1]], results[params[2]], done);7960 break;7961 default:7962 var i = -1;7963 while (++i < taskLength) {7964 params[i] = results[params[i]];7965 }7966 params[i] = done;7967 task.apply(null, params);7968 break;7969 }7970 }7971 }7972 }7973 /**7974 * @memberof async7975 * @namespace retry7976 * @param {integer|Object|Function} opts7977 * @param {Function} [task]7978 * @param {Function} [callback]7979 */7980 function retry(opts, task, callback) {7981 var times, intervalFunc, errorFilter;7982 var count = 0;7983 if (arguments.length < 3 && typeof opts === func) {7984 callback = task || noop;7985 task = opts;7986 opts = null;7987 times = DEFAULT_TIMES;7988 } else {7989 callback = callback || noop;7990 switch (typeof opts) {7991 case 'object':7992 if (typeof opts.errorFilter === func) {7993 errorFilter = opts.errorFilter;7994 }7995 var interval = opts.interval;7996 switch (typeof interval) {7997 case func:7998 intervalFunc = interval;7999 break;8000 case 'string':8001 case 'number':8002 interval = +interval;8003 intervalFunc = interval8004 ? function() {8005 return interval;8006 }8007 : function() {8008 return DEFAULT_INTERVAL;8009 };8010 break;8011 }8012 times = +opts.times || DEFAULT_TIMES;8013 break;8014 case 'number':8015 times = opts || DEFAULT_TIMES;8016 break;8017 case 'string':8018 times = +opts || DEFAULT_TIMES;8019 break;8020 default:8021 throw new Error('Invalid arguments for async.retry');8022 }8023 }8024 if (typeof task !== 'function') {8025 throw new Error('Invalid arguments for async.retry');8026 }8027 if (intervalFunc) {8028 task(intervalCallback);8029 } else {8030 task(simpleCallback);8031 }8032 function simpleIterator() {8033 task(simpleCallback);8034 }8035 function simpleCallback(err, res) {8036 if (++count === times || !err || (errorFilter && !errorFilter(err))) {8037 if (arguments.length <= 2) {8038 return callback(err, res);8039 }8040 var args = createArray(arguments);8041 return callback.apply(null, args);8042 }8043 simpleIterator();8044 }8045 function intervalIterator() {8046 task(intervalCallback);8047 }8048 function intervalCallback(err, res) {8049 if (++count === times || !err || (errorFilter && !errorFilter(err))) {8050 if (arguments.length <= 2) {8051 return callback(err, res);8052 }8053 var args = createArray(arguments);8054 return callback.apply(null, args);8055 }8056 setTimeout(intervalIterator, intervalFunc(count));8057 }8058 }8059 function retryable(opts, task) {8060 if (!task) {8061 task = opts;8062 opts = null;8063 }8064 return done;8065 function done() {8066 var taskFn;8067 var args = createArray(arguments);8068 var lastIndex = args.length - 1;8069 var callback = args[lastIndex];8070 switch (task.length) {8071 case 1:8072 taskFn = task1;8073 break;8074 case 2:8075 taskFn = task2;8076 break;8077 case 3:8078 taskFn = task3;8079 break;8080 default:8081 taskFn = task4;8082 }8083 if (opts) {8084 retry(opts, taskFn, callback);8085 } else {8086 retry(taskFn, callback);8087 }8088 function task1(done) {8089 task(done);8090 }8091 function task2(done) {8092 task(args[0], done);8093 }8094 function task3(done) {8095 task(args[0], args[1], done);8096 }8097 function task4(callback) {8098 args[lastIndex] = callback;8099 task.apply(null, args);8100 }8101 }8102 }8103 /**8104 * @memberof async8105 * @namespace iterator8106 */8107 function iterator(tasks) {8108 var size = 0;8109 var keys = [];8110 if (isArray(tasks)) {8111 size = tasks.length;8112 } else {8113 keys = nativeKeys(tasks);8114 size = keys.length;8115 }8116 return makeCallback(0);8117 function makeCallback(index) {8118 var fn = function() {8119 if (size) {8120 var key = keys[index] || index;8121 tasks[key].apply(null, createArray(arguments));8122 }8123 return fn.next();8124 };8125 fn.next = function() {8126 return index < size - 1 ? makeCallback(index + 1) : null;8127 };8128 return fn;8129 }8130 }8131 /**8132 * @memberof async8133 * @namespace apply8134 */8135 function apply(func) {8136 switch (arguments.length) {8137 case 0:8138 case 1:8139 return func;8140 case 2:8141 return func.bind(null, arguments[1]);8142 case 3:8143 return func.bind(null, arguments[1], arguments[2]);8144 case 4:8145 return func.bind(null, arguments[1], arguments[2], arguments[3]);8146 case 5:8147 return func.bind(null, arguments[1], arguments[2], arguments[3], arguments[4]);8148 default:8149 var size = arguments.length;8150 var index = 0;8151 var args = Array(size);8152 args[index] = null;8153 while (++index < size) {8154 args[index] = arguments[index];8155 }8156 return func.bind.apply(func, args);8157 }8158 }8159 /**8160 * @memberof async8161 * @namespace timeout8162 * @param {Function} func8163 * @param {number} millisec8164 * @param {*} info8165 */8166 function timeout(func, millisec, info) {8167 var callback, timer;8168 return wrappedFunc;8169 function wrappedFunc() {8170 timer = setTimeout(timeoutCallback, millisec);8171 var args = createArray(arguments);8172 var lastIndex = args.length - 1;8173 callback = args[lastIndex];8174 args[lastIndex] = injectedCallback;8175 simpleApply(func, args);8176 }8177 function timeoutCallback() {8178 var name = func.name || 'anonymous';8179 var err = new Error('Callback function "' + name + '" timed out.');8180 err.code = 'ETIMEDOUT';8181 if (info) {8182 err.info = info;8183 }8184 timer = null;8185 callback(err);8186 }8187 function injectedCallback() {8188 if (timer !== null) {8189 simpleApply(callback, createArray(arguments));8190 clearTimeout(timer);8191 }8192 }8193 function simpleApply(func, args) {8194 switch (args.length) {8195 case 0:8196 func();8197 break;8198 case 1:8199 func(args[0]);8200 break;8201 case 2:8202 func(args[0], args[1]);8203 break;8204 default:8205 func.apply(null, args);8206 break;8207 }8208 }8209 }8210 /**8211 * @memberof async8212 * @namespace times8213 * @param {number} n - n >= 18214 * @param {Function} iterator8215 * @param {Function} callback8216 * @example8217 *8218 * var iterator = function(n, done) {8219 * done(null, n);8220 * };8221 * async.times(4, iterator, function(err, res) {8222 * console.log(res); // [0, 1, 2, 3];8223 * });8224 *8225 */8226 function times(n, iterator, callback) {8227 callback = callback || noop;8228 n = +n;8229 if (isNaN(n) || n < 1) {8230 return callback(null, []);8231 }8232 var result = Array(n);8233 timesSync(n, iterate);8234 function iterate(num) {8235 iterator(num, createCallback(num));8236 }8237 function createCallback(index) {8238 return function(err, res) {8239 if (index === null) {8240 throwError();8241 }8242 result[index] = res;8243 index = null;8244 if (err) {8245 callback(err);8246 callback = noop;8247 } else if (--n === 0) {8248 callback(null, result);8249 }8250 };8251 }8252 }8253 /**8254 * @memberof async8255 * @namespace timesSeries8256 * @param {number} n - n >= 18257 * @param {Function} iterator8258 * @param {Function} callback8259 * @example8260 *8261 * var iterator = function(n, done) {8262 * done(null, n);8263 * };8264 * async.timesSeries(4, iterator, function(err, res) {8265 * console.log(res); // [0, 1, 2, 3];8266 * });8267 *8268 */8269 function timesSeries(n, iterator, callback) {8270 callback = callback || noop;8271 n = +n;8272 if (isNaN(n) || n < 1) {8273 return callback(null, []);8274 }8275 var result = Array(n);8276 var sync = false;8277 var completed = 0;8278 iterate();8279 function iterate() {8280 iterator(completed, done);8281 }8282 function done(err, res) {8283 result[completed] = res;8284 if (err) {8285 callback(err);8286 callback = throwError;8287 } else if (++completed >= n) {8288 callback(null, result);8289 callback = throwError;8290 } else if (sync) {8291 nextTick(iterate);8292 } else {8293 sync = true;8294 iterate();8295 }8296 sync = false;8297 }8298 }8299 /**8300 * @memberof async8301 * @namespace timesLimit8302 * @param {number} n - n >= 18303 * @param {number} limit - n >= 18304 * @param {Function} iterator8305 * @param {Function} callback8306 * @example8307 *8308 * var iterator = function(n, done) {8309 * done(null, n);8310 * };8311 * async.timesLimit(4, 2, iterator, function(err, res) {8312 * console.log(res); // [0, 1, 2, 3];8313 * });8314 *8315 */8316 function timesLimit(n, limit, iterator, callback) {8317 callback = callback || noop;8318 n = +n;8319 if (isNaN(n) || n < 1 || isNaN(limit) || limit < 1) {8320 return callback(null, []);8321 }8322 var result = Array(n);8323 var sync = false;8324 var started = 0;