index.d.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import {
  2. MetadataJson,
  3. Examples,
  4. PhoneNumber,
  5. E164Number,
  6. CountryCallingCode,
  7. CountryCode,
  8. CarrierCode,
  9. NationalNumber,
  10. Extension,
  11. ParseError,
  12. NumberFoundLegacy,
  13. NumberFound,
  14. NumberType,
  15. NumberFormat,
  16. NumberingPlan,
  17. ValidatePhoneNumberLengthResult
  18. } from './types.d.js';
  19. export {
  20. MetadataJson,
  21. Examples,
  22. PhoneNumber,
  23. E164Number,
  24. CountryCallingCode,
  25. CountryCode,
  26. CarrierCode,
  27. NationalNumber,
  28. Extension,
  29. ParseError,
  30. NumberFoundLegacy,
  31. NumberFound,
  32. NumberFormat,
  33. NumberType,
  34. NumberingPlan,
  35. ValidatePhoneNumberLengthResult
  36. };
  37. type FormatExtension = (number: string, extension: string, metadata: MetadataJson) => string
  38. type FormatNumberOptionsWithoutIDD = {
  39. v2?: boolean;
  40. formatExtension?: FormatExtension;
  41. };
  42. export type FormatNumberOptions = {
  43. v2?: boolean;
  44. fromCountry?: CountryCode;
  45. humanReadable?: boolean;
  46. formatExtension?: FormatExtension;
  47. };
  48. // Legacy.
  49. export type ParseNumberOptions = {
  50. defaultCountry?: CountryCode;
  51. extended?: boolean;
  52. };
  53. export interface ParsedNumber {
  54. countryCallingCode?: CountryCallingCode;
  55. country: CountryCode;
  56. phone: NationalNumber;
  57. ext?: Extension;
  58. possible?: boolean;
  59. valid?: boolean;
  60. }
  61. // `parsePhoneNumber()` named export has been renamed to `parsePhoneNumberWithError()`.
  62. export function parsePhoneNumber(text: string, defaultCountry?: CountryCode | { defaultCountry?: CountryCode, defaultCallingCode?: string, extract?: boolean }): PhoneNumber;
  63. export function parsePhoneNumberWithError(text: string, defaultCountry?: CountryCode | { defaultCountry?: CountryCode, defaultCallingCode?: string, extract?: boolean }): PhoneNumber;
  64. // `parsePhoneNumberFromString()` named export is now considered legacy:
  65. // it has been promoted to a default export due to being too verbose.
  66. export function parsePhoneNumberFromString(text: string, defaultCountry?: CountryCode | { defaultCountry?: CountryCode, defaultCallingCode?: string, extract?: boolean }): PhoneNumber | undefined;
  67. export default parsePhoneNumberFromString;
  68. export function isValidPhoneNumber(text: string, defaultCountry?: CountryCode | { defaultCountry?: CountryCode, defaultCallingCode?: string }): boolean;
  69. export function isPossiblePhoneNumber(text: string, defaultCountry?: CountryCode | { defaultCountry?: CountryCode, defaultCallingCode?: string }): boolean;
  70. export function validatePhoneNumberLength(text: string, defaultCountry?: CountryCode | { defaultCountry?: CountryCode, defaultCallingCode?: string }): ValidatePhoneNumberLengthResult | undefined;
  71. // `parse()` and `parseCustom` are deprecated.
  72. // Use `parseNumber()` and `parseNumberCustom()` instead.
  73. export function parse(text: string, options?: CountryCode | ParseNumberOptions): ParsedNumber;
  74. export function parseNumber(text: string, options?: CountryCode | ParseNumberOptions): ParsedNumber | {};
  75. // `format()` and `formatCustom` are deprecated.
  76. // Use `formatNumber()` and `formatNumberCustom()` instead.
  77. export function format(parsedNumber: ParsedNumber, format: NumberFormat): string;
  78. export function format(phone: NationalNumber, format: NumberFormat): string;
  79. export function format(phone: NationalNumber, country: CountryCode, format: NumberFormat): string;
  80. export function formatNumber(parsedNumber: ParsedNumber, format: NumberFormat, options?: FormatNumberOptions): string;
  81. export function formatNumber(phone: NationalNumber, format: NumberFormat, options?: FormatNumberOptions): string;
  82. export function formatNumber(phone: NationalNumber, country: CountryCode, format: NumberFormat, options?: FormatNumberOptions): string;
  83. export function getNumberType(parsedNumber: ParsedNumber): NumberType;
  84. export function getNumberType(phone: NationalNumber, country?: CountryCode): NumberType;
  85. export function getExampleNumber(country: CountryCode, examples: Examples): PhoneNumber | undefined;
  86. export function isPossibleNumber(parsedNumber: ParsedNumber): boolean;
  87. export function isPossibleNumber(phone: NationalNumber, country?: CountryCode): boolean;
  88. export function isValidNumber(parsedNumber: ParsedNumber): boolean;
  89. export function isValidNumber(phone: NationalNumber, country?: CountryCode): boolean;
  90. export function isValidNumberForRegion(phone: NationalNumber, country: CountryCode): boolean;
  91. // Deprecated.
  92. export function findParsedNumbers(text: string, options?: CountryCode | { defaultCountry?: CountryCode }): NumberFoundLegacy[];
  93. export function searchParsedNumbers(text: string, options?: CountryCode | { defaultCountry?: CountryCode }): IterableIterator<NumberFoundLegacy>;
  94. // Deprecated.
  95. export class ParsedNumberSearch {
  96. constructor(text: string, options?: { defaultCountry?: CountryCode });
  97. hasNext(): boolean;
  98. next(): NumberFoundLegacy | undefined;
  99. }
  100. export function findNumbers(text: string, options?: CountryCode): NumberFoundLegacy[];
  101. export function searchNumbers(text: string, options?: CountryCode): IterableIterator<NumberFoundLegacy>;
  102. export function findNumbers(text: string, options?: { defaultCountry?: CountryCode, v2: true }): NumberFound[];
  103. export function searchNumbers(text: string, options?: { defaultCountry?: CountryCode, v2: true }): IterableIterator<NumberFound>;
  104. export function findPhoneNumbersInText(text: string, options?: CountryCode | { defaultCountry?: CountryCode, defaultCallingCode?: string, extended?: boolean }): NumberFound[];
  105. export function searchPhoneNumbersInText(text: string, options?: CountryCode | { defaultCountry?: CountryCode, defaultCallingCode?: string, extended?: boolean }): IterableIterator<NumberFound>;
  106. export class PhoneNumberMatcher {
  107. constructor(text: string, options?: { defaultCountry?: CountryCode, v2: true });
  108. hasNext(): boolean;
  109. next(): NumberFound | undefined;
  110. }
  111. export function getCountries(): CountryCode[];
  112. export function getCountryCallingCode(countryCode: CountryCode): CountryCallingCode;
  113. // Deprecated.
  114. export function getPhoneCode(countryCode: CountryCode): CountryCallingCode;
  115. export function getExtPrefix(countryCode: CountryCode): string;
  116. export function isSupportedCountry(countryCode: string): countryCode is CountryCode;
  117. export function formatIncompletePhoneNumber(number: string, countryCode?: CountryCode): string;
  118. export function formatIncompletePhoneNumber(number: string, defaultCountryCode?: CountryCode | { defaultCountry?: CountryCode, defaultCallingCode?: string }): string;
  119. export function parseIncompletePhoneNumber(text: string): string;
  120. export function parsePhoneNumberCharacter(character: string): string;
  121. export function parseDigits(character: string): string;
  122. export class AsYouType {
  123. constructor(defaultCountryCode?: CountryCode | { defaultCountry?: CountryCode, defaultCallingCode?: string });
  124. input(text: string): string;
  125. reset(): void;
  126. country: CountryCode | undefined;
  127. getNumber(): PhoneNumber | undefined;
  128. getNumberValue(): E164Number | undefined;
  129. getNationalNumber(): string;
  130. getChars(): string;
  131. getTemplate(): string;
  132. getCallingCode(): string | undefined;
  133. getCountry(): CountryCode | undefined;
  134. isInternational(): boolean;
  135. isPossible(): boolean;
  136. isValid(): boolean;
  137. }
  138. export class Metadata {
  139. constructor();
  140. selectNumberingPlan(country: CountryCode): void;
  141. numberingPlan?: NumberingPlan;
  142. }