Repository: plexis-js/plexis
Pull request #82 (2019-10-26 19:26:06)

feat: toPlural #31


Patch:
diff --git a/packages/plexis/package.json b/packages/plexis/package.json
index eefabb2..9481dc0 100644
--- a/packages/plexis/package.json
+++ b/packages/plexis/package.json
@@ -27,6 +27,7 @@
     "@plexis/to-chunks": "^0.0.19",
     "@plexis/to-lower": "^0.0.19",
     "@plexis/to-pascal-case": "^0.0.19",
+    "@plexis/to-plural": "^0.0.19",
     "@plexis/to-snake-case": "^0.0.19",
     "@plexis/to-pred": "^0.0.19",
     "@plexis/to-succ": "^0.0.19",
diff --git a/packages/plexis/src/index.js b/packages/plexis/src/index.js
index 855034f..1aead9b 100644
--- a/packages/plexis/src/index.js
+++ b/packages/plexis/src/index.js
@@ -1,46 +1,47 @@
-export {default as toPred, default as toPredecessor} from '@plexis/to-pred';
-export {default as toSucc, default as toSuccessor} from '@plexis/to-succ';
-export {default as toTitle, default as titleize} from '@plexis/to-title';
-export {default as isNumeric, default as isNum} from '@plexis/is-numeric';
+export {default as compose, default as _do} from '@plexis/compose';
+export {
+  default as calcLevenshtein,
+  default as distance,
+  default as levenshtein
+} from '@plexis/distance';
+export {default as escapeHTML} from '@plexis/escape-html';
+export {default as isAlpha} from '@plexis/is-alpha';
+export {default as isLower, default as isLowerCase} from '@plexis/is-lowercase';
+export {default as isNum, default as isNumeric} from '@plexis/is-numeric';
+export {default as isString} from '@plexis/is-string';
+export {default as isUpper, default as isUpperCase} from '@plexis/is-uppercase';
+export {default as startsWith} from '@plexis/starts-with';
 export {
-  default as toCamelCase,
+  default as camelCase,
   default as camelize,
-  default as camelCase
+  default as toCamelCase
 } from '@plexis/to-camel-case';
 export {
-  default as toLower,
-  default as toFirstLower,
-  default as decapitalize
-} from '@plexis/to-lower';
-export {
-  default as withoutDiacritics,
-  default as cleanDiacritics,
-  default as removeDiacritics
-} from '@plexis/without-diacritics';
-export {default as isAlpha} from '@plexis/is-alpha';
-export {default as isLowerCase, default as isLower} from '@plexis/is-lowercase';
-export {default as isUpperCase, default as isUpper} from '@plexis/is-uppercase';
-export {default as compose, default as _do} from '@plexis/compose';
-export {
-  default as toTurabian,
   default as toChicagoManualStyle,
   default as toChicagoStyle,
-  default as toChicagoTitle
+  default as toChicagoTitle,
+  default as toTurabian
 } from '@plexis/to-chicago';
-export {default as toSnakeCase, default as snakeCase} from '@plexis/to-snake-case';
-export {default as startsWith} from '@plexis/starts-with';
 export {default as toChunks} from '@plexis/to-chunks';
+export {
+  default as decapitalize,
+  default as toFirstLower,
+  default as toLower
+} from '@plexis/to-lower';
+export {default as classify, default as toPascalCase} from '@plexis/to-pascal-case';
+export {default as pluralize, default as toPlural} from '@plexis/to-plural';
+export {default as toPred, default as toPredecessor} from '@plexis/to-pred';
+export {default as snakeCase, default as toSnakeCase} from '@plexis/to-snake-case';
+export {default as toSucc, default as toSuccessor} from '@plexis/to-succ';
+export {default as titleize, default as toTitle} from '@plexis/to-title';
 export {default as _when} from '@plexis/when';
 export {
-  default as withoutIndent,
+  default as cleanDiacritics,
+  default as removeDiacritics,
+  default as withoutDiacritics
+} from '@plexis/without-diacritics';
+export {
+  default as dedent,
   default as removeIndent,
-  default as dedent
+  default as withoutIndent
 } from '@plexis/without-indent';
-export {
-  default as distance,
-  default as calcLevenshtein,
-  default as levenshtein
-} from '@plexis/distance';
-export {default as isString} from '@plexis/is-string';
-export {default as toPascalCase, default as classify} from '@plexis/to-pascal-case';
-export {default as escapeHTML} from '@plexis/escape-html';
diff --git a/packages/to-plural/README.md b/packages/to-plural/README.md
new file mode 100644
index 0000000..1ba9f48
--- /dev/null
+++ b/packages/to-plural/README.md
@@ -0,0 +1,26 @@
+# `@plexis/to-plural`
+
+Converts a word to its plural form
+
+## Installation
+
+`npm i @plexis/to-plural`
+
+## Usage
+
+```javascript
+import toPlural from '@plexis/to-plural';
+
+toPlural('example'); // => 'examples'
+toPlural('example', 10); // => 'examples'
+toPlural('example', 1); // => 'example'
+toPlural('example', 'examplez', 10); // => 'examplez'
+toPlural('example', 'examplez', 1); // => 'example'
+```
+
+## Aliases
+
+```javascript
+import toPlural from '@plexis/to-plural';
+import {toPlural, pluralize} from 'plexis';
+```
diff --git a/packages/to-plural/package.json b/packages/to-plural/package.json
new file mode 100644
index 0000000..7959391
--- /dev/null
+++ b/packages/to-plural/package.json
@@ -0,0 +1,31 @@
+{
+  "name": "@plexis/to-plural",
+  "version": "0.0.19",
+  "main": "dist/index.js",
+  "module": "dist/index.esm.js",
+  "jsnext:main": "./src/index.js",
+  "description": "Converts a word to its plural form",
+  "keywords": [
+    "utility",
+    "string",
+    "parse",
+    "format",
+    "validate"
+  ],
+  "author": "shilangyu <xmarcinmarcin@gmail.com>",
+  "homepage": "https://github.com/plexis-js/plexis#readme",
+  "license": "MIT",
+  "files": [
+    "lib"
+  ],
+  "publishConfig": {
+    "access": "public"
+  },
+  "repository": {
+    "type": "git",
+    "url": "git+https://github.com/plexis-js/plexis.git"
+  },
+  "bugs": {
+    "url": "https://github.com/plexis-js/plexis/issues"
+  }
+}
diff --git a/packages/to-plural/src/index.js b/packages/to-plural/src/index.js
new file mode 100644
index 0000000..046079e
--- /dev/null
+++ b/packages/to-plural/src/index.js
@@ -0,0 +1,24 @@
+/**
+ * @description Converts a word to its plural form
+ * @param {String} word
+ * @param {String} pluralForm
+ * @param {Number} count
+ * @returns {String} pluralized word
+ * @example
+ * toPlural('example'); // => 'examples'
+ * toPlural('example', 10); // => 'examples'
+ * toPlural('example', 1); // => 'example'
+ * toPlural('example', 'examplez', 10); // => 'examplez'
+ * toPlural('example', 'examplez', 1); // => 'example'
+ */
+
+const toPlural = (word, ...rest) => {
+  if (rest.length === 1 && typeof rest[0] === 'string') return word;
+
+  const isPlural = rest.length === 0 || Math.abs(rest[rest.length - 1]) !== 1;
+  const pluralForm = rest.length === 2 ? rest[0] : `${word}s`;
+
+  return isPlural ? pluralForm : word;
+};
+
+export default toPlural;


Test patch:
diff --git a/packages/plexis/test/__snapshots__/index.js.snap b/packages/plexis/test/__snapshots__/index.js.snap
index 590dce3..05ddd66 100644
--- a/packages/plexis/test/__snapshots__/index.js.snap
+++ b/packages/plexis/test/__snapshots__/index.js.snap
@@ -3,48 +3,50 @@
 exports[`allows using all the available utilities 1`] = `
 Array [
   "__esModule",
-  "toPred",
-  "toPredecessor",
-  "toSucc",
-  "toSuccessor",
-  "toTitle",
-  "titleize",
-  "isNumeric",
-  "isNum",
-  "toCamelCase",
-  "camelize",
-  "camelCase",
-  "toLower",
-  "toFirstLower",
-  "decapitalize",
-  "withoutDiacritics",
-  "cleanDiacritics",
-  "removeDiacritics",
+  "compose",
+  "_do",
+  "calcLevenshtein",
+  "distance",
+  "levenshtein",
+  "escapeHTML",
   "isAlpha",
-  "isLowerCase",
   "isLower",
-  "isUpperCase",
+  "isLowerCase",
+  "isNum",
+  "isNumeric",
+  "isString",
   "isUpper",
-  "compose",
-  "_do",
-  "toTurabian",
+  "isUpperCase",
+  "startsWith",
+  "camelCase",
+  "camelize",
+  "toCamelCase",
   "toChicagoManualStyle",
   "toChicagoStyle",
   "toChicagoTitle",
-  "toSnakeCase",
-  "snakeCase",
-  "startsWith",
+  "toTurabian",
   "toChunks",
+  "decapitalize",
+  "toFirstLower",
+  "toLower",
+  "classify",
+  "toPascalCase",
+  "pluralize",
+  "toPlural",
+  "toPred",
+  "toPredecessor",
+  "snakeCase",
+  "toSnakeCase",
+  "toSucc",
+  "toSuccessor",
+  "titleize",
+  "toTitle",
   "_when",
-  "withoutIndent",
-  "removeIndent",
+  "cleanDiacritics",
+  "removeDiacritics",
+  "withoutDiacritics",
   "dedent",
-  "distance",
-  "calcLevenshtein",
-  "levenshtein",
-  "isString",
-  "toPascalCase",
-  "classify",
-  "escapeHTML",
+  "removeIndent",
+  "withoutIndent",
 ]
 `;
diff --git a/packages/to-plural/test/index.js b/packages/to-plural/test/index.js
new file mode 100644
index 0000000..e4de63b
--- /dev/null
+++ b/packages/to-plural/test/index.js
@@ -0,0 +1,16 @@
+import toPlural from '../src';
+
+describe('@plexis/to-plural', () => {
+  it('converts a given string to its plural form', () => {
+    expect(toPlural('example')).toBe('examples');
+    expect(toPlural('example', 10)).toBe('examples');
+    expect(toPlural('example', 1)).toBe('example');
+    expect(toPlural('example', 0)).toBe('examples');
+    expect(toPlural('example', -1)).toBe('example');
+    expect(toPlural('example', 'examplez', 10)).toBe('examplez');
+    expect(toPlural('example', 'examplez', 0)).toBe('examplez');
+    expect(toPlural('example', 'examplez', 1)).toBe('example');
+    expect(toPlural('example', 'examplez', -1)).toBe('example');
+    expect(toPlural('example', 'examplez')).toBe('example');
+  });
+});