all files / lib/mapreduce/ utils.js

100% Statements 43/43
87.5% Branches 7/8
100% Functions 17/17
100% Lines 42/42
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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87                1103× 52× 49× 49×           1103×     109× 109× 109×   109×         229× 225× 224×             1250× 1250× 1250× 1250× 1250×             3268×   3268× 4184×     3268× 3268×   3268× 3826×   3268×                
'use strict';
 
Object.defineProperty(exports, "__esModule", {
  value: true
});
 
var _argsarray = require('argsarray');
 
var _argsarray2 = _interopRequireDefault(_argsarray);
 
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
 
var promisedCallback = function (promise, callback) {
  if (callback) {
    promise.then(function (res) {
      process.nextTick(function () {
        callback(null, res);
      });
    }, function (reason) {
      process.nextTick(function () {
        callback(reason);
      });
    });
  }
  return promise;
};
 
var callbackify = function (fun) {
  return (0, _argsarray2.default)(function (args) {
    var cb = args.pop();
    var promise = fun.apply(this, args);
    if (typeof cb === 'function') {
      promisedCallback(promise, cb);
    }
    return promise;
  });
};
 
// Promise finally util similar to Q.finally
var fin = function (promise, finalPromiseFactory) {
  return promise.then(function (res) {
    return finalPromiseFactory().then(function () {
      return res;
    });
  }, function (reason) {
    return finalPromiseFactory().then(function () {
      throw reason;
    });
  });
};
 
var sequentialize = function (queue, promiseFactory) {
  return function () {
    var args = arguments;
    var that = this;
    return queue.add(function () {
      return promiseFactory.apply(that, args);
    });
  };
};
 
// uniq an array of strings, order not guaranteed
// similar to underscore/lodash _.uniq
var uniq = function (arr) {
  var map = {};
 
  for (var i = 0, len = arr.length; i < len; i++) {
    map['$' + arr[i]] = true;
  }
 
  var keys = Object.keys(map);
  var output = new Array(keys.length);
 
  for (i = 0, len = keys.length; i < len; i++) {
    output[i] = keys[i].substring(1);
  }
  return output;
};
 
exports.default = {
  uniq: uniq,
  sequentialize: sequentialize,
  fin: fin,
  callbackify: callbackify,
  promisedCallback: promisedCallback
};
module.exports = exports['default'];