From f985db962e02d67ec353d01ccd714d13de69884c Mon Sep 17 00:00:00 2001 From: "Brian R. Bondy" Date: Sun, 18 Jun 2017 23:48:49 -0400 Subject: [PATCH] Sort top sites more deterministically Fix #9541 --- app/common/lib/suggestion.js | 15 +++++++++++++++ js/data/top500.js | 19 +++++++++++++++++-- test/unit/app/common/lib/suggestionTest.js | 10 +++++++++- 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/app/common/lib/suggestion.js b/app/common/lib/suggestion.js index 0b1b24719cd..5ae461c0020 100644 --- a/app/common/lib/suggestion.js +++ b/app/common/lib/suggestion.js @@ -298,6 +298,21 @@ const getSortByDomainForHosts = (userInputHost) => { return 1 } } + + // The list here is sufficiently small to not be a perf concern + const topPos1 = top500.indexOf(host1) + const topPos2 = top500.indexOf(host2) + + if (topPos1 !== -1 && topPos2 === -1) { + return -1 + } + if (topPos2 !== -1 && topPos1 === -1) { + return 1 + } + if (topPos1 !== -1 && topPos2 !== -1) { + return topPos1 - topPos2 + } + // Can't determine what is the best match return 0 } diff --git a/js/data/top500.js b/js/data/top500.js index 5c6e10b8130..e04001d0a17 100644 --- a/js/data/top500.js +++ b/js/data/top500.js @@ -4,8 +4,8 @@ // Top 500 alexa sites sorted by popularity const top500 = [ - 'gmail.com', 'google.com', + 'gmail.com', 'mail.google.com', 'calendar.google.com', 'facebook.com', @@ -20,6 +20,7 @@ const top500 = [ 'linkedin.com', 'sina.com.cn', 'amazon.com', + 'amazon.ca', 'hao123.com', 'google.co.in', 'blogspot.com', @@ -65,7 +66,18 @@ const top500 = [ 'google.com.hk', 'adcash.com', 'blogger.com', + 'news.ycombinator.com', 'reddit.com', + 'slashdot.org', + 'digg.com', + 'duckduckgo.com', + 'startpage.com', + 'wolframalpha.com', + 'infogalactic.com', + 'qwant.com', + 'searx.me', + 'ecosia.org', + 'semanticscholar.org', 'fc2.com', 'cnn.com', 'google.ca', @@ -94,6 +106,7 @@ const top500 = [ 'blogspot.in', 'ebay.de', 'netflix.com', + 'khanacademy.org', 'kickass.to', 'google.pl', 'ku6.com', @@ -333,6 +346,7 @@ const top500 = [ 'mobile01.com', 'clickbank.com', 'microsoftonline.com', + 'yandex.com', 'yandex.ua', 'gsmarena.com', 'bluehost.com', @@ -489,7 +503,8 @@ const top500 = [ 'babytree.com', 'youm7.com', '123rf.com', - 'commentcamarche.net' + 'commentcamarche.net', + 'brave.com' ] module.exports = top500 diff --git a/test/unit/app/common/lib/suggestionTest.js b/test/unit/app/common/lib/suggestionTest.js index b01ac49cb5b..60b94c4222b 100644 --- a/test/unit/app/common/lib/suggestionTest.js +++ b/test/unit/app/common/lib/suggestionTest.js @@ -308,7 +308,7 @@ describe('suggestion unit tests', function () { assert(this.sort('www.brianbondy.com', 'google.com') > 0) }) it('0 if both have a matching domain from index 0', function () { - assert.equal(this.sort('google.com', 'google.ca'), 0) + assert.equal(this.sort('brianbondy.com', 'brianbondy.ca'), 0) }) it('0 if neither has a matching domain', function () { assert.equal(this.sort('brianbondy.com', 'clifton.io/'), 0) @@ -319,6 +319,14 @@ describe('suggestion unit tests', function () { it('negative if there is a pos 0 match not including www.', function () { assert(this.sort('www.google.com', 'mygoogle.com') < 0) }) + it('sorts based on top sites data file order first', function () { + const sort = suggestion.getSortByDomainForHosts('g') + assert(sort('gmail.com', 'google.com') > 0) + }) + it('user input does not change order', function () { + const sort = suggestion.getSortByDomainForHosts('google.c') + assert(sort('google.com.cr', 'google.com') > 0) + }) }) describe('getSortForSearchSuggestions', function () { it('0 if suggestions are not URLs', function () {