Skip to content
This repository has been archived by the owner on Dec 11, 2019. It is now read-only.

Commit

Permalink
Merge pull request #9557 from brave/9541
Browse files Browse the repository at this point in the history
Sort top sites more deterministically
  • Loading branch information
bsclifton authored Jun 19, 2017
2 parents 7d98f45 + f985db9 commit 40ebed2
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
15 changes: 15 additions & 0 deletions app/common/lib/suggestion.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
19 changes: 17 additions & 2 deletions js/data/top500.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -20,6 +20,7 @@ const top500 = [
'linkedin.com',
'sina.com.cn',
'amazon.com',
'amazon.ca',
'hao123.com',
'google.co.in',
'blogspot.com',
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -94,6 +106,7 @@ const top500 = [
'blogspot.in',
'ebay.de',
'netflix.com',
'khanacademy.org',
'kickass.to',
'google.pl',
'ku6.com',
Expand Down Expand Up @@ -333,6 +346,7 @@ const top500 = [
'mobile01.com',
'clickbank.com',
'microsoftonline.com',
'yandex.com',
'yandex.ua',
'gsmarena.com',
'bluehost.com',
Expand Down Expand Up @@ -489,7 +503,8 @@ const top500 = [
'babytree.com',
'youm7.com',
'123rf.com',
'commentcamarche.net'
'commentcamarche.net',
'brave.com'
]

module.exports = top500
10 changes: 9 additions & 1 deletion test/unit/app/common/lib/suggestionTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 () {
Expand Down

0 comments on commit 40ebed2

Please sign in to comment.