Skip to content

Instantly share code, notes, and snippets.

@dir01
Created May 14, 2019 09:30
Show Gist options
  • Save dir01/bf92ffc3ce3559fa2c349bc37c9aaeb1 to your computer and use it in GitHub Desktop.
Save dir01/bf92ffc3ce3559fa2c349bc37c9aaeb1 to your computer and use it in GitHub Desktop.
const uniq = <T>(arr: T[], fn: (el: T) => Object = (a) => a) =>
arr.reduce<T[]>(
(acc, curr) => (acc.some((a) => fn(a) === fn(curr)) ? acc : [...acc, curr]),
[],
);
test("uniq", () => {
const deduped = uniq([{ a: 2 }, { a: -2 }], (el) => Math.abs(el.a));
expect(deduped).toEqual([{ a: 2 }]);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment