From e9877be524c87eb68aaec7ccb7181b7b1477b7f4 Mon Sep 17 00:00:00 2001 From: atomiks Date: Thu, 4 Jan 2018 02:57:08 +1100 Subject: [PATCH 1/4] Allow for autopluralization --- snippets/pluralize.md | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/snippets/pluralize.md b/snippets/pluralize.md index decf1c2ef..87e61dcaf 100644 --- a/snippets/pluralize.md +++ b/snippets/pluralize.md @@ -1,23 +1,29 @@ -# pluralize +### pluralize -If `num` is greater than `1` returns the plural form of the given string, else return the singular form. +Returns the singular or plural form of the word based on the input number. If the first argument is an `object`, it will use a closure by returning a function that can auto-pluralize plurals that don't simply end in `s` if the supplied dictionary contains the word. -Check if `num` is greater than `0`. 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'`. +If `num` is either `-1` or `1`, return the singular form of the word. If `num` is any other number, return the plural form. Omit the third argument to use the default of the singular word + `s`, or supply a custom pluralized word when necessary. If the first argument is an `object`, utilize a closure by returning a function which can use the supplied dictionary to resolve the correct plural form of the word. ```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; +const pluralize = (val, word, plural = word + 's') => { + const _pluralize = (num, word, plural = word + 's') => + [1, -1].includes(Number(num)) ? word : plural; + if (typeof val === 'object') return (num, word) => _pluralize(num, word, val[word]); + return _pluralize(val, word, plural); +}; ``` ```js -pluralize(1, 'apple', 'apples'); // 'apple' -pluralize(3, 'apple', 'apples'); // 'apples' +pluralize(0, 'apple'); // 'apples' +pluralize(1, 'apple'); // 'apple' pluralize(2, 'apple'); // 'apples' -pluralize(0, 'apple', 'apples'); // Gives error -pluralize(-3, 'apple', 'apples'); // Gives error +pluralize(1, 'person'); // 'person' +pluralize(2, 'person', 'people'); // people + +const PLURALS = { + person: 'people', + radius: 'radii' +}; +const autoPluralize = pluralize(PLURALS); +autoPluralize(2, 'person'); // people ``` From fe4f16439fcf05b6b8d202b8f99a03fb0eb6ef3a Mon Sep 17 00:00:00 2001 From: atomiks Date: Thu, 4 Jan 2018 03:01:49 +1100 Subject: [PATCH 2/4] Update pluralize.md --- snippets/pluralize.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/pluralize.md b/snippets/pluralize.md index 87e61dcaf..df7139a77 100644 --- a/snippets/pluralize.md +++ b/snippets/pluralize.md @@ -18,7 +18,7 @@ pluralize(0, 'apple'); // 'apples' pluralize(1, 'apple'); // 'apple' pluralize(2, 'apple'); // 'apples' pluralize(1, 'person'); // 'person' -pluralize(2, 'person', 'people'); // people +pluralize(2, 'person', 'people'); // 'people' const PLURALS = { person: 'people', From c627a8002c9b1e76840ed579dbb705fa92aaa4d9 Mon Sep 17 00:00:00 2001 From: atomiks Date: Thu, 4 Jan 2018 03:05:13 +1100 Subject: [PATCH 3/4] Update pluralize.md --- snippets/pluralize.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/pluralize.md b/snippets/pluralize.md index df7139a77..bc72f4dd9 100644 --- a/snippets/pluralize.md +++ b/snippets/pluralize.md @@ -25,5 +25,5 @@ const PLURALS = { radius: 'radii' }; const autoPluralize = pluralize(PLURALS); -autoPluralize(2, 'person'); // people +autoPluralize(2, 'person'); // 'people' ``` From 857ede9f231bde250c629a0da6b5bc394d6311fc Mon Sep 17 00:00:00 2001 From: atomiks Date: Thu, 4 Jan 2018 03:08:08 +1100 Subject: [PATCH 4/4] Update pluralize.md --- snippets/pluralize.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/pluralize.md b/snippets/pluralize.md index bc72f4dd9..6ee5b41df 100644 --- a/snippets/pluralize.md +++ b/snippets/pluralize.md @@ -1,6 +1,6 @@ ### pluralize -Returns the singular or plural form of the word based on the input number. If the first argument is an `object`, it will use a closure by returning a function that can auto-pluralize plurals that don't simply end in `s` if the supplied dictionary contains the word. +Returns the singular or plural form of the word based on the input number. If the first argument is an `object`, it will use a closure by returning a function that can auto-pluralize words that don't simply end in `s` if the supplied dictionary contains the word. If `num` is either `-1` or `1`, return the singular form of the word. If `num` is any other number, return the plural form. Omit the third argument to use the default of the singular word + `s`, or supply a custom pluralized word when necessary. If the first argument is an `object`, utilize a closure by returning a function which can use the supplied dictionary to resolve the correct plural form of the word.