File: mapSeries.js

package info (click to toggle)
node-gulp 4.0.2-8
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 3,716 kB
  • sloc: javascript: 13,511; makefile: 94; sh: 12
file content (63 lines) | stat: -rw-r--r-- 1,252 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
'use strict';

var once = require('once');

var helpers = require('./helpers');

function mapSeries(values, iterator, extensions, done) {
  // Allow for extensions to not be specified
  if (typeof extensions === 'function') {
    done = extensions;
    extensions = {};
  }

  // Handle no callback case
  if (typeof done !== 'function') {
    done = helpers.noop;
  }

  done = once(done);

  // Will throw if non-object
  var keys = Object.keys(values);
  var length = keys.length;
  var idx = 0;
  // Return the same type as passed in
  var results = helpers.initializeResults(values);

  var exts = helpers.defaultExtensions(extensions);

  if (length === 0) {
    return done(null, results);
  }

  var key = keys[idx];
  next(key);

  function next(key) {
    var value = values[key];

    var storage = exts.create(value, key) || {};

    exts.before(storage);
    iterator(value, key, once(handler));

    function handler(err, result) {
      if (err) {
        exts.error(err, storage);
        return done(err, results);
      }

      exts.after(result, storage);
      results[key] = result;

      if (++idx >= length) {
        done(err, results);
      } else {
        next(keys[idx]);
      }
    }
  }
}

module.exports = mapSeries;