com.vaadin.polymer.public.bower_components.pouchdb-find.lib.utils.js Maven / Gradle / Ivy
The newest version!
'use strict';
var Promise = require('pouchdb-promise');
/* istanbul ignore next */
exports.once = function (fun) {
var called = false;
return exports.getArguments(function (args) {
if (called) {
console.trace();
throw new Error('once called more than once');
} else {
called = true;
fun.apply(this, args);
}
});
};
/* istanbul ignore next */
exports.getArguments = function (fun) {
return function () {
var len = arguments.length;
var args = new Array(len);
var i = -1;
while (++i < len) {
args[i] = arguments[i];
}
return fun.call(this, args);
};
};
/* istanbul ignore next */
exports.toPromise = function (func) {
//create the function we will be returning
return exports.getArguments(function (args) {
var self = this;
var tempCB = (typeof args[args.length - 1] === 'function') ? args.pop() : false;
// if the last argument is a function, assume its a callback
var usedCB;
if (tempCB) {
// if it was a callback, create a new callback which calls it,
// but do so async so we don't trap any errors
usedCB = function (err, resp) {
process.nextTick(function () {
tempCB(err, resp);
});
};
}
var promise = new Promise(function (fulfill, reject) {
try {
var callback = exports.once(function (err, mesg) {
if (err) {
reject(err);
} else {
fulfill(mesg);
}
});
// create a callback for this invocation
// apply the function in the orig context
args.push(callback);
func.apply(self, args);
} catch (e) {
reject(e);
}
});
// if there is a callback, call it back
if (usedCB) {
promise.then(function (result) {
usedCB(null, result);
}, usedCB);
}
promise.cancel = function () {
return this;
};
return promise;
});
};
exports.inherits = require('inherits');
exports.Promise = Promise;
exports.clone = function (obj) {
return exports.extend(true, {}, obj);
};
exports.extend = require('pouchdb-extend');
exports.callbackify = function (fun) {
return exports.getArguments(function (args) {
var cb = args.pop();
var promise = fun.apply(this, args);
exports.promisedCallback(promise, cb);
return promise;
});
};
exports.promisedCallback = function (promise, callback) {
promise.then(function (res) {
process.nextTick(function () {
callback(null, res);
});
}, function (reason) {
process.nextTick(function () {
callback(reason);
});
});
return promise;
};
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);
}
};
exports.flatten = exports.getArguments(function (args) {
var res = [];
for (var i = 0, len = args.length; i < len; i++) {
var subArr = args[i];
if (Array.isArray(subArr)) {
res = res.concat(exports.flatten.apply(null, subArr));
} else {
res.push(subArr);
}
}
return res;
});
exports.mergeObjects = function (arr) {
var res = {};
for (var i = 0, len = arr.length; i < len; i++) {
res = exports.extend(true, res, arr[i]);
}
return res;
};
// this would just be "return doc[field]", but fields
// can be "deep" due to dot notation
exports.getFieldFromDoc = function (doc, parsedField) {
var value = doc;
for (var i = 0, len = parsedField.length; i < len; i++) {
var key = parsedField[i];
value = value[key];
if (!value) {
break;
}
}
return value;
};
exports.setFieldInDoc = function (doc, parsedField, value) {
for (var i = 0, len = parsedField.length; i < len-1; i++) {
var elem = parsedField[i];
doc = doc[elem] = {};
}
doc[parsedField[len-1]] = value;
};
// Converts a string in dot notation to an array of its components, with backslash escaping
exports.parseField = function (fieldName) {
// fields may be deep (e.g. "foo.bar.baz"), so parse
var fields = [];
var current = '';
for (var i = 0, len = fieldName.length; i < len; i++) {
var ch = fieldName[i];
if (ch === '.') {
if (i > 0 && fieldName[i - 1] === '\\') { // escaped delimiter
current = current.substring(0, current.length - 1) + '.';
} else { // not escaped, so delimiter
fields.push(current);
current = '';
}
} else { // normal character
current += ch;
}
}
fields.push(current);
return fields;
};
// Selects a list of fields defined in dot notation from one doc
// and copies them to a new doc. Like underscore _.pick but supports nesting.
exports.pick = function (obj, arr) {
var res = {};
for (var i = 0, len = arr.length; i < len; i++) {
var parsedField = exports.parseField(arr[i]);
var value = exports.getFieldFromDoc(obj, parsedField);
if(typeof value !== 'undefined') {
exports.setFieldInDoc(res, parsedField, value);
}
}
return res;
};
// e.g. ['a'], ['a', 'b'] is true, but ['b'], ['a', 'b'] is false
exports.oneArrayIsSubArrayOfOther = function (left, right) {
for (var i = 0, len = Math.min(left.length, right.length); i < len; i++) {
if (left[i] !== right[i]) {
return false;
}
}
return true;
};
// e.g.['a', 'b', 'c'], ['a', 'b'] is false
exports.oneArrayIsStrictSubArrayOfOther = function (left, right) {
if (left.length > right.length) {
return false;
}
return exports.oneArrayIsSubArrayOfOther(left, right);
};
// same as above, but treat the left array as an unordered set
// e.g. ['b', 'a'], ['a', 'b', 'c'] is true, but ['c'], ['a', 'b', 'c'] is false
exports.oneSetIsSubArrayOfOther = function (left, right) {
left = left.slice();
for (var i = 0, len = right.length; i < len; i++) {
var field = right[i];
if (!left.length) {
break;
}
var leftIdx = left.indexOf(field);
if (leftIdx === -1) {
return false;
} else {
left.splice(leftIdx, 1);
}
}
return true;
};
exports.compare = function (left, right) {
return left < right ? -1 : left > right ? 1 : 0;
};
exports.arrayToObject = function (arr) {
var res = {};
for (var i = 0, len = arr.length; i < len; i++) {
res[arr[i]] = true;
}
return res;
};
exports.max = function (arr, fun) {
var max = null;
var maxScore = -1;
for (var i = 0, len = arr.length; i < len; i++) {
var element = arr[i];
var score = fun(element);
if (score > maxScore) {
maxScore = score;
max = element;
}
}
return max;
};
exports.arrayEquals = function (arr1, arr2) {
if (arr1.length !== arr2.length) {
return false;
}
for (var i = 0, len = arr1.length; i < len; i++) {
if (arr1[i] !== arr2[i]) {
return false;
}
}
return true;
};
exports.uniq = function(arr) {
var obj = {};
for (var i = 0; i < arr.length; i++) {
obj['$' + arr[i]] = true;
}
return Object.keys(obj).map(function (key) {
return key.substring(1);
});
};
exports.log = require('debug')('pouchdb:find');
© 2015 - 2024 Weber Informatics LLC | Privacy Policy