Skip to content

Commit

Permalink
fix nuxt-js window not defined issue and modify group by first name…
Browse files Browse the repository at this point in the history
… function (#41)

* add missing countries

* added group by name function

* bug fixed for nuxt js window is not defined

* update version and cdn link

* update readme

* fix duplicate group by
  • Loading branch information
necessarylion authored Jun 4, 2024
1 parent 684eaaa commit 764d6fc
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 26 deletions.
31 changes: 29 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

This package provide all country list with their flag emoji, flag svg and dial number code.

## [Full Documentation](https://zin-kyaw-kyaw.gitbook.io/country/)
## [Full Documentation](https://zin-kyaw-kyaw.gitbook.io/country-flags/)

## Installation

Expand All @@ -14,10 +14,37 @@ This package provide all country list with their flag emoji, flag svg and dial n
npm install country-list-with-dial-code-and-flag
```

```ts
import CountryList from 'country-list-with-dial-code-and-flag'

CountryList.getAll()
// Response => Array<Country>

CountryList.findOneByCountryCode('MM')
// Response => Country

CountryList.findOneByDialCode('+95')
// Response => Country

CountryList.findByDialCode('+95')
// Response => Array<Country>

CountryList.findByKeyword('united')
// Response => Array<Country>
```

#### Via CDN

```html
<script src="https://cdn.jsdelivr.net/npm/country-list-with-dial-code-and-flag/dist/main.js"></script>
<script src="https://cdn.jsdelivr.net/npm/country-list-with-dial-code-and-flag@latest/dist/main.js"></script>

https://cdn.jsdelivr.net/npm/country-list-with-dial-code-and-flag@latest/dist/main.js
```

##### With specific version number

```bash
https://cdn.jsdelivr.net/npm/[email protected]/dist/main.js
```

##### Country Flag SVG (optional)
Expand Down
6 changes: 6 additions & 0 deletions __tests__/flags.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ describe('getList', () => {
test('getList', () => {
expect(CountryList.getAll()).toBeInstanceOf(Array)
})

test('getCountriesGroupedByName', () => {
expect(CountryList.groupCountriesByFirstLetter()).toBeInstanceOf(Object)
expect(CountryList.groupCountriesByFirstLetter()['a'][0]).toBeInstanceOf(Country)
expect(CountryList.groupCountriesByFirstLetter(CountryList.getAll())).toBeInstanceOf(Object)
})
})

describe('multiple area code', () => {
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion 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": "5.0.3",
"version": "5.0.4",
"description": "Country list with dial code and flag",
"main": "dist/index.js",
"scripts": {
Expand Down
5 changes: 4 additions & 1 deletion src/flag-svg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@ declare global {
CountryFlagSvg: CountryFlagSvgInterface
}
}
window.CountryFlagSvg = CountryFlagSvg

if (typeof window !== 'undefined') {
window.CountryFlagSvg = CountryFlagSvg
}
export default CountryFlagSvg
35 changes: 18 additions & 17 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,30 +84,29 @@ class App {
}

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

/**
* group countries by the first letter of their name
* Group countries by the first letter of their name
* @param array
* @returns
* @returns {GroupedCountries}
*/
public groupCountriesByFirstLetter(array?: CountryInterface[]): GroupedCountries {
const grouped: GroupedCountries = {}
const countries = array ?? this.getAll()
countries.forEach((obj) => {
const firstLetter = obj.name.charAt(0).toLowerCase()
public groupCountriesByFirstLetter(array?: Country[] | CountryInterface[]): GroupedCountries {
const grouped: Record<string, Country[]> = {}
this.getAll().forEach((country) => {
const firstLetter = country.name.charAt(0).toLowerCase()
if (!grouped[firstLetter]) {
grouped[firstLetter] = []
}
grouped[firstLetter].push(obj)
grouped[firstLetter].push(country)
})
return grouped
}

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

const CountryList = new App()
Expand All @@ -118,7 +117,9 @@ declare global {
}
}

window.CountryList = CountryList
if (typeof window !== 'undefined') {
window.CountryList = CountryList
}

export default CountryList
export { Country, FilterOption }
8 changes: 5 additions & 3 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Country } from '..'

interface CountryInterface {
name: string
dial_code: string
Expand All @@ -17,8 +19,8 @@ interface FilterOption {
withSecondary: boolean
}

export type GroupedCountries = {
[key: string]: CountryInterface[]
type GroupedCountries = {
[key: string]: Country[]
}

export { CountryInterface, CountryFlagSvgInterface, FilterOption }
export { CountryInterface, CountryFlagSvgInterface, FilterOption, GroupedCountries }

0 comments on commit 764d6fc

Please sign in to comment.