The JavaScript Database, for Node.js, nw.js, electron and the browser
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
nedb/lib/utils.js

42 lines
1.5 KiB

/**
* Produces a duplicate-free version of the array, using === to test object equality. In particular only the first
* occurrence of each value is kept. If you want to compute unique items based on a transformation, pass an iteratee
* function.
* Heavily inspired by https://underscorejs.org/#uniq
* @param {Array} array
* @param {function} [iteratee] transformation applied to every element before checking for duplicates. This will not
* transform the items in the result.
* @return {Array}
*/
const uniq = (array, iteratee) => {
if (iteratee) return [...(new Map(array.map(x => [iteratee(x), x]))).values()]
else return [...new Set(array)]
}
/**
* Returns true if arg is an Object. Note that JavaScript arrays and functions are objects, while (normal) strings
* and numbers are not.
* Heavily inspired by https://underscorejs.org/#isObject
* @param {*} arg
* @return {boolean}
*/
const isObject = arg => typeof arg === 'object' && arg !== null
/**
* Returns true if d is a Date.
* Heavily inspired by https://underscorejs.org/#isDate
* @param {*} d
* @return {boolean}
*/
const isDate = d => isObject(d) && Object.prototype.toString.call(d) === '[object Date]'
/**
* Returns true if re is a RegExp.
* Heavily inspired by https://underscorejs.org/#isRegExp
* @param {*} re
* @return {boolean}
*/
const isRegExp = re => isObject(re) && Object.prototype.toString.call(re) === '[object RegExp]'
module.exports.uniq = uniq
module.exports.isDate = isDate
module.exports.isRegExp = isRegExp