@newdash/newdash
Version:
javascript/typescript utility library
47 lines (46 loc) • 1.78 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.intersectionBy = 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 getIteratee_1 = __importDefault(require("./.internal/getIteratee"));
const last_1 = __importDefault(require("./last"));
/**
* This method is like `intersection` except that it accepts `iteratee`
* which is invoked for each element of each `arrays` to generate the criterion
* by which they're compared. The order and references of result values are
* determined by the first array. The iteratee is invoked with one argument:
* (value).
*
* @since 5.11.0
* @category Array
* @param arrays The arrays to inspect.
* @param iteratee The iteratee invoked per element.
* @returns Returns the new array of intersecting values.
* @example
*
* ```js
* intersectionBy([2.1, 1.2], [2.3, 3.4], Math.floor)
* // => [2.1]
* ```
*/
function intersectionBy(...arrays) {
let iteratee = (0, last_1.default)(arrays);
const mapped = (0, arrayMap_1.default)(arrays, castArrayLikeObject_1.default);
if (iteratee === (0, last_1.default)(mapped)) {
iteratee = undefined;
}
else {
mapped.pop();
}
return (mapped.length && mapped[0] === arrays[0])
? (0, baseIntersection_1.default)(mapped, (0, getIteratee_1.default)(iteratee, 2))
: [];
}
exports.intersectionBy = intersectionBy;
exports.default = intersectionBy;