source: trip-planner-front/node_modules/async/eachOf.js@ 188ee53

Last change on this file since 188ee53 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

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