Code coverage report for lib/mapreduce/utils.js

Statements: 100% (46 / 46)      Branches: 100% (4 / 4)      Functions: 100% (17 / 17)      Lines: 100% (46 / 46)      Ignored: none     

All files » lib/mapreduce/ » utils.js
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    1 1 1 1   1 1049 52 49 49     3 3       1049     1 2 105 105 105 3   105         1 222 218 217     4 3         1 1212 1212 1212 1212 1212         1 402 402 3169   402         1 3223   3223 4139     3223 3223   3223 3781   3223     1
'use strict';
 
exports.Promise = require('../deps/promise');
exports.inherits = require('inherits');
exports.extend = require('pouchdb-extend');
var argsarray = require('argsarray');
 
exports.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;
};
 
exports.callbackify = function (fun) {
  return argsarray(function (args) {
    var cb = args.pop();
    var promise = fun.apply(this, args);
    if (typeof cb === 'function') {
      exports.promisedCallback(promise, cb);
    }
    return promise;
  });
};
 
// Promise finally util similar to Q.finally
exports.fin = function (promise, finalPromiseFactory) {
  return promise.then(function (res) {
    return finalPromiseFactory().then(function () {
      return res;
    });
  }, function (reason) {
    return finalPromiseFactory().then(function () {
      throw reason;
    });
  });
};
 
exports.sequentialize = function (queue, promiseFactory) {
  return function () {
    var args = arguments;
    var that = this;
    return queue.add(function () {
      return promiseFactory.apply(that, args);
    });
  };
};
 
exports.flatten = function (arrs) {
  var res = [];
  for (var i = 0, len = arrs.length; i < len; i++) {
    res = res.concat(arrs[i]);
  }
  return res;
};
 
// uniq an array of strings, order not guaranteed
// similar to underscore/lodash _.uniq
exports.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.MD5 = require('./md5');