diff --git a/README.md b/README.md index df585e098..41e6787e0 100644 --- a/README.md +++ b/README.md @@ -291,6 +291,7 @@ * [`geometricProgression`](#geometricprogression) * [`maxN`](#maxn) * [`minN`](#minn) +* [`pluralize`](#pluralize) @@ -5164,6 +5165,33 @@ minN([1, 2, 3], 4); // [1,2,3]
[⬆ back to top](#table-of-contents) +# pluralize + +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 +const pluralize = (num, item, items = item + 's') => + num <= 0 + ? (() => { + throw new Error(`'num' should be >= 1. Value povided was ${num}.`); + })() + : num === 1 ? item : items; +``` + +```js +pluralize(1, 'apple', 'apples'); // 'apple' +pluralize(3, 'apple', 'apples'); // 'apples' +pluralize(2, 'apple'); // 'apples' +pluralize(0, 'apple', 'apples'); // Gives error +pluralize(-3, 'apple', 'apples'); // Gives error +``` + +
[⬆ back to top](#table-of-contents) + + ## Collaborators | [](https://github.com/Chalarangelo)
[Angelos Chalaris](https://github.com/Chalarangelo) | [](https://github.com/Pl4gue)
[David Wu](https://github.com/Pl4gue) | [](https://github.com/fejes713)
[Stefan Feješ](https://github.com/fejes713) | [](https://github.com/kingdavidmartins)
[King David Martins](https://github.com/iamsoorena) | [](https://github.com/iamsoorena)
[Soorena Soleimani](https://github.com/iamsoorena) | diff --git a/docs/index.html b/docs/index.html index 404494945..772d7c713 100644 --- a/docs/index.html +++ b/docs/index.html @@ -59,7 +59,7 @@ wrapper.appendChild(box); box.appendChild(el); }); - }

 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

call

Given a key and a set of arguments, call them when given a context. Primarily useful in composition.

Use a closure to call a stored key with stored arguments.

const call = (key, ...args) => context => context[key](...args);
+    }

 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

call

Given a key and a set of arguments, call them when given a context. Primarily useful in composition.

Use a closure to call a stored key with stored arguments.

const call = (key, ...args) => context => context[key](...args);
 
Promise.resolve([1, 2, 3])
   .then(call('map', x => 2 * x))
   .then(console.log); //[ 2, 4, 6 ]
@@ -1168,4 +1168,15 @@ maxN([1, 2, 3], 4); // [3,2,1]
 
minN([1, 2, 3]); // [1]
 minN([1, 2, 3], 2); // [1,2]
 minN([1, 2, 3], 4); // [1,2,3]
+

pluralize

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'.

const pluralize = (num, item, items = item + 's') =>
+  num <= 0
+    ? (() => {
+        throw new Error(`'num' should be >= 1. Value povided was ${num}.`);
+      })()
+    : num === 1 ? item : items;
+
pluralize(1, 'apple', 'apples'); // 'apple'
+pluralize(3, 'apple', 'apples'); // 'apples'
+pluralize(2, 'apple'); // 'apples'
+pluralize(0, 'apple', 'apples'); // Gives error
+pluralize(-3, 'apple', 'apples'); // Gives error
 

\ No newline at end of file diff --git a/snippets/pluralize.md b/snippets/pluralize.md index 0016a8c92..445ceca3b 100644 --- a/snippets/pluralize.md +++ b/snippets/pluralize.md @@ -5,15 +5,19 @@ If `num` is greater than `1` returns the plural form of the given string, else r 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 -const pluralize = (num, item, items = item+'s') => - num <= 0 ? (() => {throw new Error(`'num' should be >= 1. Value povided was ${num}.`)})() : num === 1 ? item : items; +```js +const pluralize = (num, item, items = item + 's') => + num <= 0 + ? (() => { + throw new Error(`'num' should be >= 1. Value povided was ${num}.`); + })() + : num === 1 ? item : items; ``` ```js -pluralize(1,'apple','apples'); // 'apple' -pluralize(3,'apple','apples'); // 'apples' -pluralize(2,'apple'); // 'apples' -pluralize(0,'apple','apples'); // Gives error -pluralize(-3,'apple','apples'); // Gives error +pluralize(1, 'apple', 'apples'); // 'apple' +pluralize(3, 'apple', 'apples'); // 'apples' +pluralize(2, 'apple'); // 'apples' +pluralize(0, 'apple', 'apples'); // Gives error +pluralize(-3, 'apple', 'apples'); // Gives error ``` diff --git a/tag_database b/tag_database index e8c53adce..ea05bbcb5 100644 --- a/tag_database +++ b/tag_database @@ -111,6 +111,7 @@ palindrome:string percentile:math pick:array pipeFunctions:adapter +pluralize:uncategorized powerset:math prettyBytes:utility primes:math