generated from chibat/chrome-extension-typescript-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(phoneNumber): tests phone number variations
- Loading branch information
1 parent
9a4ca78
commit 3b89ee6
Showing
6 changed files
with
69 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,3 @@ | ||
# Send WhatsApp Extension | ||
|
||
![build](https://github.com/bolshchikov/send-whatsapp-extension/workflows/build/badge.svg) | ||
# Send WhatsApp Chrome Extension ![build](https://github.com/bolshchikov/send-whatsapp-extension/workflows/build/badge.svg) | ||
|
||
> Send a whatsapp message any number right from the browser | ||
## Development | ||
|
||
``` | ||
npm install | ||
``` | ||
|
||
## Import as Visual Studio Code project | ||
|
||
... | ||
|
||
## Build | ||
|
||
``` | ||
npm run build | ||
``` | ||
|
||
## Build in watch mode | ||
|
||
### terminal | ||
|
||
``` | ||
npm run watch | ||
``` | ||
|
||
### Visual Studio Code | ||
|
||
Run watch mode. | ||
|
||
type `Ctrl + Shift + B` | ||
|
||
## Load extension to chrome | ||
|
||
Load `dist` directory | ||
|
||
## Test | ||
`npx jest` or `npm run test` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export const getUserCountry = (): Promise<string> => { | ||
return fetch('https://api.country.is/') | ||
.then(response => response.json()) | ||
.then(data => data.country) | ||
.catch(error => console.error('Failed to fetch country:', error)); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import { codes } from 'country-calling-code'; | ||
|
||
export const getCountryDialCode = (countryIsoCode: string): string | undefined => { | ||
return codes.find(({ isoCode2 }) => countryIsoCode)?.countryCodes[0] | ||
return codes.find(({ isoCode2 }) => isoCode2 === countryIsoCode)?.countryCodes[0] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { extractPhoneNumber } from './phoneNumber'; | ||
|
||
describe('extractPhoneNumber', () => { | ||
it('should return formatted phone number', () => { | ||
const countryCode = 'US'; | ||
const maybePhoneNumber = '+1 (555) 123-4567'; | ||
const expectedPhoneNumber = '15551234567'; | ||
|
||
const result = extractPhoneNumber(countryCode, maybePhoneNumber); | ||
|
||
expect(result).toBe(expectedPhoneNumber); | ||
}); | ||
|
||
it('should handle local phone number', () => { | ||
const countryCode = 'IL'; | ||
const maybePhoneNumber = '052-534-4907'; | ||
const expectedPhoneNumber = '972525344907'; | ||
|
||
const result = extractPhoneNumber(countryCode, maybePhoneNumber); | ||
|
||
expect(result).toBe(expectedPhoneNumber); | ||
}); | ||
|
||
it('should return empty string if maybePhoneNumber is undefined', () => { | ||
const countryCode = 'US'; | ||
const maybePhoneNumber = undefined; | ||
|
||
expect(() => { | ||
extractPhoneNumber(countryCode, maybePhoneNumber); | ||
}).toThrow(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { getCountryDialCode } from './countryCodes'; | ||
|
||
const parsePhoneNumber = (text?: string): string => { | ||
if (!text) { | ||
throw new Error('No phone text provided') | ||
} | ||
const phoneNumber = text.replace(/\D/g, ''); | ||
if (phoneNumber.length === 0) { | ||
throw new Error('No phone number is provided'); | ||
} | ||
return phoneNumber; | ||
}; | ||
|
||
const formatPhoneNumber = (phoneNumber: string, countryCode: string) => { | ||
const dialCode = getCountryDialCode(countryCode); | ||
if (dialCode && !phoneNumber.startsWith(dialCode)) { | ||
const normalizedPhoneNumber = phoneNumber.replace(/^0+/, ''); // Remove leading zeros | ||
return dialCode + normalizedPhoneNumber; | ||
} else { | ||
return phoneNumber; // Default to the number without modification if country code is not found | ||
} | ||
}; | ||
|
||
export const extractPhoneNumber = (countryCode: string, maybePhoneNumber?: string): string => { | ||
return formatPhoneNumber(parsePhoneNumber(maybePhoneNumber), countryCode); | ||
}; |