Skip to content

Commit f449d60

Browse files
committed
Support domains with www in getMarketplaceByDomain
Add tests.
1 parent 750a7fd commit f449d60

File tree

5 files changed

+2042
-28
lines changed

5 files changed

+2042
-28
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
node_modules
1+
node_modules/
2+
coverage/
3+
reports/

__tests__/index.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const {
2+
getMarketplaceByDomain
3+
} = require('..')
4+
5+
describe('index', () => {
6+
describe('getMarketplaceByDomain', () => {
7+
it('should support all cases', () => {
8+
expect(getMarketplaceByDomain('Amazon.fr').domain).toBe('amazon.fr')
9+
expect(getMarketplaceByDomain('amazon.fr').domain).toBe('amazon.fr')
10+
expect(getMarketplaceByDomain('AMAZON.fr').domain).toBe('amazon.fr')
11+
expect(getMarketplaceByDomain('amazon.FR').domain).toBe('amazon.fr')
12+
})
13+
14+
it('should support domains with the www subdomain', () => {
15+
expect(getMarketplaceByDomain('www.amazon.fr').domain).toBe('amazon.fr')
16+
expect(getMarketplaceByDomain('www.amazon.co.uk').domain).toBe('amazon.co.uk')
17+
expect(getMarketplaceByDomain('www.amazon.com').domain).toBe('amazon.com')
18+
expect(getMarketplaceByDomain('www.Amazon.de').domain).toBe('amazon.de')
19+
expect(getMarketplaceByDomain('WWW.amazoN.it').domain).toBe('amazon.it')
20+
21+
expect(getMarketplaceByDomain('www.google.com')).toBeUndefined()
22+
})
23+
})
24+
})

index.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,19 @@ exports.getMarketplaceById = memoize(id => {
2424
})
2525

2626
exports.getMarketplaceByCode = memoize(code => {
27-
return marketplaces.find(marketplace => marketplace.code === code.toLowerCase())
27+
code = code.toLowerCase()
28+
29+
return marketplaces.find(marketplace => marketplace.code === code)
2830
})
2931

3032
exports.getMarketplaceByDomain = memoize(domain => {
31-
return marketplaces.find(marketplace => marketplace.domain === domain.toLowerCase())
33+
domain = domain.toLowerCase()
34+
35+
if (domain.startsWith('www.')) {
36+
domain = domain.slice(4)
37+
}
38+
39+
return marketplaces.find(marketplace => marketplace.domain === domain)
3240
})
3341

3442
exports.getMarketplacesByMwsDomain = memoize(domain => {

package.json

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
"repository": "https://github.com/bizon/amazon-ids",
77
"author": "Bertrand Marron <bertrand.marron@gmail.com>",
88
"license": "MIT",
9+
"scripts": {
10+
"test": "jest"
11+
},
912
"files": [
1013
"index.js",
1114
"marketplaces.json"
@@ -14,16 +17,39 @@
1417
"memoizee": "^0.4.14"
1518
},
1619
"devDependencies": {
20+
"jest": "^27.0.1",
1721
"xo": "^0.36.1"
1822
},
19-
"xo": {
20-
"semicolon": false,
21-
"space": 2
22-
},
2323
"keywords": [
2424
"amazon",
2525
"sellercentral",
2626
"vendorcentral",
2727
"marketplaces"
28-
]
28+
],
29+
"jest": {
30+
"testEnvironment": "node",
31+
"testMatch": [
32+
"<rootDir>/__tests__/**/*.js"
33+
],
34+
"collectCoverage": true,
35+
"collectCoverageFrom": [
36+
"index.js"
37+
],
38+
"coverageReporters": [
39+
"lcov",
40+
"text-summary"
41+
]
42+
},
43+
"xo": {
44+
"semicolon": false,
45+
"space": 2,
46+
"overrides": [
47+
{
48+
"files": "__tests__/**/*.js",
49+
"envs": [
50+
"jest"
51+
]
52+
}
53+
]
54+
}
2955
}

0 commit comments

Comments
 (0)