Created
July 25, 2017 17:30
-
-
Save piatra/1f1ea416291cf0658e321698a60e58cd to your computer and use it in GitHub Desktop.
This file contains hidden or 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
this.Dedupe = class Dedupe { | |
constructor(createKey, compare) { | |
this.createKey = createKey || this.defaultCreateKey; | |
this.compare = compare || this.defaultCompare; | |
} | |
defaultCreateKey(item) { | |
return item; | |
} | |
defaultCompare() { | |
return false; | |
} | |
one(values) { | |
const valueMap = new Map(); | |
values.forEach(value => { | |
const key = this.createKey(value); | |
if (!valueMap.has(key) || this.compare(valueMap.get(key), value)) { | |
valueMap.set(key, value); | |
} | |
}); | |
return Array.from(valueMap.values()); | |
} | |
}; | |
this.EXPORTED_SYMBOLS = ["Dedupe"]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment