Created
May 14, 2019 09:30
-
-
Save dir01/bf92ffc3ce3559fa2c349bc37c9aaeb1 to your computer and use it in GitHub Desktop.
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
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