Skip to content

Commit

Permalink
Merge pull request #55 from bizon/tusbar/support-www-in-domains
Browse files Browse the repository at this point in the history
Support www in domains in getMarketplaceByDomain
  • Loading branch information
tusbar authored May 26, 2021
2 parents 750a7fd + 0c03c43 commit 9640f86
Show file tree
Hide file tree
Showing 6 changed files with 2,108 additions and 49 deletions.
87 changes: 66 additions & 21 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: 2
version: 2.1

defaults:
workspace_root: &workspace_root
Expand All @@ -7,19 +7,54 @@ defaults:
nodejs_container: &nodejs_container
working_directory: *workspace_root
docker:
- image: circleci/node:12-stretch

- image: cimg/node:lts
attach_workspace: &attach_workspace
attach_workspace:
at: *workspace_root

restore_node_modules: &restore_node_modules
restore_cache:
name: Restore node_modules cache
keys:
- v2-amazon-ids-node-{{ .Branch }}-{{ checksum "yarn.lock" }}
- v2-amazon-ids-node-master-{{ checksum "yarn.lock" }}
- v2-amazon-ids-node-master-
commands:
restore-nodejs:
description: "Restore Node.js dependencies"
parameters:
key:
type: string
working_directory:
type: string
default: *workspace_root
default_branch:
type: string
default: master
steps:
- restore_cache:
name: Restore node_modules cache
keys:
- << parameters.key >>-nodejs-{{ .Branch }}-{{ checksum "<< parameters.working_directory >>/yarn.lock" }}
- << parameters.key >>-nodejs-<< parameters.default_branch >>-{{ checksum "<< parameters.working_directory >>/yarn.lock" }}
- << parameters.key >>-nodejs-<< parameters.default_branch >>-

install-nodejs:
description: "Install Node.js dependencies"
parameters:
key:
type: string
working_directory:
type: string
default: *workspace_root
steps:
- restore-nodejs:
key: << parameters.key >>
working_directory: << parameters.working_directory >>

- run:
name: Install dependencies
command: yarn --frozen-lockfile --non-interactive
working_directory: << parameters.working_directory >>

- save_cache:
name: Save node_modules cache
key: << parameters.key >>-nodejs-{{ .Branch }}-{{ checksum "<< parameters.working_directory >>/yarn.lock" }}
paths:
- << parameters.working_directory >>/node_modules

jobs:
checkout:
Expand All @@ -36,28 +71,34 @@ jobs:
<<: *nodejs_container
steps:
- *attach_workspace
- *restore_node_modules

- run:
name: Install dependencies
command: yarn --frozen-lockfile

- save_cache:
name: Save node_modules cache
key: v2-amazon-ids-node-{{ .Branch }}-{{ checksum "yarn.lock" }}
paths:
- node_modules/
- install-nodejs:
key: v1-amazon-ids

lint:
<<: *nodejs_container
steps:
- *attach_workspace
- *restore_node_modules

- restore-nodejs:
key: v1-amazon-ids

- run:
name: Lint JavaScript
command: yarn xo

test:
<<: *nodejs_container
steps:
- *attach_workspace

- restore-nodejs:
key: v1-amazon-ids

- run:
name: Test
command: yarn test --ci

workflows:
version: 2

Expand All @@ -72,3 +113,7 @@ workflows:
- lint:
requires:
- install

- test:
requires:
- install
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
node_modules
node_modules/
coverage/
reports/
24 changes: 24 additions & 0 deletions __tests__/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const {
getMarketplaceByDomain
} = require('..')

describe('index', () => {
describe('getMarketplaceByDomain', () => {
it('should support all cases', () => {
expect(getMarketplaceByDomain('Amazon.fr').domain).toBe('amazon.fr')
expect(getMarketplaceByDomain('amazon.fr').domain).toBe('amazon.fr')
expect(getMarketplaceByDomain('AMAZON.fr').domain).toBe('amazon.fr')
expect(getMarketplaceByDomain('amazon.FR').domain).toBe('amazon.fr')
})

it('should support domains with the www subdomain', () => {
expect(getMarketplaceByDomain('www.amazon.fr').domain).toBe('amazon.fr')
expect(getMarketplaceByDomain('www.amazon.co.uk').domain).toBe('amazon.co.uk')
expect(getMarketplaceByDomain('www.amazon.com').domain).toBe('amazon.com')
expect(getMarketplaceByDomain('www.Amazon.de').domain).toBe('amazon.de')
expect(getMarketplaceByDomain('WWW.amazoN.it').domain).toBe('amazon.it')

expect(getMarketplaceByDomain('www.google.com')).toBeUndefined()
})
})
})
12 changes: 10 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,19 @@ exports.getMarketplaceById = memoize(id => {
})

exports.getMarketplaceByCode = memoize(code => {
return marketplaces.find(marketplace => marketplace.code === code.toLowerCase())
code = code.toLowerCase()

return marketplaces.find(marketplace => marketplace.code === code)
})

exports.getMarketplaceByDomain = memoize(domain => {
return marketplaces.find(marketplace => marketplace.domain === domain.toLowerCase())
domain = domain.toLowerCase()

if (domain.startsWith('www.')) {
domain = domain.slice(4)
}

return marketplaces.find(marketplace => marketplace.domain === domain)
})

exports.getMarketplacesByMwsDomain = memoize(domain => {
Expand Down
36 changes: 31 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
"repository": "https://github.com/bizon/amazon-ids",
"author": "Bertrand Marron <bertrand.marron@gmail.com>",
"license": "MIT",
"scripts": {
"test": "jest"
},
"files": [
"index.js",
"marketplaces.json"
Expand All @@ -14,16 +17,39 @@
"memoizee": "^0.4.14"
},
"devDependencies": {
"jest": "^27.0.1",
"xo": "^0.36.1"
},
"xo": {
"semicolon": false,
"space": 2
},
"keywords": [
"amazon",
"sellercentral",
"vendorcentral",
"marketplaces"
]
],
"jest": {
"testEnvironment": "node",
"testMatch": [
"<rootDir>/__tests__/**/*.js"
],
"collectCoverage": true,
"collectCoverageFrom": [
"index.js"
],
"coverageReporters": [
"lcov",
"text-summary"
]
},
"xo": {
"semicolon": false,
"space": 2,
"overrides": [
{
"files": "__tests__/**/*.js",
"envs": [
"jest"
]
}
]
}
}
Loading

0 comments on commit 9640f86

Please sign in to comment.