UNPKG

@onesy/utils

Version:
45 lines (39 loc) 1.37 kB
import is from './is'; import getObjectValue from './getObjectValue'; /** * It returns an array with unique simple values * and / or array and object values. * * Referenced values are only compared based on * values in those reference type values based on array * of keys provided in the second argument in the method. * * Uniqueness of array and object values is separatelly * evaluated based on keys value and returned in the result. */ const unique = function (object) { for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { args[_key - 1] = arguments[_key]; } const cache = { simple: [], array: [], object: [] }; const output = []; if (is('array', object)) { object.forEach(item => { const isNotArrayObject = is('not-array-object', item); const isArray = is('array', item); const value = isNotArrayObject || !args.length ? item : getObjectValue(item, ...args); const cacheArray = cache[isNotArrayObject ? 'simple' : isArray ? 'array' : 'object']; const exists = cacheArray.find(cacheItem => value === cacheItem); if (!exists && value !== undefined) { output.push(item); cache[isNotArrayObject ? 'simple' : isArray ? 'array' : 'object'].push(value); } }); } return output; }; export default unique;