Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/joschuaschneider/mehrzahl
Tagged template literals for singular/plural formatting
https://github.com/joschuaschneider/mehrzahl
formatting javascript pluralization utility
Last synced: 3 months ago
JSON representation
Tagged template literals for singular/plural formatting
- Host: GitHub
- URL: https://github.com/joschuaschneider/mehrzahl
- Owner: JoschuaSchneider
- Created: 2021-09-17T08:42:56.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2021-10-12T06:34:03.000Z (over 3 years ago)
- Last Synced: 2024-10-06T08:09:43.332Z (4 months ago)
- Topics: formatting, javascript, pluralization, utility
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/mehrzahl
- Size: 85 KB
- Stars: 8
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
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 importconst 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(1) // 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!