afdcaae7f340a9733a892bceb8f232f2fe4068c2
[yaffs-website] / node_modules / uncss / node_modules / form-data / node_modules / async / eachOf.js
1 'use strict';
2
3 Object.defineProperty(exports, "__esModule", {
4     value: true
5 });
6
7 exports.default = function (coll, iteratee, callback) {
8     var eachOfImplementation = (0, _isArrayLike2.default)(coll) ? eachOfArrayLike : eachOfGeneric;
9     eachOfImplementation(coll, iteratee, callback);
10 };
11
12 var _isArrayLike = require('lodash/isArrayLike');
13
14 var _isArrayLike2 = _interopRequireDefault(_isArrayLike);
15
16 var _breakLoop = require('./internal/breakLoop');
17
18 var _breakLoop2 = _interopRequireDefault(_breakLoop);
19
20 var _eachOfLimit = require('./eachOfLimit');
21
22 var _eachOfLimit2 = _interopRequireDefault(_eachOfLimit);
23
24 var _doLimit = require('./internal/doLimit');
25
26 var _doLimit2 = _interopRequireDefault(_doLimit);
27
28 var _noop = require('lodash/noop');
29
30 var _noop2 = _interopRequireDefault(_noop);
31
32 var _once = require('./internal/once');
33
34 var _once2 = _interopRequireDefault(_once);
35
36 var _onlyOnce = require('./internal/onlyOnce');
37
38 var _onlyOnce2 = _interopRequireDefault(_onlyOnce);
39
40 function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
41
42 // eachOf implementation optimized for array-likes
43 function eachOfArrayLike(coll, iteratee, callback) {
44     callback = (0, _once2.default)(callback || _noop2.default);
45     var index = 0,
46         completed = 0,
47         length = coll.length;
48     if (length === 0) {
49         callback(null);
50     }
51
52     function iteratorCallback(err, value) {
53         if (err) {
54             callback(err);
55         } else if (++completed === length || value === _breakLoop2.default) {
56             callback(null);
57         }
58     }
59
60     for (; index < length; index++) {
61         iteratee(coll[index], index, (0, _onlyOnce2.default)(iteratorCallback));
62     }
63 }
64
65 // a generic version of eachOf which can handle array, object, and iterator cases.
66 var eachOfGeneric = (0, _doLimit2.default)(_eachOfLimit2.default, Infinity);
67
68 /**
69  * Like [`each`]{@link module:Collections.each}, except that it passes the key (or index) as the second argument
70  * to the iteratee.
71  *
72  * @name eachOf
73  * @static
74  * @memberOf module:Collections
75  * @method
76  * @alias forEachOf
77  * @category Collection
78  * @see [async.each]{@link module:Collections.each}
79  * @param {Array|Iterable|Object} coll - A collection to iterate over.
80  * @param {Function} iteratee - A function to apply to each
81  * item in `coll`. The `key` is the item's key, or index in the case of an
82  * array. The iteratee is passed a `callback(err)` which must be called once it
83  * has completed. If no error has occurred, the callback should be run without
84  * arguments or with an explicit `null` argument. Invoked with
85  * (item, key, callback).
86  * @param {Function} [callback] - A callback which is called when all
87  * `iteratee` functions have finished, or an error occurs. Invoked with (err).
88  * @example
89  *
90  * var obj = {dev: "/dev.json", test: "/test.json", prod: "/prod.json"};
91  * var configs = {};
92  *
93  * async.forEachOf(obj, function (value, key, callback) {
94  *     fs.readFile(__dirname + value, "utf8", function (err, data) {
95  *         if (err) return callback(err);
96  *         try {
97  *             configs[key] = JSON.parse(data);
98  *         } catch (e) {
99  *             return callback(e);
100  *         }
101  *         callback();
102  *     });
103  * }, function (err) {
104  *     if (err) console.error(err.message);
105  *     // configs is now a map of JSON data
106  *     doSomethingWith(configs);
107  * });
108  */
109 module.exports = exports['default'];