@newdash/newdash
Version:
javascript/typescript utility library
40 lines (39 loc) • 1.71 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.unionWith = void 0;
const baseFlatten_1 = __importDefault(require("./.internal/baseFlatten"));
const baseUniq_1 = __importDefault(require("./.internal/baseUniq"));
const isArrayLikeObject_1 = __importDefault(require("./isArrayLikeObject"));
const last_1 = __importDefault(require("./last"));
/**
* This method is like `union` except that it accepts `comparator` which
* is invoked to compare elements of `arrays`. Result values are chosen from
* the first array in which the value occurs. The comparator is invoked
* with two arguments: (arrVal, othVal).
*
* @since 5.13.0
* @category Array
* @param {...Array} [arrays] The arrays to inspect.
* @param {Function} [comparator] The comparator invoked per element.
* @returns {Array} Returns the new array of combined values.
* @see [[difference]], [[union]], [[unionBy]], [[without]], [[xor]], [[xorBy]]
* @example
*
* ```js
* const objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }]
* const others = [{ 'x': 1, 'y': 1 }, { 'x': 1, 'y': 2 }]
*
* unionWith(objects, others, isEqual)
* // => [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }, { 'x': 1, 'y': 1 }]
* ```
*/
function unionWith(...arrays) {
let comparator = (0, last_1.default)(arrays);
comparator = typeof comparator === "function" ? comparator : undefined;
return (0, baseUniq_1.default)((0, baseFlatten_1.default)(arrays, 1, isArrayLikeObject_1.default, true), undefined, comparator);
}
exports.unionWith = unionWith;
exports.default = unionWith;