Skip to content

Commit

Permalink
docs: update docs to use libphonenumber-js instead of google-libphone…
Browse files Browse the repository at this point in the history
…number
  • Loading branch information
FranciscoKloganB committed Jan 22, 2024
1 parent 579fc8d commit f55fdbd
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 49 deletions.
100 changes: 59 additions & 41 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# @franciscokloganb/countries

This package provides a client and server friendly objects and functions that facilitate
the way you build web-based forms that intended to collect user telephone details.
This package provides a client and server friendly objects and functions that facilitate the way you build web-based forms that intended to collect user telephone details.

With it you gain access to be country `dialCodes`, country `flags` and more.

Expand All @@ -13,7 +12,7 @@ This package provides all countries with their flag emoji, flag svg and dial num
- Server-side-rendering compatible;
- No external dependencies;
- No opinions with respect to validation;
- Example below on how to achieve that using `zod` and `google-libphonenumber`;
- Example below on how to achieve that using `zod` and `libphonenumber-js`;
- No pollution of `globalThis` (a.k.a. `window`) on the client;

## Installation
Expand All @@ -24,9 +23,7 @@ npm install @franciscokloganb/countries

## Usage

For ease of use, the API outlined below (inspired in the Repository Pattern), is
recommended. For details about the interfaces used by the methods, hover the function
name on your IDE.
For ease of use, the API outlined below (inspired in the Repository Pattern), is recommended. For details about the interfaces used by the methods, hover the function name on your IDE.

- `findByCountryCode(code: string, opts?: IFilterOption): ICountry[]`
- `findOneByCountryCode(code: string): ICountry | undefined`
Expand All @@ -35,26 +32,19 @@ name on your IDE.
- `findByKeyword(keyword: string, option?: IFilterOption): ICountry[]`
- `getAll(option?: IFilterOption): ICountry[]`

If this API is not enough, you can access the countries raw data by importing
the `countries` array. Notice however, that the objects within the array
contain more falsey values. Each country implements the `ICountry` interface,
just like the functions above. However, the functions above, return concrete
`Country` implementations which deal with falsey values.
If this API is not enough, you can access the countries raw data by importing the `countries` array. Notice however, that the objects within the array contain more falsey values. Each country implements the `ICountry` interface, just like the functions above. However, the functions above, return concrete `Country` implementations which deal with falsey values.

```ts
import { countries } from '@franciscokloganb/countries'
```

You can may import SVG strings directly if you prefer those over the emoji
flags found in `ICountry`-like objects.
You can may import SVG strings directly if you prefer those over the emoji flags found in `ICountry`-like objects.

```ts
import { FlagsSVGStrings } from '@franciscokloganb/countries'
```

If you prefer using real SVG files, you can import them from
`@franciscokloganb/countries/dist/assets`. How you import those may depend on
your bundler. An example with Vite is provided below.
If you prefer using real SVG files, you can import them from `@franciscokloganb/countries/dist/assets`. How you import those may depend on your bundler. An example with Vite is provided below.

Create an utility file.

Expand All @@ -71,62 +61,90 @@ const FlagsSVG = {
} as const

export { FlagsSVG }

```

Use it in your `react`, `vue`, `angular` or whatever component

```html
<script setup lang="ts">
import { FlagsSVG } from './your-utility-file'
import { FlagsSVG } from './your-utility-file'
</script>

<template>
<img src={FlagsSVG.portugal} alt="the portuguese flag" />
<img src="{FlagsSVG.portugal}" alt="the portuguese flag" />
</template>
```

## Example usage with google-libphonenumber and zod
## Example usage with libphonenumber-js (browser compatible) and zod

```ts
import { PhoneNumberUtil } from 'google-libphonenumber'
import { findOneByDialCode } from '@franciscokloganb/countries'
import { findOneByCountryDialCode } from '@franciscokloganb/countries'
import { type CountryCode, validatePhoneNumberLength } from 'libphonenumber-js'
import { z } from 'zod'

const phoneUtil = PhoneNumberUtil.getInstance()
import type { ZodAppErrorCode } from './ZodAppErrorCode.type'

type FailParams = { ctx: z.RefinementCtx; errorCode: ZodAppErrorCode; field: string }

const countryCallingCodeValidator = z.custom<string>(
const dialCode = z.custom<string>(
(value) => typeof value === 'string' && /^\+\d{1,4}$/.test(value),
'country_calling_code_invalid_format'
'telephone_dial_code_invalid_format' satisfies ZodAppErrorCode
)

const dialCodeExists = z.custom<string>((value) => {
if (typeof value !== 'string') {
return false
}

const maybeCountry = findOneByCountryDialCode(value)

return maybeCountry && maybeCountry.code
}, 'telephone_dial_code_unknown' satisfies ZodAppErrorCode)

const onlyNumbers = z.custom<string>(
(value) => typeof value === 'string' && /^[\d]*$/.test(value),
'only_numbers' satisfies ZodAppErrorCode
)

function fail({ ctx, errorCode, field }: FailParams) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
fatal: true,
message: errorCode,
path: [field],
})

return z.NEVER
}

const telephoneSchema = z
.object({
countryCallingCode: z.string().pipe(countryCallingCodeValidator),
localNumber: z.string().min(4).max(16),
dialCode: z.string().min(2).pipe(dialCode).pipe(dialCodeExists),
nationalNumber: z.string().max(16).pipe(onlyNumbers),
})
.superRefine(({ countryCallingCode, localNumber }, ctx) => {
const country = findOneByDialCode(countryCallingCode)
.superRefine(({ dialCode, nationalNumber }, ctx) => {
console.debug('nationalNumber', { dialCode, nationalNumber })

const country = findOneByCountryDialCode(dialCode)

if (!country) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: 'country_calling_code_unknown',
return fail({
ctx,
errorCode: `telephone_dial_code_is_required`,
field: 'dialCode',
})

return
}

const phoneNumber = phoneUtil.parse(localNumber, country.code)
const validPhoneNumber = phoneUtil.isValidNumberForRegion(phoneNumber, country.code)
const error = validatePhoneNumberLength(nationalNumber, country.code as CountryCode)

if (!validPhoneNumber) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: 'telephone_number_invalid_for_region',
})
if (error) {
const errorCode =
`telephone_national_number_${error.toLowerCase()}` as ZodAppErrorCode

return fail({ ctx, errorCode, field: 'nationalNumber' })
}

console.debug('nationalNumber is valid', { dialCode, nationalNumber })
})

type TelephoneSchema = z.infer<typeof telephoneSchema>
Expand Down
7 changes: 0 additions & 7 deletions package-lock.json

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

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
"@ianvs/prettier-plugin-sort-imports": "^4.1.1",
"@jest/types": "^29.6.3",
"@size-limit/preset-small-lib": "^11.0.1",
"@types/google-libphonenumber": "^7.4.30",
"@types/jest": "^29.5.11",
"@types/node": "^20.10.6",
"@typescript-eslint/eslint-plugin": "^6.16.0",
Expand Down

0 comments on commit f55fdbd

Please sign in to comment.