🎯 MapView v2.0 - Global Deployment Ready

 MAJOR FEATURES:
• Auto-zoom intelligence với smart bounds fitting
• Enhanced 3D GPS markers với pulsing effects
• Professional route display với 6-layer rendering
• Status-based parking icons với availability indicators
• Production-ready build optimizations

🗺️ AUTO-ZOOM FEATURES:
• Smart bounds fitting cho GPS + selected parking
• Adaptive padding (50px) cho visual balance
• Max zoom control (level 16) để tránh quá gần
• Dynamic centering khi không có selection

🎨 ENHANCED VISUALS:
• 3D GPS marker với multi-layer pulse effects
• Advanced parking icons với status colors
• Selection highlighting với animation
• Dimming system cho non-selected items

🛣️ ROUTE SYSTEM:
• OpenRouteService API integration
• Multi-layer route rendering (glow, shadow, main, animated)
• Real-time distance & duration calculation
• Visual route info trong popup

📱 PRODUCTION READY:
• SSR safe với dynamic imports
• Build errors resolved
• Global deployment via Vercel
• Optimized performance

🌍 DEPLOYMENT:
• Vercel: https://whatever-ctk2auuxr-phong12hexdockworks-projects.vercel.app
• Bundle size: 22.8 kB optimized
• Global CDN distribution
• HTTPS enabled

💾 VERSION CONTROL:
• MapView-v2.0.tsx backup created
• MAPVIEW_VERSIONS.md documentation
• Full version history tracking
This commit is contained in:
2025-07-20 19:52:16 +07:00
parent 3203463a6a
commit c65cc97a33
64624 changed files with 7199453 additions and 6462 deletions

View File

@@ -0,0 +1,461 @@
import Metadata from './metadata.js'
import PhoneNumber from './PhoneNumber.js'
import AsYouTypeState from './AsYouTypeState.js'
import AsYouTypeFormatter, { DIGIT_PLACEHOLDER } from './AsYouTypeFormatter.js'
import AsYouTypeParser, { extractFormattedDigitsAndPlus } from './AsYouTypeParser.js'
import getCountryByCallingCode from './helpers/getCountryByCallingCode.js'
import getCountryByNationalNumber from './helpers/getCountryByNationalNumber.js'
import isObject from './helpers/isObject.js'
const USE_NON_GEOGRAPHIC_COUNTRY_CODE = false
export default class AsYouType {
/**
* @param {(string|object)?} [optionsOrDefaultCountry] - The default country used for parsing non-international phone numbers. Can also be an `options` object.
* @param {Object} metadata
*/
constructor(optionsOrDefaultCountry, metadata) {
this.metadata = new Metadata(metadata)
const [defaultCountry, defaultCallingCode] = this.getCountryAndCallingCode(optionsOrDefaultCountry)
// `this.defaultCountry` and `this.defaultCallingCode` aren't required to be in sync.
// For example, `this.defaultCountry` could be `"AR"` and `this.defaultCallingCode` could be `undefined`.
// So `this.defaultCountry` and `this.defaultCallingCode` are totally independent.
this.defaultCountry = defaultCountry
this.defaultCallingCode = defaultCallingCode
this.reset()
}
getCountryAndCallingCode(optionsOrDefaultCountry) {
// Set `defaultCountry` and `defaultCallingCode` options.
let defaultCountry
let defaultCallingCode
// Turns out `null` also has type "object". Weird.
if (optionsOrDefaultCountry) {
if (isObject(optionsOrDefaultCountry)) {
defaultCountry = optionsOrDefaultCountry.defaultCountry
defaultCallingCode = optionsOrDefaultCountry.defaultCallingCode
} else {
defaultCountry = optionsOrDefaultCountry
}
}
if (defaultCountry && !this.metadata.hasCountry(defaultCountry)) {
defaultCountry = undefined
}
if (defaultCallingCode) {
/* istanbul ignore if */
if (USE_NON_GEOGRAPHIC_COUNTRY_CODE) {
if (this.metadata.isNonGeographicCallingCode(defaultCallingCode)) {
defaultCountry = '001'
}
}
}
return [defaultCountry, defaultCallingCode]
}
/**
* Inputs "next" phone number characters.
* @param {string} text
* @return {string} Formatted phone number characters that have been input so far.
*/
input(text) {
const {
digits,
justLeadingPlus
} = this.parser.input(text, this.state)
if (justLeadingPlus) {
this.formattedOutput = '+'
} else if (digits) {
this.determineTheCountryIfNeeded()
// Match the available formats by the currently available leading digits.
if (this.state.nationalSignificantNumber) {
this.formatter.narrowDownMatchingFormats(this.state)
}
let formattedNationalNumber
if (this.metadata.hasSelectedNumberingPlan()) {
formattedNationalNumber = this.formatter.format(digits, this.state)
}
if (formattedNationalNumber === undefined) {
// See if another national (significant) number could be re-extracted.
if (this.parser.reExtractNationalSignificantNumber(this.state)) {
this.determineTheCountryIfNeeded()
// If it could, then re-try formatting the new national (significant) number.
const nationalDigits = this.state.getNationalDigits()
if (nationalDigits) {
formattedNationalNumber = this.formatter.format(nationalDigits, this.state)
}
}
}
this.formattedOutput = formattedNationalNumber
? this.getFullNumber(formattedNationalNumber)
: this.getNonFormattedNumber()
}
return this.formattedOutput
}
reset() {
this.state = new AsYouTypeState({
onCountryChange: (country) => {
// Before version `1.6.0`, the official `AsYouType` formatter API
// included the `.country` property of an `AsYouType` instance.
// Since that property (along with the others) have been moved to
// `this.state`, `this.country` property is emulated for compatibility
// with the old versions.
this.country = country
},
onCallingCodeChange: (callingCode, country) => {
this.metadata.selectNumberingPlan(country, callingCode)
this.formatter.reset(this.metadata.numberingPlan, this.state)
this.parser.reset(this.metadata.numberingPlan)
}
})
this.formatter = new AsYouTypeFormatter({
state: this.state,
metadata: this.metadata
})
this.parser = new AsYouTypeParser({
defaultCountry: this.defaultCountry,
defaultCallingCode: this.defaultCallingCode,
metadata: this.metadata,
state: this.state,
onNationalSignificantNumberChange: () => {
this.determineTheCountryIfNeeded()
this.formatter.reset(this.metadata.numberingPlan, this.state)
}
})
this.state.reset({
country: this.defaultCountry,
callingCode: this.defaultCallingCode
})
this.formattedOutput = ''
return this
}
/**
* Returns `true` if the phone number is being input in international format.
* In other words, returns `true` if and only if the parsed phone number starts with a `"+"`.
* @return {boolean}
*/
isInternational() {
return this.state.international
}
/**
* Returns the "calling code" part of the phone number when it's being input
* in an international format.
* If no valid calling code has been entered so far, returns `undefined`.
* @return {string} [callingCode]
*/
getCallingCode() {
// If the number is being input in national format and some "default calling code"
// has been passed to `AsYouType` constructor, then `this.state.callingCode`
// is equal to that "default calling code".
//
// If the number is being input in national format and no "default calling code"
// has been passed to `AsYouType` constructor, then returns `undefined`,
// even if a "default country" has been passed to `AsYouType` constructor.
//
if (this.isInternational()) {
return this.state.callingCode
}
}
// A legacy alias.
getCountryCallingCode() {
return this.getCallingCode()
}
/**
* Returns a two-letter country code of the phone number.
* Returns `undefined` for "non-geographic" phone numbering plans.
* Returns `undefined` if no phone number has been input yet.
* @return {string} [country]
*/
getCountry() {
const { digits } = this.state
// Return `undefined` if no digits have been input yet.
if (digits) {
return this._getCountry()
}
}
/**
* Returns a two-letter country code of the phone number.
* Returns `undefined` for "non-geographic" phone numbering plans.
* @return {string} [country]
*/
_getCountry() {
const { country } = this.state
/* istanbul ignore if */
if (USE_NON_GEOGRAPHIC_COUNTRY_CODE) {
// `AsYouType.getCountry()` returns `undefined`
// for "non-geographic" phone numbering plans.
if (country === '001') {
return
}
}
return country
}
determineTheCountryIfNeeded() {
// Suppose a user enters a phone number in international format,
// and there're several countries corresponding to that country calling code,
// and a country has been derived from the number, and then
// a user enters one more digit and the number is no longer
// valid for the derived country, so the country should be re-derived
// on every new digit in those cases.
//
// If the phone number is being input in national format,
// then it could be a case when `defaultCountry` wasn't specified
// when creating `AsYouType` instance, and just `defaultCallingCode` was specified,
// and that "calling code" could correspond to a "non-geographic entity",
// or there could be several countries corresponding to that country calling code.
// In those cases, `this.country` is `undefined` and should be derived
// from the number. Again, if country calling code is ambiguous, then
// `this.country` should be re-derived with each new digit.
//
if (!this.state.country || this.isCountryCallingCodeAmbiguous()) {
this.determineTheCountry()
}
}
// Prepends `+CountryCode ` in case of an international phone number
getFullNumber(formattedNationalNumber) {
if (this.isInternational()) {
const prefix = (text) => this.formatter.getInternationalPrefixBeforeCountryCallingCode(this.state, {
spacing: text ? true : false
}) + text
const { callingCode } = this.state
if (!callingCode) {
return prefix(`${this.state.getDigitsWithoutInternationalPrefix()}`)
}
if (!formattedNationalNumber) {
return prefix(callingCode)
}
return prefix(`${callingCode} ${formattedNationalNumber}`)
}
return formattedNationalNumber
}
getNonFormattedNationalNumberWithPrefix() {
const {
nationalSignificantNumber,
complexPrefixBeforeNationalSignificantNumber,
nationalPrefix
} = this.state
let number = nationalSignificantNumber
const prefix = complexPrefixBeforeNationalSignificantNumber || nationalPrefix
if (prefix) {
number = prefix + number
}
return number
}
getNonFormattedNumber() {
const { nationalSignificantNumberMatchesInput } = this.state
return this.getFullNumber(
nationalSignificantNumberMatchesInput
? this.getNonFormattedNationalNumberWithPrefix()
: this.state.getNationalDigits()
)
}
getNonFormattedTemplate() {
const number = this.getNonFormattedNumber()
if (number) {
return number.replace(/[\+\d]/g, DIGIT_PLACEHOLDER)
}
}
isCountryCallingCodeAmbiguous() {
const { callingCode } = this.state
const countryCodes = this.metadata.getCountryCodesForCallingCode(callingCode)
return countryCodes && countryCodes.length > 1
}
// Determines the country of the phone number
// entered so far based on the country phone code
// and the national phone number.
determineTheCountry() {
this.state.setCountry(getCountryByCallingCode(
this.isInternational() ? this.state.callingCode : this.defaultCallingCode,
{
nationalNumber: this.state.nationalSignificantNumber,
defaultCountry: this.defaultCountry,
metadata: this.metadata
}
))
}
/**
* Returns a E.164 phone number value for the user's input.
*
* For example, for country `"US"` and input `"(222) 333-4444"`
* it will return `"+12223334444"`.
*
* For international phone number input, it will also auto-correct
* some minor errors such as using a national prefix when writing
* an international phone number. For example, if the user inputs
* `"+44 0 7400 000000"` then it will return an auto-corrected
* `"+447400000000"` phone number value.
*
* Will return `undefined` if no digits have been input,
* or when inputting a phone number in national format and no
* default country or default "country calling code" have been set.
*
* @return {string} [value]
*/
getNumberValue() {
const {
digits,
callingCode,
country,
nationalSignificantNumber
} = this.state
// Will return `undefined` if no digits have been input.
if (!digits) {
return
}
if (this.isInternational()) {
if (callingCode) {
return '+' + callingCode + nationalSignificantNumber
} else {
return '+' + digits
}
} else {
if (country || callingCode) {
const callingCode_ = country ? this.metadata.countryCallingCode() : callingCode
return '+' + callingCode_ + nationalSignificantNumber
}
}
}
/**
* Returns an instance of `PhoneNumber` class.
* Will return `undefined` if no national (significant) number
* digits have been entered so far, or if no `defaultCountry` has been
* set and the user enters a phone number not in international format.
*/
getNumber() {
const {
nationalSignificantNumber,
carrierCode,
callingCode
} = this.state
// `this._getCountry()` is basically same as `this.state.country`
// with the only change that it return `undefined` in case of a
// "non-geographic" numbering plan instead of `"001"` "internal use" value.
let country = this._getCountry()
if (!nationalSignificantNumber) {
return
}
// `state.country` and `state.callingCode` aren't required to be in sync.
// For example, `country` could be `"AR"` and `callingCode` could be `undefined`.
// So `country` and `callingCode` are totally independent.
if (!country && !callingCode) {
return
}
// By default, if `defaultCountry` parameter was passed when
// creating `AsYouType` instance, `state.country` is gonna be
// that `defaultCountry`, which doesn't entirely conform with
// `parsePhoneNumber()`'s behavior where it attempts to determine
// the country more precisely in cases when multiple countries
// could correspond to the same `countryCallingCode`.
// https://gitlab.com/catamphetamine/libphonenumber-js/-/issues/103#note_1417192969
//
// Because `AsYouType.getNumber()` method is supposed to be a 1:1
// equivalent for `parsePhoneNumber(AsYouType.getNumberValue())`,
// then it should also behave accordingly in cases of `country` ambiguity.
// That's how users of this library would expect it to behave anyway.
//
if (country) {
if (country === this.defaultCountry) {
// `state.country` and `state.callingCode` aren't required to be in sync.
// For example, `state.country` could be `"AR"` and `state.callingCode` could be `undefined`.
// So `state.country` and `state.callingCode` are totally independent.
const metadata = new Metadata(this.metadata.metadata)
metadata.selectNumberingPlan(country)
const callingCode = metadata.numberingPlan.callingCode()
const ambiguousCountries = this.metadata.getCountryCodesForCallingCode(callingCode)
if (ambiguousCountries.length > 1) {
const exactCountry = getCountryByNationalNumber(nationalSignificantNumber, {
countries: ambiguousCountries,
defaultCountry: this.defaultCountry,
metadata: this.metadata.metadata
})
if (exactCountry) {
country = exactCountry
}
}
}
}
const phoneNumber = new PhoneNumber(
country || callingCode,
nationalSignificantNumber,
this.metadata.metadata
)
if (carrierCode) {
phoneNumber.carrierCode = carrierCode
}
// Phone number extensions are not supported by "As You Type" formatter.
return phoneNumber
}
/**
* Returns `true` if the phone number is "possible".
* Is just a shortcut for `PhoneNumber.isPossible()`.
* @return {boolean}
*/
isPossible() {
const phoneNumber = this.getNumber()
if (!phoneNumber) {
return false
}
return phoneNumber.isPossible()
}
/**
* Returns `true` if the phone number is "valid".
* Is just a shortcut for `PhoneNumber.isValid()`.
* @return {boolean}
*/
isValid() {
const phoneNumber = this.getNumber()
if (!phoneNumber) {
return false
}
return phoneNumber.isValid()
}
/**
* @deprecated
* This method is used in `react-phone-number-input/source/input-control.js`
* in versions before `3.0.16`.
*/
getNationalNumber() {
return this.state.nationalSignificantNumber
}
/**
* Returns the phone number characters entered by the user.
* @return {string}
*/
getChars() {
return (this.state.international ? '+' : '') + this.state.digits
}
/**
* Returns the template for the formatted phone number.
* @return {string}
*/
getTemplate() {
return this.formatter.getTemplate(this.state) || this.getNonFormattedTemplate() || ''
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,66 @@
import {
character,
MatchTree
} from './AsYouTypeFormatter.PatternParser.d.js'
interface MatchOptions {
allowOverflow?: boolean;
}
// An "overflow" match is when the string matches the pattern
// and there're still some characters left in it.
//
// For example, "12345" matches "12[0-5]|78" pattern with an overflow
// because "123" matches the "12[0-5]" variant of the pattern
// and there're still "45" characters left.
//
// This type of match is only returned when `allowOverflow` option is `true`.
// By default, `allowOverflow` is `false` and `undefined` ("no match" result)
// is returned in case of an "overflow" match.
//
interface MatchResultOverflow {
overflow: true;
}
// When there's a ("full") match, returns a match result.
//
// A ("full") match is when the string matches the entire pattern.
//
// For example, "123" fully matches "12[0-5]|78" pattern.
//
interface MatchResultFullMatch {
match: true;
matchedChars: character[];
}
// When there's a "partial" match, returns a "partial" match result.
//
// A "partial" match is when the string is not long enough
// to match the whole matching tree.
//
// For example, "123" is a partial match for "12[0-5]4|78" pattern,
// because "123" matched the "12[0-5]" part and the "4" part of the pattern
// is left uninvolved.
//
interface MatchResultPartialMatch {
partialMatch: true;
}
// When there's no match, returns `undefined`.
//
// For example, "123" doesn't match "456|789" pattern.
//
type MatchResultNoMatch = undefined;
type MatchResult =
MatchResultOverflow |
MatchResultFullMatch |
MatchResultPartialMatch |
MatchResultNoMatch;
export default class PatternMatcher {
constructor(pattern: string);
match(string: string, options?: MatchOptions): MatchResult;
}
function match(characters: character[], tree: MatchTree, last?: boolean);

View File

@@ -0,0 +1,197 @@
import PatternParser from './AsYouTypeFormatter.PatternParser.js'
export default class PatternMatcher {
constructor(pattern) {
this.matchTree = new PatternParser().parse(pattern)
}
match(string, { allowOverflow } = {}) {
if (!string) {
throw new Error('String is required')
}
const result = match(string.split(''), this.matchTree, true)
if (result && result.match) {
delete result.matchedChars
}
if (result && result.overflow) {
if (!allowOverflow) {
return
}
}
return result
}
}
/**
* Matches `characters` against a pattern compiled into a `tree`.
* @param {string[]} characters
* @param {Tree} tree — A pattern compiled into a `tree`. See the `*.d.ts` file for the description of the `tree` structure.
* @param {boolean} last — Whether it's the last (rightmost) subtree on its level of the match tree.
* @return {object} See the `*.d.ts` file for the description of the result object.
*/
function match(characters, tree, last) {
// If `tree` is a string, then `tree` is a single character.
// That's because when a pattern is parsed, multi-character-string parts
// of a pattern are compiled into arrays of single characters.
// I still wrote this piece of code for a "general" hypothetical case
// when `tree` could be a string of several characters, even though
// such case is not possible with the current implementation.
if (typeof tree === 'string') {
const characterString = characters.join('')
if (tree.indexOf(characterString) === 0) {
// `tree` is always a single character.
// If `tree.indexOf(characterString) === 0`
// then `characters.length === tree.length`.
/* istanbul ignore else */
if (characters.length === tree.length) {
return {
match: true,
matchedChars: characters
}
}
// `tree` is always a single character.
// If `tree.indexOf(characterString) === 0`
// then `characters.length === tree.length`.
/* istanbul ignore next */
return {
partialMatch: true,
// matchedChars: characters
}
}
if (characterString.indexOf(tree) === 0) {
if (last) {
// The `else` path is not possible because `tree` is always a single character.
// The `else` case for `characters.length > tree.length` would be
// `characters.length <= tree.length` which means `characters.length <= 1`.
// `characters` array can't be empty, so that means `characters === [tree]`,
// which would also mean `tree.indexOf(characterString) === 0` and that'd mean
// that the `if (tree.indexOf(characterString) === 0)` condition before this
// `if` condition would be entered, and returned from there, not reaching this code.
/* istanbul ignore else */
if (characters.length > tree.length) {
return {
overflow: true
}
}
}
return {
match: true,
matchedChars: characters.slice(0, tree.length)
}
}
return
}
if (Array.isArray(tree)) {
let restCharacters = characters.slice()
let i = 0
while (i < tree.length) {
const subtree = tree[i]
const result = match(restCharacters, subtree, last && (i === tree.length - 1))
if (!result) {
return
} else if (result.overflow) {
return result
} else if (result.match) {
// Continue with the next subtree with the rest of the characters.
restCharacters = restCharacters.slice(result.matchedChars.length)
if (restCharacters.length === 0) {
if (i === tree.length - 1) {
return {
match: true,
matchedChars: characters
}
} else {
return {
partialMatch: true,
// matchedChars: characters
}
}
}
} else {
/* istanbul ignore else */
if (result.partialMatch) {
return {
partialMatch: true,
// matchedChars: characters
}
} else {
throw new Error(`Unsupported match result:\n${JSON.stringify(result, null, 2)}`)
}
}
i++
}
// If `last` then overflow has already been checked
// by the last element of the `tree` array.
/* istanbul ignore if */
if (last) {
return {
overflow: true
}
}
return {
match: true,
matchedChars: characters.slice(0, characters.length - restCharacters.length)
}
}
switch (tree.op) {
case '|':
let partialMatch
for (const branch of tree.args) {
const result = match(characters, branch, last)
if (result) {
if (result.overflow) {
return result
} else if (result.match) {
return {
match: true,
matchedChars: result.matchedChars
}
} else {
/* istanbul ignore else */
if (result.partialMatch) {
partialMatch = true
} else {
throw new Error(`Unsupported match result:\n${JSON.stringify(result, null, 2)}`)
}
}
}
}
if (partialMatch) {
return {
partialMatch: true,
// matchedChars: ...
}
}
// Not even a partial match.
return
case '[]':
for (const char of tree.args) {
if (characters[0] === char) {
if (characters.length === 1) {
return {
match: true,
matchedChars: characters
}
}
if (last) {
return {
overflow: true
}
}
return {
match: true,
matchedChars: [char]
}
}
}
// No character matches.
return
/* istanbul ignore next */
default:
throw new Error(`Unsupported instruction tree: ${tree}`)
}
}

View File

@@ -0,0 +1,641 @@
import PatternMatcher from './AsYouTypeFormatter.PatternMatcher.js'
describe('AsYouTypeFormatter.PatternMatcher', function() {
it('should throw when no pattern is passed', function() {
expect(() => new PatternMatcher()).to.throw('Pattern is required')
})
it('should throw when no string is passed', function() {
const matcher = new PatternMatcher('1')
expect(() => matcher.match()).to.throw('String is required')
})
it('should throw on illegal characters', function() {
expect(() => new PatternMatcher('4(5|6)7')).to.throw('Illegal characters')
})
it('should throw on an illegal ] operator', function() {
expect(() => new PatternMatcher('4]7')).to.throw('"]" operator must be preceded by "[" operator')
})
it('should throw on an illegal - operator in a one-of set', function() {
expect(() => new PatternMatcher('[-5]')).to.throw('Couldn\'t parse a one-of set pattern: -5')
})
it('should throw on a non-finalized context', function() {
expect(() => new PatternMatcher('4(?:5|7')).to.throw('Non-finalized contexts left when pattern parse ended')
})
it('should throw on an illegal (|) operator', function() {
expect(() => new PatternMatcher('4(?:5|)7')).to.throw('No instructions found after "|" operator in an "or" group')
})
it('should throw on an illegal ) operator', function() {
expect(() => new PatternMatcher('4[56)]7')).to.throw('")" operator must be preceded by "(?:" operator')
})
it('should throw on an illegal | operator', function() {
expect(() => new PatternMatcher('4[5|6]7')).to.throw('operator can only be used inside "or" groups')
})
it('should match a one-digit pattern', function() {
const matcher = new PatternMatcher('4')
expect(matcher.match('1')).to.be.undefined
matcher.match('4').should.deep.equal({
match: true
})
expect(matcher.match('44')).to.be.undefined
matcher.match('44', { allowOverflow: true }).should.deep.equal({
overflow: true
})
})
it('should match a two-digit pattern', function() {
const matcher = new PatternMatcher('44')
expect(matcher.match('1')).to.be.undefined
matcher.match('4').should.deep.equal({
partialMatch: true
})
matcher.match('44').should.deep.equal({
match: true
})
expect(matcher.match('444')).to.be.undefined
matcher.match('444', { allowOverflow: true }).should.deep.equal({
overflow: true
})
expect(matcher.match('55')).to.be.undefined
})
it('should match a one-digit one-of set (single digit)', function() {
const matcher = new PatternMatcher('[4]')
expect(matcher.match('1')).to.be.undefined
matcher.match('4').should.deep.equal({
match: true
})
expect(matcher.match('44')).to.be.undefined
matcher.match('44', { allowOverflow: true }).should.deep.equal({
overflow: true
})
})
it('should match a one-digit one-of set (multiple digits)', function() {
const matcher = new PatternMatcher('[479]')
expect(matcher.match('1')).to.be.undefined
matcher.match('4').should.deep.equal({
match: true
})
expect(matcher.match('44')).to.be.undefined
matcher.match('44', { allowOverflow: true }).should.deep.equal({
overflow: true
})
})
it('should match a one-digit one-of set using a dash notation (not inclusive)', function() {
const matcher = new PatternMatcher('[2-5]')
expect(matcher.match('1')).to.be.undefined
matcher.match('4').should.deep.equal({
match: true
})
expect(matcher.match('44')).to.be.undefined
matcher.match('44', { allowOverflow: true }).should.deep.equal({
overflow: true
})
})
it('should match a one-digit one-of set using a dash notation (inclusive)', function() {
const matcher = new PatternMatcher('[3-4]')
expect(matcher.match('1')).to.be.undefined
matcher.match('4').should.deep.equal({
match: true
})
expect(matcher.match('44')).to.be.undefined
matcher.match('44', { allowOverflow: true }).should.deep.equal({
overflow: true
})
})
it('should match a one-digit one-of set including a dash notation', function() {
const matcher = new PatternMatcher('[124-68]')
expect(matcher.match('0')).to.be.undefined
matcher.match('1').should.deep.equal({
match: true
})
matcher.match('2').should.deep.equal({
match: true
})
expect(matcher.match('3')).to.be.undefined
matcher.match('4').should.deep.equal({
match: true
})
matcher.match('5').should.deep.equal({
match: true
})
matcher.match('6').should.deep.equal({
match: true
})
expect(matcher.match('7')).to.be.undefined
matcher.match('8').should.deep.equal({
match: true
})
expect(matcher.match('9')).to.be.undefined
expect(matcher.match('88')).to.be.undefined
matcher.match('88', { allowOverflow: true }).should.deep.equal({
overflow: true
})
})
it('should match a two-digit one-of set', function() {
const matcher = new PatternMatcher('[479][45]')
expect(matcher.match('1')).to.be.undefined
matcher.match('4').should.deep.equal({
partialMatch: true
})
expect(matcher.match('5')).to.be.undefined
expect(matcher.match('55')).to.be.undefined
matcher.match('44').should.deep.equal({
match: true
})
expect(matcher.match('444')).to.be.undefined
matcher.match('444', { allowOverflow: true }).should.deep.equal({
overflow: true
})
})
it('should match a two-digit one-of set (regular digit and a one-of set)', function() {
const matcher = new PatternMatcher('1[45]')
expect(matcher.match('0')).to.be.undefined
matcher.match('1').should.deep.equal({
partialMatch: true
})
matcher.match('15').should.deep.equal({
match: true
})
expect(matcher.match('16')).to.be.undefined
})
it('should match a pattern with an or group', function() {
const matcher = new PatternMatcher('7(?:1[0-68]|2[1-9])')
expect(matcher.match('1')).to.be.undefined
matcher.match('7').should.deep.equal({
partialMatch: true
})
matcher.match('71').should.deep.equal({
partialMatch: true
})
expect(matcher.match('73')).to.be.undefined
matcher.match('711').should.deep.equal({
match: true
})
expect(matcher.match('717')).to.be.undefined
expect(matcher.match('720')).to.be.undefined
matcher.match('722').should.deep.equal({
match: true
})
expect(matcher.match('7222')).to.be.undefined
matcher.match('7222', { allowOverflow: true }).should.deep.equal({
overflow: true
})
})
it('should match an or pattern containing or groups', function() {
const matcher = new PatternMatcher('2(?:2[024-9]|3[0-59]|47|6[245]|9[02-8])|3(?:3[28]|4[03-9]|5[2-46-8]|7[1-578]|8[2-9])')
expect(matcher.match('1')).to.be.undefined
matcher.match('2').should.deep.equal({
partialMatch: true
})
matcher.match('3').should.deep.equal({
partialMatch: true
})
expect(matcher.match('4')).to.be.undefined
expect(matcher.match('21')).to.be.undefined
matcher.match('22').should.deep.equal({
partialMatch: true
})
expect(matcher.match('221')).to.be.undefined
matcher.match('222').should.deep.equal({
match: true
})
expect(matcher.match('2222')).to.be.undefined
matcher.match('2222', { allowOverflow: true }).should.deep.equal({
overflow: true
})
matcher.match('3').should.deep.equal({
partialMatch: true
})
matcher.match('33').should.deep.equal({
partialMatch: true
})
matcher.match('332').should.deep.equal({
match: true
})
expect(matcher.match('333')).to.be.undefined
})
it('should match an or pattern', function() {
const matcher = new PatternMatcher('6|8')
expect(matcher.match('5')).to.be.undefined
matcher.match('6').should.deep.equal({
match: true
})
expect(matcher.match('7')).to.be.undefined
matcher.match('8').should.deep.equal({
match: true
})
})
it('should match an or pattern (one-of sets)', function() {
const matcher = new PatternMatcher('[123]|[5-8]')
expect(matcher.match('0')).to.be.undefined
matcher.match('1').should.deep.equal({
match: true
})
matcher.match('2').should.deep.equal({
match: true
})
matcher.match('3').should.deep.equal({
match: true
})
expect(matcher.match('4')).to.be.undefined
matcher.match('5').should.deep.equal({
match: true
})
matcher.match('6').should.deep.equal({
match: true
})
matcher.match('7').should.deep.equal({
match: true
})
matcher.match('8').should.deep.equal({
match: true
})
expect(matcher.match('9')).to.be.undefined
expect(matcher.match('18')).to.be.undefined
matcher.match('18', { allowOverflow: true }).should.deep.equal({
overflow: true
})
})
it('should match an or pattern (different lengths)', function() {
const matcher = new PatternMatcher('60|8')
expect(matcher.match('5')).to.be.undefined
matcher.match('6').should.deep.equal({
partialMatch: true
})
matcher.match('60').should.deep.equal({
match: true
})
expect(matcher.match('61')).to.be.undefined
expect(matcher.match('7')).to.be.undefined
matcher.match('8').should.deep.equal({
match: true
})
expect(matcher.match('68')).to.be.undefined
})
it('should match an or pattern (one-of sets) (different lengths)', function() {
const matcher = new PatternMatcher('[123]|[5-8][2-8]')
expect(matcher.match('0')).to.be.undefined
})
it('should match an or pattern (one-of sets and regular digits) (different lengths)', function() {
const matcher = new PatternMatcher('[2358][2-5]|4')
expect(matcher.match('0')).to.be.undefined
matcher.match('2').should.deep.equal({
partialMatch: true
})
expect(matcher.match('21')).to.be.undefined
matcher.match('22').should.deep.equal({
match: true
})
matcher.match('25').should.deep.equal({
match: true
})
expect(matcher.match('26')).to.be.undefined
expect(matcher.match('222')).to.be.undefined
matcher.match('222', { allowOverflow: true }).should.deep.equal({
overflow: true
})
matcher.match('3').should.deep.equal({
partialMatch: true
})
matcher.match('4').should.deep.equal({
match: true
})
expect(matcher.match('6')).to.be.undefined
})
it('should match an or pattern (one-of sets and regular digits mixed) (different lengths)', function() {
const matcher = new PatternMatcher('[2358]2|4')
expect(matcher.match('0')).to.be.undefined
matcher.match('2').should.deep.equal({
partialMatch: true
})
expect(matcher.match('21')).to.be.undefined
matcher.match('22').should.deep.equal({
match: true
})
expect(matcher.match('222')).to.be.undefined
matcher.match('222', { allowOverflow: true }).should.deep.equal({
overflow: true
})
matcher.match('3').should.deep.equal({
partialMatch: true
})
matcher.match('4').should.deep.equal({
match: true
})
expect(matcher.match('6')).to.be.undefined
})
it('should match an or pattern (one-of sets groups and regular digits mixed) (different lengths)', function() {
const matcher = new PatternMatcher('1(?:11|[2-9])')
matcher.match('1').should.deep.equal({
partialMatch: true
})
expect(matcher.match('10')).to.be.undefined
matcher.match('11').should.deep.equal({
partialMatch: true
})
matcher.match('111').should.deep.equal({
match: true
})
expect(matcher.match('1111')).to.be.undefined
matcher.match('1111', { allowOverflow: true }).should.deep.equal({
overflow: true
})
matcher.match('12').should.deep.equal({
match: true
})
expect(matcher.match('122')).to.be.undefined
matcher.match('19').should.deep.equal({
match: true
})
expect(matcher.match('5')).to.be.undefined
})
it('should match nested or groups', function() {
const matcher = new PatternMatcher('1(?:2(?:3(?:4|5)|6)|7(?:8|9))0')
matcher.match('1').should.deep.equal({
partialMatch: true
})
expect(matcher.match('2')).to.be.undefined
expect(matcher.match('11')).to.be.undefined
matcher.match('12').should.deep.equal({
partialMatch: true
})
expect(matcher.match('121')).to.be.undefined
matcher.match('123').should.deep.equal({
partialMatch: true
})
expect(matcher.match('1231')).to.be.undefined
matcher.match('1234').should.deep.equal({
partialMatch: true
})
matcher.match('12340').should.deep.equal({
match: true
})
expect(matcher.match('123401')).to.be.undefined
matcher.match('123401', { allowOverflow: true }).should.deep.equal({
overflow: true
})
matcher.match('12350').should.deep.equal({
match: true
})
expect(matcher.match('12360')).to.be.undefined
matcher.match('1260').should.deep.equal({
match: true
})
expect(matcher.match('1270')).to.be.undefined
expect(matcher.match('1770')).to.be.undefined
matcher.match('1780').should.deep.equal({
match: true
})
matcher.match('1790').should.deep.equal({
match: true
})
expect(matcher.match('18')).to.be.undefined
})
it('should match complex patterns', function() {
const matcher = new PatternMatcher('(?:31|4)6|51|6(?:5[0-3579]|[6-9])|7(?:20|32|8)|[89]')
expect(matcher.match('0')).to.be.undefined
matcher.match('3').should.deep.equal({
partialMatch: true
})
matcher.match('31').should.deep.equal({
partialMatch: true
})
expect(matcher.match('32')).to.be.undefined
matcher.match('316').should.deep.equal({
match: true
})
expect(matcher.match('315')).to.be.undefined
matcher.match('4').should.deep.equal({
partialMatch: true
})
matcher.match('46').should.deep.equal({
match: true
})
expect(matcher.match('47')).to.be.undefined
matcher.match('5').should.deep.equal({
partialMatch: true
})
expect(matcher.match('50')).to.be.undefined
matcher.match('51').should.deep.equal({
match: true
})
matcher.match('6').should.deep.equal({
partialMatch: true
})
expect(matcher.match('64')).to.be.undefined
matcher.match('65').should.deep.equal({
partialMatch: true
})
matcher.match('650').should.deep.equal({
match: true
})
expect(matcher.match('654')).to.be.undefined
matcher.match('69').should.deep.equal({
match: true
})
matcher.match('8').should.deep.equal({
match: true
})
matcher.match('9').should.deep.equal({
match: true
})
})
it('shouldn\'t match things that shouldn\'t match', function() {
// There was a bug: "leading digits" `"2"` matched "leading digits pattern" `"90"`.
// The incorrect `.match()` function result was `{ oveflow: true }`
// while it should've been `undefined`.
// https://gitlab.com/catamphetamine/libphonenumber-js/-/issues/66
expect(new PatternMatcher('2').match('90', { allowOverflow: true })).to.be.undefined
})
})

View File

@@ -0,0 +1,58 @@
// A string of length `1`.
//
// Example: "3".
//
export type character = string;
// Matches any character.
//
// Example:
//
// String pattern "123" compiles into 3 characters:
//
// ["1", "2", "3"]
//
type Character = character;
// Matches one of characters.
//
// Example:
//
// String pattern "[5-9]" compiles into:
//
// { op: "[]", args: ["5", "6", "7", "8", "9"] }
//
interface OneOfCharacters {
op: '[]';
args: character[];
}
// Matches any of the subtrees.
//
// Example:
//
// String pattern "123|[5-9]0" compiles into:
//
// {
// op: "|",
// args: [
// // First subtree:
// ["1", "2", "3"],
// // Second subtree:
// [
// { op: "[]", args: ["5", "6", "7", "8", "9"] },
// "0"
// ]
// ]
// }
//
interface OrCondition<MatchTree> {
op: '|';
args: MatchTree[];
}
export type MatchTree = Character | OneOfCharacters | MatchTree[] | OrCondition<MatchTree>;
export default class PatternParser {
parse(pattern: string): MatchTree;
}

View File

@@ -0,0 +1,214 @@
export default class PatternParser {
parse(pattern) {
this.context = [{
or: true,
instructions: []
}]
this.parsePattern(pattern)
if (this.context.length !== 1) {
throw new Error('Non-finalized contexts left when pattern parse ended')
}
const { branches, instructions } = this.context[0]
if (branches) {
return {
op: '|',
args: branches.concat([
expandSingleElementArray(instructions)
])
}
}
/* istanbul ignore if */
if (instructions.length === 0) {
throw new Error('Pattern is required')
}
if (instructions.length === 1) {
return instructions[0]
}
return instructions
}
startContext(context) {
this.context.push(context)
}
endContext() {
this.context.pop()
}
getContext() {
return this.context[this.context.length - 1]
}
parsePattern(pattern) {
if (!pattern) {
throw new Error('Pattern is required')
}
const match = pattern.match(OPERATOR)
if (!match) {
if (ILLEGAL_CHARACTER_REGEXP.test(pattern)) {
throw new Error(`Illegal characters found in a pattern: ${pattern}`)
}
this.getContext().instructions = this.getContext().instructions.concat(
pattern.split('')
)
return
}
const operator = match[1]
const before = pattern.slice(0, match.index)
const rightPart = pattern.slice(match.index + operator.length)
switch (operator) {
case '(?:':
if (before) {
this.parsePattern(before)
}
this.startContext({
or: true,
instructions: [],
branches: []
})
break
case ')':
if (!this.getContext().or) {
throw new Error('")" operator must be preceded by "(?:" operator')
}
if (before) {
this.parsePattern(before)
}
if (this.getContext().instructions.length === 0) {
throw new Error('No instructions found after "|" operator in an "or" group')
}
const { branches } = this.getContext()
branches.push(
expandSingleElementArray(
this.getContext().instructions
)
)
this.endContext()
this.getContext().instructions.push({
op: '|',
args: branches
})
break
case '|':
if (!this.getContext().or) {
throw new Error('"|" operator can only be used inside "or" groups')
}
if (before) {
this.parsePattern(before)
}
// The top-level is an implicit "or" group, if required.
if (!this.getContext().branches) {
// `branches` are not defined only for the root implicit "or" operator.
/* istanbul ignore else */
if (this.context.length === 1) {
this.getContext().branches = []
} else {
throw new Error('"branches" not found in an "or" group context')
}
}
this.getContext().branches.push(
expandSingleElementArray(
this.getContext().instructions
)
)
this.getContext().instructions = []
break
case '[':
if (before) {
this.parsePattern(before)
}
this.startContext({
oneOfSet: true
})
break
case ']':
if (!this.getContext().oneOfSet) {
throw new Error('"]" operator must be preceded by "[" operator')
}
this.endContext()
this.getContext().instructions.push({
op: '[]',
args: parseOneOfSet(before)
})
break
/* istanbul ignore next */
default:
throw new Error(`Unknown operator: ${operator}`)
}
if (rightPart) {
this.parsePattern(rightPart)
}
}
}
function parseOneOfSet(pattern) {
const values = []
let i = 0
while (i < pattern.length) {
if (pattern[i] === '-') {
if (i === 0 || i === pattern.length - 1) {
throw new Error(`Couldn't parse a one-of set pattern: ${pattern}`)
}
const prevValue = pattern[i - 1].charCodeAt(0) + 1
const nextValue = pattern[i + 1].charCodeAt(0) - 1
let value = prevValue
while (value <= nextValue) {
values.push(String.fromCharCode(value))
value++
}
} else {
values.push(pattern[i])
}
i++
}
return values
}
const ILLEGAL_CHARACTER_REGEXP = /[\(\)\[\]\?\:\|]/
const OPERATOR = new RegExp(
// any of:
'(' +
// or operator
'\\|' +
// or
'|' +
// or group start
'\\(\\?\\:' +
// or
'|' +
// or group end
'\\)' +
// or
'|' +
// one-of set start
'\\[' +
// or
'|' +
// one-of set end
'\\]' +
')'
)
function expandSingleElementArray(array) {
if (array.length === 1) {
return array[0]
}
return array
}

View File

@@ -0,0 +1,67 @@
import PatternParser from './AsYouTypeFormatter.PatternParser.js'
describe('PatternParser', function() {
it('should parse single-character patterns', function() {
new PatternParser().parse('2').should.deep.equal('2')
})
it('should parse string patterns', function() {
new PatternParser().parse('123').should.deep.equal(['1', '2', '3'])
})
it('should parse "one of" patterns', function() {
new PatternParser().parse('[5-9]').should.deep.equal({
op: '[]',
args: ['5', '6', '7', '8', '9']
})
})
it('should parse "or" patterns', function() {
new PatternParser().parse('123|[5-9]').should.deep.equal({
op: '|',
args: [
['1', '2', '3'],
{
op: '[]',
args: ['5', '6', '7', '8', '9']
}
]
})
new PatternParser().parse('123|[5-9]0').should.deep.equal({
op: '|',
args: [
['1', '2', '3'],
[
{
op: '[]',
args: ['5', '6', '7', '8', '9']
},
'0'
]
]
})
})
it('should parse nested "or" patterns', function() {
new PatternParser().parse('123|(?:2|34)[5-9]').should.deep.equal({
op: '|',
args: [
['1', '2', '3'],
[
{
op: '|',
args: [
'2',
['3', '4']
]
},
{
op: '[]',
args: ['5', '6', '7', '8', '9']
}
]
]
})
})
})

View File

@@ -0,0 +1,135 @@
import checkNumberLength from './helpers/checkNumberLength.js'
import parseDigits from './helpers/parseDigits.js'
import formatNationalNumberUsingFormat from './helpers/formatNationalNumberUsingFormat.js'
export default function formatCompleteNumber(state, format, {
metadata,
shouldTryNationalPrefixFormattingRule,
getSeparatorAfterNationalPrefix
}) {
const matcher = new RegExp(`^(?:${format.pattern()})$`)
if (matcher.test(state.nationalSignificantNumber)) {
return formatNationalNumberWithAndWithoutNationalPrefixFormattingRule(
state,
format,
{
metadata,
shouldTryNationalPrefixFormattingRule,
getSeparatorAfterNationalPrefix
}
)
}
}
export function canFormatCompleteNumber(nationalSignificantNumber, metadata) {
return checkNumberLength(nationalSignificantNumber, metadata) === 'IS_POSSIBLE'
}
function formatNationalNumberWithAndWithoutNationalPrefixFormattingRule(state, format, {
metadata,
shouldTryNationalPrefixFormattingRule,
getSeparatorAfterNationalPrefix
}) {
// `format` has already been checked for `nationalPrefix` requirement.
const {
nationalSignificantNumber,
international,
nationalPrefix,
carrierCode
} = state
// Format the number with using `national_prefix_formatting_rule`.
// If the resulting formatted number is a valid formatted number, then return it.
//
// Google's AsYouType formatter is different in a way that it doesn't try
// to format using the "national prefix formatting rule", and instead it
// simply prepends a national prefix followed by a " " character.
// This code does that too, but as a fallback.
// The reason is that "national prefix formatting rule" may use parentheses,
// which wouldn't be included has it used the simpler Google's way.
//
if (shouldTryNationalPrefixFormattingRule(format)) {
const formattedNumber = formatNationalNumber(state, format, {
useNationalPrefixFormattingRule: true,
getSeparatorAfterNationalPrefix,
metadata
})
if (formattedNumber) {
return formattedNumber
}
}
// Format the number without using `national_prefix_formatting_rule`.
return formatNationalNumber(state, format, {
useNationalPrefixFormattingRule: false,
getSeparatorAfterNationalPrefix,
metadata
})
}
function formatNationalNumber(state, format, {
metadata,
useNationalPrefixFormattingRule,
getSeparatorAfterNationalPrefix
}) {
let formattedNationalNumber = formatNationalNumberUsingFormat(
state.nationalSignificantNumber,
format,
{
carrierCode: state.carrierCode,
useInternationalFormat: state.international,
withNationalPrefix: useNationalPrefixFormattingRule,
metadata
}
)
if (!useNationalPrefixFormattingRule) {
if (state.nationalPrefix) {
// If a national prefix was extracted, then just prepend it,
// followed by a " " character.
formattedNationalNumber = state.nationalPrefix +
getSeparatorAfterNationalPrefix(format) +
formattedNationalNumber
} else if (state.complexPrefixBeforeNationalSignificantNumber) {
formattedNationalNumber = state.complexPrefixBeforeNationalSignificantNumber +
' ' +
formattedNationalNumber
}
}
if (isValidFormattedNationalNumber(formattedNationalNumber, state)) {
return formattedNationalNumber
}
}
// Check that the formatted phone number contains exactly
// the same digits that have been input by the user.
// For example, when "0111523456789" is input for `AR` country,
// the extracted `this.nationalSignificantNumber` is "91123456789",
// which means that the national part of `this.digits` isn't simply equal to
// `this.nationalPrefix` + `this.nationalSignificantNumber`.
//
// Also, a `format` can add extra digits to the `this.nationalSignificantNumber`
// being formatted via `metadata[country].national_prefix_transform_rule`.
// For example, for `VI` country, it prepends `340` to the national number,
// and if this check hasn't been implemented, then there would be a bug
// when `340` "area coude" is "duplicated" during input for `VI` country:
// https://github.com/catamphetamine/libphonenumber-js/issues/318
//
// So, all these "gotchas" are filtered out.
//
// In the original Google's code, the comments say:
// "Check that we didn't remove nor add any extra digits when we matched
// this formatting pattern. This usually happens after we entered the last
// digit during AYTF. Eg: In case of MX, we swallow mobile token (1) when
// formatted but AYTF should retain all the number entered and not change
// in order to match a format (of same leading digits and length) display
// in that way."
// "If it's the same (i.e entered number and format is same), then it's
// safe to return this in formatted number as nothing is lost / added."
// Otherwise, don't use this format.
// https://github.com/google/libphonenumber/commit/3e7c1f04f5e7200f87fb131e6f85c6e99d60f510#diff-9149457fa9f5d608a11bb975c6ef4bc5
// https://github.com/google/libphonenumber/commit/3ac88c7106e7dcb553bcc794b15f19185928a1c6#diff-2dcb77e833422ee304da348b905cde0b
//
function isValidFormattedNationalNumber(formattedNationalNumber, state) {
return parseDigits(formattedNationalNumber) === state.getNationalDigits()
}

View File

@@ -0,0 +1,714 @@
import {
DIGIT_PLACEHOLDER,
countOccurences,
repeat,
cutAndStripNonPairedParens,
closeNonPairedParens,
stripNonPairedParens,
populateTemplateWithDigits
} from './AsYouTypeFormatter.util.js'
import formatCompleteNumber, {
canFormatCompleteNumber
} from './AsYouTypeFormatter.complete.js'
import PatternMatcher from './AsYouTypeFormatter.PatternMatcher.js'
import parseDigits from './helpers/parseDigits.js'
export { DIGIT_PLACEHOLDER } from './AsYouTypeFormatter.util.js'
import { FIRST_GROUP_PATTERN } from './helpers/formatNationalNumberUsingFormat.js'
import { VALID_PUNCTUATION } from './constants.js'
import applyInternationalSeparatorStyle from './helpers/applyInternationalSeparatorStyle.js'
// Used in phone number format template creation.
// Could be any digit, I guess.
const DUMMY_DIGIT = '9'
// I don't know why is it exactly `15`
const LONGEST_NATIONAL_PHONE_NUMBER_LENGTH = 15
// Create a phone number consisting only of the digit 9 that matches the
// `number_pattern` by applying the pattern to the "longest phone number" string.
const LONGEST_DUMMY_PHONE_NUMBER = repeat(DUMMY_DIGIT, LONGEST_NATIONAL_PHONE_NUMBER_LENGTH)
// A set of characters that, if found in a national prefix formatting rules, are an indicator to
// us that we should separate the national prefix from the number when formatting.
const NATIONAL_PREFIX_SEPARATORS_PATTERN = /[- ]/
// Deprecated: Google has removed some formatting pattern related code from their repo.
// https://github.com/googlei18n/libphonenumber/commit/a395b4fef3caf57c4bc5f082e1152a4d2bd0ba4c
// "We no longer have numbers in formatting matching patterns, only \d."
// Because this library supports generating custom metadata
// some users may still be using old metadata so the relevant
// code seems to stay until some next major version update.
const SUPPORT_LEGACY_FORMATTING_PATTERNS = true
// A pattern that is used to match character classes in regular expressions.
// An example of a character class is "[1-4]".
const CREATE_CHARACTER_CLASS_PATTERN = SUPPORT_LEGACY_FORMATTING_PATTERNS && (() => /\[([^\[\]])*\]/g)
// Any digit in a regular expression that actually denotes a digit. For
// example, in the regular expression "80[0-2]\d{6,10}", the first 2 digits
// (8 and 0) are standalone digits, but the rest are not.
// Two look-aheads are needed because the number following \\d could be a
// two-digit number, since the phone number can be as long as 15 digits.
const CREATE_STANDALONE_DIGIT_PATTERN = SUPPORT_LEGACY_FORMATTING_PATTERNS && (() => /\d(?=[^,}][^,}])/g)
// A regular expression that is used to determine if a `format` is
// suitable to be used in the "as you type formatter".
// A `format` is suitable when the resulting formatted number has
// the same digits as the user has entered.
//
// In the simplest case, that would mean that the format
// doesn't add any additional digits when formatting a number.
// Google says that it also shouldn't add "star" (`*`) characters,
// like it does in some Israeli formats.
// Such basic format would only contain "valid punctuation"
// and "captured group" identifiers ($1, $2, etc).
//
// An example of a format that adds additional digits:
//
// Country: `AR` (Argentina).
// Format:
// {
// "pattern": "(\\d)(\\d{2})(\\d{4})(\\d{4})",
// "leading_digits_patterns": ["91"],
// "national_prefix_formatting_rule": "0$1",
// "format": "$2 15-$3-$4",
// "international_format": "$1 $2 $3-$4"
// }
//
// In the format above, the `format` adds `15` to the digits when formatting a number.
// A sidenote: this format actually is suitable because `national_prefix_for_parsing`
// has previously removed `15` from a national number, so re-adding `15` in `format`
// doesn't actually result in any extra digits added to user's input.
// But verifying that would be a complex procedure, so the code chooses a simpler path:
// it simply filters out all `format`s that contain anything but "captured group" ids.
//
// This regular expression is called `ELIGIBLE_FORMAT_PATTERN` in Google's
// `libphonenumber` code.
//
const NON_ALTERING_FORMAT_REG_EXP = new RegExp(
'[' + VALID_PUNCTUATION + ']*' +
// Google developers say:
// "We require that the first matching group is present in the
// output pattern to ensure no data is lost while formatting."
'\\$1' +
'[' + VALID_PUNCTUATION + ']*' +
'(\\$\\d[' + VALID_PUNCTUATION + ']*)*' +
'$'
)
// This is the minimum length of the leading digits of a phone number
// to guarantee the first "leading digits pattern" for a phone number format
// to be preemptive.
const MIN_LEADING_DIGITS_LENGTH = 3
export default class AsYouTypeFormatter {
constructor({
state,
metadata
}) {
this.metadata = metadata
this.resetFormat()
}
resetFormat() {
this.chosenFormat = undefined
this.template = undefined
this.nationalNumberTemplate = undefined
this.populatedNationalNumberTemplate = undefined
this.populatedNationalNumberTemplatePosition = -1
}
reset(numberingPlan, state) {
this.resetFormat()
if (numberingPlan) {
this.isNANP = numberingPlan.callingCode() === '1'
this.matchingFormats = numberingPlan.formats()
if (state.nationalSignificantNumber) {
this.narrowDownMatchingFormats(state)
}
} else {
this.isNANP = undefined
this.matchingFormats = []
}
}
/**
* Formats an updated phone number.
* @param {string} nextDigits — Additional phone number digits.
* @param {object} state — `AsYouType` state.
* @return {[string]} Returns undefined if the updated phone number can't be formatted using any of the available formats.
*/
format(nextDigits, state) {
// See if the phone number digits can be formatted as a complete phone number.
// If not, use the results from `formatNationalNumberWithNextDigits()`,
// which formats based on the chosen formatting pattern.
//
// Attempting to format complete phone number first is how it's done
// in Google's `libphonenumber`, so this library just follows it.
// Google's `libphonenumber` code doesn't explain in detail why does it
// attempt to format digits as a complete phone number
// instead of just going with a previoulsy (or newly) chosen `format`:
//
// "Checks to see if there is an exact pattern match for these digits.
// If so, we should use this instead of any other formatting template
// whose leadingDigitsPattern also matches the input."
//
if (canFormatCompleteNumber(state.nationalSignificantNumber, this.metadata)) {
for (const format of this.matchingFormats) {
const formattedCompleteNumber = formatCompleteNumber(
state,
format,
{
metadata: this.metadata,
shouldTryNationalPrefixFormattingRule: (format) => this.shouldTryNationalPrefixFormattingRule(format, {
international: state.international,
nationalPrefix: state.nationalPrefix
}),
getSeparatorAfterNationalPrefix: (format) => this.getSeparatorAfterNationalPrefix(format)
}
)
if (formattedCompleteNumber) {
this.resetFormat()
this.chosenFormat = format
this.setNationalNumberTemplate(formattedCompleteNumber.replace(/\d/g, DIGIT_PLACEHOLDER), state)
this.populatedNationalNumberTemplate = formattedCompleteNumber
// With a new formatting template, the matched position
// using the old template needs to be reset.
this.populatedNationalNumberTemplatePosition = this.template.lastIndexOf(DIGIT_PLACEHOLDER)
return formattedCompleteNumber
}
}
}
// Format the digits as a partial (incomplete) phone number
// using the previously chosen formatting pattern (or a newly chosen one).
return this.formatNationalNumberWithNextDigits(nextDigits, state)
}
// Formats the next phone number digits.
formatNationalNumberWithNextDigits(nextDigits, state) {
const previouslyChosenFormat = this.chosenFormat
// Choose a format from the list of matching ones.
const newlyChosenFormat = this.chooseFormat(state)
if (newlyChosenFormat) {
if (newlyChosenFormat === previouslyChosenFormat) {
// If it can format the next (current) digits
// using the previously chosen phone number format
// then return the updated formatted number.
return this.formatNextNationalNumberDigits(nextDigits)
} else {
// If a more appropriate phone number format
// has been chosen for these "leading digits",
// then re-format the national phone number part
// using the newly selected format.
return this.formatNextNationalNumberDigits(state.getNationalDigits())
}
}
}
narrowDownMatchingFormats({
nationalSignificantNumber,
nationalPrefix,
international
}) {
const leadingDigits = nationalSignificantNumber
// "leading digits" pattern list starts with a
// "leading digits" pattern fitting a maximum of 3 leading digits.
// So, after a user inputs 3 digits of a national (significant) phone number
// this national (significant) number can already be formatted.
// The next "leading digits" pattern is for 4 leading digits max,
// and the "leading digits" pattern after it is for 5 leading digits max, etc.
// This implementation is different from Google's
// in that it searches for a fitting format
// even if the user has entered less than
// `MIN_LEADING_DIGITS_LENGTH` digits of a national number.
// Because some leading digit patterns already match for a single first digit.
let leadingDigitsPatternIndex = leadingDigits.length - MIN_LEADING_DIGITS_LENGTH
if (leadingDigitsPatternIndex < 0) {
leadingDigitsPatternIndex = 0
}
this.matchingFormats = this.matchingFormats.filter(
format => this.formatSuits(format, international, nationalPrefix)
&& this.formatMatches(format, leadingDigits, leadingDigitsPatternIndex)
)
// If there was a phone number format chosen
// and it no longer holds given the new leading digits then reset it.
// The test for this `if` condition is marked as:
// "Reset a chosen format when it no longer holds given the new leading digits".
// To construct a valid test case for this one can find a country
// in `PhoneNumberMetadata.xml` yielding one format for 3 `<leadingDigits>`
// and yielding another format for 4 `<leadingDigits>` (Australia in this case).
if (this.chosenFormat && this.matchingFormats.indexOf(this.chosenFormat) === -1) {
this.resetFormat()
}
}
formatSuits(format, international, nationalPrefix) {
// When a prefix before a national (significant) number is
// simply a national prefix, then it's parsed as `this.nationalPrefix`.
// In more complex cases, a prefix before national (significant) number
// could include a national prefix as well as some "capturing groups",
// and in that case there's no info whether a national prefix has been parsed.
// If national prefix is not used when formatting a phone number
// using this format, but a national prefix has been entered by the user,
// and was extracted, then discard such phone number format.
// In Google's "AsYouType" formatter code, the equivalent would be this part:
// https://github.com/google/libphonenumber/blob/0a45cfd96e71cad8edb0e162a70fcc8bd9728933/java/libphonenumber/src/com/google/i18n/phonenumbers/AsYouTypeFormatter.java#L175-L184
if (nationalPrefix &&
!format.usesNationalPrefix() &&
// !format.domesticCarrierCodeFormattingRule() &&
!format.nationalPrefixIsOptionalWhenFormattingInNationalFormat()) {
return false
}
// If national prefix is mandatory for this phone number format
// and there're no guarantees that a national prefix is present in user input
// then discard this phone number format as not suitable.
// In Google's "AsYouType" formatter code, the equivalent would be this part:
// https://github.com/google/libphonenumber/blob/0a45cfd96e71cad8edb0e162a70fcc8bd9728933/java/libphonenumber/src/com/google/i18n/phonenumbers/AsYouTypeFormatter.java#L185-L193
if (!international &&
!nationalPrefix &&
format.nationalPrefixIsMandatoryWhenFormattingInNationalFormat()) {
return false
}
return true
}
formatMatches(format, leadingDigits, leadingDigitsPatternIndex) {
const leadingDigitsPatternsCount = format.leadingDigitsPatterns().length
// If this format is not restricted to a certain
// leading digits pattern then it fits.
// The test case could be found by searching for "leadingDigitsPatternsCount === 0".
if (leadingDigitsPatternsCount === 0) {
return true
}
// Start narrowing down the list of possible formats based on the leading digits.
// (only previously matched formats take part in the narrowing down process)
// `leading_digits_patterns` start with 3 digits min
// and then go up from there one digit at a time.
leadingDigitsPatternIndex = Math.min(leadingDigitsPatternIndex, leadingDigitsPatternsCount - 1)
const leadingDigitsPattern = format.leadingDigitsPatterns()[leadingDigitsPatternIndex]
// Google imposes a requirement on the leading digits
// to be minimum 3 digits long in order to be eligible
// for checking those with a leading digits pattern.
//
// Since `leading_digits_patterns` start with 3 digits min,
// Google's original `libphonenumber` library only starts
// excluding any non-matching formats only when the
// national number entered so far is at least 3 digits long,
// otherwise format matching would give false negatives.
//
// For example, when the digits entered so far are `2`
// and the leading digits pattern is `21`
// it's quite obvious in this case that the format could be the one
// but due to the absence of further digits it would give false negative.
//
// Also, `leading_digits_patterns` doesn't always correspond to a single
// digits count. For example, `60|8` pattern would already match `8`
// but the `60` part would require having at least two leading digits,
// so the whole pattern would require inputting two digits first in order to
// decide on whether it matches the input, even when the input is "80".
//
// This library — `libphonenumber-js` — allows filtering by `leading_digits_patterns`
// even when there's only 1 or 2 digits of the national (significant) number.
// To do that, it uses a non-strict pattern matcher written specifically for that.
//
if (leadingDigits.length < MIN_LEADING_DIGITS_LENGTH) {
// Before leading digits < 3 matching was implemented:
// return true
//
// After leading digits < 3 matching was implemented:
try {
return new PatternMatcher(leadingDigitsPattern).match(leadingDigits, { allowOverflow: true }) !== undefined
} catch (error) /* istanbul ignore next */ {
// There's a slight possibility that there could be some undiscovered bug
// in the pattern matcher code. Since the "leading digits < 3 matching"
// feature is not "essential" for operation, it can fall back to the old way
// in case of any issues rather than halting the application's execution.
console.error(error)
return true
}
}
// If at least `MIN_LEADING_DIGITS_LENGTH` digits of a national number are
// available then use the usual regular expression matching.
//
// The whole pattern is wrapped in round brackets (`()`) because
// the pattern can use "or" operator (`|`) at the top level of the pattern.
//
return new RegExp(`^(${leadingDigitsPattern})`).test(leadingDigits)
}
getFormatFormat(format, international) {
return international ? format.internationalFormat() : format.format()
}
chooseFormat(state) {
// When there are multiple available formats, the formatter uses the first
// format where a formatting template could be created.
//
// For some weird reason, `istanbul` says "else path not taken"
// for the `for of` line below. Supposedly that means that
// the loop doesn't ever go over the last element in the list.
// That's true because there always is `this.chosenFormat`
// when `this.matchingFormats` is non-empty.
// And, for some weird reason, it doesn't think that the case
// with empty `this.matchingFormats` qualifies for a valid "else" path.
// So simply muting this `istanbul` warning.
// It doesn't skip the contents of the `for of` loop,
// it just skips the `for of` line.
//
/* istanbul ignore next */
for (const format of this.matchingFormats.slice()) {
// If this format is currently being used
// and is still suitable, then stick to it.
if (this.chosenFormat === format) {
break
}
// Sometimes, a formatting rule inserts additional digits in a phone number,
// and "as you type" formatter can't do that: it should only use the digits
// that the user has input.
//
// For example, in Argentina, there's a format for mobile phone numbers:
//
// {
// "pattern": "(\\d)(\\d{2})(\\d{4})(\\d{4})",
// "leading_digits_patterns": ["91"],
// "national_prefix_formatting_rule": "0$1",
// "format": "$2 15-$3-$4",
// "international_format": "$1 $2 $3-$4"
// }
//
// In that format, `international_format` is used instead of `format`
// because `format` inserts `15` in the formatted number,
// and `AsYouType` formatter should only use the digits
// the user has actually input, without adding any extra digits.
// In this case, it wouldn't make a difference, because the `15`
// is first stripped when applying `national_prefix_for_parsing`
// and then re-added when using `format`, so in reality it doesn't
// add any new digits to the number, but to detect that, the code
// would have to be more complex: it would have to try formatting
// the digits using the format and then see if any digits have
// actually been added or removed, and then, every time a new digit
// is input, it should re-check whether the chosen format doesn't
// alter the digits.
//
// Google's code doesn't go that far, and so does this library:
// it simply requires that a `format` doesn't add any additonal
// digits to user's input.
//
// Also, people in general should move from inputting phone numbers
// in national format (possibly with national prefixes)
// and use international phone number format instead:
// it's a logical thing in the modern age of mobile phones,
// globalization and the internet.
//
/* istanbul ignore if */
if (!NON_ALTERING_FORMAT_REG_EXP.test(this.getFormatFormat(format, state.international))) {
continue
}
if (!this.createTemplateForFormat(format, state)) {
// Remove the format if it can't generate a template.
this.matchingFormats = this.matchingFormats.filter(_ => _ !== format)
continue
}
this.chosenFormat = format
break
}
if (!this.chosenFormat) {
// No format matches the national (significant) phone number.
this.resetFormat()
}
return this.chosenFormat
}
createTemplateForFormat(format, state) {
// The formatter doesn't format numbers when numberPattern contains '|', e.g.
// (20|3)\d{4}. In those cases we quickly return.
// (Though there's no such format in current metadata)
/* istanbul ignore if */
if (SUPPORT_LEGACY_FORMATTING_PATTERNS && format.pattern().indexOf('|') >= 0) {
return
}
// Get formatting template for this phone number format
const template = this.getTemplateForFormat(format, state)
// If the national number entered is too long
// for any phone number format, then abort.
if (template) {
this.setNationalNumberTemplate(template, state)
return true
}
}
getSeparatorAfterNationalPrefix(format) {
// `US` metadata doesn't have a `national_prefix_formatting_rule`,
// so the `if` condition below doesn't apply to `US`,
// but in reality there shoudl be a separator
// between a national prefix and a national (significant) number.
// So `US` national prefix separator is a "special" "hardcoded" case.
if (this.isNANP) {
return ' '
}
// If a `format` has a `national_prefix_formatting_rule`
// and that rule has a separator after a national prefix,
// then it means that there should be a separator
// between a national prefix and a national (significant) number.
if (format &&
format.nationalPrefixFormattingRule() &&
NATIONAL_PREFIX_SEPARATORS_PATTERN.test(format.nationalPrefixFormattingRule())) {
return ' '
}
// At this point, there seems to be no clear evidence that
// there should be a separator between a national prefix
// and a national (significant) number. So don't insert one.
return ''
}
getInternationalPrefixBeforeCountryCallingCode({ IDDPrefix, missingPlus }, options) {
if (IDDPrefix) {
return options && options.spacing === false ? IDDPrefix : IDDPrefix + ' '
}
if (missingPlus) {
return ''
}
return '+'
}
getTemplate(state) {
if (!this.template) {
return
}
// `this.template` holds the template for a "complete" phone number.
// The currently entered phone number is most likely not "complete",
// so trim all non-populated digits.
let index = -1
let i = 0
const internationalPrefix = state.international ? this.getInternationalPrefixBeforeCountryCallingCode(state, { spacing: false }) : ''
while (i < internationalPrefix.length + state.getDigitsWithoutInternationalPrefix().length) {
index = this.template.indexOf(DIGIT_PLACEHOLDER, index + 1)
i++
}
return cutAndStripNonPairedParens(this.template, index + 1)
}
setNationalNumberTemplate(template, state) {
this.nationalNumberTemplate = template
this.populatedNationalNumberTemplate = template
// With a new formatting template, the matched position
// using the old template needs to be reset.
this.populatedNationalNumberTemplatePosition = -1
// For convenience, the public `.template` property
// contains the whole international number
// if the phone number being input is international:
// 'x' for the '+' sign, 'x'es for the country phone code,
// a spacebar and then the template for the formatted national number.
if (state.international) {
this.template =
this.getInternationalPrefixBeforeCountryCallingCode(state).replace(/[\d\+]/g, DIGIT_PLACEHOLDER) +
repeat(DIGIT_PLACEHOLDER, state.callingCode.length) +
' ' +
template
} else {
this.template = template
}
}
/**
* Generates formatting template for a national phone number,
* optionally containing a national prefix, for a format.
* @param {Format} format
* @param {string} nationalPrefix
* @return {string}
*/
getTemplateForFormat(format, {
nationalSignificantNumber,
international,
nationalPrefix,
complexPrefixBeforeNationalSignificantNumber
}) {
let pattern = format.pattern()
/* istanbul ignore else */
if (SUPPORT_LEGACY_FORMATTING_PATTERNS) {
pattern = pattern
// Replace anything in the form of [..] with \d
.replace(CREATE_CHARACTER_CLASS_PATTERN(), '\\d')
// Replace any standalone digit (not the one in `{}`) with \d
.replace(CREATE_STANDALONE_DIGIT_PATTERN(), '\\d')
}
// Generate a dummy national number (consisting of `9`s)
// that fits this format's `pattern`.
//
// This match will always succeed,
// because the "longest dummy phone number"
// has enough length to accomodate any possible
// national phone number format pattern.
//
let digits = LONGEST_DUMMY_PHONE_NUMBER.match(pattern)[0]
// If the national number entered is too long
// for any phone number format, then abort.
if (nationalSignificantNumber.length > digits.length) {
return
}
// Get a formatting template which can be used to efficiently format
// a partial number where digits are added one by one.
// Below `strictPattern` is used for the
// regular expression (with `^` and `$`).
// This wasn't originally in Google's `libphonenumber`
// and I guess they don't really need it
// because they're not using "templates" to format phone numbers
// but I added `strictPattern` after encountering
// South Korean phone number formatting bug.
//
// Non-strict regular expression bug demonstration:
//
// this.nationalSignificantNumber : `111111111` (9 digits)
//
// pattern : (\d{2})(\d{3,4})(\d{4})
// format : `$1 $2 $3`
// digits : `9999999999` (10 digits)
//
// '9999999999'.replace(new RegExp(/(\d{2})(\d{3,4})(\d{4})/g), '$1 $2 $3') = "99 9999 9999"
//
// template : xx xxxx xxxx
//
// But the correct template in this case is `xx xxx xxxx`.
// The template was generated incorrectly because of the
// `{3,4}` variability in the `pattern`.
//
// The fix is, if `this.nationalSignificantNumber` has already sufficient length
// to satisfy the `pattern` completely then `this.nationalSignificantNumber`
// is used instead of `digits`.
const strictPattern = new RegExp('^' + pattern + '$')
const nationalNumberDummyDigits = nationalSignificantNumber.replace(/\d/g, DUMMY_DIGIT)
// If `this.nationalSignificantNumber` has already sufficient length
// to satisfy the `pattern` completely then use it
// instead of `digits`.
if (strictPattern.test(nationalNumberDummyDigits)) {
digits = nationalNumberDummyDigits
}
let numberFormat = this.getFormatFormat(format, international)
let nationalPrefixIncludedInTemplate
// If a user did input a national prefix (and that's guaranteed),
// and if a `format` does have a national prefix formatting rule,
// then see if that national prefix formatting rule
// prepends exactly the same national prefix the user has input.
// If that's the case, then use the `format` with the national prefix formatting rule.
// Otherwise, use the `format` without the national prefix formatting rule,
// and prepend a national prefix manually to it.
if (this.shouldTryNationalPrefixFormattingRule(format, { international, nationalPrefix })) {
const numberFormatWithNationalPrefix = numberFormat.replace(
FIRST_GROUP_PATTERN,
format.nationalPrefixFormattingRule()
)
// If `national_prefix_formatting_rule` of a `format` simply prepends
// national prefix at the start of a national (significant) number,
// then such formatting can be used with `AsYouType` formatter.
// There seems to be no `else` case: everywhere in metadata,
// national prefix formatting rule is national prefix + $1,
// or `($1)`, in which case such format isn't even considered
// when the user has input a national prefix.
/* istanbul ignore else */
if (parseDigits(format.nationalPrefixFormattingRule()) === (nationalPrefix || '') + parseDigits('$1')) {
numberFormat = numberFormatWithNationalPrefix
nationalPrefixIncludedInTemplate = true
// Replace all digits of the national prefix in the formatting template
// with `DIGIT_PLACEHOLDER`s.
if (nationalPrefix) {
let i = nationalPrefix.length
while (i > 0) {
numberFormat = numberFormat.replace(/\d/, DIGIT_PLACEHOLDER)
i--
}
}
}
}
// Generate formatting template for this phone number format.
let template = digits
// Format the dummy phone number according to the format.
.replace(new RegExp(pattern), numberFormat)
// Replace each dummy digit with a DIGIT_PLACEHOLDER.
.replace(new RegExp(DUMMY_DIGIT, 'g'), DIGIT_PLACEHOLDER)
// If a prefix of a national (significant) number is not as simple
// as just a basic national prefix, then just prepend such prefix
// before the national (significant) number, optionally spacing
// the two with a whitespace.
if (!nationalPrefixIncludedInTemplate) {
if (complexPrefixBeforeNationalSignificantNumber) {
// Prepend the prefix to the template manually.
template = repeat(DIGIT_PLACEHOLDER, complexPrefixBeforeNationalSignificantNumber.length) +
' ' +
template
} else if (nationalPrefix) {
// Prepend national prefix to the template manually.
template = repeat(DIGIT_PLACEHOLDER, nationalPrefix.length) +
this.getSeparatorAfterNationalPrefix(format) +
template
}
}
if (international) {
template = applyInternationalSeparatorStyle(template)
}
return template
}
formatNextNationalNumberDigits(digits) {
const result = populateTemplateWithDigits(
this.populatedNationalNumberTemplate,
this.populatedNationalNumberTemplatePosition,
digits
)
if (!result) {
// Reset the format.
this.resetFormat()
return
}
this.populatedNationalNumberTemplate = result[0]
this.populatedNationalNumberTemplatePosition = result[1]
// Return the formatted phone number so far.
return cutAndStripNonPairedParens(this.populatedNationalNumberTemplate, this.populatedNationalNumberTemplatePosition + 1)
// The old way which was good for `input-format` but is not so good
// for `react-phone-number-input`'s default input (`InputBasic`).
// return closeNonPairedParens(this.populatedNationalNumberTemplate, this.populatedNationalNumberTemplatePosition + 1)
// .replace(new RegExp(DIGIT_PLACEHOLDER, 'g'), ' ')
}
shouldTryNationalPrefixFormattingRule(format, { international, nationalPrefix }) {
if (format.nationalPrefixFormattingRule()) {
// In some countries, `national_prefix_formatting_rule` is `($1)`,
// so it applies even if the user hasn't input a national prefix.
// `format.usesNationalPrefix()` detects such cases.
const usesNationalPrefix = format.usesNationalPrefix()
if ((usesNationalPrefix && nationalPrefix) ||
(!usesNationalPrefix && !international)) {
return true
}
}
}
}

View File

@@ -0,0 +1,100 @@
// Should be the same as `DIGIT_PLACEHOLDER` in `libphonenumber-metadata-generator`.
export const DIGIT_PLACEHOLDER = 'x' // '\u2008' (punctuation space)
const DIGIT_PLACEHOLDER_MATCHER = new RegExp(DIGIT_PLACEHOLDER)
// Counts all occurences of a symbol in a string.
// Unicode-unsafe (because using `.split()`).
export function countOccurences(symbol, string) {
let count = 0
// Using `.split('')` to iterate through a string here
// to avoid requiring `Symbol.iterator` polyfill.
// `.split('')` is generally not safe for Unicode,
// but in this particular case for counting brackets it is safe.
// for (const character of string)
for (const character of string.split('')) {
if (character === symbol) {
count++
}
}
return count
}
// Repeats a string (or a symbol) N times.
// http://stackoverflow.com/questions/202605/repeat-string-javascript
export function repeat(string, times) {
if (times < 1) {
return ''
}
let result = ''
while (times > 1) {
if (times & 1) {
result += string
}
times >>= 1
string += string
}
return result + string
}
export function cutAndStripNonPairedParens(string, cutBeforeIndex) {
if (string[cutBeforeIndex] === ')') {
cutBeforeIndex++
}
return stripNonPairedParens(string.slice(0, cutBeforeIndex))
}
export function closeNonPairedParens(template, cut_before) {
const retained_template = template.slice(0, cut_before)
const opening_braces = countOccurences('(', retained_template)
const closing_braces = countOccurences(')', retained_template)
let dangling_braces = opening_braces - closing_braces
while (dangling_braces > 0 && cut_before < template.length) {
if (template[cut_before] === ')') {
dangling_braces--
}
cut_before++
}
return template.slice(0, cut_before)
}
export function stripNonPairedParens(string) {
const dangling_braces =[]
let i = 0
while (i < string.length) {
if (string[i] === '(') {
dangling_braces.push(i)
}
else if (string[i] === ')') {
dangling_braces.pop()
}
i++
}
let start = 0
let cleared_string = ''
dangling_braces.push(string.length)
for (const index of dangling_braces) {
cleared_string += string.slice(start, index)
start = index + 1
}
return cleared_string
}
export function populateTemplateWithDigits(template, position, digits) {
// Using `.split('')` to iterate through a string here
// to avoid requiring `Symbol.iterator` polyfill.
// `.split('')` is generally not safe for Unicode,
// but in this particular case for `digits` it is safe.
// for (const digit of digits)
for (const digit of digits.split('')) {
// If there is room for more digits in current `template`,
// then set the next digit in the `template`,
// and return the formatted digits so far.
// If more digits are entered than the current format could handle.
if (template.slice(position + 1).search(DIGIT_PLACEHOLDER_MATCHER) < 0) {
return
}
position = template.search(DIGIT_PLACEHOLDER_MATCHER)
template = template.replace(DIGIT_PLACEHOLDER_MATCHER, digit)
}
return [template, position]
}

View File

@@ -0,0 +1,22 @@
import { closeNonPairedParens, stripNonPairedParens, repeat } from './AsYouTypeFormatter.util.js'
describe('closeNonPairedParens', () => {
it('should close non-paired braces', () => {
closeNonPairedParens('(000) 123-45 (9 )', 15).should.equal('(000) 123-45 (9 )')
})
})
describe('stripNonPairedParens', () => {
it('should strip non-paired braces', () => {
stripNonPairedParens('(000) 123-45 (9').should.equal('(000) 123-45 9')
stripNonPairedParens('(000) 123-45 (9)').should.equal('(000) 123-45 (9)')
})
})
describe('repeat', () => {
it('should repeat string N times', () => {
repeat('a', 0).should.equal('')
repeat('a', 3).should.equal('aaa')
repeat('a', 4).should.equal('aaaa')
})
})

View File

@@ -0,0 +1,503 @@
import extractCountryCallingCode from './helpers/extractCountryCallingCode.js'
import extractCountryCallingCodeFromInternationalNumberWithoutPlusSign from './helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js'
import extractNationalNumberFromPossiblyIncompleteNumber from './helpers/extractNationalNumberFromPossiblyIncompleteNumber.js'
import stripIddPrefix from './helpers/stripIddPrefix.js'
import parseDigits from './helpers/parseDigits.js'
import {
VALID_DIGITS,
VALID_PUNCTUATION,
PLUS_CHARS
} from './constants.js'
const VALID_FORMATTED_PHONE_NUMBER_DIGITS_PART =
'[' +
VALID_PUNCTUATION +
VALID_DIGITS +
']+'
const VALID_FORMATTED_PHONE_NUMBER_DIGITS_PART_PATTERN = new RegExp('^' + VALID_FORMATTED_PHONE_NUMBER_DIGITS_PART + '$', 'i')
const VALID_FORMATTED_PHONE_NUMBER_PART =
'(?:' +
'[' + PLUS_CHARS + ']' +
'[' +
VALID_PUNCTUATION +
VALID_DIGITS +
']*' +
'|' +
'[' +
VALID_PUNCTUATION +
VALID_DIGITS +
']+' +
')'
const AFTER_PHONE_NUMBER_DIGITS_END_PATTERN = new RegExp(
'[^' +
VALID_PUNCTUATION +
VALID_DIGITS +
']+' +
'.*' +
'$'
)
// Tests whether `national_prefix_for_parsing` could match
// different national prefixes.
// Matches anything that's not a digit or a square bracket.
const COMPLEX_NATIONAL_PREFIX = /[^\d\[\]]/
export default class AsYouTypeParser {
constructor({
defaultCountry,
defaultCallingCode,
metadata,
onNationalSignificantNumberChange
}) {
this.defaultCountry = defaultCountry
this.defaultCallingCode = defaultCallingCode
this.metadata = metadata
this.onNationalSignificantNumberChange = onNationalSignificantNumberChange
}
input(text, state) {
const [formattedDigits, hasPlus] = extractFormattedDigitsAndPlus(text)
const digits = parseDigits(formattedDigits)
// Checks for a special case: just a leading `+` has been entered.
let justLeadingPlus
if (hasPlus) {
if (!state.digits) {
state.startInternationalNumber()
if (!digits) {
justLeadingPlus = true
}
}
}
if (digits) {
this.inputDigits(digits, state)
}
return {
digits,
justLeadingPlus
}
}
/**
* Inputs "next" phone number digits.
* @param {string} digits
* @return {string} [formattedNumber] Formatted national phone number (if it can be formatted at this stage). Returning `undefined` means "don't format the national phone number at this stage".
*/
inputDigits(nextDigits, state) {
const { digits } = state
const hasReceivedThreeLeadingDigits = digits.length < 3 && digits.length + nextDigits.length >= 3
// Append phone number digits.
state.appendDigits(nextDigits)
// Attempt to extract IDD prefix:
// Some users input their phone number in international format,
// but in an "out-of-country" dialing format instead of using the leading `+`.
// https://github.com/catamphetamine/libphonenumber-js/issues/185
// Detect such numbers as soon as there're at least 3 digits.
// Google's library attempts to extract IDD prefix at 3 digits,
// so this library just copies that behavior.
// I guess that's because the most commot IDD prefixes are
// `00` (Europe) and `011` (US).
// There exist really long IDD prefixes too:
// for example, in Australia the default IDD prefix is `0011`,
// and it could even be as long as `14880011`.
// An IDD prefix is extracted here, and then every time when
// there's a new digit and the number couldn't be formatted.
if (hasReceivedThreeLeadingDigits) {
this.extractIddPrefix(state)
}
if (this.isWaitingForCountryCallingCode(state)) {
if (!this.extractCountryCallingCode(state)) {
return
}
} else {
state.appendNationalSignificantNumberDigits(nextDigits)
}
// If a phone number is being input in international format,
// then it's not valid for it to have a national prefix.
// Still, some people incorrectly input such numbers with a national prefix.
// In such cases, only attempt to strip a national prefix if the number becomes too long.
// (but that is done later, not here)
if (!state.international) {
if (!this.hasExtractedNationalSignificantNumber) {
this.extractNationalSignificantNumber(
state.getNationalDigits(),
(stateUpdate) => state.update(stateUpdate)
)
}
}
}
isWaitingForCountryCallingCode({ international, callingCode }) {
return international && !callingCode
}
// Extracts a country calling code from a number
// being entered in internatonal format.
extractCountryCallingCode(state) {
const { countryCallingCode, number } = extractCountryCallingCode(
'+' + state.getDigitsWithoutInternationalPrefix(),
this.defaultCountry,
this.defaultCallingCode,
this.metadata.metadata
)
if (countryCallingCode) {
state.setCallingCode(countryCallingCode)
state.update({
nationalSignificantNumber: number
})
return true
}
}
reset(numberingPlan) {
if (numberingPlan) {
this.hasSelectedNumberingPlan = true
const nationalPrefixForParsing = numberingPlan._nationalPrefixForParsing()
this.couldPossiblyExtractAnotherNationalSignificantNumber = nationalPrefixForParsing && COMPLEX_NATIONAL_PREFIX.test(nationalPrefixForParsing)
} else {
this.hasSelectedNumberingPlan = undefined
this.couldPossiblyExtractAnotherNationalSignificantNumber = undefined
}
}
/**
* Extracts a national (significant) number from user input.
* Google's library is different in that it only applies `national_prefix_for_parsing`
* and doesn't apply `national_prefix_transform_rule` after that.
* https://github.com/google/libphonenumber/blob/a3d70b0487875475e6ad659af404943211d26456/java/libphonenumber/src/com/google/i18n/phonenumbers/AsYouTypeFormatter.java#L539
* @return {boolean} [extracted]
*/
extractNationalSignificantNumber(nationalDigits, setState) {
if (!this.hasSelectedNumberingPlan) {
return
}
const {
nationalPrefix,
nationalNumber,
carrierCode
} = extractNationalNumberFromPossiblyIncompleteNumber(
nationalDigits,
this.metadata
)
if (nationalNumber === nationalDigits) {
return
}
this.onExtractedNationalNumber(
nationalPrefix,
carrierCode,
nationalNumber,
nationalDigits,
setState
)
return true
}
/**
* In Google's code this function is called "attempt to extract longer NDD".
* "Some national prefixes are a substring of others", they say.
* @return {boolean} [result] — Returns `true` if extracting a national prefix produced different results from what they were.
*/
extractAnotherNationalSignificantNumber(nationalDigits, prevNationalSignificantNumber, setState) {
if (!this.hasExtractedNationalSignificantNumber) {
return this.extractNationalSignificantNumber(nationalDigits, setState)
}
if (!this.couldPossiblyExtractAnotherNationalSignificantNumber) {
return
}
const {
nationalPrefix,
nationalNumber,
carrierCode
} = extractNationalNumberFromPossiblyIncompleteNumber(
nationalDigits,
this.metadata
)
// If a national prefix has been extracted previously,
// then it's always extracted as additional digits are added.
// That's assuming `extractNationalNumberFromPossiblyIncompleteNumber()`
// doesn't do anything different from what it currently does.
// So, just in case, here's this check, though it doesn't occur.
/* istanbul ignore if */
if (nationalNumber === prevNationalSignificantNumber) {
return
}
this.onExtractedNationalNumber(
nationalPrefix,
carrierCode,
nationalNumber,
nationalDigits,
setState
)
return true
}
onExtractedNationalNumber(
nationalPrefix,
carrierCode,
nationalSignificantNumber,
nationalDigits,
setState
) {
let complexPrefixBeforeNationalSignificantNumber
let nationalSignificantNumberMatchesInput
// This check also works with empty `this.nationalSignificantNumber`.
const nationalSignificantNumberIndex = nationalDigits.lastIndexOf(nationalSignificantNumber)
// If the extracted national (significant) number is the
// last substring of the `digits`, then it means that it hasn't been altered:
// no digits have been removed from the national (significant) number
// while applying `national_prefix_transform_rule`.
// https://gitlab.com/catamphetamine/libphonenumber-js/-/blob/master/METADATA.md#national_prefix_for_parsing--national_prefix_transform_rule
if (nationalSignificantNumberIndex >= 0 &&
nationalSignificantNumberIndex === nationalDigits.length - nationalSignificantNumber.length) {
nationalSignificantNumberMatchesInput = true
// If a prefix of a national (significant) number is not as simple
// as just a basic national prefix, then such prefix is stored in
// `this.complexPrefixBeforeNationalSignificantNumber` property and will be
// prepended "as is" to the national (significant) number to produce
// a formatted result.
const prefixBeforeNationalNumber = nationalDigits.slice(0, nationalSignificantNumberIndex)
// `prefixBeforeNationalNumber` is always non-empty,
// because `onExtractedNationalNumber()` isn't called
// when a national (significant) number hasn't been actually "extracted":
// when a national (significant) number is equal to the national part of `digits`,
// then `onExtractedNationalNumber()` doesn't get called.
if (prefixBeforeNationalNumber !== nationalPrefix) {
complexPrefixBeforeNationalSignificantNumber = prefixBeforeNationalNumber
}
}
setState({
nationalPrefix,
carrierCode,
nationalSignificantNumber,
nationalSignificantNumberMatchesInput,
complexPrefixBeforeNationalSignificantNumber
})
// `onExtractedNationalNumber()` is only called when
// the national (significant) number actually did change.
this.hasExtractedNationalSignificantNumber = true
this.onNationalSignificantNumberChange()
}
reExtractNationalSignificantNumber(state) {
// Attempt to extract a national prefix.
//
// Some people incorrectly input national prefix
// in an international phone number.
// For example, some people write British phone numbers as `+44(0)...`.
//
// Also, in some rare cases, it is valid for a national prefix
// to be a part of an international phone number.
// For example, mobile phone numbers in Mexico are supposed to be
// dialled internationally using a `1` national prefix,
// so the national prefix will be part of an international number.
//
// Quote from:
// https://www.mexperience.com/dialing-cell-phones-in-mexico/
//
// "Dialing a Mexican cell phone from abroad
// When you are calling a cell phone number in Mexico from outside Mexico,
// its necessary to dial an additional “1” after Mexicos country code
// (which is “52”) and before the area code.
// You also ignore the 045, and simply dial the area code and the
// cell phones number.
//
// If you dont add the “1”, youll receive a recorded announcement
// asking you to redial using it.
//
// For example, if you are calling from the USA to a cell phone
// in Mexico City, you would dial +52 1 55 1234 5678.
// (Note that this is different to calling a land line in Mexico City
// from abroad, where the number dialed would be +52 55 1234 5678)".
//
// Google's demo output:
// https://libphonenumber.appspot.com/phonenumberparser?number=%2b5215512345678&country=MX
//
if (this.extractAnotherNationalSignificantNumber(
state.getNationalDigits(),
state.nationalSignificantNumber,
(stateUpdate) => state.update(stateUpdate)
)) {
return true
}
// If no format matches the phone number, then it could be
// "a really long IDD" (quote from a comment in Google's library).
// An IDD prefix is first extracted when the user has entered at least 3 digits,
// and then here — every time when there's a new digit and the number
// couldn't be formatted.
// For example, in Australia the default IDD prefix is `0011`,
// and it could even be as long as `14880011`.
//
// Could also check `!hasReceivedThreeLeadingDigits` here
// to filter out the case when this check duplicates the one
// already performed when there're 3 leading digits,
// but it's not a big deal, and in most cases there
// will be a suitable `format` when there're 3 leading digits.
//
if (this.extractIddPrefix(state)) {
this.extractCallingCodeAndNationalSignificantNumber(state)
return true
}
// Google's AsYouType formatter supports sort of an "autocorrection" feature
// when it "autocorrects" numbers that have been input for a country
// with that country's calling code.
// Such "autocorrection" feature looks weird, but different people have been requesting it:
// https://github.com/catamphetamine/libphonenumber-js/issues/376
// https://github.com/catamphetamine/libphonenumber-js/issues/375
// https://github.com/catamphetamine/libphonenumber-js/issues/316
if (this.fixMissingPlus(state)) {
this.extractCallingCodeAndNationalSignificantNumber(state)
return true
}
}
extractIddPrefix(state) {
// An IDD prefix can't be present in a number written with a `+`.
// Also, don't re-extract an IDD prefix if has already been extracted.
const {
international,
IDDPrefix,
digits,
nationalSignificantNumber
} = state
if (international || IDDPrefix) {
return
}
// Some users input their phone number in "out-of-country"
// dialing format instead of using the leading `+`.
// https://github.com/catamphetamine/libphonenumber-js/issues/185
// Detect such numbers.
const numberWithoutIDD = stripIddPrefix(
digits,
this.defaultCountry,
this.defaultCallingCode,
this.metadata.metadata
)
if (numberWithoutIDD !== undefined && numberWithoutIDD !== digits) {
// If an IDD prefix was stripped then convert the IDD-prefixed number
// to international number for subsequent parsing.
state.update({
IDDPrefix: digits.slice(0, digits.length - numberWithoutIDD.length)
})
this.startInternationalNumber(state, {
country: undefined,
callingCode: undefined
})
return true
}
}
fixMissingPlus(state) {
if (!state.international) {
const {
countryCallingCode: newCallingCode,
number
} = extractCountryCallingCodeFromInternationalNumberWithoutPlusSign(
state.digits,
this.defaultCountry,
this.defaultCallingCode,
this.metadata.metadata
)
if (newCallingCode) {
state.update({
missingPlus: true
})
this.startInternationalNumber(state, {
country: state.country,
callingCode: newCallingCode
})
return true
}
}
}
startInternationalNumber(state, { country, callingCode }) {
state.startInternationalNumber(country, callingCode)
// If a national (significant) number has been extracted before, reset it.
if (state.nationalSignificantNumber) {
state.resetNationalSignificantNumber()
this.onNationalSignificantNumberChange()
this.hasExtractedNationalSignificantNumber = undefined
}
}
extractCallingCodeAndNationalSignificantNumber(state) {
if (this.extractCountryCallingCode(state)) {
// `this.extractCallingCode()` is currently called when the number
// couldn't be formatted during the standard procedure.
// Normally, the national prefix would be re-extracted
// for an international number if such number couldn't be formatted,
// but since it's already not able to be formatted,
// there won't be yet another retry, so also extract national prefix here.
this.extractNationalSignificantNumber(
state.getNationalDigits(),
(stateUpdate) => state.update(stateUpdate)
)
}
}
}
/**
* Extracts formatted phone number from text (if there's any).
* @param {string} text
* @return {string} [formattedPhoneNumber]
*/
function extractFormattedPhoneNumber(text) {
// Attempt to extract a possible number from the string passed in.
const startsAt = text.search(VALID_FORMATTED_PHONE_NUMBER_PART)
if (startsAt < 0) {
return
}
// Trim everything to the left of the phone number.
text = text.slice(startsAt)
// Trim the `+`.
let hasPlus
if (text[0] === '+') {
hasPlus = true
text = text.slice('+'.length)
}
// Trim everything to the right of the phone number.
text = text.replace(AFTER_PHONE_NUMBER_DIGITS_END_PATTERN, '')
// Re-add the previously trimmed `+`.
if (hasPlus) {
text = '+' + text
}
return text
}
/**
* Extracts formatted phone number digits (and a `+`) from text (if there're any).
* @param {string} text
* @return {any[]}
*/
function _extractFormattedDigitsAndPlus(text) {
// Extract a formatted phone number part from text.
const extractedNumber = extractFormattedPhoneNumber(text) || ''
// Trim a `+`.
if (extractedNumber[0] === '+') {
return [extractedNumber.slice('+'.length), true]
}
return [extractedNumber]
}
/**
* Extracts formatted phone number digits (and a `+`) from text (if there're any).
* @param {string} text
* @return {any[]}
*/
export function extractFormattedDigitsAndPlus(text) {
let [formattedDigits, hasPlus] = _extractFormattedDigitsAndPlus(text)
// If the extracted phone number part
// can possibly be a part of some valid phone number
// then parse phone number characters from a formatted phone number.
if (!VALID_FORMATTED_PHONE_NUMBER_DIGITS_PART_PATTERN.test(formattedDigits)) {
formattedDigits = ''
}
return [formattedDigits, hasPlus]
}

View File

@@ -0,0 +1,106 @@
// This "state" object simply holds the state of the "AsYouType" parser:
//
// * `country?: string`
// * `callingCode?: string`
// * `digits: string`
// * `international: boolean`
// * `missingPlus: boolean`
// * `IDDPrefix?: string`
// * `carrierCode?: string`
// * `nationalPrefix?: string`
// * `nationalSignificantNumber?: string`
// * `nationalSignificantNumberMatchesInput: boolean`
// * `complexPrefixBeforeNationalSignificantNumber?: string`
//
// `state.country` and `state.callingCode` aren't required to be in sync.
// For example, `state.country` could be `"AR"` and `state.callingCode` could be `undefined`.
// So `state.country` and `state.callingCode` are totally independent.
//
export default class AsYouTypeState {
constructor({ onCountryChange, onCallingCodeChange }) {
this.onCountryChange = onCountryChange
this.onCallingCodeChange = onCallingCodeChange
}
reset({ country, callingCode }) {
this.international = false
this.missingPlus = false
this.IDDPrefix = undefined
this.callingCode = undefined
this.digits = ''
this.resetNationalSignificantNumber()
this.initCountryAndCallingCode(country, callingCode)
}
resetNationalSignificantNumber() {
this.nationalSignificantNumber = this.getNationalDigits()
this.nationalSignificantNumberMatchesInput = true
this.nationalPrefix = undefined
this.carrierCode = undefined
this.complexPrefixBeforeNationalSignificantNumber = undefined
}
update(properties) {
for (const key of Object.keys(properties)) {
this[key] = properties[key]
}
}
initCountryAndCallingCode(country, callingCode) {
this.setCountry(country)
this.setCallingCode(callingCode)
}
setCountry(country) {
this.country = country
this.onCountryChange(country)
}
setCallingCode(callingCode) {
this.callingCode = callingCode
this.onCallingCodeChange(callingCode, this.country)
}
startInternationalNumber(country, callingCode) {
// Prepend the `+` to parsed input.
this.international = true
// If a default country was set then reset it
// because an explicitly international phone
// number is being entered.
this.initCountryAndCallingCode(country, callingCode)
}
appendDigits(nextDigits) {
this.digits += nextDigits
}
appendNationalSignificantNumberDigits(nextDigits) {
this.nationalSignificantNumber += nextDigits
}
/**
* Returns the part of `this.digits` that corresponds to the national number.
* Basically, all digits that have been input by the user, except for the
* international prefix and the country calling code part
* (if the number is an international one).
* @return {string}
*/
getNationalDigits() {
if (this.international) {
return this.digits.slice(
(this.IDDPrefix ? this.IDDPrefix.length : 0) +
(this.callingCode ? this.callingCode.length : 0)
)
}
return this.digits
}
getDigitsWithoutInternationalPrefix() {
if (this.international) {
if (this.IDDPrefix) {
return this.digits.slice(this.IDDPrefix.length)
}
}
return this.digits
}
}

View File

@@ -0,0 +1,14 @@
// https://stackoverflow.com/a/46971044/970769
// "Breaking changes in Typescript 2.1"
// "Extending built-ins like Error, Array, and Map may no longer work."
// "As a recommendation, you can manually adjust the prototype immediately after any super(...) calls."
// https://github.com/Microsoft/TypeScript-wiki/blob/main/Breaking-Changes.md#extending-built-ins-like-error-array-and-map-may-no-longer-work
export default class ParseError extends Error {
constructor(code) {
super(code)
// Set the prototype explicitly.
// Any subclass of FooError will have to manually set the prototype as well.
Object.setPrototypeOf(this, ParseError.prototype)
this.name = this.constructor.name
}
}

View File

@@ -0,0 +1,178 @@
import Metadata, { validateMetadata } from './metadata.js'
import isPossibleNumber from './isPossible.js'
import isValidNumber from './isValid.js'
// import checkNumberLength from './helpers/checkNumberLength.js'
import getNumberType from './helpers/getNumberType.js'
import getPossibleCountriesForNumber from './helpers/getPossibleCountriesForNumber.js'
import extractCountryCallingCode from './helpers/extractCountryCallingCode.js'
import isObject from './helpers/isObject.js'
import formatNumber from './format.js'
const USE_NON_GEOGRAPHIC_COUNTRY_CODE = false
export default class PhoneNumber {
/**
* @param {string} countryOrCountryCallingCode
* @param {string} nationalNumber
* @param {object} metadata — Metadata JSON
* @return {PhoneNumber}
*/
constructor(countryOrCountryCallingCode, nationalNumber, metadata) {
// Validate `countryOrCountryCallingCode` argument.
if (!countryOrCountryCallingCode) {
throw new TypeError('First argument is required')
}
if (typeof countryOrCountryCallingCode !== 'string') {
throw new TypeError('First argument must be a string')
}
// In case of public API use: `constructor(number, metadata)`.
// Transform the arguments from `constructor(number, metadata)` to
// `constructor(countryOrCountryCallingCode, nationalNumber, metadata)`.
if (countryOrCountryCallingCode[0] === '+' && !nationalNumber) {
throw new TypeError('`metadata` argument not passed')
}
if (isObject(nationalNumber) && isObject(nationalNumber.countries)) {
metadata = nationalNumber
const e164Number = countryOrCountryCallingCode
if (!E164_NUMBER_REGEXP.test(e164Number)) {
throw new Error('Invalid `number` argument passed: must consist of a "+" followed by digits')
}
const { countryCallingCode, number } = extractCountryCallingCode(e164Number, undefined, undefined, metadata)
nationalNumber = number
countryOrCountryCallingCode = countryCallingCode
if (!nationalNumber) {
throw new Error('Invalid `number` argument passed: too short')
}
}
// Validate `nationalNumber` argument.
if (!nationalNumber) {
throw new TypeError('`nationalNumber` argument is required')
}
if (typeof nationalNumber !== 'string') {
throw new TypeError('`nationalNumber` argument must be a string')
}
// Validate `metadata` argument.
validateMetadata(metadata)
// Initialize properties.
const { country, countryCallingCode } = getCountryAndCountryCallingCode(
countryOrCountryCallingCode,
metadata
)
this.country = country
this.countryCallingCode = countryCallingCode
this.nationalNumber = nationalNumber
this.number = '+' + this.countryCallingCode + this.nationalNumber
// Exclude `metadata` property output from `PhoneNumber.toString()`
// so that it doesn't clutter the console output of Node.js.
// Previously, when Node.js did `console.log(new PhoneNumber(...))`,
// it would output the whole internal structure of the `metadata` object.
this.getMetadata = () => metadata
}
setExt(ext) {
this.ext = ext
}
getPossibleCountries() {
if (this.country) {
return [this.country]
}
return getPossibleCountriesForNumber(
this.countryCallingCode,
this.nationalNumber,
this.getMetadata()
)
}
isPossible() {
return isPossibleNumber(this, { v2: true }, this.getMetadata())
}
isValid() {
return isValidNumber(this, { v2: true }, this.getMetadata())
}
isNonGeographic() {
const metadata = new Metadata(this.getMetadata())
return metadata.isNonGeographicCallingCode(this.countryCallingCode)
}
isEqual(phoneNumber) {
return this.number === phoneNumber.number && this.ext === phoneNumber.ext
}
// This function was originally meant to be an equivalent for `validatePhoneNumberLength()`,
// but later it was found out that it doesn't include the possible `TOO_SHORT` result
// returned from `parsePhoneNumberWithError()` in the original `validatePhoneNumberLength()`,
// so eventually I simply commented out this method from the `PhoneNumber` class
// and just left the `validatePhoneNumberLength()` function, even though that one would require
// and additional step to also validate the actual country / calling code of the phone number.
// validateLength() {
// const metadata = new Metadata(this.getMetadata())
// metadata.selectNumberingPlan(this.countryCallingCode)
// const result = checkNumberLength(this.nationalNumber, metadata)
// if (result !== 'IS_POSSIBLE') {
// return result
// }
// }
getType() {
return getNumberType(this, { v2: true }, this.getMetadata())
}
format(format, options) {
return formatNumber(
this,
format,
options ? { ...options, v2: true } : { v2: true },
this.getMetadata()
)
}
formatNational(options) {
return this.format('NATIONAL', options)
}
formatInternational(options) {
return this.format('INTERNATIONAL', options)
}
getURI(options) {
return this.format('RFC3966', options)
}
}
const isCountryCode = (value) => /^[A-Z]{2}$/.test(value)
function getCountryAndCountryCallingCode(countryOrCountryCallingCode, metadataJson) {
let country
let countryCallingCode
const metadata = new Metadata(metadataJson)
// If country code is passed then derive `countryCallingCode` from it.
// Also store the country code as `.country`.
if (isCountryCode(countryOrCountryCallingCode)) {
country = countryOrCountryCallingCode
metadata.selectNumberingPlan(country)
countryCallingCode = metadata.countryCallingCode()
} else {
countryCallingCode = countryOrCountryCallingCode
/* istanbul ignore if */
if (USE_NON_GEOGRAPHIC_COUNTRY_CODE) {
if (metadata.isNonGeographicCallingCode(countryCallingCode)) {
country = '001'
}
}
}
return {
country,
countryCallingCode
}
}
const E164_NUMBER_REGEXP = /^\+\d+$/

View File

@@ -0,0 +1,125 @@
import metadata from '../metadata.min.json' assert { type: 'json' }
import PhoneNumber from './PhoneNumber.js'
describe('PhoneNumber', () => {
it('should create a phone number via a public constructor', () => {
const phoneNumber = new PhoneNumber('+78005553535', metadata)
phoneNumber.setExt('1234')
expect(phoneNumber.country).to.be.undefined
phoneNumber.countryCallingCode.should.equal('7')
phoneNumber.nationalNumber.should.equal('8005553535')
phoneNumber.formatNational().should.equal('8 (800) 555-35-35 ext. 1234')
})
it('should validate constructor arguments (public constructor)', () => {
expect(() => new PhoneNumber()).to.throw('argument is required')
expect(() => new PhoneNumber(undefined, metadata)).to.throw('argument is required')
expect(() => new PhoneNumber('7', metadata)).to.throw('must consist of a "+"')
expect(() => new PhoneNumber('+7', metadata)).to.throw('too short')
expect(() => new PhoneNumber('+7800')).to.throw('`metadata` argument not passed')
expect(() => new PhoneNumber(1234567890)).to.throw('must be a string')
expect(() => new PhoneNumber('+1', 1234567890)).to.throw('must be a string')
})
it('should validate constructor arguments (private constructor)', () => {
expect(() => new PhoneNumber(undefined, '800', metadata)).to.throw('First argument is required')
expect(() => new PhoneNumber('7', undefined, metadata)).to.throw('`nationalNumber` argument is required')
expect(() => new PhoneNumber('7', '8005553535')).to.throw('`metadata` argument not passed')
})
it('should accept country code argument', () => {
const phoneNumber = new PhoneNumber('RU', '8005553535', metadata)
phoneNumber.countryCallingCode.should.equal('7')
phoneNumber.country.should.equal('RU')
phoneNumber.number.should.equal('+78005553535')
})
it('should format number with options', () => {
const phoneNumber = new PhoneNumber('7', '8005553535', metadata)
phoneNumber.ext = '123'
phoneNumber.format('NATIONAL', {
formatExtension: (number, extension) => `${number} доб. ${extension}`
})
.should.equal('8 (800) 555-35-35 доб. 123')
})
it('should compare phone numbers', () => {
new PhoneNumber('RU', '8005553535', metadata).isEqual(new PhoneNumber('RU', '8005553535', metadata)).should.equal(true)
new PhoneNumber('RU', '8005553535', metadata).isEqual(new PhoneNumber('7', '8005553535', metadata)).should.equal(true)
new PhoneNumber('RU', '8005553535', metadata).isEqual(new PhoneNumber('RU', '8005553536', metadata)).should.equal(false)
})
it('should tell if a number is non-geographic', () => {
new PhoneNumber('7', '8005553535', metadata).isNonGeographic().should.equal(false)
new PhoneNumber('870', '773111632', metadata).isNonGeographic().should.equal(true)
})
it('should allow setting extension', () => {
const phoneNumber = new PhoneNumber('1', '2133734253', metadata)
phoneNumber.setExt('1234')
phoneNumber.ext.should.equal('1234')
phoneNumber.formatNational().should.equal('(213) 373-4253 ext. 1234')
})
it('should return possible countries', () => {
// "599": [
// "CW", // "possible_lengths": [7, 8]
// "BQ" // "possible_lengths": [7]
// ]
let phoneNumber = new PhoneNumber('599', '123456', metadata)
expect(phoneNumber.country).to.be.undefined
phoneNumber.getPossibleCountries().should.deep.equal([])
phoneNumber = new PhoneNumber('599', '1234567', metadata)
expect(phoneNumber.country).to.be.undefined
phoneNumber.getPossibleCountries().should.deep.equal(['CW', 'BQ'])
phoneNumber = new PhoneNumber('599', '12345678', metadata)
expect(phoneNumber.country).to.be.undefined
phoneNumber.getPossibleCountries().should.deep.equal(['CW'])
phoneNumber = new PhoneNumber('599', '123456789', metadata)
expect(phoneNumber.country).to.be.undefined
phoneNumber.getPossibleCountries().should.deep.equal([])
})
it('should return possible countries in case of ambiguity', () => {
const phoneNumber = new PhoneNumber('1', '2223334444', metadata)
expect(phoneNumber.country).to.be.undefined
phoneNumber.getPossibleCountries().indexOf('US').should.equal(0)
phoneNumber.getPossibleCountries().length.should.equal(25)
})
// it('should return empty possible countries when no national number has been input', () => {
// const phoneNumber = new PhoneNumber('1', '', metadata)
// expect(phoneNumber.country).to.be.undefined
// phoneNumber.getPossibleCountries().should.deep.equal([])
// })
it('should return empty possible countries when not enough national number digits have been input', () => {
const phoneNumber = new PhoneNumber('1', '222', metadata)
expect(phoneNumber.country).to.be.undefined
phoneNumber.getPossibleCountries().should.deep.equal([])
})
it('should return possible countries in case of no ambiguity', () => {
const phoneNumber = new PhoneNumber('US', '2133734253', metadata)
phoneNumber.country.should.equal('US')
phoneNumber.getPossibleCountries().should.deep.equal(['US'])
})
it('should return empty possible countries in case of an unknown calling code', () => {
const phoneNumber = new PhoneNumber('777', '123', metadata)
expect(phoneNumber.country).to.be.undefined
phoneNumber.getPossibleCountries().should.deep.equal([])
})
// it('should validate phone number length', () => {
// const phoneNumber = new PhoneNumber('RU', '800', metadata)
// expect(phoneNumber.validateLength()).to.equal('TOO_SHORT')
//
// const phoneNumberValid = new PhoneNumber('RU', '8005553535', metadata)
// expect(phoneNumberValid.validateLength()).to.be.undefined
// })
})

View File

@@ -0,0 +1,396 @@
/**
* A port of Google's `PhoneNumberMatcher.java`.
* https://github.com/googlei18n/libphonenumber/blob/master/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberMatcher.java
* Date: 08.03.2018.
*/
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')
/**
* Patterns used to extract phone numbers from a larger phone-number-like pattern. These are
* ordered according to specificity. For example, white-space is last since that is frequently
* used in numbers, not just to separate two numbers. We have separate patterns since we don't
* want to break up the phone-number-like text on more than one different kind of symbol at one
* time, although symbols of the same type (e.g. space) can be safely grouped together.
*
* Note that if there is a match, we will always check any text found up to the first match as
* well.
*/
const INNER_MATCHES =
[
// Breaks on the slash - e.g. "651-234-2345/332-445-1234"
'\\/+(.*)/',
// Note that the bracket here is inside the capturing group, since we consider it part of the
// phone number. Will match a pattern like "(650) 223 3345 (754) 223 3321".
'(\\([^(]*)',
// Breaks on a hyphen - e.g. "12345 - 332-445-1234 is my number."
// We require a space on either side of the hyphen for it to be considered a separator.
`(?:${pZ}-|-${pZ})${pZ}*(.+)`,
// Various types of wide hyphens. Note we have decided not to enforce a space here, since it's
// possible that it's supposed to be used to break two numbers without spaces, and we haven't
// seen many instances of it used within a number.
`[\u2012-\u2015\uFF0D]${pZ}*(.+)`,
// Breaks on a full stop - e.g. "12345. 332-445-1234 is my number."
`\\.+${pZ}*([^.]+)`,
// Breaks on space - e.g. "3324451234 8002341234"
`${pZ}+(${PZ}+)`
]
// Limit on the number of leading (plus) characters.
const leadLimit = limit(0, 2)
// Limit on the number of consecutive punctuation characters.
const punctuationLimit = limit(0, 4)
/* The maximum number of digits allowed in a digit-separated block. As we allow all digits in a
* single block, set high enough to accommodate the entire national number and the international
* country code. */
const digitBlockLimit = MAX_LENGTH_FOR_NSN + MAX_LENGTH_COUNTRY_CODE
// Limit on the number of blocks separated by punctuation.
// Uses digitBlockLimit since some formats use spaces to separate each digit.
const blockLimit = limit(0, digitBlockLimit)
/* A punctuation sequence allowing white space. */
const punctuation = `[${VALID_PUNCTUATION}]` + punctuationLimit
// A digits block without punctuation.
const digitSequence = pNd + limit(1, digitBlockLimit)
/**
* Phone number pattern allowing optional punctuation.
* The phone number pattern used by `find()`, similar to
* VALID_PHONE_NUMBER, but with the following differences:
* <ul>
* <li>All captures are limited in order to place an upper bound to the text matched by the
* pattern.
* <ul>
* <li>Leading punctuation / plus signs are limited.
* <li>Consecutive occurrences of punctuation are limited.
* <li>Number of digits is limited.
* </ul>
* <li>No whitespace is allowed at the start or end.
* <li>No alpha digits (vanity numbers such as 1-800-SIX-FLAGS) are currently supported.
* </ul>
*/
const PATTERN = '(?:' + LEAD_CLASS + punctuation + ')' + leadLimit
+ digitSequence + '(?:' + punctuation + digitSequence + ')' + blockLimit
+ '(?:' + EXTN_PATTERNS_FOR_MATCHING + ')?'
// Regular expression of trailing characters that we want to remove.
// We remove all characters that are not alpha or numerical characters.
// The hash character is retained here, as it may signify
// the previous block was an extension.
//
// // Don't know what does '&&' mean here.
// const UNWANTED_END_CHAR_PATTERN = new RegExp(`[[\\P{N}&&\\P{L}]&&[^#]]+$`)
//
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
/**
* A stateful class that finds and extracts telephone numbers from {@linkplain CharSequence text}.
* Instances can be created using the {@linkplain PhoneNumberUtil#findNumbers factory methods} in
* {@link PhoneNumberUtil}.
*
* <p>Vanity numbers (phone numbers using alphabetic digits such as <tt>1-800-SIX-FLAGS</tt> are
* not found.
*
* <p>This class is not thread-safe.
*/
export default class PhoneNumberMatcher
{
/**
* @param {string} text — the character sequence that we will search, null for no text.
* @param {'POSSIBLE'|'VALID'|'STRICT_GROUPING'|'EXACT_GROUPING'} [options.leniency] — The leniency to use when evaluating candidate phone numbers. See `source/findNumbers/Leniency.js` for more details.
* @param {number} [options.maxTries] — The maximum number of invalid numbers to try before giving up on the text. This is to cover degenerate cases where the text has a lot of false positives in it. Must be >= 0.
*/
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
}
// Validate `leniency`.
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".`)
}
// Validate `maxTries`.
if (options.maxTries < 0) {
throw new TypeError('`maxTries` must be `>= 0`')
}
this.text = text
this.options = options
this.metadata = metadata
// The degree of phone number validation.
this.leniency = Leniency[options.leniency]
if (!this.leniency) {
throw new TypeError(`Unknown leniency: "${options.leniency}"`)
}
/** The maximum number of retries after matching an invalid number. */
this.maxTries = options.maxTries
this.PATTERN = new RegExp(PATTERN, 'ig')
/** The iteration tristate. */
this.state = 'NOT_READY'
/** The next index to start searching at. Undefined in {@link State#DONE}. */
this.searchIndex = 0
// A cache for frequently used country-specific regular expressions. Set to 32 to cover ~2-3
// countries being used for the same doc with ~10 patterns for each country. Some pages will have
// a lot more countries in use, but typically fewer numbers for each so expanding the cache for
// that use-case won't have a lot of benefit.
this.regExpCache = new RegExpCache(32)
}
/**
* Attempts to find the next subsequence in the searched sequence on or after {@code searchIndex}
* that represents a phone number. Returns the next match, null if none was found.
*
* @param index the search index to start searching at
* @return the phone number match found, null if none can be found
*/
find() {
// // Reset the regular expression.
// this.PATTERN.lastIndex = index
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 =
// Try to come up with a valid match given the entire candidate.
this.parseAndVerify(candidate, offset, this.text)
// If that failed, try to find an "inner match" -
// there might be a phone number within this candidate.
|| 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) {
/* istanbul ignore if */
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--
}
}
/**
* Attempts to extract a match from `substring`
* if the substring itself does not qualify as a match.
*/
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) {
// We should handle any group before this one too.
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])
// Java code does `groupMatcher.start(1)` here,
// but there's no way in javascript to get a `candidate` start index,
// therefore resort to using this kind of an approximation.
// (`groupMatcher` is called `candidateInSubstringMatch` in this javascript port)
// https://stackoverflow.com/questions/15934353/get-index-of-each-capture-in-a-javascript-regex
const candidateIndexGuess = substring.indexOf(candidate, candidateMatch.index)
const match = this.parseAndVerify(candidate, offset + candidateIndexGuess, text)
if (match) {
return match
}
this.maxTries--
}
}
}
/**
* Parses a phone number from the `candidate` using `parse` and
* verifies it matches the requested `leniency`. If parsing and verification succeed,
* a corresponding `PhoneNumberMatch` is returned, otherwise this method returns `null`.
*
* @param candidate the candidate match
* @param offset the offset of {@code candidate} within {@link #text}
* @return the parsed and validated phone number match, or null
*/
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() // (this.searchIndex)
if (this.lastMatch)
{
// this.searchIndex = this.lastMatch.endsAt
this.state = 'READY'
}
else
{
this.state = 'DONE'
}
}
return this.state === 'READY'
}
next()
{
// Check the state and find the next match as a side-effect if necessary.
if (!this.hasNext())
{
throw new Error('No next element')
}
// Don't retain that memory any longer than necessary.
const result = this.lastMatch
this.lastMatch = null
this.state = 'NOT_READY'
return result
}
}

View File

@@ -0,0 +1,81 @@
import PhoneNumberMatcher from './PhoneNumberMatcher.js'
import metadata from '../metadata.min.json' assert { type: 'json' }
function test(text, defaultCountry, expectedNumbers) {
if (typeof expectedNumbers === 'string') {
expectedNumbers = [{
nationalNumber: expectedNumbers
}]
}
const matcher = new PhoneNumberMatcher(text, { defaultCountry, v2: true }, metadata)
while (matcher.hasNext()) {
const number = matcher.next()
const phoneNumber = expectedNumbers.shift()
if (phoneNumber.startsAt !== undefined) {
number.startsAt.should.equal(phoneNumber.startsAt)
}
if (phoneNumber.endsAt !== undefined) {
number.endsAt.should.equal(phoneNumber.endsAt)
}
number.number.country.should.equal(phoneNumber.country || defaultCountry)
number.number.nationalNumber.should.equal(phoneNumber.nationalNumber)
}
expectedNumbers.length.should.equal(0)
}
describe('PhoneNumberMatcher', () => {
it('should find phone numbers', () => {
test(
'The number is +7 (800) 555-35-35 and not (213) 373-4253 as written in the document.',
'US',
[{
country: 'RU',
nationalNumber: '8005553535',
startsAt: 14,
endsAt: 32
}, {
country: 'US',
nationalNumber: '2133734253',
startsAt: 41,
endsAt: 55
}]
)
})
it('should find phone numbers from Mexico', () => {
// Test parsing fixed-line numbers of Mexico.
test('+52 (449)978-0001', 'MX', '4499780001')
test('01 (449)978-0001', 'MX', '4499780001')
test('(449)978-0001', 'MX', '4499780001')
// "Dialling tokens 01, 02, 044, 045 and 1 are removed as they are
// no longer valid since August 2019."
// // Test parsing mobile numbers of Mexico.
// test('+52 1 33 1234-5678', 'MX', '3312345678')
// test('044 (33) 1234-5678', 'MX', '3312345678')
// test('045 33 1234-5678', 'MX', '3312345678')
})
it('should find phone numbers from Argentina', () => {
// Test parsing mobile numbers of Argentina.
test('+54 9 343 555 1212', 'AR', '93435551212')
test('0343 15-555-1212', 'AR', '93435551212')
test('+54 9 3715 65 4320', 'AR', '93715654320')
test('03715 15 65 4320', 'AR', '93715654320')
// Test parsing fixed-line numbers of Argentina.
test('+54 11 3797 0000', 'AR', '1137970000')
test('011 3797 0000', 'AR', '1137970000')
test('+54 3715 65 4321', 'AR', '3715654321')
test('03715 65 4321', 'AR', '3715654321')
test('+54 23 1234 0000', 'AR', '2312340000')
test('023 1234 0000', 'AR', '2312340000')
})
it('should only support the supported leniency values', function() {
expect(() => new PhoneNumberMatcher('+54 23 1234 0000', { leniency: 'STRICT_GROUPING', v2: true }, metadata)).to.throw('Supported values: "POSSIBLE", "VALID".')
})
})

View File

@@ -0,0 +1,31 @@
// The minimum length of the national significant number.
export const MIN_LENGTH_FOR_NSN = 2
// The ITU says the maximum length should be 15,
// but one can find longer numbers in Germany.
export const MAX_LENGTH_FOR_NSN = 17
// The maximum length of the country calling code.
export const MAX_LENGTH_COUNTRY_CODE = 3
// Digits accepted in phone numbers
// (ascii, fullwidth, arabic-indic, and eastern arabic digits).
export const VALID_DIGITS = '0-9\uFF10-\uFF19\u0660-\u0669\u06F0-\u06F9'
// `DASHES` will be right after the opening square bracket of the "character class"
const DASHES = '-\u2010-\u2015\u2212\u30FC\uFF0D'
const SLASHES = '\uFF0F/'
const DOTS = '\uFF0E.'
export const WHITESPACE = ' \u00A0\u00AD\u200B\u2060\u3000'
const BRACKETS = '()\uFF08\uFF09\uFF3B\uFF3D\\[\\]'
// export const OPENING_BRACKETS = '(\uFF08\uFF3B\\\['
const TILDES = '~\u2053\u223C\uFF5E'
// Regular expression of acceptable punctuation found in phone numbers. This
// excludes punctuation found as a leading character only. This consists of dash
// characters, white space characters, full stops, slashes, square brackets,
// parentheses and tildes. Full-width variants are also present.
export const VALID_PUNCTUATION = `${DASHES}${SLASHES}${DOTS}${WHITESPACE}${BRACKETS}${TILDES}`
export const PLUS_CHARS = '+\uFF0B'
// const LEADING_PLUS_CHARS_PATTERN = new RegExp('^[' + PLUS_CHARS + ']+')

View File

@@ -0,0 +1,107 @@
// https://medium.com/dsinjs/implementing-lru-cache-in-javascript-94ba6755cda9
class Node {
constructor(key, value, next = null, prev = null) {
this.key = key;
this.value = value;
this.next = next;
this.prev = prev;
}
}
export default class LRUCache {
//set default limit of 10 if limit is not passed.
constructor(limit = 10) {
this.size = 0;
this.limit = limit;
this.head = null;
this.tail = null;
this.cache = {};
}
// Write Node to head of LinkedList
// update cache with Node key and Node reference
put(key, value){
this.ensureLimit();
if(!this.head){
this.head = this.tail = new Node(key, value);
}else{
const node = new Node(key, value, this.head);
this.head.prev = node;
this.head = node;
}
//Update the cache map
this.cache[key] = this.head;
this.size++;
}
// Read from cache map and make that node as new Head of LinkedList
get(key){
if(this.cache[key]){
const value = this.cache[key].value;
// node removed from it's position and cache
this.remove(key)
// write node again to the head of LinkedList to make it most recently used
this.put(key, value);
return value;
}
console.log(`Item not available in cache for key ${key}`);
}
ensureLimit(){
if(this.size === this.limit){
this.remove(this.tail.key)
}
}
remove(key){
const node = this.cache[key];
if(node.prev !== null){
node.prev.next = node.next;
}else{
this.head = node.next;
}
if(node.next !== null){
node.next.prev = node.prev;
}else{
this.tail = node.prev
}
delete this.cache[key];
this.size--;
}
clear() {
this.head = null;
this.tail = null;
this.size = 0;
this.cache = {};
}
// // Invokes the callback function with every node of the chain and the index of the node.
// forEach(fn) {
// let node = this.head;
// let counter = 0;
// while (node) {
// fn(node, counter);
// node = node.next;
// counter++;
// }
// }
// // To iterate over LRU with a 'for...of' loop
// *[Symbol.iterator]() {
// let node = this.head;
// while (node) {
// yield node;
// node = node.next;
// }
// }
}

View File

@@ -0,0 +1,410 @@
import isValidNumber from '../isValid.js'
import parseDigits from '../helpers/parseDigits.js'
import matchPhoneNumberStringAgainstPhoneNumber from './matchPhoneNumberStringAgainstPhoneNumber.js'
import Metadata from '../metadata.js'
import getCountryByCallingCode from '../helpers/getCountryByCallingCode.js'
import { chooseFormatForNumber } from '../format.js'
import {
startsWith,
endsWith
} from './util.js'
/**
* Leniency when finding potential phone numbers in text segments
* The levels here are ordered in increasing strictness.
*/
export default
{
/**
* Phone numbers accepted are "possible", but not necessarily "valid".
*/
POSSIBLE(phoneNumber, { candidate, metadata })
{
return true
},
/**
* Phone numbers accepted are "possible" and "valid".
* Numbers written in national format must have their national-prefix
* present if it is usually written for a number of this type.
*/
VALID(phoneNumber, { candidate, defaultCountry, metadata })
{
if (
!phoneNumber.isValid() ||
!containsOnlyValidXChars(phoneNumber, candidate, metadata)
)
{
return false
}
// Skipped for simplicity.
// return isNationalPrefixPresentIfRequired(phoneNumber, { defaultCountry, metadata })
return true
},
/**
* Phone numbers accepted are "valid" and
* are grouped in a possible way for this locale. For example, a US number written as
* "65 02 53 00 00" and "650253 0000" are not accepted at this leniency level, whereas
* "650 253 0000", "650 2530000" or "6502530000" are.
* Numbers with more than one '/' symbol in the national significant number
* are also dropped at this level.
*
* Warning: This level might result in lower coverage especially for regions outside of
* country code "+1". If you are not sure about which level to use,
* email the discussion group libphonenumber-discuss@googlegroups.com.
*/
STRICT_GROUPING(phoneNumber, { candidate, defaultCountry, metadata, regExpCache })
{
if (
!phoneNumber.isValid() ||
!containsOnlyValidXChars(phoneNumber, candidate, metadata) ||
containsMoreThanOneSlashInNationalNumber(phoneNumber, candidate) ||
!isNationalPrefixPresentIfRequired(phoneNumber, { defaultCountry, metadata })
)
{
return false
}
return checkNumberGroupingIsValid
(
phoneNumber,
candidate,
metadata,
allNumberGroupsRemainGrouped,
regExpCache
)
},
/**
* Phone numbers accepted are "valid" and are grouped in the same way
* that we would have formatted it, or as a single block.
* For example, a US number written as "650 2530000" is not accepted
* at this leniency level, whereas "650 253 0000" or "6502530000" are.
* Numbers with more than one '/' symbol are also dropped at this level.
*
* Warning: This level might result in lower coverage especially for regions outside of
* country code "+1". If you are not sure about which level to use, email the discussion group
* libphonenumber-discuss@googlegroups.com.
*/
EXACT_GROUPING(phoneNumber, { candidate, defaultCountry, metadata, regExpCache })
{
if (
!phoneNumber.isValid() ||
!containsOnlyValidXChars(phoneNumber, candidate, metadata) ||
containsMoreThanOneSlashInNationalNumber(phoneNumber, candidate) ||
!isNationalPrefixPresentIfRequired(phoneNumber, { defaultCountry, metadata })
)
{
return false
}
return checkNumberGroupingIsValid
(
phoneNumber,
candidate,
metadata,
allNumberGroupsAreExactlyPresent,
regExpCache
)
}
}
function containsOnlyValidXChars(phoneNumber, candidate, metadata)
{
// The characters 'x' and 'X' can be (1) a carrier code, in which case they always precede the
// national significant number or (2) an extension sign, in which case they always precede the
// extension number. We assume a carrier code is more than 1 digit, so the first case has to
// have more than 1 consecutive 'x' or 'X', whereas the second case can only have exactly 1 'x'
// or 'X'. We ignore the character if it appears as the last character of the string.
for (let index = 0; index < candidate.length - 1; index++)
{
const charAtIndex = candidate.charAt(index)
if (charAtIndex === 'x' || charAtIndex === 'X')
{
const charAtNextIndex = candidate.charAt(index + 1)
if (charAtNextIndex === 'x' || charAtNextIndex === 'X')
{
// This is the carrier code case, in which the 'X's always precede the national
// significant number.
index++
if (matchPhoneNumberStringAgainstPhoneNumber(candidate.substring(index), phoneNumber, metadata) !== 'NSN_MATCH')
{
return false
}
// This is the extension sign case, in which the 'x' or 'X' should always precede the
// extension number.
}
else {
const ext = parseDigits(candidate.substring(index))
if (ext) {
if (phoneNumber.ext !== ext) {
return false
}
} else {
if (phoneNumber.ext) {
return false
}
}
}
}
}
return true
}
function isNationalPrefixPresentIfRequired(phoneNumber, { defaultCountry, metadata: _metadata })
{
// First, check how we deduced the country code. If it was written in international format, then
// the national prefix is not required.
if (phoneNumber.__countryCallingCodeSource !== 'FROM_DEFAULT_COUNTRY')
{
return true
}
const metadata = new Metadata(_metadata)
metadata.selectNumberingPlan(phoneNumber.countryCallingCode)
const phoneNumberRegion = phoneNumber.country || getCountryByCallingCode(phoneNumber.countryCallingCode, {
nationalNumber: phoneNumber.nationalNumber,
defaultCountry,
metadata
})
// Check if a national prefix should be present when formatting this number.
const nationalNumber = phoneNumber.nationalNumber
const format = chooseFormatForNumber(metadata.numberingPlan.formats(), nationalNumber)
// To do this, we check that a national prefix formatting rule was present
// and that it wasn't just the first-group symbol ($1) with punctuation.
if (format.nationalPrefixFormattingRule())
{
if (metadata.numberingPlan.nationalPrefixIsOptionalWhenFormattingInNationalFormat())
{
// The national-prefix is optional in these cases, so we don't need to check if it was present.
return true
}
if (!format.usesNationalPrefix())
{
// National Prefix not needed for this number.
return true
}
return Boolean(phoneNumber.nationalPrefix)
}
return true
}
export function containsMoreThanOneSlashInNationalNumber(phoneNumber, candidate)
{
const firstSlashInBodyIndex = candidate.indexOf('/')
if (firstSlashInBodyIndex < 0)
{
// No slashes, this is okay.
return false
}
// Now look for a second one.
const secondSlashInBodyIndex = candidate.indexOf('/', firstSlashInBodyIndex + 1)
if (secondSlashInBodyIndex < 0)
{
// Only one slash, this is okay.
return false
}
// If the first slash is after the country calling code, this is permitted.
const candidateHasCountryCode =
phoneNumber.__countryCallingCodeSource === 'FROM_NUMBER_WITH_PLUS_SIGN' ||
phoneNumber.__countryCallingCodeSource === 'FROM_NUMBER_WITHOUT_PLUS_SIGN'
if (candidateHasCountryCode && parseDigits(candidate.substring(0, firstSlashInBodyIndex)) === phoneNumber.countryCallingCode)
{
// Any more slashes and this is illegal.
return candidate.slice(secondSlashInBodyIndex + 1).indexOf('/') >= 0
}
return true
}
function checkNumberGroupingIsValid(
number,
candidate,
metadata,
checkGroups,
regExpCache
) {
throw new Error('This part of code hasn\'t been ported')
const normalizedCandidate = normalizeDigits(candidate, true /* keep non-digits */)
let formattedNumberGroups = getNationalNumberGroups(metadata, number, null)
if (checkGroups(metadata, number, normalizedCandidate, formattedNumberGroups)) {
return true
}
// If this didn't pass, see if there are any alternate formats that match, and try them instead.
const alternateFormats = MetadataManager.getAlternateFormatsForCountry(number.getCountryCode())
const nationalSignificantNumber = util.getNationalSignificantNumber(number)
if (alternateFormats) {
for (const alternateFormat of alternateFormats.numberFormats()) {
if (alternateFormat.leadingDigitsPatterns().length > 0) {
// There is only one leading digits pattern for alternate formats.
const leadingDigitsRegExp = regExpCache.getPatternForRegExp('^' + alternateFormat.leadingDigitsPatterns()[0])
if (!leadingDigitsRegExp.test(nationalSignificantNumber)) {
// Leading digits don't match; try another one.
continue
}
}
formattedNumberGroups = getNationalNumberGroups(metadata, number, alternateFormat)
if (checkGroups(metadata, number, normalizedCandidate, formattedNumberGroups)) {
return true
}
}
}
return false
}
/**
* Helper method to get the national-number part of a number, formatted without any national
* prefix, and return it as a set of digit blocks that would be formatted together following
* standard formatting rules.
*/
function getNationalNumberGroups(
metadata,
number,
formattingPattern
) {
throw new Error('This part of code hasn\'t been ported')
if (formattingPattern) {
// We format the NSN only, and split that according to the separator.
const nationalSignificantNumber = util.getNationalSignificantNumber(number)
return util.formatNsnUsingPattern(nationalSignificantNumber,
formattingPattern, 'RFC3966', metadata).split('-')
}
// This will be in the format +CC-DG1-DG2-DGX;ext=EXT where DG1..DGX represents groups of digits.
const rfc3966Format = formatNumber(number, 'RFC3966', metadata)
// We remove the extension part from the formatted string before splitting it into different
// groups.
let endIndex = rfc3966Format.indexOf(';')
if (endIndex < 0) {
endIndex = rfc3966Format.length
}
// The country-code will have a '-' following it.
const startIndex = rfc3966Format.indexOf('-') + 1
return rfc3966Format.slice(startIndex, endIndex).split('-')
}
function allNumberGroupsAreExactlyPresent
(
metadata,
number,
normalizedCandidate,
formattedNumberGroups
)
{
throw new Error('This part of code hasn\'t been ported')
const candidateGroups = normalizedCandidate.split(NON_DIGITS_PATTERN)
// Set this to the last group, skipping it if the number has an extension.
let candidateNumberGroupIndex =
number.hasExtension() ? candidateGroups.length - 2 : candidateGroups.length - 1
// First we check if the national significant number is formatted as a block.
// We use contains and not equals, since the national significant number may be present with
// a prefix such as a national number prefix, or the country code itself.
if (candidateGroups.length == 1
|| candidateGroups[candidateNumberGroupIndex].contains(
util.getNationalSignificantNumber(number)))
{
return true
}
// Starting from the end, go through in reverse, excluding the first group, and check the
// candidate and number groups are the same.
let formattedNumberGroupIndex = (formattedNumberGroups.length - 1)
while (formattedNumberGroupIndex > 0 && candidateNumberGroupIndex >= 0)
{
if (candidateGroups[candidateNumberGroupIndex] !== formattedNumberGroups[formattedNumberGroupIndex])
{
return false
}
formattedNumberGroupIndex--
candidateNumberGroupIndex--
}
// Now check the first group. There may be a national prefix at the start, so we only check
// that the candidate group ends with the formatted number group.
return (candidateNumberGroupIndex >= 0
&& endsWith(candidateGroups[candidateNumberGroupIndex], formattedNumberGroups[0]))
}
function allNumberGroupsRemainGrouped
(
metadata,
number,
normalizedCandidate,
formattedNumberGroups
)
{
throw new Error('This part of code hasn\'t been ported')
let fromIndex = 0
if (number.getCountryCodeSource() !== CountryCodeSource.FROM_DEFAULT_COUNTRY)
{
// First skip the country code if the normalized candidate contained it.
const countryCode = String(number.getCountryCode())
fromIndex = normalizedCandidate.indexOf(countryCode) + countryCode.length()
}
// Check each group of consecutive digits are not broken into separate groupings in the
// {@code normalizedCandidate} string.
for (let i = 0; i < formattedNumberGroups.length; i++)
{
// Fails if the substring of {@code normalizedCandidate} starting from {@code fromIndex}
// doesn't contain the consecutive digits in formattedNumberGroups[i].
fromIndex = normalizedCandidate.indexOf(formattedNumberGroups[i], fromIndex)
if (fromIndex < 0) {
return false
}
// Moves {@code fromIndex} forward.
fromIndex += formattedNumberGroups[i].length()
if (i == 0 && fromIndex < normalizedCandidate.length())
{
// We are at the position right after the NDC. We get the region used for formatting
// information based on the country code in the phone number, rather than the number itself,
// as we do not need to distinguish between different countries with the same country
// calling code and this is faster.
const region = util.getRegionCodeForCountryCode(number.getCountryCode())
if (util.getNddPrefixForRegion(region, true) != null
&& Character.isDigit(normalizedCandidate.charAt(fromIndex))) {
// This means there is no formatting symbol after the NDC. In this case, we only
// accept the number if there is no formatting symbol at all in the number, except
// for extensions. This is only important for countries with national prefixes.
const nationalSignificantNumber = util.getNationalSignificantNumber(number)
return startsWith
(
normalizedCandidate.slice(fromIndex - formattedNumberGroups[i].length),
nationalSignificantNumber
)
}
}
}
// The check here makes sure that we haven't mistakenly already used the extension to
// match the last group of the subscriber number. Note the extension cannot have
// formatting in-between digits.
return normalizedCandidate.slice(fromIndex).contains(number.getExtension())
}

View File

@@ -0,0 +1,35 @@
/*
import { containsMoreThanOneSlashInNationalNumber } from './Leniency.js'
describe('Leniency', () => {
it('testContainsMoreThanOneSlashInNationalNumber', () => {
// A date should return true.
number.setCountryCode(1)
number.setCountryCodeSource(CountryCodeSource.FROM_DEFAULT_COUNTRY)
containsMoreThanOneSlashInNationalNumber(number, '1/05/2013').should.equal(true)
// Here, the country code source thinks it started with a country calling code, but this is not
// the same as the part before the slash, so it's still true.
number.setCountryCode(274)
number.setCountryCodeSource(CountryCodeSource.FROM_NUMBER_WITHOUT_PLUS_SIGN)
containsMoreThanOneSlashInNationalNumber(number, '27/4/2013').should.equal(true)
// Now it should be false, because the first slash is after the country calling code.
number.setCountryCode(49)
number.setCountryCodeSource(CountryCodeSource.FROM_NUMBER_WITH_PLUS_SIGN)
containsMoreThanOneSlashInNationalNumber(number, '49/69/2013').should.equal(false)
number.setCountryCode(49)
number.setCountryCodeSource(CountryCodeSource.FROM_NUMBER_WITHOUT_PLUS_SIGN)
containsMoreThanOneSlashInNationalNumber(number, '+49/69/2013').should.equal(false)
containsMoreThanOneSlashInNationalNumber(number, '+ 49/69/2013').should.equal(false)
containsMoreThanOneSlashInNationalNumber(number, '+ 49/69/20/13').should.equal(true)
// Here, the first group is not assumed to be the country calling code, even though it is the
// same as it, so this should return true.
number.setCountryCode(49)
number.setCountryCodeSource(CountryCodeSource.FROM_DEFAULT_COUNTRY)
containsMoreThanOneSlashInNationalNumber(number, '49/69/2013').should.equal(true)
})
})
*/

View File

@@ -0,0 +1,20 @@
import LRUCache from './LRUCache.js'
// A cache for frequently used country-specific regular expressions. Set to 32 to cover ~2-3
// countries being used for the same doc with ~10 patterns for each country. Some pages will have
// a lot more countries in use, but typically fewer numbers for each so expanding the cache for
// that use-case won't have a lot of benefit.
export default class RegExpCache {
constructor(size) {
this.cache = new LRUCache(size)
}
getPatternForRegExp(pattern) {
let regExp = this.cache.get(pattern)
if (!regExp) {
regExp = new RegExp('^' + pattern)
this.cache.put(pattern, regExp)
}
return regExp
}
}

View File

@@ -0,0 +1,86 @@
// Copy-pasted from `PhoneNumberMatcher.js`.
import { PLUS_CHARS } from '../constants.js'
import { limit } from './util.js'
import {
isLatinLetter,
isInvalidPunctuationSymbol
} from './utf-8.js'
const OPENING_PARENS = '(\\[\uFF08\uFF3B'
const CLOSING_PARENS = ')\\]\uFF09\uFF3D'
const NON_PARENS = `[^${OPENING_PARENS}${CLOSING_PARENS}]`
export const LEAD_CLASS = `[${OPENING_PARENS}${PLUS_CHARS}]`
// Punctuation that may be at the start of a phone number - brackets and plus signs.
const LEAD_CLASS_LEADING = new RegExp('^' + LEAD_CLASS)
// Limit on the number of pairs of brackets in a phone number.
const BRACKET_PAIR_LIMIT = limit(0, 3)
/**
* Pattern to check that brackets match. Opening brackets should be closed within a phone number.
* This also checks that there is something inside the brackets. Having no brackets at all is also
* fine.
*
* An opening bracket at the beginning may not be closed, but subsequent ones should be. It's
* also possible that the leading bracket was dropped, so we shouldn't be surprised if we see a
* closing bracket first. We limit the sets of brackets in a phone number to four.
*/
const MATCHING_BRACKETS_ENTIRE = new RegExp
(
'^'
+ "(?:[" + OPENING_PARENS + "])?" + "(?:" + NON_PARENS + "+" + "[" + CLOSING_PARENS + "])?"
+ NON_PARENS + "+"
+ "(?:[" + OPENING_PARENS + "]" + NON_PARENS + "+[" + CLOSING_PARENS + "])" + BRACKET_PAIR_LIMIT
+ NON_PARENS + "*"
+ '$'
)
/**
* Matches strings that look like publication pages. Example:
* <pre>Computing Complete Answers to Queries in the Presence of Limited Access Patterns.
* Chen Li. VLDB J. 12(3): 211-227 (2003).</pre>
*
* The string "211-227 (2003)" is not a telephone number.
*/
const PUB_PAGES = /\d{1,5}-+\d{1,5}\s{0,4}\(\d{1,4}/
export default function isValidCandidate(candidate, offset, text, leniency)
{
// Check the candidate doesn't contain any formatting
// which would indicate that it really isn't a phone number.
if (!MATCHING_BRACKETS_ENTIRE.test(candidate) || PUB_PAGES.test(candidate)) {
return
}
// If leniency is set to VALID or stricter, we also want to skip numbers that are surrounded
// by Latin alphabetic characters, to skip cases like abc8005001234 or 8005001234def.
if (leniency !== 'POSSIBLE')
{
// If the candidate is not at the start of the text,
// and does not start with phone-number punctuation,
// check the previous character.
if (offset > 0 && !LEAD_CLASS_LEADING.test(candidate))
{
const previousChar = text[offset - 1]
// We return null if it is a latin letter or an invalid punctuation symbol.
if (isInvalidPunctuationSymbol(previousChar) || isLatinLetter(previousChar)) {
return false
}
}
const lastCharIndex = offset + candidate.length
if (lastCharIndex < text.length)
{
const nextChar = text[lastCharIndex]
if (isInvalidPunctuationSymbol(nextChar) || isLatinLetter(nextChar)) {
return false
}
}
}
return true
}

View File

@@ -0,0 +1,29 @@
// Matches strings that look like dates using "/" as a separator.
// Examples: 3/10/2011, 31/10/96 or 08/31/95.
const SLASH_SEPARATED_DATES = /(?:(?:[0-3]?\d\/[01]?\d)|(?:[01]?\d\/[0-3]?\d))\/(?:[12]\d)?\d{2}/
// Matches timestamps.
// Examples: "2012-01-02 08:00".
// Note that the reg-ex does not include the
// trailing ":\d\d" -- that is covered by TIME_STAMPS_SUFFIX.
const TIME_STAMPS = /[12]\d{3}[-/]?[01]\d[-/]?[0-3]\d +[0-2]\d$/
const TIME_STAMPS_SUFFIX_LEADING = /^:[0-5]\d/
export default function isValidPreCandidate(candidate, offset, text)
{
// Skip a match that is more likely to be a date.
if (SLASH_SEPARATED_DATES.test(candidate)) {
return false
}
// Skip potential time-stamps.
if (TIME_STAMPS.test(candidate))
{
const followingText = text.slice(offset + candidate.length)
if (TIME_STAMPS_SUFFIX_LEADING.test(followingText)) {
return false
}
}
return true
}

View File

@@ -0,0 +1,64 @@
import parsePhoneNumber from '../parsePhoneNumber.js'
/**
* Matches a phone number object against a phone number string.
* @param {string} phoneNumberString
* @param {PhoneNumber} phoneNumber
* @param {object} metadata — Metadata JSON
* @return {'INVALID_NUMBER'|'NO_MATCH'|'SHORT_NSN_MATCH'|'NSN_MATCH'|'EXACT_MATCH'}
*/
export default function matchPhoneNumberStringAgainstPhoneNumber(phoneNumberString, phoneNumber, metadata) {
// Parse `phoneNumberString`.
let phoneNumberStringContainsCallingCode = true
let parsedPhoneNumber = parsePhoneNumber(phoneNumberString, metadata)
if (!parsedPhoneNumber) {
// If `phoneNumberString` didn't contain a country calling code
// then substitute it with the `phoneNumber`'s country calling code.
phoneNumberStringContainsCallingCode = false
parsedPhoneNumber = parsePhoneNumber(phoneNumberString, { defaultCallingCode: phoneNumber.countryCallingCode }, metadata)
}
if (!parsedPhoneNumber) {
return 'INVALID_NUMBER'
}
// Check that the extensions match.
if (phoneNumber.ext) {
if (parsedPhoneNumber.ext !== phoneNumber.ext) {
return 'NO_MATCH'
}
} else {
if (parsedPhoneNumber.ext) {
return 'NO_MATCH'
}
}
// Check that country calling codes match.
if (phoneNumberStringContainsCallingCode) {
if (phoneNumber.countryCallingCode !== parsedPhoneNumber.countryCallingCode) {
return 'NO_MATCH'
}
}
// Check if the whole numbers match.
if (phoneNumber.number === parsedPhoneNumber.number) {
if (phoneNumberStringContainsCallingCode) {
return 'EXACT_MATCH'
} else {
return 'NSN_MATCH'
}
}
// Check if one national number is a "suffix" of the other.
if (
phoneNumber.nationalNumber.indexOf(parsedPhoneNumber.nationalNumber) === 0 ||
parsedPhoneNumber.nationalNumber.indexOf(phoneNumber.nationalNumber) === 0
) {
// "A SHORT_NSN_MATCH occurs if there is a difference because of the
// presence or absence of an 'Italian leading zero', the presence or
// absence of an extension, or one NSN being a shorter variant of the
// other."
return 'SHORT_NSN_MATCH'
}
return 'NO_MATCH'
}

View File

@@ -0,0 +1,19 @@
import { trimAfterFirstMatch } from './util.js'
// Regular expression of characters typically used to start a second phone number for the purposes
// of parsing. This allows us to strip off parts of the number that are actually the start of
// another number, such as for: (530) 583-6985 x302/x2303 -> the second extension here makes this
// actually two phone numbers, (530) 583-6985 x302 and (530) 583-6985 x2303. We remove the second
// extension so that the first number is parsed correctly.
//
// Matches a slash (\ or /) followed by a space followed by an `x`.
//
const SECOND_NUMBER_START_PATTERN = /[\\/] *x/
export default function parsePreCandidate(candidate)
{
// Check for extra numbers at the end.
// TODO: This is the place to start when trying to support extraction of multiple phone number
// from split notations (+41 79 123 45 67 / 68).
return trimAfterFirstMatch(SECOND_NUMBER_START_PATTERN, candidate)
}

View File

@@ -0,0 +1,82 @@
// Javascript doesn't support UTF-8 regular expressions.
// So mimicking them here.
// Copy-pasted from `PhoneNumberMatcher.js`.
/**
* "\p{Z}" is any kind of whitespace or invisible separator ("Separator").
* http://www.regular-expressions.info/unicode.html
* "\P{Z}" is the reverse of "\p{Z}".
* "\p{N}" is any kind of numeric character in any script ("Number").
* "\p{Nd}" is a digit zero through nine in any script except "ideographic scripts" ("Decimal_Digit_Number").
* "\p{Sc}" is a currency symbol ("Currency_Symbol").
* "\p{L}" is any kind of letter from any language ("Letter").
* "\p{Mn}" is "non-spacing mark".
*
* Javascript doesn't support Unicode Regular Expressions
* so substituting it with this explicit set of characters.
*
* https://stackoverflow.com/questions/13210194/javascript-regex-equivalent-of-a-za-z-using-pl
* https://github.com/danielberndt/babel-plugin-utf-8-regex/blob/master/src/transformer.js
*/
const _pZ = '\u0020\u00A0\u1680\u180E\u2000-\u200A\u2028\u2029\u202F\u205F\u3000'
export const pZ = `[${_pZ}]`
export const PZ = `[^${_pZ}]`
export const _pN = '\u0030-\u0039\u00B2\u00B3\u00B9\u00BC-\u00BE\u0660-\u0669\u06F0-\u06F9\u07C0-\u07C9\u0966-\u096F\u09E6-\u09EF\u09F4-\u09F9\u0A66-\u0A6F\u0AE6-\u0AEF\u0B66-\u0B6F\u0B72-\u0B77\u0BE6-\u0BF2\u0C66-\u0C6F\u0C78-\u0C7E\u0CE6-\u0CEF\u0D66-\u0D75\u0E50-\u0E59\u0ED0-\u0ED9\u0F20-\u0F33\u1040-\u1049\u1090-\u1099\u1369-\u137C\u16EE-\u16F0\u17E0-\u17E9\u17F0-\u17F9\u1810-\u1819\u1946-\u194F\u19D0-\u19DA\u1A80-\u1A89\u1A90-\u1A99\u1B50-\u1B59\u1BB0-\u1BB9\u1C40-\u1C49\u1C50-\u1C59\u2070\u2074-\u2079\u2080-\u2089\u2150-\u2182\u2185-\u2189\u2460-\u249B\u24EA-\u24FF\u2776-\u2793\u2CFD\u3007\u3021-\u3029\u3038-\u303A\u3192-\u3195\u3220-\u3229\u3248-\u324F\u3251-\u325F\u3280-\u3289\u32B1-\u32BF\uA620-\uA629\uA6E6-\uA6EF\uA830-\uA835\uA8D0-\uA8D9\uA900-\uA909\uA9D0-\uA9D9\uAA50-\uAA59\uABF0-\uABF9\uFF10-\uFF19'
// const pN = `[${_pN}]`
const _pNd = '\u0030-\u0039\u0660-\u0669\u06F0-\u06F9\u07C0-\u07C9\u0966-\u096F\u09E6-\u09EF\u0A66-\u0A6F\u0AE6-\u0AEF\u0B66-\u0B6F\u0BE6-\u0BEF\u0C66-\u0C6F\u0CE6-\u0CEF\u0D66-\u0D6F\u0E50-\u0E59\u0ED0-\u0ED9\u0F20-\u0F29\u1040-\u1049\u1090-\u1099\u17E0-\u17E9\u1810-\u1819\u1946-\u194F\u19D0-\u19D9\u1A80-\u1A89\u1A90-\u1A99\u1B50-\u1B59\u1BB0-\u1BB9\u1C40-\u1C49\u1C50-\u1C59\uA620-\uA629\uA8D0-\uA8D9\uA900-\uA909\uA9D0-\uA9D9\uAA50-\uAA59\uABF0-\uABF9\uFF10-\uFF19'
export const pNd = `[${_pNd}]`
export const _pL = '\u0041-\u005A\u0061-\u007A\u00AA\u00B5\u00BA\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02C1\u02C6-\u02D1\u02E0-\u02E4\u02EC\u02EE\u0370-\u0374\u0376\u0377\u037A-\u037D\u0386\u0388-\u038A\u038C\u038E-\u03A1\u03A3-\u03F5\u03F7-\u0481\u048A-\u0527\u0531-\u0556\u0559\u0561-\u0587\u05D0-\u05EA\u05F0-\u05F2\u0620-\u064A\u066E\u066F\u0671-\u06D3\u06D5\u06E5\u06E6\u06EE\u06EF\u06FA-\u06FC\u06FF\u0710\u0712-\u072F\u074D-\u07A5\u07B1\u07CA-\u07EA\u07F4\u07F5\u07FA\u0800-\u0815\u081A\u0824\u0828\u0840-\u0858\u08A0\u08A2-\u08AC\u0904-\u0939\u093D\u0950\u0958-\u0961\u0971-\u0977\u0979-\u097F\u0985-\u098C\u098F\u0990\u0993-\u09A8\u09AA-\u09B0\u09B2\u09B6-\u09B9\u09BD\u09CE\u09DC\u09DD\u09DF-\u09E1\u09F0\u09F1\u0A05-\u0A0A\u0A0F\u0A10\u0A13-\u0A28\u0A2A-\u0A30\u0A32\u0A33\u0A35\u0A36\u0A38\u0A39\u0A59-\u0A5C\u0A5E\u0A72-\u0A74\u0A85-\u0A8D\u0A8F-\u0A91\u0A93-\u0AA8\u0AAA-\u0AB0\u0AB2\u0AB3\u0AB5-\u0AB9\u0ABD\u0AD0\u0AE0\u0AE1\u0B05-\u0B0C\u0B0F\u0B10\u0B13-\u0B28\u0B2A-\u0B30\u0B32\u0B33\u0B35-\u0B39\u0B3D\u0B5C\u0B5D\u0B5F-\u0B61\u0B71\u0B83\u0B85-\u0B8A\u0B8E-\u0B90\u0B92-\u0B95\u0B99\u0B9A\u0B9C\u0B9E\u0B9F\u0BA3\u0BA4\u0BA8-\u0BAA\u0BAE-\u0BB9\u0BD0\u0C05-\u0C0C\u0C0E-\u0C10\u0C12-\u0C28\u0C2A-\u0C33\u0C35-\u0C39\u0C3D\u0C58\u0C59\u0C60\u0C61\u0C85-\u0C8C\u0C8E-\u0C90\u0C92-\u0CA8\u0CAA-\u0CB3\u0CB5-\u0CB9\u0CBD\u0CDE\u0CE0\u0CE1\u0CF1\u0CF2\u0D05-\u0D0C\u0D0E-\u0D10\u0D12-\u0D3A\u0D3D\u0D4E\u0D60\u0D61\u0D7A-\u0D7F\u0D85-\u0D96\u0D9A-\u0DB1\u0DB3-\u0DBB\u0DBD\u0DC0-\u0DC6\u0E01-\u0E30\u0E32\u0E33\u0E40-\u0E46\u0E81\u0E82\u0E84\u0E87\u0E88\u0E8A\u0E8D\u0E94-\u0E97\u0E99-\u0E9F\u0EA1-\u0EA3\u0EA5\u0EA7\u0EAA\u0EAB\u0EAD-\u0EB0\u0EB2\u0EB3\u0EBD\u0EC0-\u0EC4\u0EC6\u0EDC-\u0EDF\u0F00\u0F40-\u0F47\u0F49-\u0F6C\u0F88-\u0F8C\u1000-\u102A\u103F\u1050-\u1055\u105A-\u105D\u1061\u1065\u1066\u106E-\u1070\u1075-\u1081\u108E\u10A0-\u10C5\u10C7\u10CD\u10D0-\u10FA\u10FC-\u1248\u124A-\u124D\u1250-\u1256\u1258\u125A-\u125D\u1260-\u1288\u128A-\u128D\u1290-\u12B0\u12B2-\u12B5\u12B8-\u12BE\u12C0\u12C2-\u12C5\u12C8-\u12D6\u12D8-\u1310\u1312-\u1315\u1318-\u135A\u1380-\u138F\u13A0-\u13F4\u1401-\u166C\u166F-\u167F\u1681-\u169A\u16A0-\u16EA\u1700-\u170C\u170E-\u1711\u1720-\u1731\u1740-\u1751\u1760-\u176C\u176E-\u1770\u1780-\u17B3\u17D7\u17DC\u1820-\u1877\u1880-\u18A8\u18AA\u18B0-\u18F5\u1900-\u191C\u1950-\u196D\u1970-\u1974\u1980-\u19AB\u19C1-\u19C7\u1A00-\u1A16\u1A20-\u1A54\u1AA7\u1B05-\u1B33\u1B45-\u1B4B\u1B83-\u1BA0\u1BAE\u1BAF\u1BBA-\u1BE5\u1C00-\u1C23\u1C4D-\u1C4F\u1C5A-\u1C7D\u1CE9-\u1CEC\u1CEE-\u1CF1\u1CF5\u1CF6\u1D00-\u1DBF\u1E00-\u1F15\u1F18-\u1F1D\u1F20-\u1F45\u1F48-\u1F4D\u1F50-\u1F57\u1F59\u1F5B\u1F5D\u1F5F-\u1F7D\u1F80-\u1FB4\u1FB6-\u1FBC\u1FBE\u1FC2-\u1FC4\u1FC6-\u1FCC\u1FD0-\u1FD3\u1FD6-\u1FDB\u1FE0-\u1FEC\u1FF2-\u1FF4\u1FF6-\u1FFC\u2071\u207F\u2090-\u209C\u2102\u2107\u210A-\u2113\u2115\u2119-\u211D\u2124\u2126\u2128\u212A-\u212D\u212F-\u2139\u213C-\u213F\u2145-\u2149\u214E\u2183\u2184\u2C00-\u2C2E\u2C30-\u2C5E\u2C60-\u2CE4\u2CEB-\u2CEE\u2CF2\u2CF3\u2D00-\u2D25\u2D27\u2D2D\u2D30-\u2D67\u2D6F\u2D80-\u2D96\u2DA0-\u2DA6\u2DA8-\u2DAE\u2DB0-\u2DB6\u2DB8-\u2DBE\u2DC0-\u2DC6\u2DC8-\u2DCE\u2DD0-\u2DD6\u2DD8-\u2DDE\u2E2F\u3005\u3006\u3031-\u3035\u303B\u303C\u3041-\u3096\u309D-\u309F\u30A1-\u30FA\u30FC-\u30FF\u3105-\u312D\u3131-\u318E\u31A0-\u31BA\u31F0-\u31FF\u3400-\u4DB5\u4E00-\u9FCC\uA000-\uA48C\uA4D0-\uA4FD\uA500-\uA60C\uA610-\uA61F\uA62A\uA62B\uA640-\uA66E\uA67F-\uA697\uA6A0-\uA6E5\uA717-\uA71F\uA722-\uA788\uA78B-\uA78E\uA790-\uA793\uA7A0-\uA7AA\uA7F8-\uA801\uA803-\uA805\uA807-\uA80A\uA80C-\uA822\uA840-\uA873\uA882-\uA8B3\uA8F2-\uA8F7\uA8FB\uA90A-\uA925\uA930-\uA946\uA960-\uA97C\uA984-\uA9B2\uA9CF\uAA00-\uAA28\uAA40-\uAA42\uAA44-\uAA4B\uAA60-\uAA76\uAA7A\uAA80-\uAAAF\uAAB1\uAAB5\uAAB6\uAAB9-\uAABD\uAAC0\uAAC2\uAADB-\uAADD\uAAE0-\uAAEA\uAAF2-\uAAF4\uAB01-\uAB06\uAB09-\uAB0E\uAB11-\uAB16\uAB20-\uAB26\uAB28-\uAB2E\uABC0-\uABE2\uAC00-\uD7A3\uD7B0-\uD7C6\uD7CB-\uD7FB\uF900-\uFA6D\uFA70-\uFAD9\uFB00-\uFB06\uFB13-\uFB17\uFB1D\uFB1F-\uFB28\uFB2A-\uFB36\uFB38-\uFB3C\uFB3E\uFB40\uFB41\uFB43\uFB44\uFB46-\uFBB1\uFBD3-\uFD3D\uFD50-\uFD8F\uFD92-\uFDC7\uFDF0-\uFDFB\uFE70-\uFE74\uFE76-\uFEFC\uFF21-\uFF3A\uFF41-\uFF5A\uFF66-\uFFBE\uFFC2-\uFFC7\uFFCA-\uFFCF\uFFD2-\uFFD7\uFFDA-\uFFDC'
const pL = `[${_pL}]`
const pL_regexp = new RegExp(pL)
const _pSc = '\u0024\u00A2-\u00A5\u058F\u060B\u09F2\u09F3\u09FB\u0AF1\u0BF9\u0E3F\u17DB\u20A0-\u20B9\uA838\uFDFC\uFE69\uFF04\uFFE0\uFFE1\uFFE5\uFFE6'
const pSc = `[${_pSc}]`
const pSc_regexp = new RegExp(pSc)
const _pMn = '\u0300-\u036F\u0483-\u0487\u0591-\u05BD\u05BF\u05C1\u05C2\u05C4\u05C5\u05C7\u0610-\u061A\u064B-\u065F\u0670\u06D6-\u06DC\u06DF-\u06E4\u06E7\u06E8\u06EA-\u06ED\u0711\u0730-\u074A\u07A6-\u07B0\u07EB-\u07F3\u0816-\u0819\u081B-\u0823\u0825-\u0827\u0829-\u082D\u0859-\u085B\u08E4-\u08FE\u0900-\u0902\u093A\u093C\u0941-\u0948\u094D\u0951-\u0957\u0962\u0963\u0981\u09BC\u09C1-\u09C4\u09CD\u09E2\u09E3\u0A01\u0A02\u0A3C\u0A41\u0A42\u0A47\u0A48\u0A4B-\u0A4D\u0A51\u0A70\u0A71\u0A75\u0A81\u0A82\u0ABC\u0AC1-\u0AC5\u0AC7\u0AC8\u0ACD\u0AE2\u0AE3\u0B01\u0B3C\u0B3F\u0B41-\u0B44\u0B4D\u0B56\u0B62\u0B63\u0B82\u0BC0\u0BCD\u0C3E-\u0C40\u0C46-\u0C48\u0C4A-\u0C4D\u0C55\u0C56\u0C62\u0C63\u0CBC\u0CBF\u0CC6\u0CCC\u0CCD\u0CE2\u0CE3\u0D41-\u0D44\u0D4D\u0D62\u0D63\u0DCA\u0DD2-\u0DD4\u0DD6\u0E31\u0E34-\u0E3A\u0E47-\u0E4E\u0EB1\u0EB4-\u0EB9\u0EBB\u0EBC\u0EC8-\u0ECD\u0F18\u0F19\u0F35\u0F37\u0F39\u0F71-\u0F7E\u0F80-\u0F84\u0F86\u0F87\u0F8D-\u0F97\u0F99-\u0FBC\u0FC6\u102D-\u1030\u1032-\u1037\u1039\u103A\u103D\u103E\u1058\u1059\u105E-\u1060\u1071-\u1074\u1082\u1085\u1086\u108D\u109D\u135D-\u135F\u1712-\u1714\u1732-\u1734\u1752\u1753\u1772\u1773\u17B4\u17B5\u17B7-\u17BD\u17C6\u17C9-\u17D3\u17DD\u180B-\u180D\u18A9\u1920-\u1922\u1927\u1928\u1932\u1939-\u193B\u1A17\u1A18\u1A56\u1A58-\u1A5E\u1A60\u1A62\u1A65-\u1A6C\u1A73-\u1A7C\u1A7F\u1B00-\u1B03\u1B34\u1B36-\u1B3A\u1B3C\u1B42\u1B6B-\u1B73\u1B80\u1B81\u1BA2-\u1BA5\u1BA8\u1BA9\u1BAB\u1BE6\u1BE8\u1BE9\u1BED\u1BEF-\u1BF1\u1C2C-\u1C33\u1C36\u1C37\u1CD0-\u1CD2\u1CD4-\u1CE0\u1CE2-\u1CE8\u1CED\u1CF4\u1DC0-\u1DE6\u1DFC-\u1DFF\u20D0-\u20DC\u20E1\u20E5-\u20F0\u2CEF-\u2CF1\u2D7F\u2DE0-\u2DFF\u302A-\u302D\u3099\u309A\uA66F\uA674-\uA67D\uA69F\uA6F0\uA6F1\uA802\uA806\uA80B\uA825\uA826\uA8C4\uA8E0-\uA8F1\uA926-\uA92D\uA947-\uA951\uA980-\uA982\uA9B3\uA9B6-\uA9B9\uA9BC\uAA29-\uAA2E\uAA31\uAA32\uAA35\uAA36\uAA43\uAA4C\uAAB0\uAAB2-\uAAB4\uAAB7\uAAB8\uAABE\uAABF\uAAC1\uAAEC\uAAED\uAAF6\uABE5\uABE8\uABED\uFB1E\uFE00-\uFE0F\uFE20-\uFE26'
const pMn = `[${_pMn}]`
const pMn_regexp = new RegExp(pMn)
const _InBasic_Latin = '\u0000-\u007F'
const _InLatin_1_Supplement = '\u0080-\u00FF'
const _InLatin_Extended_A = '\u0100-\u017F'
const _InLatin_Extended_Additional = '\u1E00-\u1EFF'
const _InLatin_Extended_B = '\u0180-\u024F'
const _InCombining_Diacritical_Marks = '\u0300-\u036F'
const latinLetterRegexp = new RegExp
(
'[' +
_InBasic_Latin +
_InLatin_1_Supplement +
_InLatin_Extended_A +
_InLatin_Extended_Additional +
_InLatin_Extended_B +
_InCombining_Diacritical_Marks +
']'
)
/**
* Helper method to determine if a character is a Latin-script letter or not.
* For our purposes, combining marks should also return true since we assume
* they have been added to a preceding Latin character.
*/
export function isLatinLetter(letter)
{
// Combining marks are a subset of non-spacing-mark.
if (!pL_regexp.test(letter) && !pMn_regexp.test(letter)) {
return false
}
return latinLetterRegexp.test(letter)
}
export function isInvalidPunctuationSymbol(character)
{
return character === '%' || pSc_regexp.test(character)
}

View File

@@ -0,0 +1,33 @@
/** Returns a regular expression quantifier with an upper and lower limit. */
export function limit(lower, upper)
{
if ((lower < 0) || (upper <= 0) || (upper < lower)) {
throw new TypeError()
}
return `{${lower},${upper}}`
}
/**
* Trims away any characters after the first match of {@code pattern} in {@code candidate},
* returning the trimmed version.
*/
export function trimAfterFirstMatch(regexp, string)
{
const index = string.search(regexp)
if (index >= 0) {
return string.slice(0, index)
}
return string
}
export function startsWith(string, substring)
{
return string.indexOf(substring) === 0
}
export function endsWith(string, substring)
{
return string.indexOf(substring, string.length - substring.length) === string.length - substring.length
}

View File

@@ -0,0 +1,39 @@
import {
limit,
trimAfterFirstMatch,
startsWith,
endsWith
} from './util.js'
describe('findNumbers/util', () =>
{
it('should generate regexp limit', () =>
{
let thrower = () => limit(1, 0)
thrower.should.throw()
thrower = () => limit(-1, 1)
thrower.should.throw()
thrower = () => limit(0, 0)
thrower.should.throw()
})
it('should trimAfterFirstMatch', () =>
{
trimAfterFirstMatch(/\d/, 'abc123').should.equal('abc')
trimAfterFirstMatch(/\d/, 'abc').should.equal('abc')
})
it('should determine if a string starts with a substring', () =>
{
startsWith('𐍈123', '𐍈').should.equal(true)
startsWith('1𐍈', '𐍈').should.equal(false)
})
it('should determine if a string ends with a substring', () =>
{
endsWith('123𐍈', '𐍈').should.equal(true)
endsWith('𐍈1', '𐍈').should.equal(false)
})
})

View File

@@ -0,0 +1,12 @@
import PhoneNumberMatcher from './PhoneNumberMatcher.js'
import normalizeArguments from './normalizeArguments.js'
export default function findPhoneNumbersInText() {
const { text, options, metadata } = normalizeArguments(arguments)
const matcher = new PhoneNumberMatcher(text, { ...options, v2: true }, metadata)
const results = []
while (matcher.hasNext()) {
results.push(matcher.next())
}
return results
}

View File

@@ -0,0 +1,268 @@
import findPhoneNumbersInText from './findPhoneNumbersInText.js'
import metadata from '../metadata.min.json' assert { type: 'json' }
import metadataMax from '../metadata.max.json' assert { type: 'json' }
function findPhoneNumbersInTextWithResults(input, options, metadata) {
const results = findPhoneNumbersInText(input, options, metadata)
return results.map((result) => {
const { startsAt, endsAt, number } = result
const data = {
phone: number.nationalNumber,
startsAt,
endsAt
}
if (number.country) {
data.country = number.country
}
if (number.ext) {
data.ext = number.ext
}
return data
})
}
describe('findPhoneNumbersInText', () => {
it('should find phone numbers in text (with default country)', () => {
findPhoneNumbersInText('+7 (800) 555-35-35', 'US', metadata)[0].number.number.should.equal('+78005553535')
})
it('should find phone numbers in text (with default country in options)', () => {
findPhoneNumbersInText('+7 (800) 555-35-35', { defaultCountry: 'US' }, metadata)[0].number.number.should.equal('+78005553535')
})
it('should find phone numbers in text (with default country and options)', () => {
findPhoneNumbersInText('+7 (800) 555-35-35', 'US', {}, metadata)[0].number.number.should.equal('+78005553535')
})
it('should find phone numbers in text (without default country, with options)', () => {
findPhoneNumbersInText('+7 (800) 555-35-35', undefined, {}, metadata)[0].number.number.should.equal('+78005553535')
})
it('should find phone numbers in text (with default country, without options)', () => {
findPhoneNumbersInText('+7 (800) 555-35-35', 'US', undefined, metadata)[0].number.number.should.equal('+78005553535')
})
it('should find phone numbers in text (with empty default country)', () => {
findPhoneNumbersInText('+7 (800) 555-35-35', undefined, metadata)[0].number.number.should.equal('+78005553535')
})
it('should find phone numbers in text', () => {
const NUMBERS = ['+78005553535', '+12133734253']
const results = findPhoneNumbersInText('The number is +7 (800) 555-35-35 and not (213) 373-4253 as written in the document.', metadata)
let i = 0
while (i < results.length) {
results[i].number.number.should.equal(NUMBERS[i])
i++
}
})
it('should find phone numbers in text (default country calling code)', () => {
const NUMBERS = ['+870773111632']
const results = findPhoneNumbersInText('The number is 773 111 632', { defaultCallingCode: '870' }, metadata)
let i = 0
while (i < results.length) {
results[i].number.number.should.equal(NUMBERS[i])
i++
}
})
it('should find numbers', () => {
findPhoneNumbersInTextWithResults('2133734253', { defaultCountry: 'US' }, metadata).should.deep.equal([{
phone : '2133734253',
country : 'US',
startsAt : 0,
endsAt : 10
}])
findPhoneNumbersInTextWithResults('(213) 373-4253', { defaultCountry: 'US' }, metadata).should.deep.equal([{
phone : '2133734253',
country : 'US',
startsAt : 0,
endsAt : 14
}])
findPhoneNumbersInTextWithResults('The number is +7 (800) 555-35-35 and not (213) 373-4253 as written in the document.', { defaultCountry: 'US' }, metadata).should.deep.equal([{
phone : '8005553535',
country : 'RU',
startsAt : 14,
endsAt : 32
}, {
phone : '2133734253',
country : 'US',
startsAt : 41,
endsAt : 55
}])
// Opening parenthesis issue.
// https://github.com/catamphetamine/libphonenumber-js/issues/252
findPhoneNumbersInTextWithResults('The number is +7 (800) 555-35-35 and not (213) 373-4253 (that\'s not even in the same country!) as written in the document.', { defaultCountry: 'US' }, metadata).should.deep.equal([{
phone : '8005553535',
country : 'RU',
startsAt : 14,
endsAt : 32
}, {
phone : '2133734253',
country : 'US',
startsAt : 41,
endsAt : 55
}])
// No default country.
findPhoneNumbersInTextWithResults('The number is +7 (800) 555-35-35 as written in the document.', undefined, metadata).should.deep.equal([{
phone : '8005553535',
country : 'RU',
startsAt : 14,
endsAt : 32
}])
// Passing `options` and default country.
findPhoneNumbersInTextWithResults('The number is +7 (800) 555-35-35 as written in the document.', { defaultCountry: 'US', leniency: 'VALID' }, metadata).should.deep.equal([{
phone : '8005553535',
country : 'RU',
startsAt : 14,
endsAt : 32
}])
// Passing `options`.
findPhoneNumbersInTextWithResults('The number is +7 (800) 555-35-35 as written in the document.', { leniency: 'VALID' }, metadata).should.deep.equal([{
phone : '8005553535',
country : 'RU',
startsAt : 14,
endsAt : 32
}])
// Not a phone number and a phone number.
findPhoneNumbersInTextWithResults('Digits 12 are not a number, but +7 (800) 555-35-35 is.', { leniency: 'VALID' }, metadata).should.deep.equal([{
phone : '8005553535',
country : 'RU',
startsAt : 32,
endsAt : 50
}])
// Phone number extension.
findPhoneNumbersInTextWithResults('Date 02/17/2018 is not a number, but +7 (800) 555-35-35 ext. 123 is.', { leniency: 'VALID' }, metadata).should.deep.equal([{
phone : '8005553535',
country : 'RU',
ext : '123',
startsAt : 37,
endsAt : 64
}])
})
it('should find numbers (v2)', () => {
const phoneNumbers = findPhoneNumbersInText('The number is +7 (800) 555-35-35 ext. 1234 and not (213) 373-4253 as written in the document.', { defaultCountry: 'US', v2: true }, metadata)
phoneNumbers.length.should.equal(2)
phoneNumbers[0].startsAt.should.equal(14)
phoneNumbers[0].endsAt.should.equal(42)
phoneNumbers[0].number.number.should.equal('+78005553535')
phoneNumbers[0].number.nationalNumber.should.equal('8005553535')
phoneNumbers[0].number.country.should.equal('RU')
phoneNumbers[0].number.countryCallingCode.should.equal('7')
phoneNumbers[0].number.ext.should.equal('1234')
phoneNumbers[1].startsAt.should.equal(51)
phoneNumbers[1].endsAt.should.equal(65)
phoneNumbers[1].number.number.should.equal('+12133734253')
phoneNumbers[1].number.nationalNumber.should.equal('2133734253')
phoneNumbers[1].number.country.should.equal('US')
phoneNumbers[1].number.countryCallingCode.should.equal('1')
})
it('shouldn\'t find non-valid numbers', () => {
// Not a valid phone number for US.
findPhoneNumbersInTextWithResults('1111111111', { defaultCountry: 'US' }, metadata).should.deep.equal([])
})
it('should find non-European digits', () => {
// E.g. in Iraq they don't write `+442323234` but rather `+٤٤٢٣٢٣٢٣٤`.
findPhoneNumbersInTextWithResults('العَرَبِيَّة‎ +٤٤٣٣٣٣٣٣٣٣٣٣عَرَبِيّ‎', undefined, metadata).should.deep.equal([{
country : 'GB',
phone : '3333333333',
startsAt : 14,
endsAt : 27
}])
})
it('should work in edge cases', () => {
let thrower
// No input
findPhoneNumbersInTextWithResults('', undefined, metadata).should.deep.equal([])
// // No country metadata for this `require` country code
// thrower = () => findPhoneNumbersInTextWithResults('123', { defaultCountry: 'ZZ' }, metadata)
// thrower.should.throw('Unknown country')
// Numerical `value`
thrower = () => findPhoneNumbersInTextWithResults(2141111111, { defaultCountry: 'US' })
thrower.should.throw('A text for parsing must be a string.')
// // No metadata
// thrower = () => findPhoneNumbersInTextWithResults('')
// thrower.should.throw('`metadata` argument not passed')
// No metadata, no default country, no phone numbers.
findPhoneNumbersInTextWithResults('').should.deep.equal([])
})
it('should find international numbers when passed a non-existent default country', () => {
const numbers = findPhoneNumbersInText('Phone: +7 (800) 555 35 35. National: 8 (800) 555-55-55', { defaultCountry: 'XX', v2: true }, metadata)
numbers.length.should.equal(1)
numbers[0].number.nationalNumber.should.equal('8005553535')
})
it('shouldn\'t find phone numbers which are not phone numbers', () => {
// A timestamp.
findPhoneNumbersInTextWithResults('2012-01-02 08:00', { defaultCountry: 'US' }, metadata).should.deep.equal([])
// A valid number (not a complete timestamp).
findPhoneNumbersInTextWithResults('2012-01-02 08', { defaultCountry: 'US' }, metadata).should.deep.equal([{
country : 'US',
phone : '2012010208',
startsAt : 0,
endsAt : 13
}])
// Invalid parens.
findPhoneNumbersInTextWithResults('213(3734253', { defaultCountry: 'US' }, metadata).should.deep.equal([])
// Letters after phone number.
findPhoneNumbersInTextWithResults('2133734253a', { defaultCountry: 'US' }, metadata).should.deep.equal([])
// Valid phone (same as the one found in the UUID below).
findPhoneNumbersInTextWithResults('The phone number is 231354125.', { defaultCountry: 'FR' }, metadata).should.deep.equal([{
country : 'FR',
phone : '231354125',
startsAt : 20,
endsAt : 29
}])
// Not a phone number (part of a UUID).
// Should parse in `{ extended: true }` mode.
const possibleNumbers = findPhoneNumbersInTextWithResults('The UUID is CA801c26f98cd16e231354125ad046e40b.', { defaultCountry: 'FR', extended: true }, metadata)
possibleNumbers.length.should.equal(1)
possibleNumbers[0].country.should.equal('FR')
possibleNumbers[0].phone.should.equal('231354125')
// Not a phone number (part of a UUID).
// Shouldn't parse by default.
findPhoneNumbersInTextWithResults('The UUID is CA801c26f98cd16e231354125ad046e40b.', { defaultCountry: 'FR' }, metadata).should.deep.equal([])
})
// https://gitlab.com/catamphetamine/libphonenumber-js/-/merge_requests/4
it('should return correct `startsAt` and `endsAt` when matching "inner" candidates in a could-be-a-candidate substring', () => {
findPhoneNumbersInTextWithResults('39945926 77200596 16533084', { defaultCountry: 'ID' }, metadataMax)
.should
.deep
.equal([{
country: 'ID',
phone: '77200596',
startsAt: 9,
endsAt: 17
}])
})
})

186
backend/node_modules/libphonenumber-js/source/format.js generated vendored Normal file
View File

@@ -0,0 +1,186 @@
// This is a port of Google Android `libphonenumber`'s
// `phonenumberutil.js` of December 31th, 2018.
//
// https://github.com/googlei18n/libphonenumber/commits/master/javascript/i18n/phonenumbers/phonenumberutil.js
import matchesEntirely from './helpers/matchesEntirely.js'
import formatNationalNumberUsingFormat from './helpers/formatNationalNumberUsingFormat.js'
import Metadata, { getCountryCallingCode } from './metadata.js'
import getIddPrefix from './helpers/getIddPrefix.js'
import { formatRFC3966 } from './helpers/RFC3966.js'
const DEFAULT_OPTIONS = {
formatExtension: (formattedNumber, extension, metadata) => `${formattedNumber}${metadata.ext()}${extension}`
}
/**
* Formats a phone number.
*
* format(phoneNumberInstance, 'INTERNATIONAL', { ..., v2: true }, metadata)
* format(phoneNumberInstance, 'NATIONAL', { ..., v2: true }, metadata)
*
* format({ phone: '8005553535', country: 'RU' }, 'INTERNATIONAL', { ... }, metadata)
* format({ phone: '8005553535', country: 'RU' }, 'NATIONAL', undefined, metadata)
*
* @param {object|PhoneNumber} input — If `options.v2: true` flag is passed, the `input` should be a `PhoneNumber` instance. Otherwise, it should be an object of shape `{ phone: '...', country: '...' }`.
* @param {string} format
* @param {object} [options]
* @param {object} metadata
* @return {string}
*/
export default function formatNumber(input, format, options, metadata) {
// Apply default options.
if (options) {
options = { ...DEFAULT_OPTIONS, ...options }
} else {
options = DEFAULT_OPTIONS
}
metadata = new Metadata(metadata)
if (input.country && input.country !== '001') {
// Validate `input.country`.
if (!metadata.hasCountry(input.country)) {
throw new Error(`Unknown country: ${input.country}`)
}
metadata.country(input.country)
}
else if (input.countryCallingCode) {
metadata.selectNumberingPlan(input.countryCallingCode)
}
else return input.phone || ''
const countryCallingCode = metadata.countryCallingCode()
const nationalNumber = options.v2 ? input.nationalNumber : input.phone
// This variable should have been declared inside `case`s
// but Babel has a bug and it says "duplicate variable declaration".
let number
switch (format) {
case 'NATIONAL':
// Legacy argument support.
// (`{ country: ..., phone: '' }`)
if (!nationalNumber) {
return ''
}
number = formatNationalNumber(nationalNumber, input.carrierCode, 'NATIONAL', metadata, options)
return addExtension(number, input.ext, metadata, options.formatExtension)
case 'INTERNATIONAL':
// Legacy argument support.
// (`{ country: ..., phone: '' }`)
if (!nationalNumber) {
return `+${countryCallingCode}`
}
number = formatNationalNumber(nationalNumber, null, 'INTERNATIONAL', metadata, options)
number = `+${countryCallingCode} ${number}`
return addExtension(number, input.ext, metadata, options.formatExtension)
case 'E.164':
// `E.164` doesn't define "phone number extensions".
return `+${countryCallingCode}${nationalNumber}`
case 'RFC3966':
return formatRFC3966({
number: `+${countryCallingCode}${nationalNumber}`,
ext: input.ext
})
// For reference, here's Google's IDD formatter:
// https://github.com/google/libphonenumber/blob/32719cf74e68796788d1ca45abc85dcdc63ba5b9/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java#L1546
// Not saying that this IDD formatter replicates it 1:1, but it seems to work.
// Who would even need to format phone numbers in IDD format anyway?
case 'IDD':
if (!options.fromCountry) {
return
// throw new Error('`fromCountry` option not passed for IDD-prefixed formatting.')
}
const formattedNumber = formatIDD(
nationalNumber,
input.carrierCode,
countryCallingCode,
options.fromCountry,
metadata
)
return addExtension(formattedNumber, input.ext, metadata, options.formatExtension)
default:
throw new Error(`Unknown "format" argument passed to "formatNumber()": "${format}"`)
}
}
function formatNationalNumber(number, carrierCode, formatAs, metadata, options) {
const format = chooseFormatForNumber(metadata.formats(), number)
if (!format) {
return number
}
return formatNationalNumberUsingFormat(
number,
format,
{
useInternationalFormat: formatAs === 'INTERNATIONAL',
withNationalPrefix: format.nationalPrefixIsOptionalWhenFormattingInNationalFormat() && (options && options.nationalPrefix === false) ? false : true,
carrierCode,
metadata
}
)
}
export function chooseFormatForNumber(availableFormats, nationalNnumber) {
for (const format of availableFormats) {
// Validate leading digits.
// The test case for "else path" could be found by searching for
// "format.leadingDigitsPatterns().length === 0".
if (format.leadingDigitsPatterns().length > 0) {
// The last leading_digits_pattern is used here, as it is the most detailed
const lastLeadingDigitsPattern = format.leadingDigitsPatterns()[format.leadingDigitsPatterns().length - 1]
// If leading digits don't match then move on to the next phone number format
if (nationalNnumber.search(lastLeadingDigitsPattern) !== 0) {
continue
}
}
// Check that the national number matches the phone number format regular expression
if (matchesEntirely(nationalNnumber, format.pattern())) {
return format
}
}
}
function addExtension(formattedNumber, ext, metadata, formatExtension) {
return ext ? formatExtension(formattedNumber, ext, metadata) : formattedNumber
}
function formatIDD(
nationalNumber,
carrierCode,
countryCallingCode,
fromCountry,
metadata
) {
const fromCountryCallingCode = getCountryCallingCode(fromCountry, metadata.metadata)
// When calling within the same country calling code.
if (fromCountryCallingCode === countryCallingCode) {
const formattedNumber = formatNationalNumber(nationalNumber, carrierCode, 'NATIONAL', metadata)
// For NANPA regions, return the national format for these regions
// but prefix it with the country calling code.
if (countryCallingCode === '1') {
return countryCallingCode + ' ' + formattedNumber
}
// If regions share a country calling code, the country calling code need
// not be dialled. This also applies when dialling within a region, so this
// if clause covers both these cases. Technically this is the case for
// dialling from La Reunion to other overseas departments of France (French
// Guiana, Martinique, Guadeloupe), but not vice versa - so we don't cover
// this edge case for now and for those cases return the version including
// country calling code. Details here:
// http://www.petitfute.com/voyage/225-info-pratiques-reunion
//
return formattedNumber
}
const iddPrefix = getIddPrefix(fromCountry, undefined, metadata.metadata)
if (iddPrefix) {
return `${iddPrefix} ${countryCallingCode} ${formatNationalNumber(nationalNumber, null, 'INTERNATIONAL', metadata)}`
}
}

View File

@@ -0,0 +1,265 @@
import metadata from '../metadata.min.json' assert { type: 'json' }
import formatNumber_ from './format.js'
import parsePhoneNumber from './parsePhoneNumber.js'
function formatNumber(...parameters) {
let v2
if (parameters.length < 1) {
// `input` parameter.
parameters.push(undefined)
} else {
// Convert string `input` to a `PhoneNumber` instance.
if (typeof parameters[0] === 'string') {
v2 = true
parameters[0] = parsePhoneNumber(parameters[0], {
...parameters[2],
extract: false
}, metadata)
}
}
if (parameters.length < 2) {
// `format` parameter.
parameters.push(undefined)
}
if (parameters.length < 3) {
// `options` parameter.
parameters.push(undefined)
}
// Set `v2` flag.
parameters[2] = {
v2,
...parameters[2]
}
// Add `metadata` parameter.
parameters.push(metadata)
// Call the function.
return formatNumber_.apply(this, parameters)
}
describe('format', () => {
it('should work with the first argument being a E.164 number', () => {
formatNumber('+12133734253', 'NATIONAL').should.equal('(213) 373-4253')
formatNumber('+12133734253', 'INTERNATIONAL').should.equal('+1 213 373 4253')
// Invalid number.
formatNumber('+12111111111', 'NATIONAL').should.equal('(211) 111-1111')
// Formatting invalid E.164 numbers.
formatNumber('+11111', 'INTERNATIONAL').should.equal('+1 1111')
formatNumber('+11111', 'NATIONAL').should.equal('1111')
})
it('should work with the first object argument expanded', () => {
formatNumber('2133734253', 'NATIONAL', { defaultCountry: 'US' }).should.equal('(213) 373-4253')
formatNumber('2133734253', 'INTERNATIONAL', { defaultCountry: 'US' }).should.equal('+1 213 373 4253')
})
it('should format using formats with no leading digits (`format.leadingDigitsPatterns().length === 0`)', () => {
formatNumber({ phone: '12345678901', countryCallingCode: 888 }, 'INTERNATIONAL').should.equal('+888 123 456 78901')
})
it('should sort out the arguments', () => {
const options = {
formatExtension: (number, extension) => `${number} доб. ${extension}`
}
formatNumber({
phone : '8005553535',
country : 'RU',
ext : '123'
},
'NATIONAL', options).should.equal('8 (800) 555-35-35 доб. 123')
// Parse number from string.
formatNumber('+78005553535', 'NATIONAL', options).should.equal('8 (800) 555-35-35')
formatNumber('8005553535', 'NATIONAL', { ...options, defaultCountry: 'RU' }).should.equal('8 (800) 555-35-35')
})
it('should format with national prefix when specifically instructed', () => {
// With national prefix.
formatNumber('88005553535', 'NATIONAL', { defaultCountry: 'RU' }).should.equal('8 (800) 555-35-35')
// Without national prefix via an explicitly set option.
formatNumber('88005553535', 'NATIONAL', { nationalPrefix: false, defaultCountry: 'RU' }).should.equal('800 555-35-35')
})
it('should format valid phone numbers', () => {
// Switzerland
formatNumber({ country: 'CH', phone: '446681800' }, 'INTERNATIONAL').should.equal('+41 44 668 18 00')
formatNumber({ country: 'CH', phone: '446681800' }, 'E.164').should.equal('+41446681800')
formatNumber({ country: 'CH', phone: '446681800' }, 'RFC3966').should.equal('tel:+41446681800')
formatNumber({ country: 'CH', phone: '446681800' }, 'NATIONAL').should.equal('044 668 18 00')
// France
formatNumber({ country: 'FR', phone: '169454850' }, 'NATIONAL').should.equal('01 69 45 48 50')
// Kazakhstan
formatNumber('+7 702 211 1111', 'NATIONAL').should.deep.equal('8 (702) 211 1111')
})
it('should format national numbers with national prefix even if it\'s optional', () => {
// Russia
formatNumber({ country: 'RU', phone: '9991234567' }, 'NATIONAL').should.equal('8 (999) 123-45-67')
})
it('should work in edge cases', () => {
let thrower
// // No phone number
// formatNumber('', 'INTERNATIONAL', { defaultCountry: 'RU' }).should.equal('')
// formatNumber('', 'NATIONAL', { defaultCountry: 'RU' }).should.equal('')
formatNumber({ country: 'RU', phone: '' }, 'INTERNATIONAL').should.equal('+7')
formatNumber({ country: 'RU', phone: '' }, 'NATIONAL').should.equal('')
// No suitable format
formatNumber('+121337342530', 'NATIONAL', { defaultCountry: 'US' }).should.equal('21337342530')
// No suitable format (leading digits mismatch)
formatNumber('28199999', 'NATIONAL', { defaultCountry: 'AD' }).should.equal('28199999')
// // Numerical `value`
// thrower = () => formatNumber(89150000000, 'NATIONAL', { defaultCountry: 'RU' })
// thrower.should.throw('A phone number must either be a string or an object of shape { phone, [country] }.')
// // No metadata for country
// expect(() => formatNumber('+121337342530', 'NATIONAL', { defaultCountry: 'USA' })).to.throw('Unknown country')
// expect(() => formatNumber('21337342530', 'NATIONAL', { defaultCountry: 'USA' })).to.throw('Unknown country')
// No format type
thrower = () => formatNumber('+123')
thrower.should.throw('Unknown "format" argument')
// Unknown format type
thrower = () => formatNumber('123', 'Gay', { defaultCountry: 'US' })
thrower.should.throw('Unknown "format" argument')
// // No metadata
// thrower = () => _formatNumber('123', 'E.164', { defaultCountry: 'RU' })
// thrower.should.throw('`metadata`')
// No formats
formatNumber('012345', 'NATIONAL', { defaultCountry: 'AC' }).should.equal('012345')
// No `fromCountry` for `IDD` format.
expect(formatNumber('+78005553535', 'IDD')).to.be.undefined
// `fromCountry` has no default IDD prefix.
expect(formatNumber('+78005553535', 'IDD', { fromCountry: 'BO' })).to.be.undefined
// No such country.
expect(() => formatNumber({ phone: '123', country: 'USA' }, 'NATIONAL')).to.throw('Unknown country')
})
it('should format phone number extensions', () => {
// National
formatNumber({
country: 'US',
phone: '2133734253',
ext: '123'
},
'NATIONAL').should.equal('(213) 373-4253 ext. 123')
// International
formatNumber({
country : 'US',
phone : '2133734253',
ext : '123'
},
'INTERNATIONAL').should.equal('+1 213 373 4253 ext. 123')
// International
formatNumber({
country : 'US',
phone : '2133734253',
ext : '123'
},
'INTERNATIONAL').should.equal('+1 213 373 4253 ext. 123')
// E.164
formatNumber({
country : 'US',
phone : '2133734253',
ext : '123'
},
'E.164').should.equal('+12133734253')
// RFC3966
formatNumber({
country : 'US',
phone : '2133734253',
ext : '123'
},
'RFC3966').should.equal('tel:+12133734253;ext=123')
// Custom ext prefix.
formatNumber({
country : 'GB',
phone : '7912345678',
ext : '123'
},
'INTERNATIONAL').should.equal('+44 7912 345678 x123')
})
it('should work with Argentina numbers', () => {
// The same mobile number is written differently
// in different formats in Argentina:
// `9` gets prepended in international format.
formatNumber({ country: 'AR', phone: '3435551212' }, 'INTERNATIONAL')
.should.equal('+54 3435 55 1212')
formatNumber({ country: 'AR', phone: '3435551212' }, 'NATIONAL')
.should.equal('03435 55-1212')
})
it('should work with Mexico numbers', () => {
// Fixed line.
formatNumber({ country: 'MX', phone: '4499780001' }, 'INTERNATIONAL')
.should.equal('+52 449 978 0001')
formatNumber({ country: 'MX', phone: '4499780001' }, 'NATIONAL')
.should.equal('449 978 0001')
// or '(449)978-0001'.
// Mobile.
// `1` is prepended before area code to mobile numbers in international format.
formatNumber({ country: 'MX', phone: '3312345678' }, 'INTERNATIONAL')
.should.equal('+52 33 1234 5678')
formatNumber({ country: 'MX', phone: '3312345678' }, 'NATIONAL')
.should.equal('33 1234 5678')
// or '045 33 1234-5678'.
})
it('should format possible numbers', () => {
formatNumber({ countryCallingCode: '7', phone: '1111111111' }, 'E.164')
.should.equal('+71111111111')
formatNumber({ countryCallingCode: '7', phone: '1111111111' }, 'NATIONAL')
.should.equal('1111111111')
formatNumber({ countryCallingCode: '7', phone: '1111111111' }, 'INTERNATIONAL')
.should.equal('+7 1111111111')
})
it('should format IDD-prefixed number', () => {
// No `fromCountry`.
expect(formatNumber('+78005553535', 'IDD')).to.be.undefined
// No default IDD prefix.
expect(formatNumber('+78005553535', 'IDD', { fromCountry: 'BO' })).to.be.undefined
// Same country calling code.
formatNumber('+12133734253', 'IDD', { fromCountry: 'CA', humanReadable: true }).should.equal('1 (213) 373-4253')
formatNumber('+78005553535', 'IDD', { fromCountry: 'KZ', humanReadable: true }).should.equal('8 (800) 555-35-35')
// formatNumber('+78005553535', 'IDD', { fromCountry: 'US' }).should.equal('01178005553535')
formatNumber('+78005553535', 'IDD', { fromCountry: 'US', humanReadable: true }).should.equal('011 7 800 555 35 35')
})
it('should format non-geographic numbering plan phone numbers', () => {
// https://github.com/catamphetamine/libphonenumber-js/issues/323
formatNumber('+870773111632', 'INTERNATIONAL').should.equal('+870 773 111 632')
formatNumber('+870773111632', 'NATIONAL').should.equal('773 111 632')
})
it('should use the default IDD prefix when formatting a phone number', () => {
// Testing preferred international prefixes with ~ are supported.
// ("~" designates waiting on a line until proceeding with the input).
formatNumber('+390236618300', 'IDD', { fromCountry: 'BY' }).should.equal('8~10 39 02 3661 8300')
})
})

View File

@@ -0,0 +1,17 @@
import AsYouType from './AsYouType.js'
/**
* Formats a (possibly incomplete) phone number.
* The phone number can be either in E.164 format
* or in a form of national number digits.
* @param {string} value - A possibly incomplete phone number. Either in E.164 format or in a form of national number digits.
* @param {string|object} [optionsOrDefaultCountry] - A two-letter ("ISO 3166-1 alpha-2") country code, or an object of shape `{ defaultCountry?: string, defaultCallingCode?: string }`.
* @return {string} Formatted (possibly incomplete) phone number.
*/
export default function formatIncompletePhoneNumber(value, optionsOrDefaultCountry, metadata) {
if (!metadata) {
metadata = optionsOrDefaultCountry
optionsOrDefaultCountry = undefined
}
return new AsYouType(optionsOrDefaultCountry, metadata).input(value)
}

View File

@@ -0,0 +1,26 @@
import formatIncompletePhoneNumber from './formatIncompletePhoneNumber.js'
import metadata from '../metadata.min.json' assert { type: 'json' }
describe('formatIncompletePhoneNumber', () => {
it('should format parsed input value', () => {
let result
// National input.
formatIncompletePhoneNumber('880055535', 'RU', metadata).should.equal('8 (800) 555-35')
// International input, no country.
formatIncompletePhoneNumber('+780055535', null, metadata).should.equal('+7 800 555 35')
// International input, no country argument.
formatIncompletePhoneNumber('+780055535', metadata).should.equal('+7 800 555 35')
// International input, with country.
formatIncompletePhoneNumber('+780055535', 'RU', metadata).should.equal('+7 800 555 35')
})
it('should support an object argument', () => {
formatIncompletePhoneNumber('880055535', { defaultCountry: 'RU' }, metadata).should.equal('8 (800) 555-35')
formatIncompletePhoneNumber('880055535', { defaultCallingCode: '7' }, metadata).should.equal('8 (800) 555-35')
})
})

View File

@@ -0,0 +1,209 @@
// This function is copy-pasted from
// https://github.com/googlei18n/libphonenumber/blob/master/javascript/i18n/phonenumbers/phonenumberutil.js
// It hasn't been tested. It's not currently exported.
// Carriers codes aren't part of this library.
// Send a PR if you want to add them.
import Metadata from './metadata.js'
import format from './format.js'
import getNumberType from './helpers/getNumberType.js'
import checkNumberLength from './helpers/checkNumberLength.js'
import getCountryCallingCode from './getCountryCallingCode.js'
const REGION_CODE_FOR_NON_GEO_ENTITY = '001'
/**
* Returns a number formatted in such a way that it can be dialed from a mobile
* phone in a specific region. If the number cannot be reached from the region
* (e.g. some countries block toll-free numbers from being called outside of the
* country), the method returns an empty string.
*
* @param {object} number - a `parse()`d phone number to be formatted.
* @param {string} from_country - the region where the call is being placed.
* @param {boolean} with_formatting - whether the number should be returned with
* formatting symbols, such as spaces and dashes.
* @return {string}
*/
export default function(number, from_country, with_formatting, metadata) {
metadata = new Metadata(metadata)
// Validate `from_country`.
if (!metadata.hasCountry(from_country)) {
throw new Error(`Unknown country: ${from_country}`)
}
// Not using the extension, as that part cannot normally be dialed
// together with the main number.
number = {
phone: number.phone,
country: number.country
}
const number_type = getNumberType(number, undefined, metadata.metadata)
const is_valid_number = number_type === number
let formatted_number
if (country === from_country) {
const is_fixed_line_or_mobile =
number_type === 'FIXED_LINE' ||
number_type === 'MOBILE' ||
number_type === 'FIXED_LINE_OR_MOBILE'
// Carrier codes may be needed in some countries. We handle this here.
if (country == 'BR' && is_fixed_line_or_mobile) {
formatted_number =
carrierCode ?
formatNationalNumberWithPreferredCarrierCode(number) :
// Brazilian fixed line and mobile numbers need to be dialed with a
// carrier code when called within Brazil. Without that, most of the
// carriers won't connect the call. Because of that, we return an
// empty string here.
''
} else if (getCountryCallingCode(country, metadata.metadata) === '1') {
// For NANPA countries, we output international format for numbers that
// can be dialed internationally, since that always works, except for
// numbers which might potentially be short numbers, which are always
// dialled in national format.
// Select country for `checkNumberLength()`.
metadata.country(country)
if (can_be_internationally_dialled(number) &&
checkNumberLength(number.phone, metadata) !== 'TOO_SHORT') {
formatted_number = format(number, 'INTERNATIONAL', metadata.metadata)
}
else {
formatted_number = format(number, 'NATIONAL', metadata.metadata)
}
}
else {
// For non-geographic countries, Mexican and Chilean fixed line and
// mobile numbers, we output international format for numbers that can be
// dialed internationally, as that always works.
if (
(
country === REGION_CODE_FOR_NON_GEO_ENTITY
||
// MX fixed line and mobile numbers should always be formatted in
// international format, even when dialed within MX. For national
// format to work, a carrier code needs to be used, and the correct
// carrier code depends on if the caller and callee are from the
// same local area. It is trickier to get that to work correctly than
// using international format, which is tested to work fine on all
// carriers.
//
// CL fixed line numbers need the national prefix when dialing in the
// national format, but don't have it when used for display. The
// reverse is true for mobile numbers. As a result, we output them in
// the international format to make it work.
//
// UZ mobile and fixed-line numbers have to be formatted in
// international format or prefixed with special codes like 03, 04
// (for fixed-line) and 05 (for mobile) for dialling successfully
// from mobile devices. As we do not have complete information on
// special codes and to be consistent with formatting across all
// phone types we return the number in international format here.
//
((country === 'MX' || country === 'CL' || country == 'UZ') && is_fixed_line_or_mobile)
)
&&
can_be_internationally_dialled(number)
) {
formatted_number = format(number, 'INTERNATIONAL')
}
else {
formatted_number = format(number, 'NATIONAL')
}
}
}
else if (is_valid_number && can_be_internationally_dialled(number)) {
// We assume that short numbers are not diallable from outside their region,
// so if a number is not a valid regular length phone number, we treat it as
// if it cannot be internationally dialled.
return with_formatting ?
format(number, 'INTERNATIONAL', metadata.metadata) :
format(number, 'E.164', metadata.metadata)
}
if (!with_formatting) {
return diallable_chars(formatted_number)
}
return formatted_number
}
function can_be_internationally_dialled(number) {
return true
}
/**
* A map that contains characters that are essential when dialling. That means
* any of the characters in this map must not be removed from a number when
* dialling, otherwise the call will not reach the intended destination.
*/
const DIALLABLE_CHARACTERS = {
'0': '0',
'1': '1',
'2': '2',
'3': '3',
'4': '4',
'5': '5',
'6': '6',
'7': '7',
'8': '8',
'9': '9',
'+': '+',
'*': '*',
'#': '#'
}
function diallable_chars(formatted_number) {
let result = ''
let i = 0
while (i < formatted_number.length) {
const character = formatted_number[i]
if (DIALLABLE_CHARACTERS[character]) {
result += character
}
i++
}
return result
}
function getPreferredDomesticCarrierCodeOrDefault() {
throw new Error('carrier codes are not part of this library')
}
function formatNationalNumberWithCarrierCode() {
throw new Error('carrier codes are not part of this library')
}
/**
* Formats a phone number in national format for dialing using the carrier as
* specified in the preferred_domestic_carrier_code field of the PhoneNumber
* object passed in. If that is missing, use the {@code fallbackCarrierCode}
* passed in instead. If there is no {@code preferred_domestic_carrier_code},
* and the {@code fallbackCarrierCode} contains an empty string, return the
* number in national format without any carrier code.
*
* <p>Use {@link #formatNationalNumberWithCarrierCode} instead if the carrier
* code passed in should take precedence over the number's
* {@code preferred_domestic_carrier_code} when formatting.
*
* @param {i18n.phonenumbers.PhoneNumber} number the phone number to be
* formatted.
* @param {string} fallbackCarrierCode the carrier selection code to be used, if
* none is found in the phone number itself.
* @return {string} the formatted phone number in national format for dialing
* using the number's preferred_domestic_carrier_code, or the
* {@code fallbackCarrierCode} passed in if none is found.
*/
function formatNationalNumberWithPreferredCarrierCode(number) {
return formatNationalNumberWithCarrierCode(
number,
carrierCode
);
}

View File

@@ -0,0 +1,16 @@
// Google's tests:
// https://github.com/googlei18n/libphonenumber/blob/597983dc4d56ed7e5337a8e74316dc7a3d02d794/javascript/i18n/phonenumbers/phonenumberutil_test.js
// import metadata from '../metadata.min.json' assert { type: 'json' }
// import formatPhoneNumberForMobileDialing from './formatPhoneNumberForMobileDialing.js'
// describe('formatPhoneNumberForMobileDialing', () =>
// {
// it('should format for mobile dialing', () =>
// {
// formatPhoneNumberForMobileDialing({ phone: '8005553535', country: 'RU' }, 'US', true, metadata).should.equal('+7 800 555 3535')
// formatPhoneNumberForMobileDialing({ phone: '8005553535', country: 'RU' }, 'US', false, metadata).should.equal('+78005553535')
// formatPhoneNumberForMobileDialing({ phone: '8005553535', country: 'RU' }, 'RU', false, metadata).should.equal('8005553535')
// })
// })

View File

@@ -0,0 +1,5 @@
import Metadata from './metadata.js'
export default function getCountries(metadata) {
return new Metadata(metadata).getCountries()
}

View File

@@ -0,0 +1,9 @@
import metadata from '../metadata.min.json' assert { type: 'json' }
import getCountries from './getCountries.js'
describe('getCountries', () => {
it('should get countries list', () => {
expect(getCountries(metadata).indexOf('RU') > 0).to.be.true;
})
})

View File

@@ -0,0 +1,2 @@
// Deprecated. Import from 'metadata.js' directly instead.
export { getCountryCallingCode as default } from './metadata.js'

View File

@@ -0,0 +1,13 @@
import metadata from '../metadata.min.json' assert { type: 'json' }
import getCountryCallingCode from './getCountryCallingCode.js'
describe('getCountryCallingCode', () => {
it('should get country calling code', () => {
getCountryCallingCode('US', metadata).should.equal('1')
})
it('should throw if country is unknown', () => {
expect(() => getCountryCallingCode('ZZ', metadata)).to.throw('Unknown country: ZZ')
})
})

View File

@@ -0,0 +1,7 @@
import PhoneNumber from './PhoneNumber.js'
export default function getExampleNumber(country, examples, metadata) {
if (examples[country]) {
return new PhoneNumber(country, examples[country], metadata)
}
}

View File

@@ -0,0 +1,17 @@
import examples from '../examples.mobile.json' assert { type: 'json' }
import metadata from '../metadata.min.json' assert { type: 'json' }
import getExampleNumber from './getExampleNumber.js'
describe('getExampleNumber', () => {
it('should get an example number', () => {
const phoneNumber = getExampleNumber('RU', examples, metadata)
phoneNumber.nationalNumber.should.equal('9123456789')
phoneNumber.number.should.equal('+79123456789')
phoneNumber.countryCallingCode.should.equal('7')
phoneNumber.country.should.equal('RU')
})
it('should handle a non-existing country', () => {
expect(getExampleNumber('XX', examples, metadata)).to.be.undefined
})
})

View File

@@ -0,0 +1,59 @@
import isViablePhoneNumber from './isViablePhoneNumber.js'
// https://www.ietf.org/rfc/rfc3966.txt
/**
* @param {string} text - Phone URI (RFC 3966).
* @return {object} `{ ?number, ?ext }`.
*/
export function parseRFC3966(text) {
let number
let ext
// Replace "tel:" with "tel=" for parsing convenience.
text = text.replace(/^tel:/, 'tel=')
for (const part of text.split(';')) {
const [name, value] = part.split('=')
switch (name) {
case 'tel':
number = value
break
case 'ext':
ext = value
break
case 'phone-context':
// Only "country contexts" are supported.
// "Domain contexts" are ignored.
if (value[0] === '+') {
number = value + number
}
break
}
}
// If the phone number is not viable, then abort.
if (!isViablePhoneNumber(number)) {
return {}
}
const result = { number }
if (ext) {
result.ext = ext
}
return result
}
/**
* @param {object} - `{ ?number, ?extension }`.
* @return {string} Phone URI (RFC 3966).
*/
export function formatRFC3966({ number, ext }) {
if (!number) {
return ''
}
if (number[0] !== '+') {
throw new Error(`"formatRFC3966()" expects "number" to be in E.164 format.`)
}
return `tel:${number}${ext ? ';ext=' + ext : ''}`
}

View File

@@ -0,0 +1,36 @@
import { parseRFC3966, formatRFC3966 } from './RFC3966.js'
describe('RFC3966', () => {
it('should format', () => {
expect(() => formatRFC3966({ number: '123' })).to.throw('expects "number" to be in E.164 format')
formatRFC3966({}).should.equal('')
formatRFC3966({ number: '+78005553535' }).should.equal('tel:+78005553535')
formatRFC3966({ number: '+78005553535', ext: '123' }).should.equal('tel:+78005553535;ext=123')
})
it('should parse', () => {
parseRFC3966('tel:+78005553535').should.deep.equal({
number : '+78005553535'
})
parseRFC3966('tel:+78005553535;ext=123').should.deep.equal({
number : '+78005553535',
ext : '123'
})
// With `phone-context`
parseRFC3966('tel:8005553535;ext=123;phone-context=+7').should.deep.equal({
number : '+78005553535',
ext : '123'
})
// "Domain contexts" are ignored
parseRFC3966('tel:8005553535;ext=123;phone-context=www.leningrad.spb.ru').should.deep.equal({
number : '8005553535',
ext : '123'
})
// Not a viable phone number.
parseRFC3966('tel:3').should.deep.equal({})
})
})

View File

@@ -0,0 +1,35 @@
import { VALID_PUNCTUATION } from '../constants.js'
// Removes brackets and replaces dashes with spaces.
//
// E.g. "(999) 111-22-33" -> "999 111 22 33"
//
// For some reason Google's metadata contains `<intlFormat/>`s with brackets and dashes.
// Meanwhile, there's no single opinion about using punctuation in international phone numbers.
//
// For example, Google's `<intlFormat/>` for USA is `+1 213-373-4253`.
// And here's a quote from WikiPedia's "North American Numbering Plan" page:
// https://en.wikipedia.org/wiki/North_American_Numbering_Plan
//
// "The country calling code for all countries participating in the NANP is 1.
// In international format, an NANP number should be listed as +1 301 555 01 00,
// where 301 is an area code (Maryland)."
//
// I personally prefer the international format without any punctuation.
// For example, brackets are remnants of the old age, meaning that the
// phone number part in brackets (so called "area code") can be omitted
// if dialing within the same "area".
// And hyphens were clearly introduced for splitting local numbers into memorizable groups.
// For example, remembering "5553535" is difficult but "555-35-35" is much simpler.
// Imagine a man taking a bus from home to work and seeing an ad with a phone number.
// He has a couple of seconds to memorize that number until it passes by.
// If it were spaces instead of hyphens the man wouldn't necessarily get it,
// but with hyphens instead of spaces the grouping is more explicit.
// I personally think that hyphens introduce visual clutter,
// so I prefer replacing them with spaces in international numbers.
// In the modern age all output is done on displays where spaces are clearly distinguishable
// so hyphens can be safely replaced with spaces without losing any legibility.
//
export default function applyInternationalSeparatorStyle(formattedNumber) {
return formattedNumber.replace(new RegExp(`[${VALID_PUNCTUATION}]+`, 'g'), ' ').trim()
}

View File

@@ -0,0 +1,8 @@
import applyInternationalSeparatorStyle from './applyInternationalSeparatorStyle.js'
describe('applyInternationalSeparatorStyle', () => {
it('should change Google\'s international format style', () => {
applyInternationalSeparatorStyle('(xxx) xxx-xx-xx').should.equal('xxx xxx xx xx')
applyInternationalSeparatorStyle('(xxx)xxx').should.equal('xxx xxx')
})
})

View File

@@ -0,0 +1,86 @@
import mergeArrays from './mergeArrays.js'
export default function checkNumberLength(nationalNumber, metadata) {
return checkNumberLengthForType(nationalNumber, undefined, metadata)
}
// Checks whether a number is possible for the country based on its length.
// Should only be called for the "new" metadata which has "possible lengths".
export function checkNumberLengthForType(nationalNumber, type, metadata) {
const type_info = metadata.type(type)
// There should always be "<possiblePengths/>" set for every type element.
// This is declared in the XML schema.
// For size efficiency, where a sub-description (e.g. fixed-line)
// has the same "<possiblePengths/>" as the "general description", this is missing,
// so we fall back to the "general description". Where no numbers of the type
// exist at all, there is one possible length (-1) which is guaranteed
// not to match the length of any real phone number.
let possible_lengths = type_info && type_info.possibleLengths() || metadata.possibleLengths()
// let local_lengths = type_info && type.possibleLengthsLocal() || metadata.possibleLengthsLocal()
// Metadata before version `1.0.18` didn't contain `possible_lengths`.
if (!possible_lengths) {
return 'IS_POSSIBLE'
}
if (type === 'FIXED_LINE_OR_MOBILE') {
// No such country in metadata.
/* istanbul ignore next */
if (!metadata.type('FIXED_LINE')) {
// The rare case has been encountered where no fixedLine data is available
// (true for some non-geographic entities), so we just check mobile.
return checkNumberLengthForType(nationalNumber, 'MOBILE', metadata)
}
const mobile_type = metadata.type('MOBILE')
if (mobile_type) {
// Merge the mobile data in if there was any. "Concat" creates a new
// array, it doesn't edit possible_lengths in place, so we don't need a copy.
// Note that when adding the possible lengths from mobile, we have
// to again check they aren't empty since if they are this indicates
// they are the same as the general desc and should be obtained from there.
possible_lengths = mergeArrays(possible_lengths, mobile_type.possibleLengths())
// The current list is sorted; we need to merge in the new list and
// re-sort (duplicates are okay). Sorting isn't so expensive because
// the lists are very small.
// if (local_lengths) {
// local_lengths = mergeArrays(local_lengths, mobile_type.possibleLengthsLocal())
// } else {
// local_lengths = mobile_type.possibleLengthsLocal()
// }
}
}
// If the type doesn't exist then return 'INVALID_LENGTH'.
else if (type && !type_info) {
return 'INVALID_LENGTH'
}
const actual_length = nationalNumber.length
// In `libphonenumber-js` all "local-only" formats are dropped for simplicity.
// // This is safe because there is never an overlap beween the possible lengths
// // and the local-only lengths; this is checked at build time.
// if (local_lengths && local_lengths.indexOf(nationalNumber.length) >= 0)
// {
// return 'IS_POSSIBLE_LOCAL_ONLY'
// }
const minimum_length = possible_lengths[0]
if (minimum_length === actual_length) {
return 'IS_POSSIBLE'
}
if (minimum_length > actual_length) {
return 'TOO_SHORT'
}
if (possible_lengths[possible_lengths.length - 1] < actual_length) {
return 'TOO_LONG'
}
// We skip the first element since we've already checked it.
return possible_lengths.indexOf(actual_length, 1) >= 0 ? 'IS_POSSIBLE' : 'INVALID_LENGTH'
}

View File

@@ -0,0 +1,40 @@
import Metadata from '../metadata.js'
import metadata from '../../metadata.max.json' assert { type: 'json' }
import oldMetadata from '../../test/metadata/1.0.0/metadata.min.json' assert { type: 'json' }
import { checkNumberLengthForType } from './checkNumberLength.js'
describe('checkNumberLength', () => {
it('should check phone number length', () => {
// Too short.
checkNumberLength('800555353', 'FIXED_LINE', 'RU').should.equal('TOO_SHORT')
// Normal.
checkNumberLength('8005553535', 'FIXED_LINE', 'RU').should.equal('IS_POSSIBLE')
// Too long.
checkNumberLength('80055535355', 'FIXED_LINE', 'RU').should.equal('TOO_LONG')
// No such type.
checkNumberLength('169454850', 'VOIP', 'AC').should.equal('INVALID_LENGTH')
// No such possible length.
checkNumberLength('1694548', undefined, 'AD').should.equal('INVALID_LENGTH')
// FIXED_LINE_OR_MOBILE
checkNumberLength('1694548', 'FIXED_LINE_OR_MOBILE', 'AD').should.equal('INVALID_LENGTH')
// No mobile phones.
checkNumberLength('8123', 'FIXED_LINE_OR_MOBILE', 'TA').should.equal('IS_POSSIBLE')
// No "possible lengths" for "mobile".
checkNumberLength('81234567', 'FIXED_LINE_OR_MOBILE', 'SZ').should.equal('IS_POSSIBLE')
})
it('should work for old metadata', function() {
const _oldMetadata = new Metadata(oldMetadata)
_oldMetadata.country('RU')
checkNumberLengthForType('8005553535', 'FIXED_LINE', _oldMetadata).should.equal('IS_POSSIBLE')
})
})
function checkNumberLength(number, type, country) {
const _metadata = new Metadata(metadata)
_metadata.country(country)
return checkNumberLengthForType(number, type, _metadata)
}

View File

@@ -0,0 +1,112 @@
import { VALID_DIGITS } from '../../constants.js'
// The RFC 3966 format for extensions.
const RFC3966_EXTN_PREFIX = ';ext='
/**
* Helper method for constructing regular expressions for parsing. Creates
* an expression that captures up to max_length digits.
* @return {string} RegEx pattern to capture extension digits.
*/
const getExtensionDigitsPattern = (maxLength) => `([${VALID_DIGITS}]{1,${maxLength}})`
/**
* Helper initialiser method to create the regular-expression pattern to match
* extensions.
* Copy-pasted from Google's `libphonenumber`:
* https://github.com/google/libphonenumber/blob/55b2646ec9393f4d3d6661b9c82ef9e258e8b829/javascript/i18n/phonenumbers/phonenumberutil.js#L759-L766
* @return {string} RegEx pattern to capture extensions.
*/
export default function createExtensionPattern(purpose) {
// We cap the maximum length of an extension based on the ambiguity of the way
// the extension is prefixed. As per ITU, the officially allowed length for
// extensions is actually 40, but we don't support this since we haven't seen real
// examples and this introduces many false interpretations as the extension labels
// are not standardized.
/** @type {string} */
var extLimitAfterExplicitLabel = '20';
/** @type {string} */
var extLimitAfterLikelyLabel = '15';
/** @type {string} */
var extLimitAfterAmbiguousChar = '9';
/** @type {string} */
var extLimitWhenNotSure = '6';
/** @type {string} */
var possibleSeparatorsBetweenNumberAndExtLabel = "[ \u00A0\\t,]*";
// Optional full stop (.) or colon, followed by zero or more spaces/tabs/commas.
/** @type {string} */
var possibleCharsAfterExtLabel = "[:\\.\uFF0E]?[ \u00A0\\t,-]*";
/** @type {string} */
var optionalExtnSuffix = "#?";
// Here the extension is called out in more explicit way, i.e mentioning it obvious
// patterns like "ext.".
/** @type {string} */
var explicitExtLabels =
"(?:e?xt(?:ensi(?:o\u0301?|\u00F3))?n?|\uFF45?\uFF58\uFF54\uFF4E?|\u0434\u043E\u0431|anexo)";
// One-character symbols that can be used to indicate an extension, and less
// commonly used or more ambiguous extension labels.
/** @type {string} */
var ambiguousExtLabels = "(?:[x\uFF58#\uFF03~\uFF5E]|int|\uFF49\uFF4E\uFF54)";
// When extension is not separated clearly.
/** @type {string} */
var ambiguousSeparator = "[- ]+";
// This is the same as possibleSeparatorsBetweenNumberAndExtLabel, but not matching
// comma as extension label may have it.
/** @type {string} */
var possibleSeparatorsNumberExtLabelNoComma = "[ \u00A0\\t]*";
// ",," is commonly used for auto dialling the extension when connected. First
// comma is matched through possibleSeparatorsBetweenNumberAndExtLabel, so we do
// not repeat it here. Semi-colon works in Iphone and Android also to pop up a
// button with the extension number following.
/** @type {string} */
var autoDiallingAndExtLabelsFound = "(?:,{2}|;)";
/** @type {string} */
var rfcExtn = RFC3966_EXTN_PREFIX
+ getExtensionDigitsPattern(extLimitAfterExplicitLabel);
/** @type {string} */
var explicitExtn = possibleSeparatorsBetweenNumberAndExtLabel + explicitExtLabels
+ possibleCharsAfterExtLabel
+ getExtensionDigitsPattern(extLimitAfterExplicitLabel)
+ optionalExtnSuffix;
/** @type {string} */
var ambiguousExtn = possibleSeparatorsBetweenNumberAndExtLabel + ambiguousExtLabels
+ possibleCharsAfterExtLabel
+ getExtensionDigitsPattern(extLimitAfterAmbiguousChar)
+ optionalExtnSuffix;
/** @type {string} */
var americanStyleExtnWithSuffix = ambiguousSeparator
+ getExtensionDigitsPattern(extLimitWhenNotSure) + "#";
/** @type {string} */
var autoDiallingExtn = possibleSeparatorsNumberExtLabelNoComma
+ autoDiallingAndExtLabelsFound + possibleCharsAfterExtLabel
+ getExtensionDigitsPattern(extLimitAfterLikelyLabel)
+ optionalExtnSuffix;
/** @type {string} */
var onlyCommasExtn = possibleSeparatorsNumberExtLabelNoComma
+ "(?:,)+" + possibleCharsAfterExtLabel
+ getExtensionDigitsPattern(extLimitAfterAmbiguousChar)
+ optionalExtnSuffix;
// The first regular expression covers RFC 3966 format, where the extension is added
// using ";ext=". The second more generic where extension is mentioned with explicit
// labels like "ext:". In both the above cases we allow more numbers in extension than
// any other extension labels. The third one captures when single character extension
// labels or less commonly used labels are used. In such cases we capture fewer
// extension digits in order to reduce the chance of falsely interpreting two
// numbers beside each other as a number + extension. The fourth one covers the
// special case of American numbers where the extension is written with a hash
// at the end, such as "- 503#". The fifth one is exclusively for extension
// autodialling formats which are used when dialling and in this case we accept longer
// extensions. The last one is more liberal on the number of commas that acts as
// extension labels, so we have a strict cap on the number of digits in such extensions.
return rfcExtn + "|"
+ explicitExtn + "|"
+ ambiguousExtn + "|"
+ americanStyleExtnWithSuffix + "|"
+ autoDiallingExtn + "|"
+ onlyCommasExtn;
}

View File

@@ -0,0 +1,29 @@
import createExtensionPattern from './createExtensionPattern.js'
// Regexp of all known extension prefixes used by different regions followed by
// 1 or more valid digits, for use when parsing.
const EXTN_PATTERN = new RegExp('(?:' + createExtensionPattern() + ')$', 'i')
// Strips any extension (as in, the part of the number dialled after the call is
// connected, usually indicated with extn, ext, x or similar) from the end of
// the number, and returns it.
export default function extractExtension(number) {
const start = number.search(EXTN_PATTERN)
if (start < 0) {
return {}
}
// If we find a potential extension, and the number preceding this is a viable
// number, we assume it is an extension.
const numberWithoutExtension = number.slice(0, start)
const matches = number.match(EXTN_PATTERN)
let i = 1
while (i < matches.length) {
if (matches[i]) {
return {
number: numberWithoutExtension,
ext: matches[i]
}
}
i++
}
}

View File

@@ -0,0 +1,151 @@
import stripIddPrefix from './stripIddPrefix.js'
import extractCountryCallingCodeFromInternationalNumberWithoutPlusSign from './extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js'
import Metadata from '../metadata.js'
import { MAX_LENGTH_COUNTRY_CODE } from '../constants.js'
/**
* Converts a phone number digits (possibly with a `+`)
* into a calling code and the rest phone number digits.
* The "rest phone number digits" could include
* a national prefix, carrier code, and national
* (significant) number.
* @param {string} number — Phone number digits (possibly with a `+`).
* @param {string} [country] — Default country.
* @param {string} [callingCode] — Default calling code (some phone numbering plans are non-geographic).
* @param {object} metadata
* @return {object} `{ countryCallingCodeSource: string?, countryCallingCode: string?, number: string }`
* @example
* // Returns `{ countryCallingCode: "1", number: "2133734253" }`.
* extractCountryCallingCode('2133734253', 'US', null, metadata)
* extractCountryCallingCode('2133734253', null, '1', metadata)
* extractCountryCallingCode('+12133734253', null, null, metadata)
* extractCountryCallingCode('+12133734253', 'RU', null, metadata)
*/
export default function extractCountryCallingCode(
number,
country,
callingCode,
metadata
) {
if (!number) {
return {}
}
let isNumberWithIddPrefix
// If this is not an international phone number,
// then either extract an "IDD" prefix, or extract a
// country calling code from a number by autocorrecting it
// by prepending a leading `+` in cases when it starts
// with the country calling code.
// https://wikitravel.org/en/International_dialling_prefix
// https://github.com/catamphetamine/libphonenumber-js/issues/376
if (number[0] !== '+') {
// Convert an "out-of-country" dialing phone number
// to a proper international phone number.
const numberWithoutIDD = stripIddPrefix(number, country, callingCode, metadata)
// If an IDD prefix was stripped then
// convert the number to international one
// for subsequent parsing.
if (numberWithoutIDD && numberWithoutIDD !== number) {
isNumberWithIddPrefix = true
number = '+' + numberWithoutIDD
} else {
// Check to see if the number starts with the country calling code
// for the default country. If so, we remove the country calling code,
// and do some checks on the validity of the number before and after.
// https://github.com/catamphetamine/libphonenumber-js/issues/376
if (country || callingCode) {
const {
countryCallingCode,
number: shorterNumber
} = extractCountryCallingCodeFromInternationalNumberWithoutPlusSign(
number,
country,
callingCode,
metadata
)
if (countryCallingCode) {
return {
countryCallingCodeSource: 'FROM_NUMBER_WITHOUT_PLUS_SIGN',
countryCallingCode,
number: shorterNumber
}
}
}
return {
// No need to set it to `UNSPECIFIED`. It can be just `undefined`.
// countryCallingCodeSource: 'UNSPECIFIED',
number
}
}
}
// Fast abortion: country codes do not begin with a '0'
if (number[1] === '0') {
return {}
}
metadata = new Metadata(metadata)
// The thing with country phone codes
// is that they are orthogonal to each other
// i.e. there's no such country phone code A
// for which country phone code B exists
// where B starts with A.
// Therefore, while scanning digits,
// if a valid country code is found,
// that means that it is the country code.
//
let i = 2
while (i - 1 <= MAX_LENGTH_COUNTRY_CODE && i <= number.length) {
const countryCallingCode = number.slice(1, i)
if (metadata.hasCallingCode(countryCallingCode)) {
metadata.selectNumberingPlan(countryCallingCode)
return {
countryCallingCodeSource: isNumberWithIddPrefix ? 'FROM_NUMBER_WITH_IDD' : 'FROM_NUMBER_WITH_PLUS_SIGN',
countryCallingCode,
number: number.slice(i)
}
}
i++
}
return {}
}
// The possible values for the returned `countryCallingCodeSource` are:
//
// Copy-pasted from:
// https://github.com/google/libphonenumber/blob/master/resources/phonenumber.proto
//
// // The source from which the country_code is derived. This is not set in the
// // general parsing method, but in the method that parses and keeps raw_input.
// // New fields could be added upon request.
// enum CountryCodeSource {
// // Default value returned if this is not set, because the phone number was
// // created using parse, not parseAndKeepRawInput. hasCountryCodeSource will
// // return false if this is the case.
// UNSPECIFIED = 0;
//
// // The country_code is derived based on a phone number with a leading "+",
// // e.g. the French number "+33 1 42 68 53 00".
// FROM_NUMBER_WITH_PLUS_SIGN = 1;
//
// // The country_code is derived based on a phone number with a leading IDD,
// // e.g. the French number "011 33 1 42 68 53 00", as it is dialled from US.
// FROM_NUMBER_WITH_IDD = 5;
//
// // The country_code is derived based on a phone number without a leading
// // "+", e.g. the French number "33 1 42 68 53 00" when defaultCountry is
// // supplied as France.
// FROM_NUMBER_WITHOUT_PLUS_SIGN = 10;
//
// // The country_code is derived NOT based on the phone number itself, but
// // from the defaultCountry parameter provided in the parsing function by the
// // clients. This happens mostly for numbers written in the national format
// // (without country code). For example, this would be set when parsing the
// // French number "01 42 68 53 00", when defaultCountry is supplied as
// // France.
// FROM_DEFAULT_COUNTRY = 20;
// }

View File

@@ -0,0 +1,20 @@
import extractCountryCallingCode from './extractCountryCallingCode.js'
import metadata from '../../metadata.min.json' assert { type: 'json' }
describe('extractCountryCallingCode', () => {
it('should extract country calling code from a number', () => {
extractCountryCallingCode('+78005553535', null, null, metadata).should.deep.equal({
countryCallingCodeSource: 'FROM_NUMBER_WITH_PLUS_SIGN',
countryCallingCode: '7',
number: '8005553535'
})
extractCountryCallingCode('+7800', null, null, metadata).should.deep.equal({
countryCallingCodeSource: 'FROM_NUMBER_WITH_PLUS_SIGN',
countryCallingCode: '7',
number: '800'
})
extractCountryCallingCode('', null, null, metadata).should.deep.equal({})
})
})

View File

@@ -0,0 +1,63 @@
import Metadata from '../metadata.js'
import matchesEntirely from './matchesEntirely.js'
import extractNationalNumber from './extractNationalNumber.js'
import checkNumberLength from './checkNumberLength.js'
import getCountryCallingCode from '../getCountryCallingCode.js'
/**
* Sometimes some people incorrectly input international phone numbers
* without the leading `+`. This function corrects such input.
* @param {string} number — Phone number digits.
* @param {string?} country
* @param {string?} callingCode
* @param {object} metadata
* @return {object} `{ countryCallingCode: string?, number: string }`.
*/
export default function extractCountryCallingCodeFromInternationalNumberWithoutPlusSign(
number,
country,
callingCode,
metadata
) {
const countryCallingCode = country ? getCountryCallingCode(country, metadata) : callingCode
if (number.indexOf(countryCallingCode) === 0) {
metadata = new Metadata(metadata)
metadata.selectNumberingPlan(country, callingCode)
const possibleShorterNumber = number.slice(countryCallingCode.length)
const {
nationalNumber: possibleShorterNationalNumber,
} = extractNationalNumber(
possibleShorterNumber,
metadata
)
const {
nationalNumber
} = extractNationalNumber(
number,
metadata
)
// If the number was not valid before but is valid now,
// or if it was too long before, we consider the number
// with the country calling code stripped to be a better result
// and keep that instead.
// For example, in Germany (+49), `49` is a valid area code,
// so if a number starts with `49`, it could be both a valid
// national German number or an international number without
// a leading `+`.
if (
(
!matchesEntirely(nationalNumber, metadata.nationalNumberPattern())
&&
matchesEntirely(possibleShorterNationalNumber, metadata.nationalNumberPattern())
)
||
checkNumberLength(nationalNumber, metadata) === 'TOO_LONG'
) {
return {
countryCallingCode,
number: possibleShorterNumber
}
}
}
return { number }
}

View File

@@ -0,0 +1,74 @@
import extractPhoneContext, {
isPhoneContextValid,
PLUS_SIGN,
RFC3966_PREFIX_,
RFC3966_PHONE_CONTEXT_,
RFC3966_ISDN_SUBADDRESS_
} from './extractPhoneContext.js'
import ParseError from '../ParseError.js'
/**
* @param {string} numberToParse
* @param {string} nationalNumber
* @return {}
*/
export default function extractFormattedPhoneNumberFromPossibleRfc3966NumberUri(numberToParse, {
extractFormattedPhoneNumber
}) {
const phoneContext = extractPhoneContext(numberToParse)
if (!isPhoneContextValid(phoneContext)) {
throw new ParseError('NOT_A_NUMBER')
}
let phoneNumberString
if (phoneContext === null) {
// Extract a possible number from the string passed in.
// (this strips leading characters that could not be the start of a phone number)
phoneNumberString = extractFormattedPhoneNumber(numberToParse) || ''
} else {
phoneNumberString = ''
// If the phone context contains a phone number prefix, we need to capture
// it, whereas domains will be ignored.
if (phoneContext.charAt(0) === PLUS_SIGN) {
phoneNumberString += phoneContext
}
// Now append everything between the "tel:" prefix and the phone-context.
// This should include the national number, an optional extension or
// isdn-subaddress component. Note we also handle the case when "tel:" is
// missing, as we have seen in some of the phone number inputs.
// In that case, we append everything from the beginning.
const indexOfRfc3966Prefix = numberToParse.indexOf(RFC3966_PREFIX_)
let indexOfNationalNumber
// RFC 3966 "tel:" prefix is preset at this stage because
// `isPhoneContextValid()` requires it to be present.
/* istanbul ignore else */
if (indexOfRfc3966Prefix >= 0) {
indexOfNationalNumber = indexOfRfc3966Prefix + RFC3966_PREFIX_.length
} else {
indexOfNationalNumber = 0
}
const indexOfPhoneContext = numberToParse.indexOf(RFC3966_PHONE_CONTEXT_)
phoneNumberString += numberToParse.substring(indexOfNationalNumber, indexOfPhoneContext)
}
// Delete the isdn-subaddress and everything after it if it is present.
// Note extension won't appear at the same time with isdn-subaddress
// according to paragraph 5.3 of the RFC3966 spec.
const indexOfIsdn = phoneNumberString.indexOf(RFC3966_ISDN_SUBADDRESS_)
if (indexOfIsdn > 0) {
phoneNumberString = phoneNumberString.substring(0, indexOfIsdn)
}
// If both phone context and isdn-subaddress are absent but other
// parameters are present, the parameters are left in nationalNumber.
// This is because we are concerned about deleting content from a potential
// number string when there is no strong evidence that the number is
// actually written in RFC3966.
if (phoneNumberString !== '') {
return phoneNumberString
}
}

View File

@@ -0,0 +1,106 @@
import extractNationalNumberFromPossiblyIncompleteNumber from './extractNationalNumberFromPossiblyIncompleteNumber.js'
import matchesEntirely from './matchesEntirely.js'
import checkNumberLength from './checkNumberLength.js'
/**
* Strips national prefix and carrier code from a complete phone number.
* The difference from the non-"FromCompleteNumber" function is that
* it won't extract national prefix if the resultant number is too short
* to be a complete number for the selected phone numbering plan.
* @param {string} number — Complete phone number digits.
* @param {Metadata} metadata — Metadata with a phone numbering plan selected.
* @return {object} `{ nationalNumber: string, carrierCode: string? }`.
*/
export default function extractNationalNumber(number, metadata) {
// Parsing national prefixes and carrier codes
// is only required for local phone numbers
// but some people don't understand that
// and sometimes write international phone numbers
// with national prefixes (or maybe even carrier codes).
// http://ucken.blogspot.ru/2016/03/trunk-prefixes-in-skype4b.html
// Google's original library forgives such mistakes
// and so does this library, because it has been requested:
// https://github.com/catamphetamine/libphonenumber-js/issues/127
const {
carrierCode,
nationalNumber
} = extractNationalNumberFromPossiblyIncompleteNumber(
number,
metadata
)
if (nationalNumber !== number) {
if (!shouldHaveExtractedNationalPrefix(number, nationalNumber, metadata)) {
// Don't strip the national prefix.
return { nationalNumber: number }
}
// Check the national (significant) number length after extracting national prefix and carrier code.
// Legacy generated metadata (before `1.0.18`) didn't support the "possible lengths" feature.
if (metadata.possibleLengths()) {
// The number remaining after stripping the national prefix and carrier code
// should be long enough to have a possible length for the country.
// Otherwise, don't strip the national prefix and carrier code,
// since the original number could be a valid number.
// This check has been copy-pasted "as is" from Google's original library:
// https://github.com/google/libphonenumber/blob/876268eb1ad6cdc1b7b5bef17fc5e43052702d57/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java#L3236-L3250
// It doesn't check for the "possibility" of the original `number`.
// I guess it's fine not checking that one. It works as is anyway.
if (!isPossibleIncompleteNationalNumber(nationalNumber, metadata)) {
// Don't strip the national prefix.
return { nationalNumber: number }
}
}
}
return { nationalNumber, carrierCode }
}
// In some countries, the same digit could be a national prefix
// or a leading digit of a valid phone number.
// For example, in Russia, national prefix is `8`,
// and also `800 555 35 35` is a valid number
// in which `8` is not a national prefix, but the first digit
// of a national (significant) number.
// Same's with Belarus:
// `82004910060` is a valid national (significant) number,
// but `2004910060` is not.
// To support such cases (to prevent the code from always stripping
// national prefix), a condition is imposed: a national prefix
// is not extracted when the original number is "viable" and the
// resultant number is not, a "viable" national number being the one
// that matches `national_number_pattern`.
function shouldHaveExtractedNationalPrefix(nationalNumberBefore, nationalNumberAfter, metadata) {
// The equivalent in Google's code is:
// https://github.com/google/libphonenumber/blob/e326fa1fc4283bb05eb35cb3c15c18f98a31af33/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java#L2969-L3004
if (matchesEntirely(nationalNumberBefore, metadata.nationalNumberPattern()) &&
!matchesEntirely(nationalNumberAfter, metadata.nationalNumberPattern())) {
return false
}
// This "is possible" national number (length) check has been commented out
// because it's superceded by the (effectively) same check done in the
// `extractNationalNumber()` function after it calls `shouldHaveExtractedNationalPrefix()`.
// In other words, why run the same check twice if it could only be run once.
// // Check the national (significant) number length after extracting national prefix and carrier code.
// // Fixes a minor "weird behavior" bug: https://gitlab.com/catamphetamine/libphonenumber-js/-/issues/57
// // (Legacy generated metadata (before `1.0.18`) didn't support the "possible lengths" feature).
// if (metadata.possibleLengths()) {
// if (isPossibleIncompleteNationalNumber(nationalNumberBefore, metadata) &&
// !isPossibleIncompleteNationalNumber(nationalNumberAfter, metadata)) {
// return false
// }
// }
return true
}
function isPossibleIncompleteNationalNumber(nationalNumber, metadata) {
switch (checkNumberLength(nationalNumber, metadata)) {
case 'TOO_SHORT':
case 'INVALID_LENGTH':
// This library ignores "local-only" phone numbers (for simplicity).
// See the readme for more info on what are "local-only" phone numbers.
// case 'IS_POSSIBLE_LOCAL_ONLY':
return false
default:
return true
}
}

View File

@@ -0,0 +1,15 @@
import extractNationalNumber from './extractNationalNumber.js'
import Metadata from '../metadata.js'
import oldMetadata from '../../test/metadata/1.0.0/metadata.min.json' assert { type: 'json' }
describe('extractNationalNumber', function() {
it('should extract a national number when using old metadata', function() {
const _oldMetadata = new Metadata(oldMetadata)
_oldMetadata.selectNumberingPlan('RU')
extractNationalNumber('88005553535', _oldMetadata).should.deep.equal({
nationalNumber: '8005553535',
carrierCode: undefined
})
})
})

View File

@@ -0,0 +1,104 @@
/**
* Strips any national prefix (such as 0, 1) present in a
* (possibly incomplete) number provided.
* "Carrier codes" are only used in Colombia and Brazil,
* and only when dialing within those countries from a mobile phone to a fixed line number.
* Sometimes it won't actually strip national prefix
* and will instead prepend some digits to the `number`:
* for example, when number `2345678` is passed with `VI` country selected,
* it will return `{ number: "3402345678" }`, because `340` area code is prepended.
* @param {string} number — National number digits.
* @param {object} metadata — Metadata with country selected.
* @return {object} `{ nationalNumber: string, nationalPrefix: string? carrierCode: string? }`. Even if a national prefix was extracted, it's not necessarily present in the returned object, so don't rely on its presence in the returned object in order to find out whether a national prefix has been extracted or not.
*/
export default function extractNationalNumberFromPossiblyIncompleteNumber(number, metadata) {
if (number && metadata.numberingPlan.nationalPrefixForParsing()) {
// See METADATA.md for the description of
// `national_prefix_for_parsing` and `national_prefix_transform_rule`.
// Attempt to parse the first digits as a national prefix.
const prefixPattern = new RegExp('^(?:' + metadata.numberingPlan.nationalPrefixForParsing() + ')')
const prefixMatch = prefixPattern.exec(number)
if (prefixMatch) {
let nationalNumber
let carrierCode
// https://gitlab.com/catamphetamine/libphonenumber-js/-/blob/master/METADATA.md#national_prefix_for_parsing--national_prefix_transform_rule
// If a `national_prefix_for_parsing` has any "capturing groups"
// then it means that the national (significant) number is equal to
// those "capturing groups" transformed via `national_prefix_transform_rule`,
// and nothing could be said about the actual national prefix:
// what is it and was it even there.
// If a `national_prefix_for_parsing` doesn't have any "capturing groups",
// then everything it matches is a national prefix.
// To determine whether `national_prefix_for_parsing` matched any
// "capturing groups", the value of the result of calling `.exec()`
// is looked at, and if it has non-undefined values where there're
// "capturing groups" in the regular expression, then it means
// that "capturing groups" have been matched.
// It's not possible to tell whether there'll be any "capturing gropus"
// before the matching process, because a `national_prefix_for_parsing`
// could exhibit both behaviors.
const capturedGroupsCount = prefixMatch.length - 1
const hasCapturedGroups = capturedGroupsCount > 0 && prefixMatch[capturedGroupsCount]
if (metadata.nationalPrefixTransformRule() && hasCapturedGroups) {
nationalNumber = number.replace(
prefixPattern,
metadata.nationalPrefixTransformRule()
)
// If there's more than one captured group,
// then carrier code is the second one.
if (capturedGroupsCount > 1) {
carrierCode = prefixMatch[1]
}
}
// If there're no "capturing groups",
// or if there're "capturing groups" but no
// `national_prefix_transform_rule`,
// then just strip the national prefix from the number,
// and possibly a carrier code.
// Seems like there could be more.
else {
// `prefixBeforeNationalNumber` is the whole substring matched by
// the `national_prefix_for_parsing` regular expression.
// There seem to be no guarantees that it's just a national prefix.
// For example, if there's a carrier code, it's gonna be a
// part of `prefixBeforeNationalNumber` too.
const prefixBeforeNationalNumber = prefixMatch[0]
nationalNumber = number.slice(prefixBeforeNationalNumber.length)
// If there's at least one captured group,
// then carrier code is the first one.
if (hasCapturedGroups) {
carrierCode = prefixMatch[1]
}
}
// Tries to guess whether a national prefix was present in the input.
// This is not something copy-pasted from Google's library:
// they don't seem to have an equivalent for that.
// So this isn't an "officially approved" way of doing something like that.
// But since there seems no other existing method, this library uses it.
let nationalPrefix
if (hasCapturedGroups) {
const possiblePositionOfTheFirstCapturedGroup = number.indexOf(prefixMatch[1])
const possibleNationalPrefix = number.slice(0, possiblePositionOfTheFirstCapturedGroup)
// Example: an Argentinian (AR) phone number `0111523456789`.
// `prefixMatch[0]` is `01115`, and `$1` is `11`,
// and the rest of the phone number is `23456789`.
// The national number is transformed via `9$1` to `91123456789`.
// National prefix `0` is detected being present at the start.
// if (possibleNationalPrefix.indexOf(metadata.numberingPlan.nationalPrefix()) === 0) {
if (possibleNationalPrefix === metadata.numberingPlan.nationalPrefix()) {
nationalPrefix = metadata.numberingPlan.nationalPrefix()
}
} else {
nationalPrefix = prefixMatch[0]
}
return {
nationalNumber,
nationalPrefix,
carrierCode
}
}
}
return {
nationalNumber: number
}
}

View File

@@ -0,0 +1,15 @@
import Metadata from '../metadata.js'
import metadata from '../../metadata.min.json' assert { type: 'json' }
import extractNationalNumberFromPossiblyIncompleteNumber from './extractNationalNumberFromPossiblyIncompleteNumber.js'
describe('extractNationalNumberFromPossiblyIncompleteNumber', () => {
it('should parse a carrier code when there is no national prefix transform rule', () => {
const meta = new Metadata(metadata)
meta.country('AU')
extractNationalNumberFromPossiblyIncompleteNumber('18311800123', meta).should.deep.equal({
nationalPrefix: undefined,
carrierCode: '1831',
nationalNumber: '1800123'
})
})
})

View File

@@ -0,0 +1,103 @@
// When phone numbers are written in `RFC3966` format — `"tel:+12133734253"` —
// they can have their "calling code" part written separately in a `phone-context` parameter.
// Example: `"tel:12133734253;phone-context=+1"`.
// This function parses the full phone number from the local number and the `phone-context`
// when the `phone-context` contains a `+` sign.
import {
VALID_DIGITS,
// PLUS_CHARS
} 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_ +
'*' +
'$'
/**
* Regular expression of valid global-number-digits for the phone-context
* parameter, following the syntax defined in RFC3966.
*/
const RFC3966_GLOBAL_NUMBER_DIGITS_PATTERN_ = new RegExp(RFC3966_GLOBAL_NUMBER_DIGITS_, 'g')
// In this port of Google's library, we don't accept alpha characters in phone numbers.
// const ALPHANUM_ = VALID_ALPHA_ + VALID_DIGITS
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_ + '\\.?$'
/**
* Regular expression of valid domainname for the phone-context parameter,
* following the syntax defined in RFC3966.
*/
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='
/**
* Extracts the value of the phone-context parameter of `numberToExtractFrom`,
* following the syntax defined in RFC3966.
*
* @param {string} numberToExtractFrom
* @return {string|null} the extracted string (possibly empty), or `null` if no phone-context parameter is found.
*/
export default function extractPhoneContext(numberToExtractFrom) {
const indexOfPhoneContext = numberToExtractFrom.indexOf(RFC3966_PHONE_CONTEXT_)
// If no phone-context parameter is present
if (indexOfPhoneContext < 0) {
return null
}
const phoneContextStart = indexOfPhoneContext + RFC3966_PHONE_CONTEXT_.length
// If phone-context parameter is empty
if (phoneContextStart >= numberToExtractFrom.length) {
return ''
}
const phoneContextEnd = numberToExtractFrom.indexOf(';', phoneContextStart)
// If phone-context is not the last parameter
if (phoneContextEnd >= 0) {
return numberToExtractFrom.substring(phoneContextStart, phoneContextEnd)
} else {
return numberToExtractFrom.substring(phoneContextStart)
}
}
/**
* Returns whether the value of phoneContext follows the syntax defined in RFC3966.
*
* @param {string|null} phoneContext
* @return {boolean}
*/
export function isPhoneContextValid(phoneContext) {
if (phoneContext === null) {
return true
}
if (phoneContext.length === 0) {
return false
}
// Does phone-context value match pattern of global-number-digits or domainname.
return RFC3966_GLOBAL_NUMBER_DIGITS_PATTERN_.test(phoneContext) ||
RFC3966_DOMAINNAME_PATTERN_.test(phoneContext)
}

View File

@@ -0,0 +1,104 @@
import parsePhoneNumber_ from '../parsePhoneNumber.js'
import PhoneNumber from '../PhoneNumber.js'
import metadata from '../../metadata.min.json' assert { type: 'json' }
function parsePhoneNumber(...parameters) {
parameters.push(metadata)
return parsePhoneNumber_.apply(this, parameters)
}
describe('extractPhoneContext', function() {
it('should parse RFC 3966 phone number URIs', function() {
// context = ";phone-context=" descriptor
// descriptor = domainname / global-number-digits
const NZ_NUMBER = new PhoneNumber('64', '33316005', metadata)
// Valid global-phone-digits
expectPhoneNumbersToBeEqual(
parsePhoneNumber('tel:033316005;phone-context=+64'),
NZ_NUMBER
)
expectPhoneNumbersToBeEqual(
parsePhoneNumber('tel:033316005;phone-context=+64;{this isn\'t part of phone-context anymore!}'),
NZ_NUMBER
)
const nzFromPhoneContext = new PhoneNumber('64', '3033316005', metadata)
expectPhoneNumbersToBeEqual(
parsePhoneNumber('tel:033316005;phone-context=+64-3'),
nzFromPhoneContext
)
const brFromPhoneContext = new PhoneNumber('55', '5033316005', metadata)
expectPhoneNumbersToBeEqual(
parsePhoneNumber('tel:033316005;phone-context=+(555)'),
brFromPhoneContext
)
const usFromPhoneContext = new PhoneNumber('1', '23033316005', metadata)
expectPhoneNumbersToBeEqual(
parsePhoneNumber('tel:033316005;phone-context=+-1-2.3()'),
usFromPhoneContext
)
// Valid domainname.
expectPhoneNumbersToBeEqual(
parsePhoneNumber('tel:033316005;phone-context=abc.nz', 'NZ'),
NZ_NUMBER
)
expectPhoneNumbersToBeEqual(
parsePhoneNumber('tel:033316005;phone-context=www.PHONE-numb3r.com', 'NZ'),
NZ_NUMBER
)
expectPhoneNumbersToBeEqual(
parsePhoneNumber('tel:033316005;phone-context=a', 'NZ'),
NZ_NUMBER
)
expectPhoneNumbersToBeEqual(
parsePhoneNumber('tel:033316005;phone-context=3phone.J.', 'NZ'),
NZ_NUMBER
)
expectPhoneNumbersToBeEqual(
parsePhoneNumber('tel:033316005;phone-context=a--z', 'NZ'),
NZ_NUMBER
)
// Should strip ISDN subaddress.
expectPhoneNumbersToBeEqual(
parsePhoneNumber('tel:033316005;isub=/@;phone-context=+64', 'NZ'),
NZ_NUMBER
)
// // Should support incorrectly-written RFC 3966 phone numbers:
// // the ones written without a `tel:` prefix.
// expectPhoneNumbersToBeEqual(
// parsePhoneNumber('033316005;phone-context=+64', 'NZ'),
// NZ_NUMBER
// )
// Invalid descriptor.
expectToThrowForInvalidPhoneContext('tel:033316005;phone-context=')
expectToThrowForInvalidPhoneContext('tel:033316005;phone-context=+')
expectToThrowForInvalidPhoneContext('tel:033316005;phone-context=64')
expectToThrowForInvalidPhoneContext('tel:033316005;phone-context=++64')
expectToThrowForInvalidPhoneContext('tel:033316005;phone-context=+abc')
expectToThrowForInvalidPhoneContext('tel:033316005;phone-context=.')
expectToThrowForInvalidPhoneContext('tel:033316005;phone-context=3phone')
expectToThrowForInvalidPhoneContext('tel:033316005;phone-context=a-.nz')
expectToThrowForInvalidPhoneContext('tel:033316005;phone-context=a{b}c')
})
})
function expectToThrowForInvalidPhoneContext(string) {
expect(parsePhoneNumber(string)).to.be.undefined
}
function expectPhoneNumbersToBeEqual(phoneNumber1, phoneNumber2) {
if (!phoneNumber1 || !phoneNumber2) {
return false
}
return phoneNumber1.number === phoneNumber2.number &&
phoneNumber1.ext === phoneNumber2.ext
}

View File

@@ -0,0 +1,46 @@
import applyInternationalSeparatorStyle from './applyInternationalSeparatorStyle.js'
// This was originally set to $1 but there are some countries for which the
// first group is not used in the national pattern (e.g. Argentina) so the $1
// group does not match correctly. Therefore, we use `\d`, so that the first
// group actually used in the pattern will be matched.
export const FIRST_GROUP_PATTERN = /(\$\d)/
export default function formatNationalNumberUsingFormat(
number,
format,
{
useInternationalFormat,
withNationalPrefix,
carrierCode,
metadata
}
) {
const formattedNumber = number.replace(
new RegExp(format.pattern()),
useInternationalFormat
? format.internationalFormat()
: (
// This library doesn't use `domestic_carrier_code_formatting_rule`,
// because that one is only used when formatting phone numbers
// for dialing from a mobile phone, and this is not a dialing library.
// carrierCode && format.domesticCarrierCodeFormattingRule()
// // First, replace the $CC in the formatting rule with the desired carrier code.
// // Then, replace the $FG in the formatting rule with the first group
// // and the carrier code combined in the appropriate way.
// ? format.format().replace(FIRST_GROUP_PATTERN, format.domesticCarrierCodeFormattingRule().replace('$CC', carrierCode))
// : (
// withNationalPrefix && format.nationalPrefixFormattingRule()
// ? format.format().replace(FIRST_GROUP_PATTERN, format.nationalPrefixFormattingRule())
// : format.format()
// )
withNationalPrefix && format.nationalPrefixFormattingRule()
? format.format().replace(FIRST_GROUP_PATTERN, format.nationalPrefixFormattingRule())
: format.format()
)
)
if (useInternationalFormat) {
return applyInternationalSeparatorStyle(formattedNumber)
}
return formattedNumber
}

View File

@@ -0,0 +1,30 @@
import getCountryByNationalNumber from './getCountryByNationalNumber.js'
const USE_NON_GEOGRAPHIC_COUNTRY_CODE = false
export default function getCountryByCallingCode(callingCode, {
nationalNumber: nationalPhoneNumber,
defaultCountry,
metadata
}) {
/* istanbul ignore if */
if (USE_NON_GEOGRAPHIC_COUNTRY_CODE) {
if (metadata.isNonGeographicCallingCode(callingCode)) {
return '001'
}
}
const possibleCountries = metadata.getCountryCodesForCallingCode(callingCode)
if (!possibleCountries) {
return
}
// If there's just one country corresponding to the country code,
// then just return it, without further phone number digits validation.
if (possibleCountries.length === 1) {
return possibleCountries[0]
}
return getCountryByNationalNumber(nationalPhoneNumber, {
countries: possibleCountries,
defaultCountry,
metadata: metadata.metadata
})
}

View File

@@ -0,0 +1,52 @@
import Metadata from '../metadata.js'
import getNumberType from './getNumberType.js'
export default function getCountryByNationalNumber(nationalPhoneNumber, {
countries,
defaultCountry,
metadata
}) {
// Re-create `metadata` because it will be selecting a `country`.
metadata = new Metadata(metadata)
// const matchingCountries = []
for (const country of countries) {
metadata.country(country)
// "Leading digits" patterns are only defined for about 20% of all countries.
// By definition, matching "leading digits" is a sufficient but not a necessary
// condition for a phone number to belong to a country.
// The point of "leading digits" check is that it's the fastest one to get a match.
// https://gitlab.com/catamphetamine/libphonenumber-js/blob/master/METADATA.md#leading_digits
// I'd suppose that "leading digits" patterns are mutually exclusive for different countries
// because of the intended use of that feature.
if (metadata.leadingDigits()) {
if (nationalPhoneNumber &&
nationalPhoneNumber.search(metadata.leadingDigits()) === 0) {
return country
}
}
// Else perform full validation with all of those
// fixed-line/mobile/etc regular expressions.
else if (getNumberType({ phone: nationalPhoneNumber, country }, undefined, metadata.metadata)) {
// If both the `defaultCountry` and the "main" one match the phone number,
// don't prefer the `defaultCountry` over the "main" one.
// https://gitlab.com/catamphetamine/libphonenumber-js/-/issues/154
return country
// // If the `defaultCountry` is among the `matchingCountries` then return it.
// if (defaultCountry) {
// if (country === defaultCountry) {
// return country
// }
// matchingCountries.push(country)
// } else {
// return country
// }
}
}
// // Return the first ("main") one of the `matchingCountries`.
// if (matchingCountries.length > 0) {
// return matchingCountries[0]
// }
}

View File

@@ -0,0 +1,25 @@
import Metadata from '../metadata.js'
/**
* Pattern that makes it easy to distinguish whether a region has a single
* international dialing prefix or not. If a region has a single international
* prefix (e.g. 011 in USA), it will be represented as a string that contains
* a sequence of ASCII digits, and possibly a tilde, which signals waiting for
* the tone. If there are multiple available international prefixes in a
* region, they will be represented as a regex string that always contains one
* or more characters that are not ASCII digits or a tilde.
*/
const SINGLE_IDD_PREFIX_REG_EXP = /^[\d]+(?:[~\u2053\u223C\uFF5E][\d]+)?$/
// For regions that have multiple IDD prefixes
// a preferred IDD prefix is returned.
export default function getIddPrefix(country, callingCode, metadata) {
const countryMetadata = new Metadata(metadata)
countryMetadata.selectNumberingPlan(country, callingCode)
if (countryMetadata.defaultIDDPrefix()) {
return countryMetadata.defaultIDDPrefix()
}
if (SINGLE_IDD_PREFIX_REG_EXP.test(countryMetadata.IDDPrefix())) {
return countryMetadata.IDDPrefix()
}
}

View File

@@ -0,0 +1,98 @@
import Metadata from '../metadata.js'
import matchesEntirely from './matchesEntirely.js'
const NON_FIXED_LINE_PHONE_TYPES = [
'MOBILE',
'PREMIUM_RATE',
'TOLL_FREE',
'SHARED_COST',
'VOIP',
'PERSONAL_NUMBER',
'PAGER',
'UAN',
'VOICEMAIL'
]
// Finds out national phone number type (fixed line, mobile, etc)
export default function getNumberType(input, options, metadata)
{
// If assigning the `{}` default value is moved to the arguments above,
// code coverage would decrease for some weird reason.
options = options || {}
// When `parse()` returns an empty object — `{}` —
// that means that the phone number is malformed,
// so it can't possibly be valid.
if (!input.country && !input.countryCallingCode) {
return
}
metadata = new Metadata(metadata)
metadata.selectNumberingPlan(input.country, input.countryCallingCode)
const nationalNumber = options.v2 ? input.nationalNumber : input.phone
// The following is copy-pasted from the original function:
// https://github.com/googlei18n/libphonenumber/blob/3ea547d4fbaa2d0b67588904dfa5d3f2557c27ff/javascript/i18n/phonenumbers/phonenumberutil.js#L2835
// Is this national number even valid for this country
if (!matchesEntirely(nationalNumber, metadata.nationalNumberPattern())) {
return
}
// Is it fixed line number
if (isNumberTypeEqualTo(nationalNumber, 'FIXED_LINE', metadata)) {
// Because duplicate regular expressions are removed
// to reduce metadata size, if "mobile" pattern is ""
// then it means it was removed due to being a duplicate of the fixed-line pattern.
//
if (metadata.type('MOBILE') && metadata.type('MOBILE').pattern() === '') {
return 'FIXED_LINE_OR_MOBILE'
}
// `MOBILE` type pattern isn't included if it matched `FIXED_LINE` one.
// For example, for "US" country.
// Old metadata (< `1.0.18`) had a specific "types" data structure
// that happened to be `undefined` for `MOBILE` in that case.
// Newer metadata (>= `1.0.18`) has another data structure that is
// not `undefined` for `MOBILE` in that case (it's just an empty array).
// So this `if` is just for backwards compatibility with old metadata.
if (!metadata.type('MOBILE')) {
return 'FIXED_LINE_OR_MOBILE'
}
// Check if the number happens to qualify as both fixed line and mobile.
// (no such country in the minimal metadata set)
/* istanbul ignore if */
if (isNumberTypeEqualTo(nationalNumber, 'MOBILE', metadata)) {
return 'FIXED_LINE_OR_MOBILE'
}
return 'FIXED_LINE'
}
for (const type of NON_FIXED_LINE_PHONE_TYPES) {
if (isNumberTypeEqualTo(nationalNumber, type, metadata)) {
return type
}
}
}
export function isNumberTypeEqualTo(nationalNumber, type, metadata) {
type = metadata.type(type)
if (!type || !type.pattern()) {
return false
}
// Check if any possible number lengths are present;
// if so, we use them to avoid checking
// the validation pattern if they don't match.
// If they are absent, this means they match
// the general description, which we have
// already checked before a specific number type.
if (type.possibleLengths() &&
type.possibleLengths().indexOf(nationalNumber.length) < 0) {
return false
}
return matchesEntirely(nationalNumber, type.pattern())
}

View File

@@ -0,0 +1,26 @@
import getNumberType from './getNumberType.js'
import oldMetadata from '../../test/metadata/1.0.0/metadata.min.json' assert { type: 'json' }
import Metadata from '../metadata.js'
describe('getNumberType', function() {
it('should get number type when using old metadata', function() {
getNumberType(
{
nationalNumber: '2133734253',
country: 'US'
},
{ v2: true },
oldMetadata
).should.equal('FIXED_LINE_OR_MOBILE')
})
it('should return `undefined` when the phone number is a malformed one', function() {
expect(getNumberType(
{},
{ v2: true },
oldMetadata
)).to.equal(undefined)
})
})

View File

@@ -0,0 +1,28 @@
import Metadata from '../metadata.js'
/**
* Returns a list of countries that the phone number could potentially belong to.
* @param {string} callingCode — Calling code.
* @param {string} nationalNumber — National (significant) number.
* @param {object} metadata — Metadata.
* @return {string[]} A list of possible countries.
*/
export default function getPossibleCountriesForNumber(callingCode, nationalNumber, metadata) {
const _metadata = new Metadata(metadata)
let possibleCountries = _metadata.getCountryCodesForCallingCode(callingCode)
if (!possibleCountries) {
return []
}
return possibleCountries.filter((country) => {
return couldNationalNumberBelongToCountry(nationalNumber, country, metadata)
})
}
function couldNationalNumberBelongToCountry(nationalNumber, country, metadata) {
const _metadata = new Metadata(metadata)
_metadata.selectNumberingPlan(country)
if (_metadata.numberingPlan.possibleLengths().indexOf(nationalNumber.length) >= 0) {
return true
}
return false
}

View File

@@ -0,0 +1,5 @@
const objectConstructor = {}.constructor;
export default function isObject(object) {
return object !== undefined && object !== null && object.constructor === objectConstructor;
}

View File

@@ -0,0 +1,108 @@
import {
MIN_LENGTH_FOR_NSN,
VALID_DIGITS,
VALID_PUNCTUATION,
PLUS_CHARS
} from '../constants.js'
import createExtensionPattern from './extension/createExtensionPattern.js'
// Regular expression of viable phone numbers. This is location independent.
// Checks we have at least three leading digits, and only valid punctuation,
// alpha characters and digits in the phone number. Does not include extension
// data. The symbol 'x' is allowed here as valid punctuation since it is often
// used as a placeholder for carrier codes, for example in Brazilian phone
// numbers. We also allow multiple '+' characters at the start.
//
// Corresponds to the following:
// [digits]{minLengthNsn}|
// plus_sign*
// (([punctuation]|[star])*[digits]){3,}([punctuation]|[star]|[digits]|[alpha])*
//
// The first reg-ex is to allow short numbers (two digits long) to be parsed if
// they are entered as "15" etc, but only if there is no punctuation in them.
// The second expression restricts the number of digits to three or more, but
// then allows them to be in international form, and to have alpha-characters
// and punctuation. We split up the two reg-exes here and combine them when
// creating the reg-ex VALID_PHONE_NUMBER_PATTERN itself so we can prefix it
// with ^ and append $ to each branch.
//
// "Note VALID_PUNCTUATION starts with a -,
// so must be the first in the range" (c) Google devs.
// (wtf did they mean by saying that; probably nothing)
//
const MIN_LENGTH_PHONE_NUMBER_PATTERN = '[' + VALID_DIGITS + ']{' + MIN_LENGTH_FOR_NSN + '}'
//
// And this is the second reg-exp:
// (see MIN_LENGTH_PHONE_NUMBER_PATTERN for a full description of this reg-exp)
//
export const VALID_PHONE_NUMBER =
'[' + PLUS_CHARS + ']{0,1}' +
'(?:' +
'[' + VALID_PUNCTUATION + ']*' +
'[' + VALID_DIGITS + ']' +
'){3,}' +
'[' +
VALID_PUNCTUATION +
VALID_DIGITS +
']*'
// This regular expression isn't present in Google's `libphonenumber`
// and is only used to determine whether the phone number being input
// is too short for it to even consider it a "valid" number.
// This is just a way to differentiate between a really invalid phone
// number like "abcde" and a valid phone number that a user has just
// started inputting, like "+1" or "1": both these cases would be
// considered `NOT_A_NUMBER` by Google's `libphonenumber`, but this
// library can provide a more detailed error message — whether it's
// really "not a number", or is it just a start of a valid phone number.
const VALID_PHONE_NUMBER_START_REG_EXP = new RegExp(
'^' +
'[' + PLUS_CHARS + ']{0,1}' +
'(?:' +
'[' + VALID_PUNCTUATION + ']*' +
'[' + VALID_DIGITS + ']' +
'){1,2}' +
'$'
, 'i')
export const VALID_PHONE_NUMBER_WITH_EXTENSION =
VALID_PHONE_NUMBER +
// Phone number extensions
'(?:' + createExtensionPattern() + ')?'
// The combined regular expression for valid phone numbers:
//
const VALID_PHONE_NUMBER_PATTERN = new RegExp(
// Either a short two-digit-only phone number
'^' +
MIN_LENGTH_PHONE_NUMBER_PATTERN +
'$' +
'|' +
// Or a longer fully parsed phone number (min 3 characters)
'^' +
VALID_PHONE_NUMBER_WITH_EXTENSION +
'$'
, 'i')
// Checks to see if the string of characters could possibly be a phone number at
// all. At the moment, checks to see that the string begins with at least 2
// digits, ignoring any punctuation commonly found in phone numbers. This method
// does not require the number to be normalized in advance - but does assume
// that leading non-number symbols have been removed, such as by the method
// `extract_possible_number`.
//
export default function isViablePhoneNumber(number) {
return number.length >= MIN_LENGTH_FOR_NSN &&
VALID_PHONE_NUMBER_PATTERN.test(number)
}
// This is just a way to differentiate between a really invalid phone
// number like "abcde" and a valid phone number that a user has just
// started inputting, like "+1" or "1": both these cases would be
// considered `NOT_A_NUMBER` by Google's `libphonenumber`, but this
// library can provide a more detailed error message — whether it's
// really "not a number", or is it just a start of a valid phone number.
export function isViablePhoneNumberStart(number) {
return VALID_PHONE_NUMBER_START_REG_EXP.test(number)
}

View File

@@ -0,0 +1,11 @@
/**
* Checks whether the entire input sequence can be matched
* against the regular expression.
* @return {boolean}
*/
export default function matchesEntirely(text, regular_expression) {
// If assigning the `''` default value is moved to the arguments above,
// code coverage would decrease for some weird reason.
text = text || ''
return new RegExp('^(?:' + regular_expression + ')$').test(text)
}

View File

@@ -0,0 +1,11 @@
import matchesEntirely from './matchesEntirely.js'
describe('matchesEntirely', () => {
it('should work in edge cases', () => {
// No text.
matchesEntirely(undefined, '').should.equal(true)
// "OR" in regexp.
matchesEntirely('911231231', '4\d{8}|[1-9]\d{7}').should.equal(false)
})
})

View File

@@ -0,0 +1,24 @@
/**
* Merges two arrays.
* @param {*} a
* @param {*} b
* @return {*}
*/
export default function mergeArrays(a, b) {
const merged = a.slice()
for (const element of b) {
if (a.indexOf(element) < 0) {
merged.push(element)
}
}
return merged.sort((a, b) => a - b)
// ES6 version, requires Set polyfill.
// let merged = new Set(a)
// for (const element of b) {
// merged.add(i)
// }
// return Array.from(merged).sort((a, b) => a - b)
}

View File

@@ -0,0 +1,7 @@
import mergeArrays from './mergeArrays.js'
describe('mergeArrays', () => {
it('should merge arrays', () => {
mergeArrays([1, 2], [2, 3]).should.deep.equal([1, 2, 3])
})
})

View File

@@ -0,0 +1,82 @@
// These mappings map a character (key) to a specific digit that should
// replace it for normalization purposes. Non-European digits that
// may be used in phone numbers are mapped to a European equivalent.
//
// E.g. in Iraq they don't write `+442323234` but rather `+٤٤٢٣٢٣٢٣٤`.
//
export const DIGITS = {
'0': '0',
'1': '1',
'2': '2',
'3': '3',
'4': '4',
'5': '5',
'6': '6',
'7': '7',
'8': '8',
'9': '9',
'\uFF10': '0', // Fullwidth digit 0
'\uFF11': '1', // Fullwidth digit 1
'\uFF12': '2', // Fullwidth digit 2
'\uFF13': '3', // Fullwidth digit 3
'\uFF14': '4', // Fullwidth digit 4
'\uFF15': '5', // Fullwidth digit 5
'\uFF16': '6', // Fullwidth digit 6
'\uFF17': '7', // Fullwidth digit 7
'\uFF18': '8', // Fullwidth digit 8
'\uFF19': '9', // Fullwidth digit 9
'\u0660': '0', // Arabic-indic digit 0
'\u0661': '1', // Arabic-indic digit 1
'\u0662': '2', // Arabic-indic digit 2
'\u0663': '3', // Arabic-indic digit 3
'\u0664': '4', // Arabic-indic digit 4
'\u0665': '5', // Arabic-indic digit 5
'\u0666': '6', // Arabic-indic digit 6
'\u0667': '7', // Arabic-indic digit 7
'\u0668': '8', // Arabic-indic digit 8
'\u0669': '9', // Arabic-indic digit 9
'\u06F0': '0', // Eastern-Arabic digit 0
'\u06F1': '1', // Eastern-Arabic digit 1
'\u06F2': '2', // Eastern-Arabic digit 2
'\u06F3': '3', // Eastern-Arabic digit 3
'\u06F4': '4', // Eastern-Arabic digit 4
'\u06F5': '5', // Eastern-Arabic digit 5
'\u06F6': '6', // Eastern-Arabic digit 6
'\u06F7': '7', // Eastern-Arabic digit 7
'\u06F8': '8', // Eastern-Arabic digit 8
'\u06F9': '9' // Eastern-Arabic digit 9
}
export function parseDigit(character) {
return DIGITS[character]
}
/**
* Parses phone number digits from a string.
* Drops all punctuation leaving only digits.
* Also converts wide-ascii and arabic-indic numerals to conventional numerals.
* E.g. in Iraq they don't write `+442323234` but rather `+٤٤٢٣٢٣٢٣٤`.
* @param {string} string
* @return {string}
* @example
* ```js
* parseDigits('8 (800) 555')
* // Outputs '8800555'.
* ```
*/
export default function parseDigits(string) {
let result = ''
// Using `.split('')` here instead of normal `for ... of`
// because the importing application doesn't neccessarily include an ES6 polyfill.
// The `.split('')` approach discards "exotic" UTF-8 characters
// (the ones consisting of four bytes) but digits
// (including non-European ones) don't fall into that range
// so such "exotic" characters would be discarded anyway.
for (const character of string.split('')) {
const digit = parseDigit(character)
if (digit) {
result += digit
}
}
return result
}

View File

@@ -0,0 +1,7 @@
import parseDigits from './parseDigits.js'
describe('parseDigits', () => {
it('should parse digits', () => {
parseDigits('+٤٤٢٣٢٣٢٣٤').should.equal('442323234')
})
})

View File

@@ -0,0 +1,30 @@
import Metadata from '../metadata.js'
import { VALID_DIGITS } from '../constants.js'
const CAPTURING_DIGIT_PATTERN = new RegExp('([' + VALID_DIGITS + '])')
export default function stripIddPrefix(number, country, callingCode, metadata) {
if (!country) {
return
}
// Check if the number is IDD-prefixed.
const countryMetadata = new Metadata(metadata)
countryMetadata.selectNumberingPlan(country, callingCode)
const IDDPrefixPattern = new RegExp(countryMetadata.IDDPrefix())
if (number.search(IDDPrefixPattern) !== 0) {
return
}
// Strip IDD prefix.
number = number.slice(number.match(IDDPrefixPattern)[0].length)
// If there're any digits after an IDD prefix,
// then those digits are a country calling code.
// Since no country code starts with a `0`,
// the code below validates that the next digit (if present) is not `0`.
const matchedGroups = number.match(CAPTURING_DIGIT_PATTERN)
if (matchedGroups && matchedGroups[1] != null && matchedGroups[1].length > 0) {
if (matchedGroups[1] === '0') {
return
}
}
return number
}

View File

@@ -0,0 +1,21 @@
import stripIddPrefix from './stripIddPrefix.js'
import metadata from '../../metadata.min.json' assert { type: 'json' }
describe('stripIddPrefix', () => {
it('should strip a valid IDD prefix', () => {
stripIddPrefix('01178005553535', 'US', '1', metadata).should.equal('78005553535')
})
it('should strip a valid IDD prefix (no country calling code)', () => {
stripIddPrefix('011', 'US', '1', metadata).should.equal('')
})
it('should strip a valid IDD prefix (valid country calling code)', () => {
stripIddPrefix('0117', 'US', '1', metadata).should.equal('7')
})
it('should strip a valid IDD prefix (not a valid country calling code)', () => {
expect(stripIddPrefix('0110', 'US', '1', metadata)).to.be.undefined
})
})

View File

@@ -0,0 +1,78 @@
import Metadata from './metadata.js'
import checkNumberLength from './helpers/checkNumberLength.js'
/**
* Checks if a phone number is "possible" (basically just checks its length).
*
* isPossible(phoneNumberInstance, { ..., v2: true }, metadata)
*
* isPossible({ phone: '8005553535', country: 'RU' }, { ... }, metadata)
* isPossible({ phone: '8005553535', country: 'RU' }, undefined, metadata)
*
* @param {object|PhoneNumber} input — If `options.v2: true` flag is passed, the `input` should be a `PhoneNumber` instance. Otherwise, it should be an object of shape `{ phone: '...', country: '...' }`.
* @param {object} [options]
* @param {object} metadata
* @return {string}
*/
export default function isPossiblePhoneNumber(input, options, metadata) {
/* istanbul ignore if */
if (options === undefined) {
options = {}
}
metadata = new Metadata(metadata)
if (options.v2) {
if (!input.countryCallingCode) {
throw new Error('Invalid phone number object passed')
}
metadata.selectNumberingPlan(input.countryCallingCode)
} else {
if (!input.phone) {
return false
}
if (input.country) {
if (!metadata.hasCountry(input.country)) {
throw new Error(`Unknown country: ${input.country}`)
}
metadata.country(input.country)
} else {
if (!input.countryCallingCode) {
throw new Error('Invalid phone number object passed')
}
metadata.selectNumberingPlan(input.countryCallingCode)
}
}
// Old metadata (< 1.0.18) had no "possible length" data.
if (metadata.possibleLengths()) {
return isPossibleNumber(input.phone || input.nationalNumber, metadata)
} else {
// There was a bug between `1.7.35` and `1.7.37` where "possible_lengths"
// were missing for "non-geographical" numbering plans.
// Just assume the number is possible in such cases:
// it's unlikely that anyone generated their custom metadata
// in that short period of time (one day).
// This code can be removed in some future major version update.
if (input.countryCallingCode && metadata.isNonGeographicCallingCode(input.countryCallingCode)) {
// "Non-geographic entities" did't have `possibleLengths`
// due to a bug in metadata generation process.
return true
} else {
throw new Error('Missing "possibleLengths" in metadata. Perhaps the metadata has been generated before v1.0.18.');
}
}
}
export function isPossibleNumber(nationalNumber, metadata) { //, isInternational) {
switch (checkNumberLength(nationalNumber, metadata)) {
case 'IS_POSSIBLE':
return true
// This library ignores "local-only" phone numbers (for simplicity).
// See the readme for more info on what are "local-only" phone numbers.
// case 'IS_POSSIBLE_LOCAL_ONLY':
// return !isInternational
default:
return false
}
}

View File

@@ -0,0 +1,66 @@
import metadata from '../metadata.min.json' assert { type: 'json' }
import _isPossibleNumber from './isPossible.js'
import parsePhoneNumber from './parsePhoneNumber.js'
function isPossibleNumber(...parameters) {
let v2
if (parameters.length < 1) {
// `input` parameter.
parameters.push(undefined)
} else {
// Convert string `input` to a `PhoneNumber` instance.
if (typeof parameters[0] === 'string') {
v2 = true
parameters[0] = parsePhoneNumber(parameters[0], {
...parameters[1],
extract: false
}, metadata)
}
}
if (parameters.length < 2) {
// `options` parameter.
parameters.push(undefined)
}
// Set `v2` flag.
parameters[1] = {
v2,
...parameters[1]
}
// Add `metadata` parameter.
parameters.push(metadata)
// Call the function.
return _isPossibleNumber.apply(this, parameters)
}
describe('isPossible', () => {
it('should work', function()
{
isPossibleNumber('+79992223344').should.equal(true)
isPossibleNumber({ phone: '1112223344', country: 'RU' }).should.equal(true)
isPossibleNumber({ phone: '111222334', country: 'RU' }).should.equal(false)
isPossibleNumber({ phone: '11122233445', country: 'RU' }).should.equal(false)
isPossibleNumber({ phone: '1112223344', countryCallingCode: 7 }).should.equal(true)
})
it('should work v2', () => {
isPossibleNumber({ nationalNumber: '111222334', countryCallingCode: 7 }, { v2: true }).should.equal(false)
isPossibleNumber({ nationalNumber: '1112223344', countryCallingCode: 7 }, { v2: true }).should.equal(true)
isPossibleNumber({ nationalNumber: '11122233445', countryCallingCode: 7 }, { v2: true }).should.equal(false)
})
it('should work in edge cases', () => {
// Invalid `PhoneNumber` argument.
expect(() => isPossibleNumber({}, { v2: true })).to.throw('Invalid phone number object passed')
// Empty input is passed.
// This is just to support `isValidNumber({})`
// for cases when `parseNumber()` returns `{}`.
isPossibleNumber({}).should.equal(false)
expect(() => isPossibleNumber({ phone: '1112223344' })).to.throw('Invalid phone number object passed')
// Incorrect country.
expect(() => isPossibleNumber({ phone: '1112223344', country: 'XX' })).to.throw('Unknown country')
})
})

View File

@@ -0,0 +1,12 @@
import normalizeArguments from './normalizeArguments.js'
import parsePhoneNumber from './parsePhoneNumber_.js'
export default function isPossiblePhoneNumber() {
let { text, options, metadata } = normalizeArguments(arguments)
options = {
...options,
extract: false
}
const phoneNumber = parsePhoneNumber(text, options, metadata)
return phoneNumber && phoneNumber.isPossible() || false
}

View File

@@ -0,0 +1,27 @@
import _isPossiblePhoneNumber from './isPossiblePhoneNumber.js'
import metadata from '../metadata.min.json' assert { type: 'json' }
import oldMetadata from '../test/metadata/1.0.0/metadata.min.json' assert { type: 'json' }
function isPossiblePhoneNumber(...parameters) {
parameters.push(metadata)
return _isPossiblePhoneNumber.apply(this, parameters)
}
describe('isPossiblePhoneNumber', () => {
it('should detect whether a phone number is possible', () => {
isPossiblePhoneNumber('8 (800) 555 35 35', 'RU').should.equal(true)
isPossiblePhoneNumber('8 (800) 555 35 35 0', 'RU').should.equal(false)
isPossiblePhoneNumber('Call: 8 (800) 555 35 35', 'RU').should.equal(false)
isPossiblePhoneNumber('8 (800) 555 35 35', { defaultCountry: 'RU' }).should.equal(true)
isPossiblePhoneNumber('+7 (800) 555 35 35').should.equal(true)
isPossiblePhoneNumber('+7 1 (800) 555 35 35').should.equal(false)
isPossiblePhoneNumber(' +7 (800) 555 35 35').should.equal(false)
isPossiblePhoneNumber(' ').should.equal(false)
})
it('should detect whether a phone number is possible when using old metadata', () => {
expect(() => _isPossiblePhoneNumber('8 (800) 555 35 35', 'RU', oldMetadata))
.to.throw('Missing "possibleLengths" in metadata.')
_isPossiblePhoneNumber('+888 123 456 78901', oldMetadata).should.equal(true)
})
})

View File

@@ -0,0 +1,59 @@
import Metadata from './metadata.js'
import matchesEntirely from './helpers/matchesEntirely.js'
import getNumberType from './helpers/getNumberType.js'
/**
* Checks if a given phone number is valid.
*
* isValid(phoneNumberInstance, { ..., v2: true }, metadata)
*
* isPossible({ phone: '8005553535', country: 'RU' }, { ... }, metadata)
* isPossible({ phone: '8005553535', country: 'RU' }, undefined, metadata)
*
* If the `number` is a string, it will be parsed to an object,
* but only if it contains only valid phone number characters (including punctuation).
* If the `number` is an object, it is used as is.
*
* The optional `defaultCountry` argument is the default country.
* I.e. it does not restrict to just that country,
* e.g. in those cases where several countries share
* the same phone numbering rules (NANPA, Britain, etc).
* For example, even though the number `07624 369230`
* belongs to the Isle of Man ("IM" country code)
* calling `isValidNumber('07624369230', 'GB', metadata)`
* still returns `true` because the country is not restricted to `GB`,
* it's just that `GB` is the default one for the phone numbering rules.
* For restricting the country see `isValidNumberForRegion()`
* though restricting a country might not be a good idea.
* https://github.com/googlei18n/libphonenumber/blob/master/FAQ.md#when-should-i-use-isvalidnumberforregion
*
* Examples:
*
* ```js
* isValidNumber('+78005553535', metadata)
* isValidNumber('8005553535', 'RU', metadata)
* isValidNumber('88005553535', 'RU', metadata)
* isValidNumber({ phone: '8005553535', country: 'RU' }, metadata)
* ```
*/
export default function isValidNumber(input, options, metadata)
{
// If assigning the `{}` default value is moved to the arguments above,
// code coverage would decrease for some weird reason.
options = options || {}
metadata = new Metadata(metadata)
metadata.selectNumberingPlan(input.country, input.countryCallingCode)
// By default, countries only have type regexps when it's required for
// distinguishing different countries having the same `countryCallingCode`.
if (metadata.hasTypes()) {
return getNumberType(input, options, metadata.metadata) !== undefined
}
// If there are no type regexps for this country in metadata then use
// `nationalNumberPattern` as a "better than nothing" replacement.
const nationalNumber = options.v2 ? input.nationalNumber : input.phone
return matchesEntirely(nationalNumber, metadata.nationalNumberPattern())
}

View File

@@ -0,0 +1,120 @@
import metadata from '../metadata.min.json' assert { type: 'json' }
import _isValidNumber from './isValid.js'
import parsePhoneNumber from './parsePhoneNumber.js'
function isValidNumber(...parameters) {
let v2
if (parameters.length < 1) {
// `input` parameter.
parameters.push(undefined)
} else {
// Convert string `input` to a `PhoneNumber` instance.
if (typeof parameters[0] === 'string') {
v2 = true
parameters[0] = parsePhoneNumber(parameters[0], {
...parameters[1],
extract: false
}, metadata)
}
}
if (parameters.length < 2) {
// `options` parameter.
parameters.push(undefined)
}
// Set `v2` flag.
parameters[1] = {
v2,
...parameters[1]
}
// Add `metadata` parameter.
parameters.push(metadata)
// Call the function.
return _isValidNumber.apply(this, parameters)
}
describe('validate', () => {
it('should validate phone numbers', () => {
isValidNumber('+1-213-373-4253').should.equal(true)
isValidNumber('+1-213-373').should.equal(false)
isValidNumber('+1-213-373-4253', undefined).should.equal(true)
isValidNumber('(213) 373-4253', { defaultCountry: 'US' }).should.equal(true)
isValidNumber('(213) 37', { defaultCountry: 'US' }).should.equal(false)
isValidNumber({ country: 'US', phone: '2133734253' }).should.equal(true)
// No "types" info: should return `true`.
isValidNumber('+380972423740').should.equal(true)
isValidNumber('0912345678', { defaultCountry: 'TW' }).should.equal(true)
// Moible numbers starting 07624* are Isle of Man
// which has its own "country code" "IM"
// which is in the "GB" "country calling code" zone.
// So while this number is for "IM" it's still supposed to
// be valid when passed "GB" as a default country.
isValidNumber('07624369230', { defaultCountry: 'GB' }).should.equal(true)
})
it('should refine phone number validation in case extended regular expressions are set for a country', () => {
// Germany general validation must pass
isValidNumber('961111111', { defaultCountry: 'UZ' }).should.equal(true)
const phoneNumberTypePatterns = metadata.countries.UZ[11]
// Different regular expressions for precise national number validation.
// `types` index in compressed array is `9` for v1.
// For v2 it's 10.
// For v3 it's 11.
metadata.countries.UZ[11] =
[
["(?:6(?:1(?:22|3[124]|4[1-4]|5[123578]|64)|2(?:22|3[0-57-9]|41)|5(?:22|3[3-7]|5[024-8])|6\\d{2}|7(?:[23]\\d|7[69])|9(?:22|4[1-8]|6[135]))|7(?:0(?:5[4-9]|6[0146]|7[12456]|9[135-8])|1[12]\\d|2(?:22|3[1345789]|4[123579]|5[14])|3(?:2\\d|3[1578]|4[1-35-7]|5[1-57]|61)|4(?:2\\d|3[1-579]|7[1-79])|5(?:22|5[1-9]|6[1457])|6(?:22|3[12457]|4[13-8])|9(?:22|5[1-9])))\\d{5}"],
["6(?:1(?:2(?:98|2[01])|35[0-4]|50\\d|61[23]|7(?:[01][017]|4\\d|55|9[5-9]))|2(?:11\\d|2(?:[12]1|9[01379])|5(?:[126]\\d|3[0-4])|7\\d{2})|5(?:19[01]|2(?:27|9[26])|30\\d|59\\d|7\\d{2})|6(?:2(?:1[5-9]|2[0367]|38|41|52|60)|3[79]\\d|4(?:56|83)|7(?:[07]\\d|1[017]|3[07]|4[047]|5[057]|67|8[0178]|9[79])|9[0-3]\\d)|7(?:2(?:24|3[237]|4[5-9]|7[15-8])|5(?:7[12]|8[0589])|7(?:0\\d|[39][07])|9(?:0\\d|7[079]))|9(?:2(?:1[1267]|5\\d|3[01]|7[0-4])|5[67]\\d|6(?:2[0-26]|8\\d)|7\\d{2}))\\d{4}|7(?:0\\d{3}|1(?:13[01]|6(?:0[47]|1[67]|66)|71[3-69]|98\\d)|2(?:2(?:2[79]|95)|3(?:2[5-9]|6[0-6])|57\\d|7(?:0\\d|1[17]|2[27]|3[37]|44|5[057]|66|88))|3(?:2(?:1[0-6]|21|3[469]|7[159])|33\\d|5(?:0[0-4]|5[579]|9\\d)|7(?:[0-3579]\\d|4[0467]|6[67]|8[078])|9[4-6]\\d)|4(?:2(?:29|5[0257]|6[0-7]|7[1-57])|5(?:1[0-4]|8\\d|9[5-9])|7(?:0\\d|1[024589]|2[0127]|3[0137]|[46][07]|5[01]|7[5-9]|9[079])|9(?:7[015-9]|[89]\\d))|5(?:112|2(?:0\\d|2[29]|[49]4)|3[1568]\\d|52[6-9]|7(?:0[01578]|1[017]|[23]7|4[047]|[5-7]\\d|8[78]|9[079]))|6(?:2(?:2[1245]|4[2-4])|39\\d|41[179]|5(?:[349]\\d|5[0-2])|7(?:0[017]|[13]\\d|22|44|55|67|88))|9(?:22[128]|3(?:2[0-4]|7\\d)|57[05629]|7(?:2[05-9]|3[37]|4\\d|60|7[2579]|87|9[07])))\\d{4}|9[0-57-9]\\d{7}"]
]
// Extended validation must not pass for an invalid phone number
isValidNumber('961111111', { defaultCountry: 'UZ' }).should.equal(false)
// Extended validation must pass for a valid phone number
isValidNumber('912345678', { defaultCountry: 'UZ' }).should.equal(true)
metadata.countries.UZ[11] = phoneNumberTypePatterns
})
it('should work in edge cases', () => {
// No metadata
let thrower = () => _isValidNumber('+78005553535')
thrower.should.throw('`metadata` argument not passed')
// // Non-phone-number characters in a phone number
// isValidNumber('+499821958a').should.equal(false)
// isValidNumber('88005553535x', { defaultCountry: 'RU' }).should.equal(false)
// Doesn't have `types` regexps in default metadata.
isValidNumber({ country: 'UA', phone: '300000000' }).should.equal(true)
isValidNumber({ country: 'UA', phone: '200000000' }).should.equal(false)
// // Numerical `value`
// thrower = () => isValidNumber(88005553535, { defaultCountry: 'RU' })
// thrower.should.throw('A phone number must either be a string or an object of shape { phone, [country] }.')
// Long country phone code
isValidNumber('+3725555555').should.equal(true)
// // Invalid country
// thrower = () => isValidNumber({ phone: '8005553535', country: 'RUS' })
// thrower.should.throw('Unknown country')
})
it('should accept phone number extensions', () => {
// International
isValidNumber('+12133734253 ext. 123').should.equal(true)
// National
isValidNumber('88005553535 x123', { defaultCountry: 'RU' }).should.equal(true)
})
it('should validate non-geographic toll-free phone numbers', () => {
isValidNumber('+80074454123').should.equal(true)
})
})

View File

@@ -0,0 +1,12 @@
import normalizeArguments from './normalizeArguments.js'
import parsePhoneNumber from './parsePhoneNumber_.js'
export default function isValidPhoneNumber() {
let { text, options, metadata } = normalizeArguments(arguments)
options = {
...options,
extract: false
}
const phoneNumber = parsePhoneNumber(text, options, metadata)
return phoneNumber && phoneNumber.isValid() || false
}

View File

@@ -0,0 +1,20 @@
import _isValidPhoneNumber from './isValidPhoneNumber.js'
import metadata from '../metadata.min.json' assert { type: 'json' }
function isValidPhoneNumber(...parameters) {
parameters.push(metadata)
return _isValidPhoneNumber.apply(this, parameters)
}
describe('isValidPhoneNumber', () => {
it('should detect whether a phone number is valid', () => {
isValidPhoneNumber('8 (800) 555 35 35', 'RU').should.equal(true)
isValidPhoneNumber('8 (800) 555 35 35 0', 'RU').should.equal(false)
isValidPhoneNumber('Call: 8 (800) 555 35 35', 'RU').should.equal(false)
isValidPhoneNumber('8 (800) 555 35 35', { defaultCountry: 'RU' }).should.equal(true)
isValidPhoneNumber('+7 (800) 555 35 35').should.equal(true)
isValidPhoneNumber('+7 1 (800) 555 35 35').should.equal(false)
isValidPhoneNumber(' +7 (800) 555 35 35').should.equal(false)
isValidPhoneNumber(' ').should.equal(false)
})
})

View File

@@ -0,0 +1,12 @@
import PhoneNumberMatcher from '../PhoneNumberMatcher.js'
import normalizeArguments from '../normalizeArguments.js'
export default function findNumbers() {
const { text, options, metadata } = normalizeArguments(arguments)
const matcher = new PhoneNumberMatcher(text, options, metadata)
const results = []
while (matcher.hasNext()) {
results.push(matcher.next())
}
return results
}

View File

@@ -0,0 +1,204 @@
import findNumbers from './findNumbers.js'
import metadata from '../../metadata.max.json' assert { type: 'json' }
describe('findNumbers', () => {
it('should find numbers', () => {
findNumbers('2133734253', 'US', metadata).should.deep.equal([{
phone : '2133734253',
country : 'US',
startsAt : 0,
endsAt : 10
}])
findNumbers('(213) 373-4253', 'US', metadata).should.deep.equal([{
phone : '2133734253',
country : 'US',
startsAt : 0,
endsAt : 14
}])
findNumbers('The number is +7 (800) 555-35-35 and not (213) 373-4253 as written in the document.', 'US', metadata).should.deep.equal([{
phone : '8005553535',
country : 'RU',
startsAt : 14,
endsAt : 32
}, {
phone : '2133734253',
country : 'US',
startsAt : 41,
endsAt : 55
}])
// Opening parenthesis issue.
// https://github.com/catamphetamine/libphonenumber-js/issues/252
findNumbers('The number is +7 (800) 555-35-35 and not (213) 373-4253 (that\'s not even in the same country!) as written in the document.', 'US', metadata).should.deep.equal([{
phone : '8005553535',
country : 'RU',
startsAt : 14,
endsAt : 32
}, {
phone : '2133734253',
country : 'US',
startsAt : 41,
endsAt : 55
}])
// No default country.
findNumbers('The number is +7 (800) 555-35-35 as written in the document.', metadata).should.deep.equal([{
phone : '8005553535',
country : 'RU',
startsAt : 14,
endsAt : 32
}])
// Passing `options` and default country.
findNumbers('The number is +7 (800) 555-35-35 as written in the document.', 'US', { leniency: 'VALID' }, metadata).should.deep.equal([{
phone : '8005553535',
country : 'RU',
startsAt : 14,
endsAt : 32
}])
// Passing `options`.
findNumbers('The number is +7 (800) 555-35-35 as written in the document.', { leniency: 'VALID' }, metadata).should.deep.equal([{
phone : '8005553535',
country : 'RU',
startsAt : 14,
endsAt : 32
}])
// Not a phone number and a phone number.
findNumbers('Digits 12 are not a number, but +7 (800) 555-35-35 is.', { leniency: 'VALID' }, metadata).should.deep.equal([{
phone : '8005553535',
country : 'RU',
startsAt : 32,
endsAt : 50
}])
// Phone number extension.
findNumbers('Date 02/17/2018 is not a number, but +7 (800) 555-35-35 ext. 123 is.', { leniency: 'VALID' }, metadata).should.deep.equal([{
phone : '8005553535',
country : 'RU',
ext : '123',
startsAt : 37,
endsAt : 64
}])
})
it('should find numbers (v2)', () => {
const phoneNumbers = findNumbers('The number is +7 (800) 555-35-35 ext. 1234 and not (213) 373-4253 as written in the document.', 'US', { v2: true }, metadata)
phoneNumbers.length.should.equal(2)
phoneNumbers[0].startsAt.should.equal(14)
phoneNumbers[0].endsAt.should.equal(42)
phoneNumbers[0].number.number.should.equal('+78005553535')
phoneNumbers[0].number.nationalNumber.should.equal('8005553535')
phoneNumbers[0].number.country.should.equal('RU')
phoneNumbers[0].number.countryCallingCode.should.equal('7')
phoneNumbers[0].number.ext.should.equal('1234')
phoneNumbers[1].startsAt.should.equal(51)
phoneNumbers[1].endsAt.should.equal(65)
phoneNumbers[1].number.number.should.equal('+12133734253')
phoneNumbers[1].number.nationalNumber.should.equal('2133734253')
phoneNumbers[1].number.country.should.equal('US')
phoneNumbers[1].number.countryCallingCode.should.equal('1')
})
it('shouldn\'t find non-valid numbers', () => {
// Not a valid phone number for US.
findNumbers('1111111111', 'US', metadata).should.deep.equal([])
})
it('should find non-European digits', () => {
// E.g. in Iraq they don't write `+442323234` but rather `+٤٤٢٣٢٣٢٣٤`.
findNumbers('العَرَبِيَّة‎ +٤٤٣٣٣٣٣٣٣٣٣٣عَرَبِيّ‎', metadata).should.deep.equal([{
country : 'GB',
phone : '3333333333',
startsAt : 14,
endsAt : 27
}])
})
it('should work in edge cases', () => {
let thrower
// No input
findNumbers('', metadata).should.deep.equal([])
// // No country metadata for this `require` country code
// thrower = () => findNumbers('123', 'ZZ', metadata)
// thrower.should.throw('Unknown country')
// Numerical `value`
thrower = () => findNumbers(2141111111, 'US')
thrower.should.throw('A text for parsing must be a string.')
// // No metadata
// thrower = () => findNumbers('')
// thrower.should.throw('`metadata` argument not passed')
// No metadata, no default country, no phone numbers.
findNumbers('').should.deep.equal([])
})
it('should find international numbers when passed a non-existent default country', () => {
const numbers = findNumbers('Phone: +7 (800) 555 35 35. National: 8 (800) 555-55-55', { defaultCountry: 'XX', v2: true }, metadata)
numbers.length.should.equal(1)
numbers[0].number.nationalNumber.should.equal('8005553535')
})
it('shouldn\'t find phone numbers which are not phone numbers', () => {
// A timestamp.
findNumbers('2012-01-02 08:00', 'US', metadata).should.deep.equal([])
// A valid number (not a complete timestamp).
findNumbers('2012-01-02 08', 'US', metadata).should.deep.equal([{
country : 'US',
phone : '2012010208',
startsAt : 0,
endsAt : 13
}])
// Invalid parens.
findNumbers('213(3734253', 'US', metadata).should.deep.equal([])
// Letters after phone number.
findNumbers('2133734253a', 'US', metadata).should.deep.equal([])
// Valid phone (same as the one found in the UUID below).
findNumbers('The phone number is 231354125.', 'FR', metadata).should.deep.equal([{
country : 'FR',
phone : '231354125',
startsAt : 20,
endsAt : 29
}])
// Not a phone number (part of a UUID).
// Should parse in `{ extended: true }` mode.
const possibleNumbers = findNumbers('The UUID is CA801c26f98cd16e231354125ad046e40b.', 'FR', { extended: true }, metadata)
possibleNumbers.length.should.equal(1)
possibleNumbers[0].country.should.equal('FR')
possibleNumbers[0].phone.should.equal('231354125')
// Not a phone number (part of a UUID).
// Shouldn't parse by default.
findNumbers('The UUID is CA801c26f98cd16e231354125ad046e40b.', 'FR', metadata).should.deep.equal([])
})
// https://gitlab.com/catamphetamine/libphonenumber-js/-/merge_requests/4
it('should return correct `startsAt` and `endsAt` when matching "inner" candidates in a could-be-a-candidate substring', () => {
findNumbers('39945926 77200596 16533084', 'ID', metadata)
.should
.deep
.equal([{
country: 'ID',
phone: '77200596',
startsAt: 9,
endsAt: 17
}])
})
})

View File

@@ -0,0 +1,20 @@
// This is a legacy function.
// Use `findNumbers()` instead.
import _findPhoneNumbers, { searchPhoneNumbers as _searchPhoneNumbers } from './findPhoneNumbersInitialImplementation.js'
import normalizeArguments from '../normalizeArguments.js'
export default function findPhoneNumbers()
{
const { text, options, metadata } = normalizeArguments(arguments)
return _findPhoneNumbers(text, options, metadata)
}
/**
* @return ES6 `for ... of` iterator.
*/
export function searchPhoneNumbers()
{
const { text, options, metadata } = normalizeArguments(arguments)
return _searchPhoneNumbers(text, options, metadata)
}

View File

@@ -0,0 +1,233 @@
// This is a legacy function.
// Use `findNumbers()` instead.
import findNumbers, { searchPhoneNumbers } from './findPhoneNumbers.js'
import { PhoneNumberSearch } from './findPhoneNumbersInitialImplementation.js'
import metadata from '../../metadata.min.json' assert { type: 'json' }
describe('findPhoneNumbers', () => {
it('should find numbers', () => {
findNumbers('2133734253', 'US', metadata).should.deep.equal([{
phone : '2133734253',
country : 'US',
startsAt : 0,
endsAt : 10
}])
findNumbers('(213) 373-4253', 'US', metadata).should.deep.equal([{
phone : '2133734253',
country : 'US',
startsAt : 0,
endsAt : 14
}])
findNumbers('The number is +7 (800) 555-35-35 and not (213) 373-4253 as written in the document.', 'US', metadata).should.deep.equal([{
phone : '8005553535',
country : 'RU',
startsAt : 14,
endsAt : 32
}, {
phone : '2133734253',
country : 'US',
startsAt : 41,
endsAt : 55
}])
// Opening parenthesis issue.
// https://github.com/catamphetamine/libphonenumber-js/issues/252
findNumbers('The number is +7 (800) 555-35-35 and not (213) 373-4253 (that\'s not even in the same country!) as written in the document.', 'US', metadata).should.deep.equal([{
phone : '8005553535',
country : 'RU',
startsAt : 14,
endsAt : 32
}, {
phone : '2133734253',
country : 'US',
startsAt : 41,
endsAt : 55
}])
// No default country.
findNumbers('The number is +7 (800) 555-35-35 as written in the document.', metadata).should.deep.equal([{
phone : '8005553535',
country : 'RU',
startsAt : 14,
endsAt : 32
}])
// Passing `options` and default country.
findNumbers('The number is +7 (800) 555-35-35 as written in the document.', 'US', { leniency: 'VALID' }, metadata).should.deep.equal([{
phone : '8005553535',
country : 'RU',
startsAt : 14,
endsAt : 32
}])
// Passing `options`.
findNumbers('The number is +7 (800) 555-35-35 as written in the document.', { leniency: 'VALID' }, metadata).should.deep.equal([{
phone : '8005553535',
country : 'RU',
startsAt : 14,
endsAt : 32
}])
// Not a phone number and a phone number.
findNumbers('Digits 12 are not a number, but +7 (800) 555-35-35 is.', { leniency: 'VALID' }, metadata).should.deep.equal([{
phone : '8005553535',
country : 'RU',
startsAt : 32,
endsAt : 50
}])
// Phone number extension.
findNumbers('Date 02/17/2018 is not a number, but +7 (800) 555-35-35 ext. 123 is.', { leniency: 'VALID' }, metadata).should.deep.equal([{
phone : '8005553535',
country : 'RU',
ext : '123',
startsAt : 37,
endsAt : 64
}])
})
it('shouldn\'t find non-valid numbers', () => {
// Not a valid phone number for US.
findNumbers('1111111111', 'US', metadata).should.deep.equal([])
})
it('should find non-European digits', () => {
// E.g. in Iraq they don't write `+442323234` but rather `+٤٤٢٣٢٣٢٣٤`.
findNumbers('العَرَبِيَّة‎ +٤٤٣٣٣٣٣٣٣٣٣٣عَرَبِيّ‎', metadata).should.deep.equal([{
country : 'GB',
phone : '3333333333',
startsAt : 14,
endsAt : 27
}])
})
it('should iterate', () => {
const expected_numbers = [{
country : 'RU',
phone : '8005553535',
// number : '+7 (800) 555-35-35',
startsAt : 14,
endsAt : 32
}, {
country : 'US',
phone : '2133734253',
// number : '(213) 373-4253',
startsAt : 41,
endsAt : 55
}]
for (const number of searchPhoneNumbers('The number is +7 (800) 555-35-35 and not (213) 373-4253 as written in the document.', 'US', metadata)) {
number.should.deep.equal(expected_numbers.shift())
}
expected_numbers.length.should.equal(0)
})
it('should work in edge cases', () => {
let thrower
// No input
findNumbers('', metadata).should.deep.equal([])
// No country metadata for this `require` country code
thrower = () => findNumbers('123', 'ZZ', metadata)
thrower.should.throw('Unknown country')
// Numerical `value`
thrower = () => findNumbers(2141111111, 'US')
thrower.should.throw('A text for parsing must be a string.')
// // No metadata
// thrower = () => findNumbers('')
// thrower.should.throw('`metadata` argument not passed')
})
it('shouldn\'t find phone numbers which are not phone numbers', () => {
// A timestamp.
findNumbers('2012-01-02 08:00', 'US', metadata).should.deep.equal([])
// A valid number (not a complete timestamp).
findNumbers('2012-01-02 08', 'US', metadata).should.deep.equal([{
country : 'US',
phone : '2012010208',
startsAt : 0,
endsAt : 13
}])
// Invalid parens.
findNumbers('213(3734253', 'US', metadata).should.deep.equal([])
// Letters after phone number.
findNumbers('2133734253a', 'US', metadata).should.deep.equal([])
// Valid phone (same as the one found in the UUID below).
findNumbers('The phone number is 231354125.', 'FR', metadata).should.deep.equal([{
country : 'FR',
phone : '231354125',
startsAt : 20,
endsAt : 29
}])
// Not a phone number (part of a UUID).
// Should parse in `{ extended: true }` mode.
const possibleNumbers = findNumbers('The UUID is CA801c26f98cd16e231354125ad046e40b.', 'FR', { extended: true }, metadata)
possibleNumbers.length.should.equal(3)
possibleNumbers[1].country.should.equal('FR')
possibleNumbers[1].phone.should.equal('231354125')
// Not a phone number (part of a UUID).
// Shouldn't parse by default.
findNumbers('The UUID is CA801c26f98cd16e231354125ad046e40b.', 'FR', metadata).should.deep.equal([])
})
})
describe('PhoneNumberSearch', () => {
it('should search for phone numbers', () => {
const finder = new PhoneNumberSearch('The number is +7 (800) 555-35-35 and not (213) 373-4253 as written in the document.', { defaultCountry: 'US' }, metadata)
finder.hasNext().should.equal(true)
finder.next().should.deep.equal({
country : 'RU',
phone : '8005553535',
// number : '+7 (800) 555-35-35',
startsAt : 14,
endsAt : 32
})
finder.hasNext().should.equal(true)
finder.next().should.deep.equal({
country : 'US',
phone : '2133734253',
// number : '(213) 373-4253',
startsAt : 41,
endsAt : 55
})
finder.hasNext().should.equal(false)
})
it('should search for phone numbers (no options)', () => {
const finder = new PhoneNumberSearch('The number is +7 (800) 555-35-35', undefined, metadata)
finder.hasNext().should.equal(true)
finder.next().should.deep.equal({
country : 'RU',
phone : '8005553535',
// number : '+7 (800) 555-35-35',
startsAt : 14,
endsAt : 32
})
finder.hasNext().should.equal(false)
})
it('should work in edge cases', () => {
// No options
const search = new PhoneNumberSearch('', undefined, metadata)
// No next element
let thrower = () => search.next()
thrower.should.throw('No next element')
})
})

View File

@@ -0,0 +1,191 @@
// This is a legacy function.
// Use `findNumbers()` instead.
import {
PLUS_CHARS,
VALID_PUNCTUATION,
VALID_DIGITS,
WHITESPACE
} from '../constants.js'
import parse from '../parse.js'
import { VALID_PHONE_NUMBER_WITH_EXTENSION } from '../helpers/isViablePhoneNumber.js'
import createExtensionPattern from '../helpers/extension/createExtensionPattern.js'
import parsePreCandidate from '../findNumbers/parsePreCandidate.js'
import isValidPreCandidate from '../findNumbers/isValidPreCandidate.js'
import isValidCandidate from '../findNumbers/isValidCandidate.js'
/**
* Regexp of all possible ways to write extensions, for use when parsing. This
* will be run as a case-insensitive regexp match. Wide character versions are
* also provided after each ASCII version. There are three regular expressions
* here. The first covers RFC 3966 format, where the extension is added using
* ';ext='. The second more generic one starts with optional white space and
* ends with an optional full stop (.), followed by zero or more spaces/tabs
* /commas and then the numbers themselves. The other one covers the special
* case of American numbers where the extension is written with a hash at the
* end, such as '- 503#'. Note that the only capturing groups should be around
* the digits that you want to capture as part of the extension, or else parsing
* will fail! We allow two options for representing the accented o - the
* character itself, and one in the unicode decomposed form with the combining
* acute accent.
*/
export const EXTN_PATTERNS_FOR_PARSING = createExtensionPattern('parsing')
const WHITESPACE_IN_THE_BEGINNING_PATTERN = new RegExp('^[' + WHITESPACE + ']+')
const PUNCTUATION_IN_THE_END_PATTERN = new RegExp('[' + VALID_PUNCTUATION + ']+$')
// // Regular expression for getting opening brackets for a valid number
// // found using `PHONE_NUMBER_START_PATTERN` for prepending those brackets to the number.
// const BEFORE_NUMBER_DIGITS_PUNCTUATION = new RegExp('[' + OPENING_BRACKETS + ']+' + '[' + WHITESPACE + ']*' + '$')
const VALID_PRECEDING_CHARACTER_PATTERN = /[^a-zA-Z0-9]/
export default function findPhoneNumbers(text, options, metadata) {
/* istanbul ignore if */
if (options === undefined) {
options = {}
}
const search = new PhoneNumberSearch(text, options, metadata)
const phones = []
while (search.hasNext()) {
phones.push(search.next())
}
return phones
}
/**
* @return ES6 `for ... of` iterator.
*/
export function searchPhoneNumbers(text, options, metadata) {
/* istanbul ignore if */
if (options === undefined) {
options = {}
}
const search = new PhoneNumberSearch(text, options, metadata)
return {
[Symbol.iterator]() {
return {
next: () => {
if (search.hasNext()) {
return {
done: false,
value: search.next()
}
}
return {
done: true
}
}
}
}
}
}
/**
* Extracts a parseable phone number including any opening brackets, etc.
* @param {string} text - Input.
* @return {object} `{ ?number, ?startsAt, ?endsAt }`.
*/
export class PhoneNumberSearch {
constructor(text, options, metadata) {
this.text = text
// If assigning the `{}` default value is moved to the arguments above,
// code coverage would decrease for some weird reason.
this.options = options || {}
this.metadata = metadata
// Iteration tristate.
this.state = 'NOT_READY'
this.regexp = new RegExp(VALID_PHONE_NUMBER_WITH_EXTENSION, 'ig')
}
find() {
const matches = this.regexp.exec(this.text)
if (!matches) {
return
}
let number = matches[0]
let startsAt = matches.index
number = number.replace(WHITESPACE_IN_THE_BEGINNING_PATTERN, '')
startsAt += matches[0].length - number.length
// Fixes not parsing numbers with whitespace in the end.
// Also fixes not parsing numbers with opening parentheses in the end.
// https://github.com/catamphetamine/libphonenumber-js/issues/252
number = number.replace(PUNCTUATION_IN_THE_END_PATTERN, '')
number = parsePreCandidate(number)
const result = this.parseCandidate(number, startsAt)
if (result) {
return result
}
// Tail recursion.
// Try the next one if this one is not a valid phone number.
return this.find()
}
parseCandidate(number, startsAt) {
if (!isValidPreCandidate(number, startsAt, this.text)) {
return
}
// Don't parse phone numbers which are non-phone numbers
// due to being part of something else (e.g. a UUID).
// https://github.com/catamphetamine/libphonenumber-js/issues/213
// Copy-pasted from Google's `PhoneNumberMatcher.js` (`.parseAndValidate()`).
if (!isValidCandidate(number, startsAt, this.text, this.options.extended ? 'POSSIBLE' : 'VALID')) {
return
}
// // Prepend any opening brackets left behind by the
// // `PHONE_NUMBER_START_PATTERN` regexp.
// const text_before_number = text.slice(this.searching_from, startsAt)
// const full_number_starts_at = text_before_number.search(BEFORE_NUMBER_DIGITS_PUNCTUATION)
// if (full_number_starts_at >= 0)
// {
// number = text_before_number.slice(full_number_starts_at) + number
// startsAt = full_number_starts_at
// }
//
// this.searching_from = matches.lastIndex
const result = parse(number, this.options, this.metadata)
if (!result.phone) {
return
}
result.startsAt = startsAt
result.endsAt = startsAt + number.length
return result
}
hasNext() {
if (this.state === 'NOT_READY') {
this.last_match = this.find()
if (this.last_match) {
this.state = 'READY'
} else {
this.state = 'DONE'
}
}
return this.state === 'READY'
}
next() {
// Check the state and find the next match as a side-effect if necessary.
if (!this.hasNext()) {
throw new Error('No next element')
}
// Don't retain that memory any longer than necessary.
const result = this.last_match
this.last_match = null
this.state = 'NOT_READY'
return result
}
}

View File

@@ -0,0 +1,106 @@
import _formatNumber from '../format.js'
import parse from '../parse.js'
import isObject from '../helpers/isObject.js'
export default function formatNumber() {
const {
input,
format,
options,
metadata
} = normalizeArguments(arguments)
return _formatNumber(input, format, options, metadata)
}
// Sort out arguments
function normalizeArguments(args)
{
const [arg_1, arg_2, arg_3, arg_4, arg_5] = Array.prototype.slice.call(args)
let input
let format
let options
let metadata
// Sort out arguments.
// If the phone number is passed as a string.
// `format('8005553535', ...)`.
if (typeof arg_1 === 'string')
{
// If country code is supplied.
// `format('8005553535', 'RU', 'NATIONAL', [options], metadata)`.
if (typeof arg_3 === 'string')
{
format = arg_3
if (arg_5)
{
options = arg_4
metadata = arg_5
}
else
{
metadata = arg_4
}
input = parse(arg_1, { defaultCountry: arg_2, extended: true }, metadata)
}
// Just an international phone number is supplied
// `format('+78005553535', 'NATIONAL', [options], metadata)`.
else
{
if (typeof arg_2 !== 'string')
{
throw new Error('`format` argument not passed to `formatNumber(number, format)`')
}
format = arg_2
if (arg_4)
{
options = arg_3
metadata = arg_4
}
else
{
metadata = arg_3
}
input = parse(arg_1, { extended: true }, metadata)
}
}
// If the phone number is passed as a parsed number object.
// `format({ phone: '8005553535', country: 'RU' }, 'NATIONAL', [options], metadata)`.
else if (isObject(arg_1))
{
input = arg_1
format = arg_2
if (arg_4)
{
options = arg_3
metadata = arg_4
}
else
{
metadata = arg_3
}
}
else throw new TypeError('A phone number must either be a string or an object of shape { phone, [country] }.')
// Legacy lowercase formats.
if (format === 'International') {
format = 'INTERNATIONAL'
} else if (format === 'National') {
format = 'NATIONAL'
}
return {
input,
format,
options,
metadata
}
}

View File

@@ -0,0 +1,240 @@
import metadata from '../../metadata.min.json' assert { type: 'json' }
import _formatNumber from './format.js'
function formatNumber(...parameters) {
parameters.push(metadata)
return _formatNumber.apply(this, parameters)
}
describe('format', () => {
it('should work with the first argument being a E.164 number', () => {
formatNumber('+12133734253', 'NATIONAL').should.equal('(213) 373-4253')
formatNumber('+12133734253', 'INTERNATIONAL').should.equal('+1 213 373 4253')
// Invalid number.
formatNumber('+12111111111', 'NATIONAL').should.equal('(211) 111-1111')
// Formatting invalid E.164 numbers.
formatNumber('+11111', 'INTERNATIONAL').should.equal('+1 1111')
formatNumber('+11111', 'NATIONAL').should.equal('1111')
})
it('should work with the first object argument expanded', () => {
formatNumber('2133734253', 'US', 'NATIONAL').should.equal('(213) 373-4253')
formatNumber('2133734253', 'US', 'INTERNATIONAL').should.equal('+1 213 373 4253')
})
it('should support legacy "National" / "International" formats', () => {
formatNumber('2133734253', 'US', 'National').should.equal('(213) 373-4253')
formatNumber('2133734253', 'US', 'International').should.equal('+1 213 373 4253')
})
it('should format using formats with no leading digits (`format.leadingDigitsPatterns().length === 0`)', () => {
formatNumber({ phone: '12345678901', countryCallingCode: 888 }, 'INTERNATIONAL').should.equal('+888 123 456 78901')
})
it('should sort out the arguments', () => {
const options = {
formatExtension: (number, extension) => `${number} доб. ${extension}`
}
formatNumber({
phone : '8005553535',
country : 'RU',
ext : '123'
},
'NATIONAL', options).should.equal('8 (800) 555-35-35 доб. 123')
// Parse number from string.
formatNumber('+78005553535', 'NATIONAL', options).should.equal('8 (800) 555-35-35')
formatNumber('8005553535', 'RU', 'NATIONAL', options).should.equal('8 (800) 555-35-35')
})
it('should format with national prefix when specifically instructed', () => {
// With national prefix.
formatNumber('88005553535', 'RU', 'NATIONAL').should.equal('8 (800) 555-35-35')
// Without national prefix via an explicitly set option.
formatNumber('88005553535', 'RU', 'NATIONAL', { nationalPrefix: false }).should.equal('800 555-35-35')
})
it('should format valid phone numbers', () => {
// Switzerland
formatNumber({ country: 'CH', phone: '446681800' }, 'INTERNATIONAL').should.equal('+41 44 668 18 00')
formatNumber({ country: 'CH', phone: '446681800' }, 'E.164').should.equal('+41446681800')
formatNumber({ country: 'CH', phone: '446681800' }, 'RFC3966').should.equal('tel:+41446681800')
formatNumber({ country: 'CH', phone: '446681800' }, 'NATIONAL').should.equal('044 668 18 00')
// France
formatNumber({ country: 'FR', phone: '169454850' }, 'NATIONAL').should.equal('01 69 45 48 50')
// Kazakhstan
formatNumber('+7 702 211 1111', 'NATIONAL').should.deep.equal('8 (702) 211 1111')
})
it('should format national numbers with national prefix even if it\'s optional', () => {
// Russia
formatNumber({ country: 'RU', phone: '9991234567' }, 'NATIONAL').should.equal('8 (999) 123-45-67')
})
it('should work in edge cases', () => {
let thrower
// No phone number
formatNumber('', 'RU', 'INTERNATIONAL').should.equal('')
formatNumber('', 'RU', 'NATIONAL').should.equal('')
formatNumber({ country: 'RU', phone: '' }, 'INTERNATIONAL').should.equal('+7')
formatNumber({ country: 'RU', phone: '' }, 'NATIONAL').should.equal('')
// No suitable format
formatNumber('+121337342530', 'US', 'NATIONAL').should.equal('21337342530')
// No suitable format (leading digits mismatch)
formatNumber('28199999', 'AD', 'NATIONAL').should.equal('28199999')
// Numerical `value`
thrower = () => formatNumber(89150000000, 'RU', 'NATIONAL')
thrower.should.throw('A phone number must either be a string or an object of shape { phone, [country] }.')
// No metadata for country
expect(() => formatNumber('+121337342530', 'USA', 'NATIONAL')).to.throw('Unknown country')
expect(() => formatNumber('21337342530', 'USA', 'NATIONAL')).to.throw('Unknown country')
// No format type
thrower = () => formatNumber('+123')
thrower.should.throw('`format` argument not passed')
// Unknown format type
thrower = () => formatNumber('123', 'US', 'Gay')
thrower.should.throw('Unknown "format" argument')
// No metadata
thrower = () => _formatNumber('123', 'US', 'E.164')
thrower.should.throw('`metadata`')
// No formats
formatNumber('012345', 'AC', 'NATIONAL').should.equal('012345')
// No `fromCountry` for `IDD` format.
expect(formatNumber('+78005553535', 'IDD')).to.be.undefined
// `fromCountry` has no default IDD prefix.
expect(formatNumber('+78005553535', 'IDD', { fromCountry: 'BO' })).to.be.undefined
// No such country.
expect(() => formatNumber({ phone: '123', country: 'USA' }, 'NATIONAL')).to.throw('Unknown country')
})
it('should format phone number extensions', () => {
// National
formatNumber({
country: 'US',
phone: '2133734253',
ext: '123'
},
'NATIONAL').should.equal('(213) 373-4253 ext. 123')
// International
formatNumber({
country : 'US',
phone : '2133734253',
ext : '123'
},
'INTERNATIONAL').should.equal('+1 213 373 4253 ext. 123')
// International
formatNumber({
country : 'US',
phone : '2133734253',
ext : '123'
},
'INTERNATIONAL').should.equal('+1 213 373 4253 ext. 123')
// E.164
formatNumber({
country : 'US',
phone : '2133734253',
ext : '123'
},
'E.164').should.equal('+12133734253')
// RFC3966
formatNumber({
country : 'US',
phone : '2133734253',
ext : '123'
},
'RFC3966').should.equal('tel:+12133734253;ext=123')
// Custom ext prefix.
formatNumber({
country : 'GB',
phone : '7912345678',
ext : '123'
},
'INTERNATIONAL').should.equal('+44 7912 345678 x123')
})
it('should work with Argentina numbers', () => {
// The same mobile number is written differently
// in different formats in Argentina:
// `9` gets prepended in international format.
formatNumber({ country: 'AR', phone: '3435551212' }, 'INTERNATIONAL')
.should.equal('+54 3435 55 1212')
formatNumber({ country: 'AR', phone: '3435551212' }, 'NATIONAL')
.should.equal('03435 55-1212')
})
it('should work with Mexico numbers', () => {
// Fixed line.
formatNumber({ country: 'MX', phone: '4499780001' }, 'INTERNATIONAL')
.should.equal('+52 449 978 0001')
formatNumber({ country: 'MX', phone: '4499780001' }, 'NATIONAL')
.should.equal('449 978 0001')
// or '(449)978-0001'.
// Mobile.
// `1` is prepended before area code to mobile numbers in international format.
formatNumber({ country: 'MX', phone: '3312345678' }, 'INTERNATIONAL')
.should.equal('+52 33 1234 5678')
formatNumber({ country: 'MX', phone: '3312345678' }, 'NATIONAL')
.should.equal('33 1234 5678')
// or '045 33 1234-5678'.
})
it('should format possible numbers', () => {
formatNumber({ countryCallingCode: '7', phone: '1111111111' }, 'E.164')
.should.equal('+71111111111')
formatNumber({ countryCallingCode: '7', phone: '1111111111' }, 'NATIONAL')
.should.equal('1111111111')
formatNumber({ countryCallingCode: '7', phone: '1111111111' }, 'INTERNATIONAL')
.should.equal('+7 1111111111')
})
it('should format IDD-prefixed number', () => {
// No `fromCountry`.
expect(formatNumber('+78005553535', 'IDD')).to.be.undefined
// No default IDD prefix.
expect(formatNumber('+78005553535', 'IDD', { fromCountry: 'BO' })).to.be.undefined
// Same country calling code.
formatNumber('+12133734253', 'IDD', { fromCountry: 'CA', humanReadable: true }).should.equal('1 (213) 373-4253')
formatNumber('+78005553535', 'IDD', { fromCountry: 'KZ', humanReadable: true }).should.equal('8 (800) 555-35-35')
// formatNumber('+78005553535', 'IDD', { fromCountry: 'US' }).should.equal('01178005553535')
formatNumber('+78005553535', 'IDD', { fromCountry: 'US', humanReadable: true }).should.equal('011 7 800 555 35 35')
})
it('should format non-geographic numbering plan phone numbers', () => {
// https://github.com/catamphetamine/libphonenumber-js/issues/323
formatNumber('+870773111632', 'INTERNATIONAL').should.equal('+870 773 111 632')
formatNumber('+870773111632', 'NATIONAL').should.equal('773 111 632')
})
it('should use the default IDD prefix when formatting a phone number', () => {
// Testing preferred international prefixes with ~ are supported.
// ("~" designates waiting on a line until proceeding with the input).
formatNumber('+390236618300', 'IDD', { fromCountry: 'BY' }).should.equal('8~10 39 02 3661 8300')
})
})

View File

@@ -0,0 +1,111 @@
import isViablePhoneNumber from '../helpers/isViablePhoneNumber.js'
import _getNumberType from '../helpers/getNumberType.js'
import isObject from '../helpers/isObject.js'
import parse from '../parse.js'
// Finds out national phone number type (fixed line, mobile, etc)
export default function getNumberType() {
const { input, options, metadata } = normalizeArguments(arguments)
// `parseNumber()` would return `{}` when no phone number could be parsed from the input.
if (!input.phone) {
return
}
return _getNumberType(input, options, metadata)
}
// Sort out arguments
export function normalizeArguments(args)
{
const [arg_1, arg_2, arg_3, arg_4] = Array.prototype.slice.call(args)
let input
let options = {}
let metadata
// If the phone number is passed as a string.
// `getNumberType('88005553535', ...)`.
if (typeof arg_1 === 'string')
{
// If "default country" argument is being passed
// then convert it to an `options` object.
// `getNumberType('88005553535', 'RU', metadata)`.
if (!isObject(arg_2))
{
if (arg_4)
{
options = arg_3
metadata = arg_4
}
else
{
metadata = arg_3
}
// `parse` extracts phone numbers from raw text,
// therefore it will cut off all "garbage" characters,
// while this `validate` function needs to verify
// that the phone number contains no "garbage"
// therefore the explicit `isViablePhoneNumber` check.
if (isViablePhoneNumber(arg_1))
{
input = parse(arg_1, { defaultCountry: arg_2 }, metadata)
}
else
{
input = {}
}
}
// No "resrict country" argument is being passed.
// International phone number is passed.
// `getNumberType('+78005553535', metadata)`.
else
{
if (arg_3)
{
options = arg_2
metadata = arg_3
}
else
{
metadata = arg_2
}
// `parse` extracts phone numbers from raw text,
// therefore it will cut off all "garbage" characters,
// while this `validate` function needs to verify
// that the phone number contains no "garbage"
// therefore the explicit `isViablePhoneNumber` check.
if (isViablePhoneNumber(arg_1))
{
input = parse(arg_1, undefined, metadata)
}
else
{
input = {}
}
}
}
// If the phone number is passed as a parsed phone number.
// `getNumberType({ phone: '88005553535', country: 'RU' }, ...)`.
else if (isObject(arg_1))
{
input = arg_1
if (arg_3)
{
options = arg_2
metadata = arg_3
}
else
{
metadata = arg_2
}
}
else throw new TypeError('A phone number must either be a string or an object of shape { phone, [country] }.')
return {
input,
options,
metadata
}
}

View File

@@ -0,0 +1,65 @@
import metadata from '../../metadata.max.json' assert { type: 'json' }
import Metadata from '../metadata.js'
import _getNumberType from './getNumberType.js'
function getNumberType(...parameters) {
parameters.push(metadata)
return _getNumberType.apply(this, parameters)
}
describe('getNumberType', () => {
it('should infer phone number type MOBILE', () => {
getNumberType('9150000000', 'RU').should.equal('MOBILE')
getNumberType('7912345678', 'GB').should.equal('MOBILE')
getNumberType('51234567', 'EE').should.equal('MOBILE')
})
it('should infer phone number types', () => {
getNumberType('88005553535', 'RU').should.equal('TOLL_FREE')
getNumberType('8005553535', 'RU').should.equal('TOLL_FREE')
getNumberType('4957777777', 'RU').should.equal('FIXED_LINE')
getNumberType('8030000000', 'RU').should.equal('PREMIUM_RATE')
getNumberType('2133734253', 'US').should.equal('FIXED_LINE_OR_MOBILE')
getNumberType('5002345678', 'US').should.equal('PERSONAL_NUMBER')
})
it('should work when no country is passed', () => {
getNumberType('+79150000000').should.equal('MOBILE')
})
it('should return FIXED_LINE_OR_MOBILE when there is ambiguity', () => {
// (no such country in the metadata, therefore no unit test for this `if`)
})
it('should work in edge cases', function() {
let thrower
// // No metadata
// thrower = () => _getNumberType({ phone: '+78005553535' })
// thrower.should.throw('`metadata` argument not passed')
// Parsed phone number
getNumberType({ phone: '8005553535', country: 'RU' }).should.equal('TOLL_FREE')
// Invalid phone number
type(getNumberType('123', 'RU')).should.equal('undefined')
// Invalid country
thrower = () => getNumberType({ phone: '8005553535', country: 'RUS' })
thrower.should.throw('Unknown country')
// Numerical `value`
thrower = () => getNumberType(89150000000, 'RU')
thrower.should.throw('A phone number must either be a string or an object of shape { phone, [country] }.')
// When `options` argument is passed.
getNumberType('8005553535', 'RU', {}).should.equal('TOLL_FREE')
getNumberType('+78005553535', {}).should.equal('TOLL_FREE')
getNumberType({ phone: '8005553535', country: 'RU' }, {}).should.equal('TOLL_FREE')
})
})
function type(something) {
return typeof something
}

View File

@@ -0,0 +1,25 @@
import { normalizeArguments } from './getNumberType.js'
import _isPossibleNumber from '../isPossible.js'
/**
* Checks if a given phone number is possible.
* Which means it only checks phone number length
* and doesn't test any regular expressions.
*
* Examples:
*
* ```js
* isPossibleNumber('+78005553535', metadata)
* isPossibleNumber('8005553535', 'RU', metadata)
* isPossibleNumber('88005553535', 'RU', metadata)
* isPossibleNumber({ phone: '8005553535', country: 'RU' }, metadata)
* ```
*/
export default function isPossibleNumber() {
const { input, options, metadata } = normalizeArguments(arguments)
// `parseNumber()` would return `{}` when no phone number could be parsed from the input.
if (!input.phone && !(options && options.v2)) {
return false
}
return _isPossibleNumber(input, options, metadata)
}

View File

@@ -0,0 +1,40 @@
import metadata from '../../metadata.min.json' assert { type: 'json' }
import _isPossibleNumber from './isPossibleNumber.js'
function isPossibleNumber(...parameters) {
parameters.push(metadata)
return _isPossibleNumber.apply(this, parameters)
}
describe('isPossibleNumber', () => {
it('should work', function()
{
isPossibleNumber('+79992223344').should.equal(true)
isPossibleNumber({ phone: '1112223344', country: 'RU' }).should.equal(true)
isPossibleNumber({ phone: '111222334', country: 'RU' }).should.equal(false)
isPossibleNumber({ phone: '11122233445', country: 'RU' }).should.equal(false)
isPossibleNumber({ phone: '1112223344', countryCallingCode: 7 }).should.equal(true)
})
it('should work v2', () => {
isPossibleNumber({ nationalNumber: '111222334', countryCallingCode: 7 }, { v2: true }).should.equal(false)
isPossibleNumber({ nationalNumber: '1112223344', countryCallingCode: 7 }, { v2: true }).should.equal(true)
isPossibleNumber({ nationalNumber: '11122233445', countryCallingCode: 7 }, { v2: true }).should.equal(false)
})
it('should work in edge cases', () => {
// Invalid `PhoneNumber` argument.
expect(() => isPossibleNumber({}, { v2: true })).to.throw('Invalid phone number object passed')
// Empty input is passed.
// This is just to support `isValidNumber({})`
// for cases when `parseNumber()` returns `{}`.
isPossibleNumber({}).should.equal(false)
expect(() => isPossibleNumber({ phone: '1112223344' })).to.throw('Invalid phone number object passed')
// Incorrect country.
expect(() => isPossibleNumber({ phone: '1112223344', country: 'XX' })).to.throw('Unknown country')
})
})

View File

@@ -0,0 +1,12 @@
import _isValidNumber from '../isValid.js'
import { normalizeArguments } from './getNumberType.js'
// Finds out national phone number type (fixed line, mobile, etc)
export default function isValidNumber() {
const { input, options, metadata } = normalizeArguments(arguments)
// `parseNumber()` would return `{}` when no phone number could be parsed from the input.
if (!input.phone) {
return false
}
return _isValidNumber(input, options, metadata)
}

Some files were not shown because too many files have changed in this diff Show More