Skip to content

Commit

Permalink
test(phoneNumber): tests phone number variations
Browse files Browse the repository at this point in the history
  • Loading branch information
bolshchikov committed Apr 19, 2024
1 parent 9a4ca78 commit 3b89ee6
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 71 deletions.
41 changes: 1 addition & 40 deletions README.md
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`
33 changes: 3 additions & 30 deletions src/background.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getCountryDialCode } from './countryCodes';
import { getUserCountry } from './services/country';
import { extractPhoneNumber } from './services/phoneNumber';

chrome.runtime.onInstalled.addListener(() => {
chrome.contextMenus.create({
Expand All @@ -11,7 +12,7 @@ chrome.runtime.onInstalled.addListener(() => {
chrome.contextMenus.onClicked.addListener((info, tab) => {
if (info.menuItemId === 'sendWhatsApp' && info.selectionText) {
getUserCountry().then(countryCode => {
const phoneNumber = formatPhoneNumber(extractPhoneNumber(info.selectionText), countryCode);
const phoneNumber = extractPhoneNumber(countryCode, info.selectionText);
if (phoneNumber) {
const url = `https://api.whatsapp.com/send/?phone=${phoneNumber}&text&type=phone_number&app_absent=0`;
chrome.tabs.create({ url: url });
Expand All @@ -21,31 +22,3 @@ chrome.contextMenus.onClicked.addListener((info, tab) => {
});
}
});

function extractPhoneNumber(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;
}

function getUserCountry() {
return fetch('https://api.country.is/')
.then(response => response.json())
.then(data => data.country)
.catch(error => console.error('Failed to fetch country:', error));
}

function formatPhoneNumber(phoneNumber: string, countryCode: string) {
const dialCode = getCountryDialCode(countryCode);
if (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
}
}
6 changes: 6 additions & 0 deletions src/services/country.ts
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));
};
2 changes: 1 addition & 1 deletion src/countryCodes.ts → src/services/countryCodes.ts
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]
}
32 changes: 32 additions & 0 deletions src/services/phoneNumber.spec.ts
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();
});
});
26 changes: 26 additions & 0 deletions src/services/phoneNumber.ts
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);
};

0 comments on commit 3b89ee6

Please sign in to comment.