Skip to content

Instantly share code, notes, and snippets.

@matthieuh
Last active June 24, 2019 16:13
Show Gist options
  • Save matthieuh/74602b10feadbbe928e467b4d1d1ec33 to your computer and use it in GitHub Desktop.
Save matthieuh/74602b10feadbbe928e467b4d1d1ec33 to your computer and use it in GitHub Desktop.
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