Skip to content

Commit

Permalink
Set libraryTarget to commonjs (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
necessarylion authored Apr 21, 2023
1 parent 187e37c commit 8bf556d
Show file tree
Hide file tree
Showing 7 changed files with 5,191 additions and 14 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,17 @@ console.log(list.length) // 3

#### Sample Usage of `formatPhoneNumber()`

```bash
npm i google-libphonenumber
npm i --save-dev @types/google-libphonenumber // require for typescript
```

```js
import CountryList , { PhoneNumberFormat } from 'country-list-with-dial-code-and-flag'
import { PhoneNumberUtil, PhoneNumberFormat } from 'google-libphonenumber'

// required from version 5.0 due to performance issue
CountryList.setPhoneNumberUtil(PhoneNumberUtil.getInstance())

const mm = CountryList.findOneByCountryCode('mm')
if (mm) {
Expand Down
5 changes: 4 additions & 1 deletion __tests__/flags.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import CountryList, { Country, PhoneNumberFormat } from '../src/index'
import CountryList, { Country } from '../src/index'
import CountryFlagSvg from '../src/flag-svg'
import { PhoneNumberUtil, PhoneNumberFormat } from 'google-libphonenumber'

CountryList.setPhoneNumberUtil(PhoneNumberUtil.getInstance())

describe('getList', () => {
test('getList', () => {
Expand Down
6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "country-list-with-dial-code-and-flag",
"version": "4.1.0",
"version": "5.0.0",
"description": "Country list with dial code and flag",
"main": "dist/index.js",
"scripts": {
Expand Down Expand Up @@ -47,6 +47,7 @@
"@typescript-eslint/eslint-plugin": "^5.57.0",
"@typescript-eslint/parser": "^5.57.0",
"eslint": "^8.37.0",
"google-libphonenumber": "^3.2.32",
"html-webpack-plugin": "^5.5.0",
"husky": "^8.0.0",
"jest": "^29.2.2",
Expand All @@ -60,8 +61,5 @@
"webpack": "^5.77.0",
"webpack-cli": "^5.0.1",
"webpack-dev-server": "^4.13.1"
},
"dependencies": {
"google-libphonenumber": "^3.2.32"
}
}
16 changes: 10 additions & 6 deletions src/country.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { CountryInterface } from './types'
import { PhoneNumberFormat, PhoneNumberUtil } from 'google-libphonenumber'
import CountryList from '.'

class Country {
private data: CountryInterface
private phoneUtil: PhoneNumberUtil

constructor(data: CountryInterface) {
this.data = data
this.phoneUtil = PhoneNumberUtil.getInstance()
}

public get name(): string {
Expand Down Expand Up @@ -46,12 +44,18 @@ class Country {
return this.data.country_code ?? this.data.dial_code
}

public formatPhoneNumber(phoneNumber: string | number, format?: PhoneNumberFormat) {
const number = this.phoneUtil.parseAndKeepRawInput(
public formatPhoneNumber(phoneNumber: string | number, format?: any) {
if (!CountryList.phoneNumberUtil) {
console.warn(
'PhoneNumberUtil is not being use please check our documentation for more detail',
)
return
}
const number = CountryList.phoneNumberUtil.parseAndKeepRawInput(
this.parsePhoneNumber(phoneNumber),
this.code.toUpperCase(),
)
return this.phoneUtil.format(number, format ?? PhoneNumberFormat.E164)
return CountryList.phoneNumberUtil.format(number, format ?? 0)
}

private parsePhoneNumber(phoneNumber: string | number): string {
Expand Down
12 changes: 10 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Country } from './country'
import countries from './data'
import { CountryInterface, FilterOption } from './types'
import { PhoneNumberFormat, PhoneNumberUtil } from 'google-libphonenumber'

class App {
public phoneNumberUtil: any
/**
* find one by ISO 3166-1 alpha-2 eg. US, MM
*
Expand Down Expand Up @@ -82,6 +82,14 @@ class App {
}
return list.map((data: CountryInterface) => new Country(data))
}

/**
* set phone number util to use phone number formatter
* @param phoneNumberUtil
*/
public setPhoneNumberUtil(phoneNumberUtil: any) {
this.phoneNumberUtil = phoneNumberUtil
}
}

const CountryList = new App()
Expand All @@ -95,4 +103,4 @@ declare global {
window.CountryList = CountryList

export default CountryList
export { Country, FilterOption, PhoneNumberFormat, PhoneNumberUtil }
export { Country, FilterOption }
2 changes: 1 addition & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = {
output: {
filename: '[name].js',
path: path.resolve(__dirname, 'dist'),
libraryTarget: 'module'
libraryTarget: 'commonjs'
},
experiments: {
outputModule: true,
Expand Down
Loading

0 comments on commit 8bf556d

Please sign in to comment.