Telefonnummer
Telefonnummer is phone number in Swedish. This package formats all Swedish phone numbers, both mobile and landline, to a standard format. Area code information is provided by Post- och telestyrelsen (PTS).
Installation
npm install @opendevtools/telefonnummer --save
Parse
parse(phoneNumber: string, options?: ParserOptions ): string
Take a phone number and return a parsed version of the number. Parser is also default export of package, but might be removed as default in the future.
Example
import { parse } from '@opendevtools/telefonnummer'
parse('222')
parse('0701234567')
parse('468123456')
parse('031626262')
parse('050012345')
parse('0701234567', { separator: ':' })
parse('0701234567', { internationalized: true })
Parser options
Property | Type | Default | Description |
---|
separator | string | '-' | Separator between area code and phone number |
internationalized | boolean | false | If true returns on the format +46xxxxxxxxxx |
Area code
areaCode(area: string): string
Returns the area code of the provided city
Example
import { areaCode } from '@opendevtools/telefonnummer'
areaCode('Stockholm')
areaCode('Korpilombolo')
Numbering area
numberingArea(areaCode: string | number): string
Returns the numbering area for a provided area code or phone number. Also handles numbers without leading zero.
Example
import { numberingArea } from '@opendevtools/telefonnummer'
numberingArea('0977-123 45')
numberingArea('081234567')
numberingArea('08')
numberingArea('031')
numberingArea(8)
Validator
interface ValidatorOptions {
onlyMobile?: boolean
}
validator(phoneNumber: string, options?: ValidatorOptions): boolean
Validates both mobile and landline numbers.
Example
import { validator } from '@opendevtools/telefonnummer'
validator('0977-123 45')
validator('081234567')
validator('050012123456')
validator('0500123456', { onlyMobile: true })
validator('0701234567', { onlyMobile: true })
Area codes
areaCodes(): string[]
Returns a number sorted array of all the area codes.
Example
import { areaCodes } from '@opendevtools/telefonnummer'
areaCodes()
Normalize
normalize(string): string
Clean up any non-digits and country codes from phone number.
Example
import { normalize } from '@opendevtools/telefonnummer'
normalize('070-123.45x67')
normalize('+46701234567')
AreEqual
areEqual(string, string): boolean
Cleans up provided strings and checks if the two phone number values match.
Example
import { areEqual } from '@opendevtools/telefonnummer'
areEqual('0701234567', '0701234567')
areEqual('070-123.45 67', '070123--45 67')
Tests
npm test
Contributors
Thanks goes to these wonderful people (emoji key):
This project follows the all-contributors specification. Contributions of any kind welcome!