# format-string-by-pattern

> A module that formats a string based on a pattern

Latest version **1.2.2** (published 2021-01-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install format-string-by-pattern
pnpm add format-string-by-pattern
yarn add format-string-by-pattern
bun add format-string-by-pattern
```

## Health

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

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

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.2 |
| Published | 2021-01-28 |
| First published | 2018-05-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=10 |
| Dependencies | 0 |
| Unpacked size | 21.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 78 |
| Author | Arthur Denner |
| Maintainers | arthurdenner |
| Keywords | format, string, pattern |

## Links

- npm: https://www.npmjs.com/package/format-string-by-pattern
- Repository: https://github.com/arthurdenner/format-string-by-pattern
- Homepage: https://github.com/arthurdenner/format-string-by-pattern#readme
- Issues: https://github.com/arthurdenner/format-string-by-pattern/issues
- npm.io page: https://npm.io/package/format-string-by-pattern

## 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

- 1.2.2 (latest) — 2021-01-28
- 1.2.1 — 2020-02-29
- 1.1.1 — 2019-02-25
- 1.1.0 — 2019-02-25
- 1.0.0 — 2018-08-24
- 0.3.1 — 2018-07-02
- 0.3.0 — 2018-05-20
- 0.2.4 — 2018-05-14
- 0.2.3 — 2018-05-13
- 0.2.2 — 2018-05-13
- 0.2.1 — 2018-05-11
- 0.2.0 — 2018-05-10
- 0.1.0 — 2018-05-10

## README

<h1 align="center">
  format-string-by-pattern
</h1>

<p align="center">
<a href="https://www.npmjs.org/package/format-string-by-pattern"><img src="https://img.shields.io/npm/dw/format-string-by-pattern?style=flat" alt="npm"></a>
<a href="https://www.npmjs.org/package/format-string-by-pattern"><img src="https://img.shields.io/npm/v/format-string-by-pattern.svg?style=flat" alt="npm"></a>
<a href="https://codecov.io/gh/arthurdenner/format-string-by-pattern"><img src="https://img.shields.io/codecov/c/github/arthurdenner/format-string-by-pattern" alt="codecov" /></a>
<a href="https://david-dm.org/arthurdenner/format-string-by-pattern"><img src="https://david-dm.org/arthurdenner/format-string-by-pattern/status.svg" alt="dependencies Status"></a>
<a href="https://unpkg.com/format-string-by-pattern/dist/format-string-by-pattern.umd.js"><img src="https://img.badgesize.io/https://unpkg.com/format-string-by-pattern/dist/format-string-by-pattern.umd.js?compression=gzip" alt="gzip size"></a>
<a href="https://packagephobia.now.sh/result?p=format-string-by-pattern"><img src="https://packagephobia.now.sh/badge?p=format-string-by-pattern" alt="install size"></a>
</p>

## Overview

This module consists on a function that receives a pattern and a value and returns this value formatted according to the pattern. It doesn't work with every pattern, but it works with a lot of use cases, especially if you build functions on top of this module.

## Install

```
yarn add format-string-by-pattern
```

```
npm i format-string-by-pattern
```

### UMD

```
<script src="https://unpkg.com/format-string-by-pattern/dist/format-string-by-pattern.umd.js"></script>
```

## Usage

```js
const formatStringByPattern = require('format-string-by-pattern');

formatStringByPattern('YYYY-MM-DD', '20180508');
// '2018-05-08'

formatStringByPattern('2018-05-08', '20151217');
// '2015-12-17'

const formatOnlyNumbers = (anyString) => {
  const onlyNumbers = anyString.replace(/[^\d]/g, '');

  return formatStringByPattern('999-999-9999', onlyNumbers);
};
formatOnlyNumbers('1A2B3C4D5E6F7G8H9');
// '123-456-789'

// It works with curry too
const someFormat = formatStringByPattern('00.00');
someFormat(1234);
// 12.34
```

> NOTE: The returned value will be sliced to the size of the pattern.

This library is pretty useful with forms, where you need to parse the input values.
See the usage with the [react-final-form](https://github.com/final-form/react-final-form) library [here](https://codesandbox.io/s/no20p7z3l).

## API

### formatStringByPattern(pattern, valueToFormat)

Returns a `string`.

#### pattern

Type: `string`

A string where anything that is not a number or letter will be treated as a separator.

#### valueToFormat

Type: `string` or `number`

A value to be formatted.

## License

MIT © [Arthur Denner](https://github.com/arthurdenner/)

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