Skip to content

Commit

Permalink
feat(*): support all country codes
Browse files Browse the repository at this point in the history
  • Loading branch information
bolshchikov committed Apr 19, 2024
1 parent 138b82e commit 9a4ca78
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
6 changes: 6 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"url": "https://github.com/chibat/chrome-extension-typescript-starter.git"
},
"dependencies": {
"country-calling-code": "^0.0.3",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
Expand Down
4 changes: 2 additions & 2 deletions src/background.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { countryDialCodes } from './countryCodes';
import { getCountryDialCode } from './countryCodes';

chrome.runtime.onInstalled.addListener(() => {
chrome.contextMenus.create({
Expand Down Expand Up @@ -41,7 +41,7 @@ function getUserCountry() {
}

function formatPhoneNumber(phoneNumber: string, countryCode: string) {
const dialCode = countryDialCodes[countryCode];
const dialCode = getCountryDialCode(countryCode);
if (dialCode) {
const normalizedPhoneNumber = phoneNumber.replace(/^0+/, ''); // Remove leading zeros
return dialCode + normalizedPhoneNumber;
Expand Down
10 changes: 5 additions & 5 deletions src/countryCodes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export const countryDialCodes: Record<string, string> = {
'US': '1',
'UK': '44',
'IL': '972',
};
import { codes } from 'country-calling-code';

export const getCountryDialCode = (countryIsoCode: string): string | undefined => {
return codes.find(({ isoCode2 }) => countryIsoCode)?.countryCodes[0]
}

0 comments on commit 9a4ca78

Please sign in to comment.