2024-10-17 21:31:23 +03:00

127 lines
3.0 KiB
JavaScript

'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
var pouchdbCollections = require('pouchdb-collections');
var argsarray = _interopDefault(require('argsarray'));
var pouchdbUtils = require('pouchdb-utils');
var inherits = _interopDefault(require('inherits'));
function QueryParseError(message) {
this.status = 400;
this.name = 'query_parse_error';
this.message = message;
this.error = true;
try {
Error.captureStackTrace(this, QueryParseError);
} catch (e) {}
}
inherits(QueryParseError, Error);
function NotFoundError(message) {
this.status = 404;
this.name = 'not_found';
this.message = message;
this.error = true;
try {
Error.captureStackTrace(this, NotFoundError);
} catch (e) {}
}
inherits(NotFoundError, Error);
function BuiltInError(message) {
this.status = 500;
this.name = 'invalid_value';
this.message = message;
this.error = true;
try {
Error.captureStackTrace(this, BuiltInError);
} catch (e) {}
}
inherits(BuiltInError, Error);
function promisedCallback(promise, callback) {
if (callback) {
promise.then(function (res) {
pouchdbUtils.nextTick(function () {
callback(null, res);
});
}, function (reason) {
pouchdbUtils.nextTick(function () {
callback(reason);
});
});
}
return promise;
}
function callbackify(fun) {
return argsarray(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
function fin(promise, finalPromiseFactory) {
return promise.then(function (res) {
return finalPromiseFactory().then(function () {
return res;
});
}, function (reason) {
return finalPromiseFactory().then(function () {
throw reason;
});
});
}
function sequentialize(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
function uniq(arr) {
var theSet = new pouchdbCollections.Set(arr);
var result = new Array(theSet.size);
var index = -1;
theSet.forEach(function (value) {
result[++index] = value;
});
return result;
}
function mapToKeysArray(map) {
var result = new Array(map.size);
var index = -1;
map.forEach(function (value, key) {
result[++index] = key;
});
return result;
}
exports.uniq = uniq;
exports.sequentialize = sequentialize;
exports.fin = fin;
exports.callbackify = callbackify;
exports.promisedCallback = promisedCallback;
exports.mapToKeysArray = mapToKeysArray;
exports.QueryParseError = QueryParseError;
exports.NotFoundError = NotFoundError;
exports.BuiltInError = BuiltInError;