-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Joschua Schneider
committed
Sep 17, 2021
1 parent
6620e70
commit 26a7056
Showing
5 changed files
with
98 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,7 @@ | |
lib/ | ||
|
||
# dependencies | ||
node_modules/ | ||
node_modules/ | ||
|
||
# bench | ||
bench/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
bench/ | ||
.github/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,72 @@ | ||
# Mehrzahl | ||
# 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." | ||
``` | ||
|
||
💡 `$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! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters