@onesy/utils
Version:
42 lines (41 loc) • 1.64 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const is_1 = __importDefault(require("./is"));
const getObjectValue_1 = __importDefault(require("./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 = (object, ...args) => {
const cache = {
simple: [],
array: [],
object: [],
};
const output = [];
if ((0, is_1.default)('array', object)) {
object.forEach(item => {
const isNotArrayObject = (0, is_1.default)('not-array-object', item);
const isArray = (0, is_1.default)('array', item);
const value = (isNotArrayObject || !args.length) ? item : (0, getObjectValue_1.default)(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;
};
exports.default = unique;
;