-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
3 changed files
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
type by<T> = (item: T) => number | string | ||
|
||
export function intersection<T>(...args: Array<T[] | by<T>>): 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<T> = typeof args[args.length - 1] === 'function' ? args[args.length - 1] as by<T> : (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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { intersection } from '../src' | ||
import { intersectionBy } from '../src' | ||
|
||
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 }]) | ||
}) | ||
}) |
011c3c8
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
midash – ./
midash.vercel.app
midash-shanyue.vercel.app
midash-git-master-shanyue.vercel.app
midash.devtool.tech