UNPKG

@newdash/newdash

Version:

javascript/typescript utility library

68 lines (67 loc) 2.63 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const SetCache_1 = __importDefault(require("./SetCache")); const arrayIncludes_1 = __importDefault(require("./arrayIncludes")); const arrayIncludesWith_1 = __importDefault(require("./arrayIncludesWith")); const map_1 = __importDefault(require("../map")); const cacheHas_1 = __importDefault(require("./cacheHas")); /** * The base implementation of methods like `intersection` that accepts an * array of arrays to inspect. * * @private * @param {Array} arrays The arrays to inspect. * @param {Function} [iteratee] The iteratee invoked per element. * @param {Function} [comparator] The comparator invoked per element. * @returns {Array} Returns the new array of shared values. */ function baseIntersection(arrays, iteratee, comparator) { const includes = comparator ? arrayIncludesWith_1.default : arrayIncludes_1.default; const length = arrays[0].length; const othLength = arrays.length; const caches = new Array(othLength); const result = []; let array; let maxLength = Infinity; let othIndex = othLength; while (othIndex--) { array = arrays[othIndex]; if (othIndex && iteratee) { array = (0, map_1.default)(array, (value) => iteratee(value)); } maxLength = Math.min(array.length, maxLength); caches[othIndex] = !comparator && (iteratee || (length >= 120 && array.length >= 120)) ? new SetCache_1.default(othIndex && array) : undefined; } array = arrays[0]; let index = -1; const seen = caches[0]; outer: while (++index < length && result.length < maxLength) { let value = array[index]; const computed = iteratee ? iteratee(value) : value; value = (comparator || value !== 0) ? value : 0; if (!(seen ? (0, cacheHas_1.default)(seen, computed) : includes(result, computed, comparator))) { othIndex = othLength; while (--othIndex) { const cache = caches[othIndex]; if (!(cache ? (0, cacheHas_1.default)(cache, computed) : includes(arrays[othIndex], computed, comparator))) { continue outer; } } if (seen) { seen.push(computed); } result.push(value); } } return result; } exports.default = baseIntersection;