# mehrzahl

> Tagged template literals for singular/plural formatting

Latest version **1.1.0** (published 2021-10-11) · MIT license · 0 weekly downloads

## Install

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

## 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.1.0 |
| Published | 2021-10-11 |
| First published | 2021-09-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 22 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 9 |
| Author | Joschua Schneider |
| Maintainers | joschuaschneider |
| Keywords | singular/plural, tagged-template-literal, formatting, helper, plural-tag |

## Links

- npm: https://www.npmjs.com/package/mehrzahl
- Repository: https://github.com/JoschuaSchneider/mehrzahl
- Homepage: https://github.com/JoschuaSchneider/mehrzahl#readme
- Issues: https://github.com/JoschuaSchneider/mehrzahl/issues
- npm.io page: https://npm.io/package/mehrzahl

## 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.1.0 (latest) — 2021-10-11
- 1.0.1 — 2021-09-28
- 1.0.0 — 2021-09-17
- 0.0.1-alpha3 — 2021-09-17
- 0.0.1-alpha2 — 2021-09-17
- 0.0.1-alpha1 — 2021-09-17

## README

# mehrzahl

[![npm version](https://badgen.net/npm/v/mehrzahl)](https://www.npmjs.com/package/mehrzahl)
![Bundlephobia Minzipped Size](https://badgen.net/bundlephobia/minzip/mehrzahl)
![build workflow](https://github.com/JoschuaSchneider/mehrzahl/actions/workflows/build.yml/badge.svg?branch=main)
![test workflow](https://github.com/JoschuaSchneider/mehrzahl/actions/workflows/test.yml/badge.svg?branch=main)
![TypeScript](https://badgen.net/badge/-/TypeScript/blue?icon=typescript&label)
![License: MIT](https://badgen.net/npm/license/mehrzahl)

Tiny utility to format parts of a template string in singular or plural.

### Installation

```bash
npm i mehrzahl
```
```bash
yarn add mehrzahl
```

### Usage

```ts
import { mz } from "mehrzahl"
// import mz from "mehrzahl" // default import

const str = mz(1)`There {was|were} $value person{|s} at this event.`
// str = "There was 1 person at this event."
const str = mz(5)`There {was|were} $value person{|s} at this event.`
// str = "There were 5 persons at this event."
```
#### Reversed usage
You can use the exported `zm` function to create a function curried with the template first and then calling it with the amount:
```ts
import { zm } from "mehrzahl"

const template = zm`There {was|were} $value person{|s} at this event.`
template(5) // There were 5 persons at this event.
template(0) // There was 1 person at this event.
```

💡 `$value` is replaced by the amount specified.

#### Singular/Plural tuples

```ts
const str = mz(1)`There ${['was', 'were']} $value person${[,'s']} at this event.`
// str = "There was 1 person at this event."
const str = mz(5)`There ${['was', 'were']} $value person${[,'s']} at this event.`
// str = "There were 5 persons at this event."
```

💡 Left or right value of the delimiter `|` can be omitted:
`{|plural}` or `{singular|}`

💡 First or second value of tuples can be omitted:
`[,'plural']` or `['singular']`

**Singular/Plural formatter function**

```ts
const wasWereFormatter = (plural) => plural ? 'were' : 'was'
const personPersonsFormatter = (plural) => plural ? 'persons' : 'person'

const str = mz(1)`There ${wasWereFormatter} $value ${personPersonsFormatter} at this event.`
// str = "There was 1 person at this event."
const str = mz(5)`There ${wasWereFormatter} $value ${personPersonsFormatter} at this event.`
// str = "There were 5 persons at this event."
```

#### Customizing the delimiter
Pass your custom delimiter as a second argument.
```ts
const str = mz(1, ';')`There {was;were} $value person{;s} at this event.`
```

## Contributions

Contributions are always welcome.

Feel free to open issues or pull requests!

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