# phonumber

> Phone number parser. Good for formatting phone numbers entered by users.

Latest version **1.0.12** (published 2022-08-31) · MIT license · 0 weekly downloads

## Install

```sh
npm install phonumber
pnpm add phonumber
yarn add phonumber
bun add phonumber
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.12 |
| Published | 2022-08-31 |
| First published | 2022-08-30 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 77 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 6 |
| Author | Alex Baretsky |
| Maintainers | barrsan |
| Keywords | phonumber, phone, parser, format, international, country |

## Links

- npm: https://www.npmjs.com/package/phonumber
- Repository: https://github.com/barrsan/phonumber
- Issues: https://github.com/barrsan/phonumber/issues
- npm.io page: https://npm.io/package/phonumber

## Alternatives

- [babylon](https://npm.io/package/babylon.md) — 5.1M weekly downloads
- [csscolorparser](https://npm.io/package/csscolorparser.md) — 3.7M weekly downloads
- [expr-eval-fork](https://npm.io/package/expr-eval-fork.md) — 1.5M weekly downloads
- [@leeoniya/ufuzzy](https://npm.io/package/@leeoniya/ufuzzy.md) — 247.7K weekly downloads
- [xml-parser](https://npm.io/package/xml-parser.md) — 78.4K weekly downloads

## Recent versions

- 1.0.12 (latest) — 2022-08-31
- 1.0.10 — 2022-08-30
- 1.0.8 — 2022-08-30
- 1.0.7 — 2022-08-30
- 1.0.6 — 2022-08-30
- 1.0.5 — 2022-08-30
- 1.0.4 — 2022-08-30
- 1.0.3 — 2022-08-30
- 1.0.1 — 2022-08-30
- 1.0.0 — 2022-08-30

## README

# Phonumber

Phonumber - it is phone number parser. Good for formatting phone numbers entered by users.


## Installation

```bash
npm i phonumber
```

## Usage

```javascript

import { parsePhoneNumber } from 'phonumber';

const result = parsePhoneNumber('12089999999');

/*

result = {
  code: 'US',
  dialCode: '+1',
  nationalNumber: '2089999999',
  formattedNumber: '+1 2089999999',
}

*/

```

## Config

### Resolvers

You can tell the parser how to resolve the value passed to it.

**resolvers** - it is an array contains objects with the following properties:

| Property | Type | Description|
|---|---|---|
| **firstInputChars** | Object (required) | It is an object where keys are first input char sended to parser and values are object which contains resolver options for this char. |
| **resolveAs** | Object (required) | Data whitch detect how resolve first input char. |

#### firstInputChars property

| Property | Type | Description|
|---|---|---|
| **mode** | "add" \| "replace" (required) | **"Replace"** mode replaces first input char with the dial code.<br /> **"Add"** mode puts the dial code before this first input char. The dial code is determined based on data from "resolveAs". |

#### resolveAs

| Property | Type | Description|
|---|---|---|
| **firstChar** | string (required) | The first digit of the country code |
| **code** | string (required) | Country code (US, RU, ...) |

```javascript

const resolvers = [
  {
    firstInputChars: {
      9: {
        mode: 'add',
      },
      8: {
        mode: 'replace',
      },
    },
    resolveAs: {
      firstChar: 7,
      code: 'RU',
    },
  },
];

const result1 = parsePhoneNumber('89191238899', {
  resolvers,
});

const result2 = parsePhoneNumber('9191238899', {
  resolvers,
});

/*

result1 = {
  code: 'RU',
  dialCode: '+7',
  nationalNumber: '9191238899',
  formattedNumber: '+7 9191238899',
}

result2 = {
  code: 'RU',
  dialCode: '+7',
  nationalNumber: '9191238899',
  formattedNumber: '+7 9191238899',
}

*/

```

### Formats

**Formats** - it is an object where keys are country codes and values are objects with the following properties:

| Property | Type | Description|
|---|---|---|
| **mask** | string | Mask for the entered number without a dial code. The dial code will be filled automatically. |
| **withTail** | boolean | Add numbers that are not counted in the mask to the phone number. |

```javascript

const formats = {
    RU: {
      mask: '(###) ### ####',
    },
    BY: {
      mask: '## ### ## ##',
    },
    US: {
      mask: '###-###-####',
      withTail: true,
    },
  };

const result = parsePhoneNumber('12089999999', {
  formats,
});

/*

result = {
  code: 'US',
  dialCode: '+1',
  nationalNumber: '2089999999',
  formattedNumber: '+1 208-999-9999',
}

*/

---
_Source: https://npm.io/package/phonumber · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
