diff options
| author | tamaina <tamaina@hotmail.co.jp> | 2022-03-27 22:42:05 +0900 |
|---|---|---|
| committer | tamaina <tamaina@hotmail.co.jp> | 2022-03-27 22:42:05 +0900 |
| commit | 7314643b8d80445bcc24c0056f9236763efec4de (patch) | |
| tree | 2c2b67bb7bc406c0d2633eaddfa438c1f528138c /packages/client/src/scripts/array.ts | |
| parent | Merge branch 'develop' into pizzax-indexeddb (diff) | |
| parent | Update CONTRIBUTING.md (diff) | |
| download | misskey-7314643b8d80445bcc24c0056f9236763efec4de.tar.gz misskey-7314643b8d80445bcc24c0056f9236763efec4de.tar.bz2 misskey-7314643b8d80445bcc24c0056f9236763efec4de.zip | |
Merge branch 'develop' into pizzax-indexeddb
Diffstat (limited to 'packages/client/src/scripts/array.ts')
| -rw-r--r-- | packages/client/src/scripts/array.ts | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/packages/client/src/scripts/array.ts b/packages/client/src/scripts/array.ts index d63f0475d0..29d027de14 100644 --- a/packages/client/src/scripts/array.ts +++ b/packages/client/src/scripts/array.ts @@ -52,6 +52,17 @@ export function unique<T>(xs: T[]): T[] { return [...new Set(xs)]; } +export function uniqueBy<TValue, TKey>(values: TValue[], keySelector: (value: TValue) => TKey): TValue[] { + const map = new Map<TKey, TValue>(); + + for (const value of values) { + const key = keySelector(value); + if (!map.has(key)) map.set(key, value); + } + + return [...map.values()]; +} + export function sum(xs: number[]): number { return xs.reduce((a, b) => a + b, 0); } |