UNPKG

pouchdb-find

Version:
105 lines (93 loc) 2.43 kB
'use strict'; /* istanbul ignore if */ exports.Promise = require('pouchdb/extras/promise'); exports.inherits = require('inherits'); exports.extend = require('pouchdb-extend'); var argsarray = require('argsarray'); /* istanbul ignore next */ 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; }; /* istanbul ignore next */ 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 /* istanbul ignore next */ exports.fin = function (promise, cb) { return promise.then(function (res) { var promise2 = cb(); if (typeof promise2.then === 'function') { return promise2.then(function () { return res; }); } return res; }, function (reason) { var promise2 = cb(); if (typeof promise2.then === 'function') { return promise2.then(function () { throw reason; }); } 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; }; var crypto = require('crypto'); var Md5 = require('spark-md5'); exports.MD5 = function (string) { /* istanbul ignore else */ if (!process.browser) { return crypto.createHash('md5').update(string).digest('hex'); } else { return Md5.hash(string); } };