Last active
June 24, 2019 16:13
-
-
Save matthieuh/74602b10feadbbe928e467b4d1d1ec33 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 unique = (list, key) => | |
key | |
? list.map(e => e !== null && e[key]) | |
.map((e, i, final) => final.indexOf(e) === i && i) | |
.filter(e => e !== null && list[e]) | |
.map(e => list[e]) | |
: [...new Set(list)].filter(entry => entry !== null) | |
describe('uniq()', () => { | |
it('should work as expected', () => { | |
assert.deepStrictEqual(helpers.uniq([1, 1, 2, 1, 3, 3]), [1, 2, 3]) | |
assert.deepStrictEqual(helpers.uniq([1, 2, 3, null]), [1, 2, 3]) | |
assert.deepStrictEqual(helpers.uniq( | |
[{ id: 1 }, { id: 1 }, { id: 2 }, { id: 2 }, { id: 1 }, { id: 3 }, null], | |
'id' | |
), [{ id: 1 }, { id: 2 }, { id: 3 }]) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment