This repository has been archived by the owner on Feb 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(systemaddon): Dedupe Topsites. Closes #2933.
- Loading branch information
Showing
13 changed files
with
215 additions
and
49 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
this.Dedupe = class Dedupe { | ||
constructor(createKey, compare) { | ||
this.createKey = createKey || this.defaultCreateKey; | ||
this.compare = compare || this.defaultCompare; | ||
} | ||
|
||
defaultCreateKey(item) { | ||
return item; | ||
} | ||
|
||
defaultCompare() { | ||
return false; | ||
} | ||
|
||
/** | ||
* Dedupe an array containing groups of elements. | ||
* Duplicate removal favors earlier groups. | ||
* | ||
* @param {Array} groups Contains an arbitrary number of arrays of elements. | ||
* @returns {Array} | ||
*/ | ||
group(groups) { | ||
const globalKeys = new Set(); | ||
const result = []; | ||
for (const values of groups) { | ||
const valueMap = new Map(); | ||
for (const value of values) { | ||
const key = this.createKey(value); | ||
if (!globalKeys.has(key) && (!valueMap.has(key) || this.compare(valueMap.get(key), value))) { | ||
valueMap.set(key, value); | ||
} | ||
} | ||
result.push(valueMap); | ||
valueMap.forEach((value, key) => globalKeys.add(key)); | ||
} | ||
return result.map(m => Array.from(m.values())); | ||
} | ||
}; | ||
|
||
this.EXPORTED_SYMBOLS = ["Dedupe"]; |
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
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
const {Dedupe} = require("common/Dedupe.jsm"); | ||
|
||
describe("Dedupe", () => { | ||
let instance; | ||
beforeEach(() => { | ||
instance = new Dedupe(); | ||
}); | ||
describe("group", () => { | ||
it("should remove duplicates inside the groups", () => { | ||
const beforeItems = [[1, 1, 1], [2, 2, 2], [3, 3, 3]]; | ||
const afterItems = [[1], [2], [3]]; | ||
assert.deepEqual(instance.group(beforeItems), afterItems); | ||
}); | ||
it("should remove duplicates between groups, favouring earlier groups", () => { | ||
const beforeItems = [[1, 2, 3], [2, 3, 4], [3, 4, 5]]; | ||
const afterItems = [[1, 2, 3], [4], [5]]; | ||
assert.deepEqual(instance.group(beforeItems), afterItems); | ||
}); | ||
it("should remove duplicates from groups of objects", () => { | ||
instance = new Dedupe(item => item.id); | ||
const beforeItems = [[{id: 1}, {id: 1}, {id: 2}], [{id: 1}, {id: 3}, {id: 2}], [{id: 1}, {id: 2}, {id: 5}]]; | ||
const afterItems = [[{id: 1}, {id: 2}], [{id: 3}], [{id: 5}]]; | ||
assert.deepEqual(instance.group(beforeItems), afterItems); | ||
}); | ||
it("should take a custom comparison function", () => { | ||
function compare(previous, current) { | ||
return current.amount > previous.amount; | ||
} | ||
instance = new Dedupe(item => item.id, compare); | ||
const beforeItems = [ | ||
[{id: 1, amount: 50}, {id: 1, amount: 100}], | ||
[{id: 1, amount: 200}, {id: 2, amount: 0}, {id: 2, amount: 100}] | ||
]; | ||
const afterItems = [ | ||
[{id: 1, amount: 100}], | ||
[{id: 2, amount: 100}] | ||
]; | ||
|
||
assert.deepEqual(instance.group(beforeItems), afterItems); | ||
}); | ||
}); | ||
}); |
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
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
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
Oops, something went wrong.