123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396 |
- import PhoneNumber from './PhoneNumber.js'
- import {
- MAX_LENGTH_FOR_NSN,
- MAX_LENGTH_COUNTRY_CODE,
- VALID_PUNCTUATION
- } from './constants.js'
- import createExtensionPattern from './helpers/extension/createExtensionPattern.js'
- import RegExpCache from './findNumbers/RegExpCache.js'
- import {
- limit,
- trimAfterFirstMatch
- } from './findNumbers/util.js'
- import {
- _pL,
- _pN,
- pZ,
- PZ,
- pNd
- } from './findNumbers/utf-8.js'
- import Leniency from './findNumbers/Leniency.js'
- import parsePreCandidate from './findNumbers/parsePreCandidate.js'
- import isValidPreCandidate from './findNumbers/isValidPreCandidate.js'
- import isValidCandidate, { LEAD_CLASS } from './findNumbers/isValidCandidate.js'
- import { isSupportedCountry } from './metadata.js'
- import parsePhoneNumber from './parsePhoneNumber.js'
- const USE_NON_GEOGRAPHIC_COUNTRY_CODE = false
- const EXTN_PATTERNS_FOR_MATCHING = createExtensionPattern('matching')
- const INNER_MATCHES =
- [
-
- '\\/+(.*)/',
-
-
- '(\\([^(]*)',
-
-
- `(?:${pZ}-|-${pZ})${pZ}*(.+)`,
-
-
-
- `[\u2012-\u2015\uFF0D]${pZ}*(.+)`,
-
- `\\.+${pZ}*([^.]+)`,
-
- `${pZ}+(${PZ}+)`
- ]
- const leadLimit = limit(0, 2)
- const punctuationLimit = limit(0, 4)
- const digitBlockLimit = MAX_LENGTH_FOR_NSN + MAX_LENGTH_COUNTRY_CODE
- const blockLimit = limit(0, digitBlockLimit)
- const punctuation = `[${VALID_PUNCTUATION}]` + punctuationLimit
- const digitSequence = pNd + limit(1, digitBlockLimit)
- const PATTERN = '(?:' + LEAD_CLASS + punctuation + ')' + leadLimit
- + digitSequence + '(?:' + punctuation + digitSequence + ')' + blockLimit
- + '(?:' + EXTN_PATTERNS_FOR_MATCHING + ')?'
- const UNWANTED_END_CHAR_PATTERN = new RegExp(`[^${_pN}${_pL}#]+$`)
- const NON_DIGITS_PATTERN = /(\D+)/
- const MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER || Math.pow(2, 53) - 1
- export default class PhoneNumberMatcher
- {
-
- constructor(text = '', options = {}, metadata)
- {
- options = {
- v2: options.v2,
- defaultCallingCode: options.defaultCallingCode,
- defaultCountry: options.defaultCountry && isSupportedCountry(options.defaultCountry, metadata) ? options.defaultCountry : undefined,
- leniency: options.leniency || (options.extended ? 'POSSIBLE' : 'VALID'),
- maxTries: options.maxTries || MAX_SAFE_INTEGER
- }
-
- if (!options.leniency) {
- throw new TypeError('`leniency` is required')
- }
- if (options.leniency !== 'POSSIBLE' && options.leniency !== 'VALID') {
- throw new TypeError(`Invalid \`leniency\`: "${options.leniency}". Supported values: "POSSIBLE", "VALID".`)
- }
-
- if (options.maxTries < 0) {
- throw new TypeError('`maxTries` must be `>= 0`')
- }
- this.text = text
- this.options = options
- this.metadata = metadata
-
- this.leniency = Leniency[options.leniency]
- if (!this.leniency) {
- throw new TypeError(`Unknown leniency: "${options.leniency}"`)
- }
-
- this.maxTries = options.maxTries
- this.PATTERN = new RegExp(PATTERN, 'ig')
-
- this.state = 'NOT_READY'
-
- this.searchIndex = 0
-
-
-
-
- this.regExpCache = new RegExpCache(32)
- }
-
- find() {
-
-
- let matches
- while ((this.maxTries > 0) && (matches = this.PATTERN.exec(this.text)) !== null) {
- let candidate = matches[0]
- const offset = matches.index
- candidate = parsePreCandidate(candidate)
- if (isValidPreCandidate(candidate, offset, this.text)) {
- const match =
-
- this.parseAndVerify(candidate, offset, this.text)
-
-
- || this.extractInnerMatch(candidate, offset, this.text)
- if (match) {
- if (this.options.v2) {
- return {
- startsAt: match.startsAt,
- endsAt: match.endsAt,
- number: match.phoneNumber
- }
- } else {
- const { phoneNumber } = match
- const result = {
- startsAt: match.startsAt,
- endsAt: match.endsAt,
- phone: phoneNumber.nationalNumber
- }
- if (phoneNumber.country) {
-
- if (USE_NON_GEOGRAPHIC_COUNTRY_CODE && country === '001') {
- result.countryCallingCode = phoneNumber.countryCallingCode
- } else {
- result.country = phoneNumber.country
- }
- } else {
- result.countryCallingCode = phoneNumber.countryCallingCode
- }
- if (phoneNumber.ext) {
- result.ext = phoneNumber.ext
- }
- return result
- }
- }
- }
- this.maxTries--
- }
- }
-
- extractInnerMatch(substring, offset, text) {
- for (const innerMatchPattern of INNER_MATCHES) {
- let isFirstMatch = true
- let candidateMatch
- const innerMatchRegExp = new RegExp(innerMatchPattern, 'g')
- while (this.maxTries > 0 && (candidateMatch = innerMatchRegExp.exec(substring)) !== null) {
- if (isFirstMatch) {
-
- const candidate = trimAfterFirstMatch(
- UNWANTED_END_CHAR_PATTERN,
- substring.slice(0, candidateMatch.index)
- )
- const match = this.parseAndVerify(candidate, offset, text)
- if (match) {
- return match
- }
- this.maxTries--
- isFirstMatch = false
- }
- const candidate = trimAfterFirstMatch(UNWANTED_END_CHAR_PATTERN, candidateMatch[1])
-
-
-
-
-
- const candidateIndexGuess = substring.indexOf(candidate, candidateMatch.index)
- const match = this.parseAndVerify(candidate, offset + candidateIndexGuess, text)
- if (match) {
- return match
- }
- this.maxTries--
- }
- }
- }
-
- parseAndVerify(candidate, offset, text) {
- if (!isValidCandidate(candidate, offset, text, this.options.leniency)) {
- return
- }
- const phoneNumber = parsePhoneNumber(
- candidate,
- {
- extended: true,
- defaultCountry: this.options.defaultCountry,
- defaultCallingCode: this.options.defaultCallingCode
- },
- this.metadata
- )
- if (!phoneNumber) {
- return
- }
- if (!phoneNumber.isPossible()) {
- return
- }
- if (this.leniency(phoneNumber, {
- candidate,
- defaultCountry: this.options.defaultCountry,
- metadata: this.metadata,
- regExpCache: this.regExpCache
- })) {
- return {
- startsAt: offset,
- endsAt: offset + candidate.length,
- phoneNumber
- }
- }
- }
- hasNext()
- {
- if (this.state === 'NOT_READY')
- {
- this.lastMatch = this.find()
- if (this.lastMatch)
- {
-
- this.state = 'READY'
- }
- else
- {
- this.state = 'DONE'
- }
- }
- return this.state === 'READY'
- }
- next()
- {
-
- if (!this.hasNext())
- {
- throw new Error('No next element')
- }
-
- const result = this.lastMatch
- this.lastMatch = null
- this.state = 'NOT_READY'
- return result
- }
- }
|