Advertisement
Guest User

Untitled

a guest
Jul 25th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.58 KB | None | 0 0
  1. this.Dedupe = class Dedupe {
  2. constructor(createKey, compare) {
  3. this.createKey = createKey || this.defaultCreateKey;
  4. this.compare = compare || this.defaultCompare;
  5. }
  6.  
  7. defaultCreateKey(item) {
  8. return item;
  9. }
  10.  
  11. defaultCompare() {
  12. return false;
  13. }
  14.  
  15. one(values) {
  16. const valueMap = new Map();
  17. values.forEach(value => {
  18. const key = this.createKey(value);
  19. if (!valueMap.has(key) || this.compare(valueMap.get(key), value)) {
  20. valueMap.set(key, value);
  21. }
  22. });
  23. return Array.from(valueMap.values());
  24. }
  25. };
  26.  
  27. this.EXPORTED_SYMBOLS = ["Dedupe"];
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement