Skip to content

Instantly share code, notes, and snippets.

@piatra
Created July 25, 2017 17:30
Show Gist options
  • Save piatra/1f1ea416291cf0658e321698a60e58cd to your computer and use it in GitHub Desktop.
Save piatra/1f1ea416291cf0658e321698a60e58cd to your computer and use it in GitHub Desktop.
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