Millwork
    Preparing search index...

    Module @millwork-ts/phone

    @millwork-ts/phone

    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"
    • The codec reads an international number in any format, such as "+1 (416) 555-1234".
    • The codec reads a national number, such as "(416) 555-1234", with the defaultCountry option.
    • The codec writes E.164.
    • A number that has a length that is not possible for its country is bad input. The codec does not do a full validity check, because the metadata can be older than the number plan. Use 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' })],
    })
    PhoneOptions
    codecs
    phone