Modules@robinpath/phone
phone

@robinpath/phone

0.1.2Node.jsPublic

Phone number parsing, formatting, validation, country detection, and comparison

Phone

Phone number parsing, formatting, validation, country detection, and comparison

Package: @robinpath/phone | Category: Utility | Type: Utility

Authentication

No authentication required. All functions are available immediately.

Use Cases

Use the phone module when you need to:

  • Parse phone number -- Use phone.parse to perform this operation
  • Format phone in national format -- Use phone.format to perform this operation
  • Format to E.164 -- Use phone.formatE164 to perform this operation
  • Format as international -- Use phone.formatInternational to perform this operation
  • Validate phone number -- Use phone.validate to perform this operation

Quick Reference

FunctionDescriptionReturns
parseParse phone number{countryCode, dialCode, nationalNumber, e164, isValid}
formatFormat phone in national formatFormatted string
formatE164Format to E.164E.164 string
formatInternationalFormat as internationalInternational format
validateValidate phone numbertrue if valid
getCountryDetect country from phoneCountry code or null
getTypeGuess phone type`mobile
normalizeStrip non-digitsDigits only
maskMask phone for displayMasked string
dialCodeGet dial code for countryDial code
countryInfoGet country phone infoCountry info
listCountriesList supported countriesCountry codes
compareCompare two phone numberstrue if same

Functions

parse

Parse phone number

Module: phone | Returns: object -- {countryCode, dialCode, nationalNumber, e164, isValid}

phone.parse "+15551234567"
ParameterTypeRequiredDescription
phonestringYesPhone number
countrystringNoDefault country code

format

Format phone in national format

Module: phone | Returns: string -- Formatted string

phone.format "5551234567" "US"
ParameterTypeRequiredDescription
phonestringYesPhone
countrystringNoCountry code

formatE164

Format to E.164

Module: phone | Returns: string -- E.164 string

phone.formatE164 "5551234567"
ParameterTypeRequiredDescription
phonestringYesPhone
countrystringNoCountry code

formatInternational

Format as international

Module: phone | Returns: string -- International format

phone.formatInternational "5551234567"
ParameterTypeRequiredDescription
phonestringYesPhone
countrystringNoCountry code

validate

Validate phone number

Module: phone | Returns: boolean -- true if valid

phone.validate "+15551234567"
ParameterTypeRequiredDescription
phonestringYesPhone
countrystringNoCountry code

getCountry

Detect country from phone

Module: phone | Returns: string -- Country code or null

phone.getCountry "+44123456789"
ParameterTypeRequiredDescription
phonestringYesPhone with + prefix

getType

Guess phone type

Module: phone | Returns: string -- mobile|landline|unknown

phone.getType "5551234567"
ParameterTypeRequiredDescription
phonestringYesPhone
countrystringNoCountry

normalize

Strip non-digits

Module: phone | Returns: string -- Digits only

phone.normalize "(555) 123-4567"
ParameterTypeRequiredDescription
phonestringYesPhone

mask

Mask phone for display

Module: phone | Returns: string -- Masked string

phone.mask "5551234567"
ParameterTypeRequiredDescription
phonestringYesPhone
visibleDigitsnumberNoVisible digits (default 4)

dialCode

Get dial code for country

Module: phone | Returns: string -- Dial code

phone.dialCode "GB"
ParameterTypeRequiredDescription
countrystringYesCountry code

countryInfo

Get country phone info

Module: phone | Returns: object -- Country info

phone.countryInfo "US"
ParameterTypeRequiredDescription
countrystringYesCountry code

listCountries

List supported countries

Module: phone | Returns: array -- Country codes

phone.listCountries
ParameterTypeRequiredDescription
(none)NoCall with no arguments

compare

Compare two phone numbers

Module: phone | Returns: boolean -- true if same

phone.compare "(555) 123-4567" "+15551234567"
ParameterTypeRequiredDescription
phone1stringYesFirst phone
phone2stringYesSecond phone
countrystringNoDefault country

Error Handling

All functions throw on failure. Common errors:

ErrorCause
(standard errors)Check function parameters and authentication
@desc "Parse and validate result"
do
  set $result as phone.parse "+15551234567"
  if $result != null
    print "Success"
  else
    print "No result"
  end
enddo

Recipes

1. List and iterate Country

Retrieve all items and loop through them.

@desc "Get country and iterate results"
do
  set $result as phone.getCountry "+44123456789"
  each $item in $result
    print $item
  end
enddo

2. Multi-step Phone workflow

Chain multiple phone operations together.

@desc "Parse, format, and more"
do
  set $r_parse as phone.parse "+15551234567"
  set $r_format as phone.format "5551234567" "US"
  set $r_formatE164 as phone.formatE164 "5551234567"
  print "All operations complete"
enddo

3. Safe parse with validation

Check results before proceeding.

@desc "Parse and validate result"
do
  set $result as phone.parse "+15551234567"
  if $result != null
    print "Success: " + $result
  else
    print "Operation returned no data"
  end
enddo

Related Modules

  • json -- JSON module for complementary functionality

Versions (1)

VersionTagPublished
0.1.2latest1 months ago
Install
$ robinpath add @robinpath/phone

Collaborators

Dumitru Balaban
Dumitru Balaban
@dumitru
View all @robinpath modules
Version0.1.2
LicenseMIT
Unpacked Size5.3 KB
Versions1
Weekly Downloads21
Total Downloads21
Stars0
Last Publish1 months ago
Created1 months ago

Keywords

Category

utilities