parent
a05df9d7b6
commit
0652131de8
4 changed files with 199 additions and 79 deletions
@ -0,0 +1,72 @@ |
||||
const filterHttp = (items) => { |
||||
let httpsHosts = items.map(x => new URL(x.url)) |
||||
.filter(x => x.protocol === 'https:') |
||||
.map(x => x.host); |
||||
httpsHosts = new Set(httpsHosts); |
||||
|
||||
return items.filter((item) => { |
||||
let url = new URL(item.url); |
||||
return url.protocol === 'https:' || !httpsHosts.has(url.host); |
||||
}); |
||||
}; |
||||
|
||||
const filterBlankTitle = (items) => { |
||||
return items.filter(item => item.title && item.title !== ''); |
||||
}; |
||||
|
||||
const filterByTailingSlash = (items) => { |
||||
let urls = items.map(item => new URL(item.url)); |
||||
let simplePaths = urls |
||||
.filter(url => url.hash === '' && url.search === '') |
||||
.map(url => url.origin + url.pathname); |
||||
simplePaths = new Set(simplePaths); |
||||
|
||||
return items.filter((item) => { |
||||
let url = new URL(item.url); |
||||
if (url.hash !== '' || url.search !== '' || |
||||
url.pathname.slice(-1) !== '/') { |
||||
return true; |
||||
} |
||||
return !simplePaths.has(url.origin + url.pathname.slice(0, -1)); |
||||
}); |
||||
}; |
||||
|
||||
const filterByPathname = (items, min) => { |
||||
let hash = {}; |
||||
for (let item of items) { |
||||
let url = new URL(item.url); |
||||
let pathname = url.origin + url.pathname; |
||||
if (!hash[pathname]) { |
||||
hash[pathname] = item; |
||||
} else if (hash[pathname].url.length > item.url.length) { |
||||
hash[pathname] = item; |
||||
} |
||||
} |
||||
let filtered = Object.values(hash); |
||||
if (filtered.length < min) { |
||||
return items; |
||||
} |
||||
return filtered; |
||||
}; |
||||
|
||||
const filterByOrigin = (items, min) => { |
||||
let hash = {}; |
||||
for (let item of items) { |
||||
let origin = new URL(item.url).origin; |
||||
if (!hash[origin]) { |
||||
hash[origin] = item; |
||||
} else if (hash[origin].url.length > item.url.length) { |
||||
hash[origin] = item; |
||||
} |
||||
} |
||||
let filtered = Object.values(hash); |
||||
if (filtered.length < min) { |
||||
return items; |
||||
} |
||||
return filtered; |
||||
}; |
||||
|
||||
export { |
||||
filterHttp, filterBlankTitle, filterByTailingSlash, |
||||
filterByPathname, filterByOrigin |
||||
}; |
@ -0,0 +1,113 @@ |
||||
import * as filters from 'background/usecases/filters'; |
||||
|
||||
describe("background/usecases/filters", () => { |
||||
describe('filterHttp', () => { |
||||
it('filters http URLs duplicates to https hosts', () => { |
||||
let pages = [ |
||||
{ url: 'http://i-beam.org/foo' }, |
||||
{ url: 'https://i-beam.org/bar' }, |
||||
{ url: 'http://i-beam.net/hoge' }, |
||||
{ url: 'http://i-beam.net/fuga' }, |
||||
]; |
||||
let filtered = filters.filterHttp(pages); |
||||
|
||||
let urls = filtered.map(x => x.url); |
||||
expect(urls).to.deep.equal([ |
||||
'https://i-beam.org/bar', 'http://i-beam.net/hoge', 'http://i-beam.net/fuga' |
||||
]); |
||||
}) |
||||
}); |
||||
|
||||
describe('filterBlankTitle', () => { |
||||
it('filters blank titles', () => { |
||||
let pages = [ |
||||
{ title: 'hello' }, |
||||
{ title: '' }, |
||||
{}, |
||||
]; |
||||
let filtered = filters.filterBlankTitle(pages); |
||||
|
||||
expect(filtered).to.deep.equal([{ title: 'hello' }]); |
||||
}); |
||||
}) |
||||
|
||||
describe('filterByTailingSlash', () => { |
||||
it('filters duplicated pathname on tailing slash', () => { |
||||
let pages = [ |
||||
{ url: 'http://i-beam.org/content' }, |
||||
{ url: 'http://i-beam.org/content/' }, |
||||
{ url: 'http://i-beam.org/search' }, |
||||
{ url: 'http://i-beam.org/search?q=apple_banana_cherry' }, |
||||
]; |
||||
let filtered = filters.filterByTailingSlash(pages); |
||||
|
||||
let urls = filtered.map(x => x.url); |
||||
expect(urls).to.deep.equal([ |
||||
'http://i-beam.org/content', |
||||
'http://i-beam.org/search', |
||||
'http://i-beam.org/search?q=apple_banana_cherry', |
||||
]); |
||||
}); |
||||
}) |
||||
|
||||
describe('filterByPathname', () => { |
||||
it('remains items less than minimam length', () => { |
||||
let pages = [ |
||||
{ url: 'http://i-beam.org/search?q=apple' }, |
||||
{ url: 'http://i-beam.org/search?q=apple_banana' }, |
||||
{ url: 'http://i-beam.org/search?q=apple_banana_cherry' }, |
||||
{ url: 'http://i-beam.org/request?q=apple' }, |
||||
{ url: 'http://i-beam.org/request?q=apple_banana' }, |
||||
{ url: 'http://i-beam.org/request?q=apple_banana_cherry' }, |
||||
]; |
||||
let filtered = filters.filterByPathname(pages, 10); |
||||
expect(filtered).to.have.lengthOf(6); |
||||
}); |
||||
|
||||
it('filters by length of pathname', () => { |
||||
let pages = [ |
||||
{ url: 'http://i-beam.org/search?q=apple' }, |
||||
{ url: 'http://i-beam.org/search?q=apple_banana' }, |
||||
{ url: 'http://i-beam.org/search?q=apple_banana_cherry' }, |
||||
{ url: 'http://i-beam.net/search?q=apple' }, |
||||
{ url: 'http://i-beam.net/search?q=apple_banana' }, |
||||
{ url: 'http://i-beam.net/search?q=apple_banana_cherry' }, |
||||
]; |
||||
let filtered = filters.filterByPathname(pages, 0); |
||||
expect(filtered).to.deep.equal([ |
||||
{ url: 'http://i-beam.org/search?q=apple' }, |
||||
{ url: 'http://i-beam.net/search?q=apple' }, |
||||
]); |
||||
}); |
||||
}) |
||||
|
||||
describe('filterByOrigin', () => { |
||||
it('remains items less than minimam length', () => { |
||||
let pages = [ |
||||
{ url: 'http://i-beam.org/search?q=apple' }, |
||||
{ url: 'http://i-beam.org/search?q=apple_banana' }, |
||||
{ url: 'http://i-beam.org/search?q=apple_banana_cherry' }, |
||||
{ url: 'http://i-beam.org/request?q=apple' }, |
||||
{ url: 'http://i-beam.org/request?q=apple_banana' }, |
||||
{ url: 'http://i-beam.org/request?q=apple_banana_cherry' }, |
||||
]; |
||||
let filtered = filters.filterByOrigin(pages, 10); |
||||
expect(filtered).to.have.lengthOf(6); |
||||
}); |
||||
|
||||
it('filters by length of pathname', () => { |
||||
let pages = [ |
||||
{ url: 'http://i-beam.org/search?q=apple' }, |
||||
{ url: 'http://i-beam.org/search?q=apple_banana' }, |
||||
{ url: 'http://i-beam.org/search?q=apple_banana_cherry' }, |
||||
{ url: 'http://i-beam.org/request?q=apple' }, |
||||
{ url: 'http://i-beam.org/request?q=apple_banana' }, |
||||
{ url: 'http://i-beam.org/request?q=apple_banana_cherry' }, |
||||
]; |
||||
let filtered = filters.filterByOrigin(pages, 0); |
||||
expect(filtered).to.deep.equal([ |
||||
{ url: 'http://i-beam.org/search?q=apple' }, |
||||
]); |
||||
}); |
||||
}) |
||||
}); |
Reference in new issue