UNPKG

@newdash/newdash

Version:

javascript/typescript utility library

46 lines (45 loc) 1.79 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.intersectionWith = void 0; // @ts-nocheck const arrayMap_1 = __importDefault(require("./.internal/arrayMap")); const baseIntersection_1 = __importDefault(require("./.internal/baseIntersection")); const castArrayLikeObject_1 = __importDefault(require("./.internal/castArrayLikeObject")); const last_1 = __importDefault(require("./last")); /** * This method is like `intersection` except that it accepts `comparator` * which is invoked to compare elements of `arrays`. The order and references * of result values are determined by the first array. The comparator is * invoked with two arguments: (arrVal, othVal). * * @since 5.11.0 * @category Array * @param arrays The arrays to inspect. * @param comparator The comparator invoked per element. * @returns Returns the new array of intersecting values. * @example * * ```js * const objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }] * const others = [{ 'x': 1, 'y': 1 }, { 'x': 1, 'y': 2 }] * * intersectionWith(objects, others, isEqual) * // => [{ 'x': 1, 'y': 2 }] * ``` */ function intersectionWith(...arrays) { let comparator = (0, last_1.default)(arrays); const mapped = (0, arrayMap_1.default)(arrays, castArrayLikeObject_1.default); comparator = typeof comparator === "function" ? comparator : undefined; if (comparator) { mapped.pop(); } return (mapped.length && mapped[0] === arrays[0]) ? (0, baseIntersection_1.default)(mapped, undefined, comparator) : []; } exports.intersectionWith = intersectionWith; exports.default = intersectionWith;