summaryrefslogtreecommitdiff
path: root/packages/backend/src/misc/unique-by-key.ts
blob: 4308e29d2177f30e53729d66d2499663b0963a30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*
 * SPDX-FileCopyrightText: syuilo and misskey-project
 * SPDX-License-Identifier: AGPL-3.0-only
 */

/**
 * itemsの中でkey関数が返す値が重複しないようにした配列を返す
 * @param items 重複を除去したい配列
 * @param key 重複判定に使うキーを返す関数
 * @returns 重複を除去した配列
 */
export function uniqueByKey<TItem, TKey = string>(items: Iterable<TItem>, key: (item: TItem) => TKey): TItem[] {
	const map = new Map<TKey, TItem>();
	for (const item of items) {
		const k = key(item);
		if (!map.has(k)) {
			map.set(k, item);
		}
	}
	return [...map.values()];
}