Skip to content

Commit

Permalink
Update data & types (new partOf, userAssigned params) (#135)
Browse files Browse the repository at this point in the history
* Bump modules

* Update types and data for cases like #36 and #132

* Update comments in types.ts

* Update phone code for Kazakhstan to just `7` #134

* Case insensitive search in getCountryCode function #131

* Rebuild dist data
  • Loading branch information
dmythro authored Mar 4, 2024
1 parent d92daf3 commit db04d49
Show file tree
Hide file tree
Showing 16 changed files with 699 additions and 358 deletions.
2 changes: 1 addition & 1 deletion dist/cjs/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/countries.csv
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
"KR","South Korea","대한민국","82","Asia","Seoul","KRW","ko"
"KW","Kuwait","الكويت","965","Asia","Kuwait City","KWD","ar"
"KY","Cayman Islands","Cayman Islands","1345","North America","George Town","KYD","en"
"KZ","Kazakhstan","Қазақстан","76,77","Asia","Astana","KZT","kk,ru"
"KZ","Kazakhstan","Қазақстан","7","Asia","Astana","KZT","kk,ru"
"LA","Laos","ສປປລາວ","856","Asia","Vientiane","LAK","lo"
"LB","Lebanon","لبنان","961","Asia","Beirut","LBP","ar,fr"
"LC","Saint Lucia","Saint Lucia","1758","North America","Castries","XCD","en"
Expand Down
2 changes: 1 addition & 1 deletion dist/countries.min.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ INSERT INTO `countries` (`code`, `name`, `native`, `phone`, `continent`, `capita
('KR', 'South Korea', '대한민국', '82', 'AS', 'Seoul', 'KRW', 'ko'),
('KW', 'Kuwait', 'الكويت', '965', 'AS', 'Kuwait City', 'KWD', 'ar'),
('KY', 'Cayman Islands', 'Cayman Islands', '1345', 'NA', 'George Town', 'KYD', 'en'),
('KZ', 'Kazakhstan', 'Қазақстан', '76,77', 'AS', 'Astana', 'KZT', 'kk,ru'),
('KZ', 'Kazakhstan', 'Қазақстан', '7', 'AS', 'Astana', 'KZT', 'kk,ru'),
('LA', 'Laos', 'ສປປລາວ', '856', 'AS', 'Vientiane', 'LAK', 'lo'),
('LB', 'Lebanon', 'لبنان', '961', 'AS', 'Beirut', 'LBP', 'ar,fr'),
('LC', 'Saint Lucia', 'Saint Lucia', '1758', 'NA', 'Castries', 'XCD', 'en'),
Expand Down
10 changes: 9 additions & 1 deletion dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,20 @@ export interface ICountry {
native: string
/**
* Specified in cases when entity is currently a part of another one.
* Example: Åland is an autonomous and demilitarised region of Finland and has own ISO code.
* @see: https://en.wikipedia.org/wiki/Åland
* @todo: Type should be TCountryCode, but need to resolve cyclic referencing on dynamically generated type.
*/
parent?: TCountryCode
partOf?: string
/**
* Calling phone codes.
*/
phone: number[]
/**
* Specified in cases when entity is not a part of the main ISO 3166-1 standart, but a User assigned code.
* @see: https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2#User-assigned_code_elements
*/
userAssigned?: boolean
}

export interface ILanguage {
Expand Down
2 changes: 1 addition & 1 deletion dist/index.iife.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/mjs/index.js

Large diffs are not rendered by default.

977 changes: 644 additions & 333 deletions package-lock.json

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
"version": "cd ./packages/scripts && npm run version"
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "6.8.0",
"@typescript-eslint/parser": "6.8.0",
"eslint": "8.51.0",
"@typescript-eslint/eslint-plugin": "7.1.0",
"@typescript-eslint/parser": "7.1.0",
"eslint": "8.57.0",
"eslint-config-custom": "*",
"husky": "8.0.3",
"lint-staged": "15.0.2",
"prettier": "3.0.3",
"turbo": "1.10.16"
"husky": "9.0.11",
"lint-staged": "15.2.2",
"prettier": "3.2.5",
"turbo": "1.12.4"
},
"devEngines": {
"node": ">=18"
Expand Down
4 changes: 2 additions & 2 deletions packages/countries/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
"lint:fix": "eslint \"{/**,}/*.ts\" --fix"
},
"devDependencies": {
"tsup": "7.2.0",
"typescript": "~5.2.2"
"tsup": "8.0.2",
"typescript": "~5.3.3"
},
"license": "MIT"
}
4 changes: 3 additions & 1 deletion packages/countries/src/data/countries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ export default {
capital: 'Mariehamn',
currency: ['EUR'],
languages: ['sv'],
partOf: 'FI',
},
AZ: {
name: 'Azerbaijan',
Expand Down Expand Up @@ -1124,7 +1125,7 @@ export default {
KZ: {
name: 'Kazakhstan',
native: 'Қазақстан',
phone: [76, 77],
phone: [7],
continent: 'AS',
continents: ['AS', 'EU'],
capital: 'Astana',
Expand Down Expand Up @@ -2212,6 +2213,7 @@ export default {
capital: 'Pristina',
currency: ['EUR'],
languages: ['sq', 'sr'],
userAssigned: true,
},
YE: {
name: 'Yemen',
Expand Down
12 changes: 9 additions & 3 deletions packages/countries/src/getCountryCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ import { getCountryDataList } from './getCountryData.ts'

const countryDataList = getCountryDataList()

export const getCountryCode = (countryName: string): TCountryCode | false =>
countryDataList.find(({ name, native }) => countryName === name || countryName === native)
?.iso2 || false
export const getCountryCode = (countryName: string): TCountryCode | false => {
// Match exact country name, but case insensitive
const nameRegex = new RegExp('^' + countryName + '$', 'i')

return (
countryDataList.find(({ name, native }) => nameRegex.test(name) || nameRegex.test(native))
?.iso2 || false
)
}
10 changes: 9 additions & 1 deletion packages/countries/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,20 @@ export interface ICountry {
native: string
/**
* Specified in cases when entity is currently a part of another one.
* Example: Åland is an autonomous and demilitarised region of Finland and has own ISO code.
* @see: https://en.wikipedia.org/wiki/Åland
* @todo: Type should be TCountryCode, but need to resolve cyclic referencing on dynamically generated type.
*/
parent?: TCountryCode
partOf?: string
/**
* Calling phone codes.
*/
phone: number[]
/**
* Specified in cases when entity is not a part of the main ISO 3166-1 standart, but a User assigned code.
* @see: https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2#User-assigned_code_elements
*/
userAssigned?: boolean
}

export interface ILanguage {
Expand Down
6 changes: 3 additions & 3 deletions packages/scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
"access": "public"
},
"devDependencies": {
"@types/node": "20.8.7",
"@types/semver-compare": "1.0.2",
"@types/node": "20.11.24",
"@types/semver-compare": "1.0.3",
"chalk": "5.3.0",
"countries-list": "*",
"pretty-bytes": "6.1.1",
"semver-compare": "1.0.0",
"tsx": "3.14.0"
"tsx": "4.7.1"
},
"scripts": {
"build": "tsx index.ts",
Expand Down
6 changes: 6 additions & 0 deletions packages/test-node/getCountryCode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,11 @@ import { getCountryCode } from 'src/getCountryCode.ts'

test('getCountryCode()', () => {
assert.equal(getCountryCode('Ukraine'), 'UA')
assert.equal(getCountryCode('uKraine'), 'UA')
assert.equal(getCountryCode('Україна'), 'UA')
assert.equal(getCountryCode('уКраїна'), 'UA')

assert.equal(getCountryCode('Ukrain'), false)
assert.equal(getCountryCode('Ukraine1'), false)
assert.equal(getCountryCode('Unknown'), false)
})
2 changes: 1 addition & 1 deletion packages/test-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"type": "module",
"devDependencies": {
"@swc-node/register": "1.6.7",
"typescript": "5.2.2"
"typescript": "5.3.3"
},
"devEngines": {
"node": ">=18"
Expand Down

0 comments on commit db04d49

Please sign in to comment.