awesome-phonenumber
Advanced tools
Comparing version 6.12.0 to 7.0.0
@@ -110,3 +110,57 @@ export type PhoneNumberFormat = | ||
export type FindNumbersLeniency = | ||
/** | ||
* Phone numbers accepted are possible, but not necessarily valid. | ||
*/ | ||
| 'possible' | ||
/** | ||
* Phone numbers accepted are possible and valid. | ||
*/ | ||
| 'valid'; | ||
export interface FindNumbersOptions | ||
{ | ||
/** | ||
* A default region code, to find local (non-e164) formatted phone numbers | ||
*/ | ||
defaultRegionCode?: string; | ||
/** Leniency options */ | ||
leniency?: FindNumbersLeniency; | ||
/** | ||
* The maximum number of invalid numbers to try before giving up on the text | ||
*/ | ||
maxTries?: number; | ||
} | ||
export interface PhoneNumberMatch | ||
{ | ||
/** The raw string found */ | ||
text: string; | ||
/** The parsed phone number object */ | ||
phoneNumber: ParsedPhoneNumber; | ||
/** Start offset of the found number */ | ||
start: number; | ||
/** End offset of the found number */ | ||
end: number; | ||
} | ||
/** | ||
* Find phone numbers in text. | ||
* | ||
* If the text is expected to have phone numbers for a certain region code, | ||
* the option `defaultRegionCode` can be set. Phone numbers without the | ||
* international prefix `+` will then be found too. | ||
* | ||
* Leniency can be specified using the `leniency` option, in which case | ||
* `maxTries` needs to be set too. | ||
*/ | ||
export function findNumbers( text: string, options?: FindNumbersOptions ) | ||
: PhoneNumberMatch[ ]; | ||
/** | ||
* Get an example phone number, given a region code and a phone number | ||
@@ -113,0 +167,0 @@ * {@link PhoneNumberTypes type}. |
@@ -52,1 +52,2 @@ const exportedName = 'PhoneNumber$$module$src$index'; | ||
module.exports.getNumberFrom = module.exports.getNumberFrom; | ||
module.exports.findNumbers = module.exports.findNumbers; |
@@ -5,3 +5,3 @@ { | ||
"license": "MIT", | ||
"version": "6.12.0", | ||
"version": "7.0.0", | ||
"author": "Gustaf Räntilä <g.rantila@gmail.com>", | ||
@@ -8,0 +8,0 @@ "repository": { |
@@ -12,2 +12,4 @@ [![npm version][npm-image]][npm-url] | ||
Unlike libphonenumber, it includes a `findNumbers( )` function to find phone numbers in text. | ||
TypeScript typings are provided within the package. | ||
@@ -36,2 +38,5 @@ | ||
- Dropped Node 16 support | ||
- v7: | ||
- Added `findNumbers( )` feature, to find phone numbers in text | ||
- Added support for _short_ numbers | ||
@@ -144,2 +149,3 @@ | ||
parsePhoneNumber, | ||
findNumbers, | ||
getNumberFrom, | ||
@@ -163,2 +169,44 @@ getExample, | ||
### findNumbers | ||
To find (extract) phone numbers in text, use `findNumbers( )`: | ||
```ts | ||
import { findNumbers } from 'awesome-phonenumber' | ||
const text = 'My number is +46 707 123 456, otherwise call +33777777777.'; | ||
const numbers = findNumbers( text ); | ||
``` | ||
The returned list of numbers is of the type `PhoneNumberMatch` such as: | ||
```ts | ||
interface PhoneNumberMatch | ||
{ | ||
text: string; // The raw string found | ||
phoneNumber: object; // Same as the result of parsePhoneNumber() | ||
start: number; // Start offset in the text | ||
end: number; // End offset in the text | ||
} | ||
``` | ||
A second options argument to `findNumbers( text, options )` can be provided on the form: | ||
```ts | ||
interface FindNumbersOptions | ||
{ | ||
defaultRegionCode?: string; | ||
leniency?: FindNumbersLeniency; | ||
maxTries?: number; | ||
} | ||
``` | ||
where `FindNumbersLeniency` is an enum of `'valid'` or `'possible'`. The default is `'valid'` meaning that only valid phone numbers are found. If this is set to `'possible'` also possible (but invalid) phone numbers are found. | ||
`defaultRegionCode` can be set (e.g. to `'SE'` for Sweden), in which case phone numbers on _national_ form (i.e. without `+` prefix) will be found, as long as they are from that region. | ||
For really large texts, `maxTries` will set the maximum number of phone numbers to _try_ to find (not necessary actually find). | ||
### getNumberFrom | ||
@@ -165,0 +213,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
721935
3494
410