Update pluralize.md
This commit is contained in:
@ -1,17 +1,19 @@
|
|||||||
# pluralize
|
# pluralize
|
||||||
|
|
||||||
Checks if the provided `num` is equal to `1`. If yes return
|
If `num` is greater than `1` returns the plural form of the given string, else return the singular form.
|
||||||
|
|
||||||
|
Check if `num` is positive. Throw an appropriate `Error` if not, return the appropriate string otherwise.
|
||||||
|
Omit the third argument, `items`, to use a default plural form same as `item` suffixed with a single `'s'`.
|
||||||
|
|
||||||
``` js
|
``` js
|
||||||
const pluralize = (num, item, items) => {
|
const pluralize = (num, item, items = item+'s') =>
|
||||||
if (num <= 0) throw new Error(`num takes value greater than equal to 1. Value povided was ${num} `)
|
num <= 0 ? (() => {throw new Error(`'num' should be >= 1. Value povided was ${num}.`)})() : num === 1 ? item : items;
|
||||||
else return num === 1 ? item : items
|
|
||||||
}
|
|
||||||
```
|
```
|
||||||
|
|
||||||
```js
|
```js
|
||||||
pluralize(1,'apple','apples'); // 'apple'
|
pluralize(1,'apple','apples'); // 'apple'
|
||||||
pluralize(3,'apple','apples'); // 'apples'
|
pluralize(3,'apple','apples'); // 'apples'
|
||||||
|
pluralize(2,'apple'); // 'apples'
|
||||||
pluralize(0,'apple','apples'); // Gives error
|
pluralize(0,'apple','apples'); // Gives error
|
||||||
pluralize(-3,'apple','apples'); // Gives error
|
pluralize(-3,'apple','apples'); // Gives error
|
||||||
```
|
```
|
||||||
|
|||||||
Reference in New Issue
Block a user