Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve getCountryCode(), support special characters and trim any whitespace around the name #142

Merged
merged 2 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "annexare/countries-list",
"version": "3.1.0",
"version": "3.1.1",
"description": "Continents & countries: ISO 3166-1 alpha-2 code, name, ISO 639-1 languages, capital, currency, native name, phone. JSON, CSV and SQL.",
"type": "library",
"keywords": [
Expand Down
4 changes: 2 additions & 2 deletions dist/cjs/index.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/index.iife.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/mjs/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "countries-list",
"version": "3.1.0",
"version": "3.1.1",
"description": "Continents & countries: ISO 3166-1 alpha-2 code, name, ISO 639-1 languages, capital, currency, native name, phone. JSON, CSV and SQL.",
"main": "cjs/index.js",
"module": "mjs/index.js",
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/countries/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "countries",
"version": "3.1.0",
"version": "3.1.1",
"type": "module",
"publishConfig": {
"access": "public"
Expand All @@ -23,4 +23,4 @@
"typescript": "~5.5.4"
},
"license": "MIT"
}
}
8 changes: 7 additions & 1 deletion packages/countries/src/getCountryCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@ import { getCountryDataList } from './getCountryData.ts'
const countryDataList = getCountryDataList()

export const getCountryCode = (countryName: string): TCountryCode | false => {
// Escape special RegExp characters
const name = `${countryName}`
.trim()
.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&')
.replace(/-/g, '\\x2d')

// Match exact country name, but case insensitive
const nameRegex = new RegExp('^' + countryName + '$', 'i')
const nameRegex = new RegExp('^' + name + '$', 'i')

return (
countryDataList.find(({ name, native }) => nameRegex.test(name) || nameRegex.test(native))
Expand Down
11 changes: 11 additions & 0 deletions packages/test-node/getCountryCode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,15 @@ test('getCountryCode()', () => {
assert.equal(getCountryCode('Ukrain'), false)
assert.equal(getCountryCode('Ukraine1'), false)
assert.equal(getCountryCode('Unknown'), false)

// Should not care about leading/trailing spaces
assert.equal(getCountryCode(' Ukraine '), 'UA')

// More unicode tests
assert.equal(getCountryCode('မြန်မာ'), 'MM')
assert.equal(getCountryCode('澳門'), 'MO')

// Special symbols
assert.equal(getCountryCode('Myanmar (Burma)'), 'MM')
assert.equal(getCountryCode('Cocos (Keeling) Islands'), 'CC')
})
Loading