From b0b0208883af7e0fc6bfa1d5c6402dadc77a68a7 Mon Sep 17 00:00:00 2001 From: buzuosheng <1015350043@qq.com> Date: Thu, 20 Jul 2023 18:52:26 +0800 Subject: [PATCH 1/2] intersection --- src/index.ts | 1 + src/intersection.ts | 5 +++++ test/intersection.spec.ts | 8 ++++++++ 3 files changed, 14 insertions(+) create mode 100644 src/intersection.ts create mode 100644 test/intersection.spec.ts diff --git a/src/index.ts b/src/index.ts index a784ab6..efba292 100644 --- a/src/index.ts +++ b/src/index.ts @@ -33,3 +33,4 @@ export * from './compose' export * from './difference' export * from './async' export * from './nth' +export * from './intersection' \ No newline at end of file diff --git a/src/intersection.ts b/src/intersection.ts new file mode 100644 index 0000000..cc4c312 --- /dev/null +++ b/src/intersection.ts @@ -0,0 +1,5 @@ +export function intersection( + ...list: T[][] +): T[] { + return list.reduce((a, b) => a.filter(c => b.includes(c))); +} diff --git a/test/intersection.spec.ts b/test/intersection.spec.ts new file mode 100644 index 0000000..59d62b0 --- /dev/null +++ b/test/intersection.spec.ts @@ -0,0 +1,8 @@ +import { intersection } from '../src' + +describe('isObject', function() { + let list = [['a', 'b','c'], ['a','c'], ['a'], ['a', 'd']] + it('should work', function() { + expect(intersection(...list)).toEqual(['a']) + }) +}) From 2da2b5461c850a1f6a793dcfd13099f5e5b6fc39 Mon Sep 17 00:00:00 2001 From: buzuosheng <1015350043@qq.com> Date: Fri, 21 Jul 2023 14:55:51 +0800 Subject: [PATCH 2/2] intersectionBy --- src/intersection.ts | 17 +++++++++++++---- test/intersection.spec.ts | 19 ++++++++++++++++--- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/src/intersection.ts b/src/intersection.ts index cc4c312..a190694 100644 --- a/src/intersection.ts +++ b/src/intersection.ts @@ -1,5 +1,14 @@ -export function intersection( - ...list: T[][] -): T[] { - return list.reduce((a, b) => a.filter(c => b.includes(c))); +type by = (item: T) => number | string + +export function intersection(...args: Array>): T[] { + if (!args || !args.length) return [] + if (args.slice(0, args.length - 1).find(item => typeof item === 'function')) return [] + const list: T[][] = args.filter(Array.isArray) + const by: by = typeof args[args.length - 1] === 'function' ? args[args.length - 1] as by : (x => x as number); + return list?.reduce((a, b) => { + const setB = new Set(b.map(by)); + return a.filter(c => setB.has(by(c))); + }); } + +export { intersection as intersectionBy } diff --git a/test/intersection.spec.ts b/test/intersection.spec.ts index 59d62b0..ebafd96 100644 --- a/test/intersection.spec.ts +++ b/test/intersection.spec.ts @@ -1,8 +1,21 @@ import { intersection } from '../src' +import { intersectionBy } from '../src' -describe('isObject', function() { - let list = [['a', 'b','c'], ['a','c'], ['a'], ['a', 'd']] - it('should work', function() { +describe('should work', function () { + let list = [['a', 'b', 'c'], ['a', 'c'], ['a'], ['a', 'd']] + it('should work', function () { + expect(intersection()).toEqual([]) + expect(intersection([])).toEqual([]) + expect(intersection([2, 1], [4, 2], [1, 2])).toEqual([2]) + expect(intersection([2, 1])).toEqual([2, 1]) expect(intersection(...list)).toEqual(['a']) }) + + it('should work with by', function () { + expect(intersectionBy()).toEqual([]) + expect(intersectionBy([])).toEqual([]) + expect(intersectionBy([{ 'x': 1 }])).toEqual([{ 'x': 1 }]) + expect(intersectionBy([2, 1], [4, 2], [1, 2], Math.floor)).toEqual([2]) + expect(intersectionBy([{ 'x': 1 }], [{ 'x': 2 }, { 'x': 1 }], x => x.x)).toEqual([{ 'x': 1 }]) + }) })