# @optima-lib/phone-utils

> This an Angular library (and can be used with Angular projects only).

Latest version **17.0.0** (published 2024-06-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install @optima-lib/phone-utils
pnpm add @optima-lib/phone-utils
yarn add @optima-lib/phone-utils
bun add @optima-lib/phone-utils
```

## Health

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

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 17.0.0 |
| Published | 2024-06-11 |
| First published | 2021-11-20 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 34.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Venelin Manchev |
| Maintainers | vmanchev |
| Keywords | angular, phone, format, formatter, libphonenumber |

## Links

- npm: https://www.npmjs.com/package/@optima-lib/phone-utils
- Repository: https://github.com/optima-digital/optima-lib
- Homepage: https://github.com/optima-digital/optima-lib#readme
- Issues: https://github.com/optima-digital/optima-lib/issues
- npm.io page: https://npm.io/package/@optima-lib/phone-utils

## Dependencies (2)

- [tslib](https://npm.io/package/tslib.md) ^2.3.1
- [libphonenumber-js](https://npm.io/package/libphonenumber-js.md) ^1.9.43

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 17.0.0 (latest) — 2024-06-11
- 16.0.0 — 2024-06-11
- 15.0.0 — 2024-06-11
- 14.0.0 — 2024-06-11
- 13.0.0 — 2021-11-20

## README

# OptimaLib Phone Utils

This an Angular library (and can be used with Angular projects only).

It uses [libphonenumber-js](https://gitlab.com/catamphetamine/libphonenumber-js) to provide developers with the angular way of formatting and validating phone numbers.

## Versions grid

| Angular | OptimaLib |
| ------- | --------- |
| 17.x    | 17.x      |
| 16.x    | 16.x      |
| 15.x    | 15.x      |
| 14.x    | 14.x      |
| 13.x    | 13.x      |

## Install

```
npm install --save @optima-lib/phone-utils
```

## OptimaPhoneUtilsService

### Usage

```
import { OptimaPhoneUtilsService } from '@optima-lib/phone-utils';

// Inject in your component or service:
constructor(private phoneUtils: OptimaPhoneUtilsService) { }
```

### Methods

- **getInternational(value: string, country: string): string** - Transform a given phone number to international format

  | Param   | Description              |
  | ------- | ------------------------ |
  | value   | Phone numer              |
  | country | Two-letters country code |

- **getNational(value: string, country: string): string** - Transform a given phone number to its national format

  | Param   | Description              |
  | ------- | ------------------------ |
  | value   | Phone numer              |
  | country | Two-letters country code |

- **getPlain(value: string, country: string): string** - International format without inner spaces (PNF.E164)

  | Param   | Description              |
  | ------- | ------------------------ |
  | value   | Phone numer              |
  | country | Two-letters country code |

- **isValid(value: string, country: string): boolean** - General purpose validator. Use in conjunction with other services
  and components. The method will return true when arguments relates to a phone number from the selected country.

  | Param   | Description              |
  | ------- | ------------------------ |
  | value   | Phone numer              |
  | country | Two-letters country code |

- **isValidFormControl(country: string): boolean** - Reactive form validator. It will set **phoneInvalid** property
  in related form control error's object.

  | Param   | Description               |
  | ------- | ------------------------- |
  | country | Two-letters country code. |

  Usage:

```
// app.component.ts
constructor(public phoneValidatorService: OptimaPhoneValidatorService) { }

public getForm() {
  this.form = new FormGroup({
    phone: new FormControl('', [
      Validators.required,
      this.phoneValidatorService.isValidPhone('BG')
    ])
  });
}

// app.component.html
<span *ngIf="form.get('phone')?.errors?.phoneInvalid">{{ 'FORM.ERROR.PHONE_FORMAT.INVALID' | translate }}</span>
```

## OptimaInternationalFormatPipe

### Usage

Import **OptimaInternationalFormatPipe** in the component where you want to use it:

```
import { OptimaInternationalFormatPipe } from '@optima-lib/phone-utils';

@Component({
  imports: [
    ...,
    OptimaInternationalFormatPipe
  ]
})
export class UserComponent() { }
```

And then it can be used as:

```
{{ '02 1234567' | internationalFormat : 'BG' }}
```

which will produce:

```
+359 2 123 4567
```

## OptimaNationalFormatPipe

### Usage

Import **OptimaNationalFormatPipe** in the component where you want to use it:

```
import { OptimaNationalFormatPipe } from '@optima-lib/phone-utils';

@Component({
  imports: [
    ...,
    OptimaNationalFormatPipe
  ]
})
export class UserComponent() { }
```

And then it can be used as:

```
{{ '02 1234567' | nationalFormat : 'BG' }}
```

which will produce:

```
02 123 4567
```

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