mirror of https://github.com/seald/nedb
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.
52 lines
1.7 KiB
52 lines
1.7 KiB
/**
|
|
* Utility functions for all environments.
|
|
* This replaces the underscore dependency.
|
|
*
|
|
* @module utils
|
|
*/
|
|
|
|
/**
|
|
* 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}
|
|
* @alias module:utils.uniq
|
|
*/
|
|
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}
|
|
* @alias module:utils.isDate
|
|
*/
|
|
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}
|
|
* @alias module:utils.isRegExp
|
|
*/
|
|
const isRegExp = re => isObject(re) && Object.prototype.toString.call(re) === '[object RegExp]'
|
|
|
|
module.exports.uniq = uniq
|
|
module.exports.isDate = isDate
|
|
module.exports.isRegExp = isRegExp
|
|
|