Repository: plexis-js/plexis
Pull request #85 (2019-10-27 08:47:42)

Feature: `toHuman / humanize` #76


Patch:
diff --git a/packages/plexis/package.json b/packages/plexis/package.json
index 38a7215..1fbabd9 100644
--- a/packages/plexis/package.json
+++ b/packages/plexis/package.json
@@ -30,6 +30,7 @@
     "@plexis/to-plural": "^0.0.20",
     "@plexis/to-pred": "^0.0.20",
     "@plexis/to-snake-case": "^0.0.20",
+    "@plexis/to-human": "^0.0.20",
     "@plexis/to-succ": "^0.0.20",
     "@plexis/to-title": "^0.0.20",
     "@plexis/when": "^0.0.20",
diff --git a/packages/plexis/src/index.js b/packages/plexis/src/index.js
index 1aead9b..7f64fbd 100644
--- a/packages/plexis/src/index.js
+++ b/packages/plexis/src/index.js
@@ -35,6 +35,7 @@ export {default as snakeCase, default as toSnakeCase} from '@plexis/to-snake-cas
 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 humanize, default as toHuman} from '@plexis/to-human';
 export {
   default as cleanDiacritics,
   default as removeDiacritics,
diff --git a/packages/toHuman/README.md b/packages/toHuman/README.md
new file mode 100644
index 0000000..e8edfce
--- /dev/null
+++ b/packages/toHuman/README.md
@@ -0,0 +1,25 @@
+# `@plexis/to-human`
+
+Converts a string into a human readable text
+
+## Installation
+
+`npm i @plexis/to-human`
+
+## Usage
+
+```javascript
+import toHuman from '@plexis/to-human';
+
+toHuman('foo'); // => 'Foo'
+toHuman('foo     bar'); // => 'Foo bar'
+toHuman(' foo-bar  baz quxQuux corge_grault GarplyWaldo'); // => 'Foo bar baz qux quux corge grault garply waldo'
+toHuman(' foo-bar-baz qux       quux'); // => 'Foo bar baz qux quux'
+```
+
+## Aliases
+
+```javascript
+import toHuman from '@plexis/to-human';
+import {toHuman, humanize} from 'plexis';
+```
diff --git a/packages/toHuman/package.json b/packages/toHuman/package.json
new file mode 100644
index 0000000..7fccf25
--- /dev/null
+++ b/packages/toHuman/package.json
@@ -0,0 +1,31 @@
+{
+  "name": "@plexis/to-human",
+  "version": "0.0.20",
+  "main": "dist/index.js",
+  "module": "dist/index.esm.js",
+  "jsnext:main": "./src/index.js",
+  "description": "Converts a string into a human readable text",
+  "keywords": [
+    "utility",
+    "string",
+    "parse",
+    "format",
+    "validate"
+  ],
+  "author": "Angel Gonzalez <angelbroz@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"
+  }
+}
\ No newline at end of file
diff --git a/packages/toHuman/src/index.js b/packages/toHuman/src/index.js
new file mode 100644
index 0000000..de1e99d
--- /dev/null
+++ b/packages/toHuman/src/index.js
@@ -0,0 +1,20 @@
+/**
+ * @description Converts a string into a human readable text
+ * @param {String} text
+ * @example
+ * toHuman('foo') // returns 'Foo'
+ * toHuman('foo     bar') // returns 'Foo bar'
+ * toHuman(' foo-bar-baz qux       quux') // returns 'Foo bar baz qux quux'
+ */
+const toHuman = text =>
+  text
+    .trim()
+    .replace(/_|-/g, ' ')
+    .trim()
+    .replace(/([A-Z])/g, word => ' ' + word.toLowerCase())
+    .trim()
+    .replace(/^[a-z]/g, first => first.toUpperCase())
+    .replace(/\s\s+/g, ' ')
+    .trim();
+
+export default toHuman;


Test patch:
diff --git a/packages/plexis/test/__snapshots__/index.js.snap b/packages/plexis/test/__snapshots__/index.js.snap
index 05ddd66..2aad53d 100644
--- a/packages/plexis/test/__snapshots__/index.js.snap
+++ b/packages/plexis/test/__snapshots__/index.js.snap
@@ -42,6 +42,8 @@ Array [
   "titleize",
   "toTitle",
   "_when",
+  "humanize",
+  "toHuman",
   "cleanDiacritics",
   "removeDiacritics",
   "withoutDiacritics",
diff --git a/packages/toHuman/test/index.js b/packages/toHuman/test/index.js
new file mode 100644
index 0000000..bba4503
--- /dev/null
+++ b/packages/toHuman/test/index.js
@@ -0,0 +1,22 @@
+import toHuman from '../src';
+
+describe('@plexis/to-human', () => {
+  it('Converts a string into a human readable text', () => {
+    expect(toHuman('foo')).toBe('Foo');
+    expect(toHuman('foo     bar')).toBe('Foo bar');
+    expect(toHuman(' foo-bar  baz quxQuux corge_grault GarplyWaldo')).toBe(
+      'Foo bar baz qux quux corge grault garply waldo'
+    );
+    expect(toHuman(' foo-bar-baz qux       quux')).toBe('Foo bar baz qux quux');
+    expect(toHuman('fooBar qux-core - _ ')).toBe('Foo bar qux core');
+    expect(toHuman('foo_')).toBe('Foo');
+    expect(toHuman('foo-')).toBe('Foo');
+    expect(toHuman('_foo')).toBe('Foo');
+    expect(toHuman('Foo bar')).toBe('Foo bar');
+    expect(toHuman('Foo')).toBe('Foo');
+    expect(toHuman(' foo ')).toBe('Foo');
+    expect(toHuman(' Foo ')).toBe('Foo');
+    expect(toHuman('-Foo ')).toBe('Foo');
+    expect(toHuman('Foo Bar foo')).toBe('Foo bar foo');
+  });
+});