Repository: plexis-js/plexis
Pull request #112 (2019-11-05 05:08:09)

feat: `countWords`
Please merge feature countWords

codecov[bot]: # [Codecov](https://codecov.io/gh/plexis-js/plexis/pull/112?src=pr&el=h1) Report
> Merging [#112](https://codecov.io/gh/plexis-js/plexis/pull/112?src=pr&el=desc) into [master](https://codecov.io/gh/plexis-js/plexis/commit/26ca84e0b4dd70472922efb4a17b13b6cf03fc97?src=pr&el=desc) will **not change** coverage.
> The diff coverage is `100%`.


[![Impacted file tree graph](https://codecov.io/gh/plexis-js/plexis/pull/112/graphs/tree.svg?width=650&token=DdDVC9ByG1&height=150&src=pr)](https://codecov.io/gh/plexis-js/plexis/pull/112?src=pr&el=tree)

```diff
@@          Coverage Diff          @@
##           master   #112   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files          38     39    +1     
  Lines         182    185    +3     
=====================================
+ Hits          182    185    +3
```


| [Impacted Files](https://codecov.io/gh/plexis-js/plexis/pull/112?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [packages/countWords/src/index.js](https://codecov.io/gh/plexis-js/plexis/pull/112/diff?src=pr&el=tree#diff-cGFja2FnZXMvY291bnRXb3Jkcy9zcmMvaW5kZXguanM=) | `100% <100%> (ø)` | |

stale[bot]: This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. May the force be with you!

protob: What about "wontfix" issues? How much time has to pass before they close?
stale[bot]: This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. May the force be with you!

papb: > 
> 
> This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. May the force be with you!

:eyes:
stale[bot]: This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. May the force be with you!

papb: > This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. May the force be with you!

:eyes:
stale[bot]: This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. May the force be with you!

papb: > 
> 
> This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. May the force be with you!

:eyes:
stale[bot]: This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. May the force be with you!

protob: @vorillaz, Could you merge this feature please?
stale[bot]: This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. May the force be with you!

papb: > 
> 
> This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. May the force be with you!

:eyes:
stale[bot]: This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. May the force be with you!

papb: > 
> 
> This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. May the force be with you!

:eyes:
stale[bot]: This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. May the force be with you!

papb: > This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. May the force be with you!

:eyes:
stale[bot]: This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. May the force be with you!

papb: > This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. May the force be with you!

:eyes:
stale[bot]: This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. May the force be with you!

papb: > This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. May the force be with you!

:eyes:
stale[bot]: This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. May the force be with you!

stale[bot]: This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. May the force be with you!

papb: > This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. May the force be with you!

:eyes:
stale[bot]: This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. May the force be with you!

papb: > This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. May the force be with you!

:eyes:
stale[bot]: This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. May the force be with you!

papb: > 
> 
> This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. May the force be with you!

:eyes:
papb: :eyes: 
papb: Oh no I lost to stalebot
vorillaz: Sorry for that @papb , merging as we speak.

Patch:
diff --git a/docs/pages/api.md b/docs/pages/api.md
index 1efe3fe..81ba951 100644
--- a/docs/pages/api.md
+++ b/docs/pages/api.md
@@ -3,6 +3,7 @@
 - [cache](/packages/cache)
 - [compare](/packages/compare)
 - [compose](/packages/compose)
+- [countWords](/packages/countWords)
 - [distance](/packages/distance)
 - [endsWith](/packages/endsWith)
 - [escapeHTML](/packages/escapeHTML)
diff --git a/packages/countWords/README.md b/packages/countWords/README.md
new file mode 100644
index 0000000..03f0fb6
--- /dev/null
+++ b/packages/countWords/README.md
@@ -0,0 +1,24 @@
+# `@plexis/count-words`
+
+Returns the number of words contained in the input text.
+
+**Installation**
+`npm i @plexis/count-words`
+
+**Usage**
+
+```javascript
+import countWords from '@plexis/count-words';
+
+countWords(); // returns 0
+countWords(''); // returns 0
+countWords(' '); // returns 0
+countWords('Hello   World    !!'); // returns 2
+```
+
+## Aliases
+
+```javascript
+import countWords from '@plexis/count-words';
+import {countWords} from 'plexis';
+```
diff --git a/packages/countWords/package.json b/packages/countWords/package.json
new file mode 100644
index 0000000..8b29473
--- /dev/null
+++ b/packages/countWords/package.json
@@ -0,0 +1,30 @@
+{
+  "name": "@plexis/count-words",
+  "version": "0.0.22",
+  "description": "Returns the number of words contained in the input text.",
+  "keywords": [
+    "utility",
+    "string",
+    "count"
+  ],
+  "author": "protob <protobx@gmail.com>",
+  "homepage": "https://github.com/plexis-js/plexis.git",
+  "license": "MIT",
+  "main": "dist/index.js",
+  "module": "dist/index.esm.js",
+  "jsnext:main": "./src/index.js",
+  "files": [
+    "dist",
+    "src"
+  ],
+  "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/countWords/src/index.js b/packages/countWords/src/index.js
new file mode 100644
index 0000000..ced1d97
--- /dev/null
+++ b/packages/countWords/src/index.js
@@ -0,0 +1,19 @@
+/**
+ * @description Returns the number of words contained in the input text.
+ * @param {String} string
+ * @example
+ * countWords(); // => 0
+ * countWords(''); // => 0
+ * countWords(' ') // => 0
+ * countWords('Hello   World') // => 2
+ * countWords('Hello   World    !!') // => 2
+ */
+
+const countWords = (string = '') => {
+  return string
+    .replace(/[^a-z0-9]+/gi, ',')
+    .split(',')
+    .filter(e => String(e).trim()).length;
+};
+
+export default countWords;
diff --git a/packages/plexis/package.json b/packages/plexis/package.json
index 4ac49db..19a5e22 100644
--- a/packages/plexis/package.json
+++ b/packages/plexis/package.json
@@ -49,6 +49,7 @@
     "@plexis/without-html": "^0.0.22",
     "@plexis/without-indent": "^0.0.22",
     "@plexis/is-empty": "^0.0.22",
+    "@plexis/count-words": "^0.0.22",
     "@plexis/to-ellipsis": "^0.0.22"
   },
   "keywords": [
diff --git a/packages/plexis/src/index.js b/packages/plexis/src/index.js
index 80929ea..5df33fe 100644
--- a/packages/plexis/src/index.js
+++ b/packages/plexis/src/index.js
@@ -71,4 +71,5 @@ export {default as withoutHTML, default as removeHTML} from '@plexis/without-htm
 export {default as isEmpty} from '@plexis/is-empty';
 export {default as tail, default as rest, default as last, default as pop} from '@plexis/tail';
 export {default as toSwap, default as swap} from '@plexis/to-swap';
+export {default as countWords} from '@plexis/count-words';
 export {default as toEllipsis, default as truncate, default as fit} from '@plexis/to-ellipsis';


Test patch:
diff --git a/packages/countWords/test/index.js b/packages/countWords/test/index.js
new file mode 100644
index 0000000..b063086
--- /dev/null
+++ b/packages/countWords/test/index.js
@@ -0,0 +1,24 @@
+'use strict';
+
+import countWords from '../src';
+
+describe('@plexis/countWords', () => {
+  it('should retrun 0', () => {
+    expect(countWords()).toBe(0);
+  });
+  it('should retrun 0', () => {
+    expect(countWords('  ')).toBe(0);
+  });
+  it('should retrun 2', () => {
+    expect(countWords('Hello   World')).toBe(2);
+  });
+  it('should retrun 2', () => {
+    expect(countWords('Hello   World    !!')).toBe(2);
+  });
+  it('should retrun 3', () => {
+    expect(countWords('Hello   Another  Dimension    !!')).toBe(3);
+  });
+  it('should retrun 3', () => {
+    expect(countWords('Hello  Another  Dimension ,.!?;:+-`@#$%^&*()')).toBe(3);
+  });
+});
diff --git a/packages/plexis/test/__snapshots__/index.js.snap b/packages/plexis/test/__snapshots__/index.js.snap
index 17b1fd7..68fcf72 100644
--- a/packages/plexis/test/__snapshots__/index.js.snap
+++ b/packages/plexis/test/__snapshots__/index.js.snap
@@ -78,6 +78,7 @@ Array [
   "pop",
   "toSwap",
   "swap",
+  "countWords",
   "toEllipsis",
   "truncate",
   "fit",