Welcome to mirror list, hosted at ThFree Co, Russian Federation.

uniqueBy.js.flow « utils « lib « package « popperjs - github.com/gohugoio/hugo-mod-jslibs-dist.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0f79ae9c1ab6d5aa4cdda31e77401f8514604bcb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// @flow

export default function uniqueBy<T>(arr: Array<T>, fn: T => any): Array<T> {
  const identifiers = new Set();

  return arr.filter(item => {
    const identifier = fn(item);

    if (!identifiers.has(identifier)) {
      identifiers.add(identifier);
      return true;
    }
  });
}