remeda
Version:
A utility library for JavaScript and Typescript.
1 lines • 2.69 kB
Source Map (JSON)
{"version":3,"file":"intersectionWith.cjs","names":["purryFromLazy","SKIP_ITEM"],"sources":["../src/intersectionWith.ts"],"sourcesContent":["import { purryFromLazy } from \"./internal/purryFromLazy\";\nimport type { LazyEvaluator } from \"./internal/types/LazyEvaluator\";\nimport { SKIP_ITEM } from \"./internal/utilityEvaluators\";\n\ntype Comparator<TFirst, TSecond> = (a: TFirst, b: TSecond) => boolean;\n\n/**\n * Returns a list of intersecting values based on a custom\n * comparator function that compares elements of both arrays.\n *\n * @param array - The source array.\n * @param other - The second array.\n * @param comparator - The custom comparator.\n * @signature\n * R.intersectionWith(array, other, comparator)\n * @example\n * R.intersectionWith(\n * [\n * { id: 1, name: 'Ryan' },\n * { id: 3, name: 'Emma' },\n * ],\n * [3, 5],\n * (a, b) => a.id === b,\n * ) // => [{ id: 3, name: 'Emma' }]\n * @dataFirst\n * @lazy\n * @category Array\n */\nexport function intersectionWith<TFirst, TSecond>(\n array: ReadonlyArray<TFirst>,\n other: ReadonlyArray<TSecond>,\n comparator: Comparator<TFirst, TSecond>,\n): Array<TFirst>;\n\n/**\n * Returns a list of intersecting values based on a custom\n * comparator function that compares elements of both arrays.\n *\n * @param other - The second array.\n * @param comparator - The custom comparator.\n * @signature\n * R.intersectionWith(other, comparator)(array)\n * @example\n * R.intersectionWith(\n * [3, 5],\n * (a, b) => a.id === b\n * )([\n * { id: 1, name: 'Ryan' },\n * { id: 3, name: 'Emma' },\n * ]); // => [{ id: 3, name: 'Emma' }]\n * @dataLast\n * @lazy\n * @category Array\n */\nexport function intersectionWith<TFirst, TSecond>(\n other: ReadonlyArray<TSecond>,\n /**\n * Type inference doesn't work properly for the comparator's first parameter\n * in data last variant.\n */\n comparator: Comparator<TFirst, TSecond>,\n): (array: ReadonlyArray<TFirst>) => Array<TFirst>;\n\nexport function intersectionWith(...args: ReadonlyArray<unknown>): unknown {\n return purryFromLazy(lazyImplementation, args);\n}\n\nconst lazyImplementation =\n <TFirst, TSecond>(\n other: ReadonlyArray<TSecond>,\n comparator: Comparator<TFirst, TSecond>,\n ): LazyEvaluator<TFirst> =>\n (value) =>\n other.some((otherValue) => comparator(value, otherValue))\n ? { done: false, hasNext: true, next: value }\n : SKIP_ITEM;\n"],"mappings":"8FA+DA,SAAgB,EAAiB,GAAG,EAAuC,CACzE,OAAOA,EAAAA,EAAc,EAAoB,EAAK,CAGhD,MAAM,GAEF,EACA,IAED,GACC,EAAM,KAAM,GAAe,EAAW,EAAO,EAAW,CAAC,CACrD,CAAE,KAAM,GAAO,QAAS,GAAM,KAAM,EAAO,CAC3CC,EAAAA"}