123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- import {
- VALID_DIGITS,
-
- } from '../constants.js'
- export const PLUS_SIGN = '+'
- const RFC3966_VISUAL_SEPARATOR_ = '[\\-\\.\\(\\)]?'
- const RFC3966_PHONE_DIGIT_ = '(' + '[' + VALID_DIGITS + ']' + '|' + RFC3966_VISUAL_SEPARATOR_ + ')'
- const RFC3966_GLOBAL_NUMBER_DIGITS_ =
- '^' +
- '\\' +
- PLUS_SIGN +
- RFC3966_PHONE_DIGIT_ +
- '*' +
- '[' + VALID_DIGITS + ']' +
- RFC3966_PHONE_DIGIT_ +
- '*' +
- '$'
- const RFC3966_GLOBAL_NUMBER_DIGITS_PATTERN_ = new RegExp(RFC3966_GLOBAL_NUMBER_DIGITS_, 'g')
- const ALPHANUM_ = VALID_DIGITS
- const RFC3966_DOMAINLABEL_ = '[' + ALPHANUM_ + ']+((\\-)*[' + ALPHANUM_ + '])*'
- const VALID_ALPHA_ = 'a-zA-Z'
- const RFC3966_TOPLABEL_ = '[' + VALID_ALPHA_ + ']+((\\-)*[' + ALPHANUM_ + '])*'
- const RFC3966_DOMAINNAME_ = '^(' + RFC3966_DOMAINLABEL_ + '\\.)*' + RFC3966_TOPLABEL_ + '\\.?$'
- const RFC3966_DOMAINNAME_PATTERN_ = new RegExp(RFC3966_DOMAINNAME_, 'g')
- export const RFC3966_PREFIX_ = 'tel:'
- export const RFC3966_PHONE_CONTEXT_ = ';phone-context='
- export const RFC3966_ISDN_SUBADDRESS_ = ';isub='
- export default function extractPhoneContext(numberToExtractFrom) {
- const indexOfPhoneContext = numberToExtractFrom.indexOf(RFC3966_PHONE_CONTEXT_)
-
- if (indexOfPhoneContext < 0) {
- return null
- }
- const phoneContextStart = indexOfPhoneContext + RFC3966_PHONE_CONTEXT_.length
-
- if (phoneContextStart >= numberToExtractFrom.length) {
- return ''
- }
- const phoneContextEnd = numberToExtractFrom.indexOf(';', phoneContextStart)
-
- if (phoneContextEnd >= 0) {
- return numberToExtractFrom.substring(phoneContextStart, phoneContextEnd)
- } else {
- return numberToExtractFrom.substring(phoneContextStart)
- }
- }
- export function isPhoneContextValid(phoneContext) {
- if (phoneContext === null) {
- return true
- }
- if (phoneContext.length === 0) {
- return false
- }
-
- return RFC3966_GLOBAL_NUMBER_DIGITS_PATTERN_.test(phoneContext) ||
- RFC3966_DOMAINNAME_PATTERN_.test(phoneContext)
- }
|