A Millwork codec for phone numbers with libphonenumber-js.
pnpm add @millwork-ts/phone @millwork-ts/core libphonenumber-js
| Codec | Value | Raw |
|---|---|---|
phone |
a frozen libphonenumber-js PhoneNumber |
an E.164 string, "+14165551234" |
"+1 (416) 555-1234"."(416) 555-1234", with the defaultCountry
option.value.isValid() for that check.| Option | Type | Use |
|---|---|---|
defaultCountry |
CountryCode |
The country of a national number, such as 'CA'. |
metadata |
MetadataJson |
The metadata flavour. The default is min. |
The min metadata is the smallest. It does not give the number type. To get the type, such
as MOBILE, give the max or mobile flavour:
import max from 'libphonenumber-js/max/metadata'
phone.hydrate('+14165551234', { metadata: max })?.getType()
equals compares the E.164 numbers. "+1 416 555 1234" equals "(416) 555-1234" with
defaultCountry: 'CA'.
Register the codecs before the store calls transformations(), because transformations()
reads the registry when it runs. See the
@millwork-ts/warp-drive README.
import { codecs } from '@millwork-ts/phone'
import { registerCodecs, valueField } from '@millwork-ts/warp-drive'
import { withDefaults } from '@warp-drive/core/reactive'
registerCodecs(codecs)
export const ContactSchema = withDefaults({
type: 'contact',
fields: [valueField('mobile', 'phone', { defaultCountry: 'CA' })],
})