Repository: plexis-js/plexis
Pull request #67 (2019-10-25 17:09:44)

feat: escapeHTML
Implements #50 


Patch:
diff --git a/docs/pages/api.md b/docs/pages/api.md
index c338e34..6b9b1c6 100644
--- a/docs/pages/api.md
+++ b/docs/pages/api.md
@@ -14,3 +14,4 @@
 - [withoutDiacritics](/packages/withoutDiacritics)
 - [withoutIndent](/packages/withoutIndent)
 - [isString](/packages/isString)
+- [escapeHTML](/packages/escapeHTML)
diff --git a/packages/escapeHTML/README.md b/packages/escapeHTML/README.md
new file mode 100644
index 0000000..541d211
--- /dev/null
+++ b/packages/escapeHTML/README.md
@@ -0,0 +1,22 @@
+# `@plexis/escapeHTML`
+
+Takes the input text and converts HTML special characters to their entity equivalents.
+
+**Installation**
+`npm i @plexis/escape-html`
+
+**Usage**
+
+```javascript
+import escapeHTML from '@plexis/escape-html';
+
+escapeHTML('ABCD'); // returns 'ABCD'
+escapeHtml('<3'); // returns '&lt;3'
+escapeHtml('<p>This is cool</p>'); // returns '&lt;p&gt;This is cool&lt;/p&gt;'
+```
+
+**Aliases**
+
+```javascript
+import {escapeHTML} from 'plexis';
+```
diff --git a/packages/escapeHTML/package.json b/packages/escapeHTML/package.json
new file mode 100644
index 0000000..45e1cdb
--- /dev/null
+++ b/packages/escapeHTML/package.json
@@ -0,0 +1,29 @@
+{
+  "name": "@plexis/escape-html",
+  "version": "0.0.17",
+  "description": "Takes the input text and converts HTML special characters to their entity equivalents.",
+  "main": "dist/index.js",
+  "module": "dist/index.esm.js",
+  "jsnext:main": "./src/index.js",
+  "files": [
+    "dist",
+    "src"
+  ],
+  "authors":[
+     "Nikhil Vats <nikvats5499@gmail.com>"
+  ],
+  "license": "MIT",
+  "keywords": [
+    "utility",
+    "string",
+    "parse",
+    "escapeHTML"
+  ],
+  "repository": {
+    "type": "git",
+    "url": "git+https://github.com/plexis-js/plexis.git"
+  },
+  "publishConfig": {
+    "access": "public"
+  }
+}
diff --git a/packages/escapeHTML/src/index.js b/packages/escapeHTML/src/index.js
new file mode 100644
index 0000000..cb53961
--- /dev/null
+++ b/packages/escapeHTML/src/index.js
@@ -0,0 +1,23 @@
+/**
+ * @description Takes the input text and converts HTML special characters to their entity equivalents.
+ * @param {String} text
+ * @example
+ * escapeHTML('ABCD'); // returns 'ABCD'
+ * escapeHtml('<3') // returns '&lt;3'
+ * escapeHtml('<p>This is cool</p>') // returns '&lt;p&gt;This is cool&lt;/p&gt;'
+ */
+const escapeHTML = text => {
+  var map = {
+    '&': '&amp;',
+    '<': '&lt;',
+    '>': '&gt;',
+    '"': '&quot;',
+    "'": '&#039;'
+  };
+
+  return text.replace(/[&<>"']/g, function(m) {
+    return map[m];
+  });
+};
+
+export default escapeHTML;
diff --git a/packages/plexis/package.json b/packages/plexis/package.json
index 9c45e81..d36ba88 100644
--- a/packages/plexis/package.json
+++ b/packages/plexis/package.json
@@ -15,6 +15,7 @@
   "dependencies": {
     "@plexis/compose": "^0.0.17",
     "@plexis/distance": "^0.0.17",
+    "@plexis/escape-html": "^0.0.17",
     "@plexis/is-lowercase": "^0.0.17",
     "@plexis/is-string": "^0.0.17",
     "@plexis/is-uppercase": "^0.0.17",
diff --git a/packages/plexis/src/index.js b/packages/plexis/src/index.js
index b5734f2..c06eec1 100644
--- a/packages/plexis/src/index.js
+++ b/packages/plexis/src/index.js
@@ -39,3 +39,4 @@ export {
 } 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';


Test patch:
diff --git a/packages/escapeHTML/test/index.js b/packages/escapeHTML/test/index.js
new file mode 100644
index 0000000..a042e71
--- /dev/null
+++ b/packages/escapeHTML/test/index.js
@@ -0,0 +1,10 @@
+import escapeHTML from '../src';
+
+it('converts HTML special characters in a string to entity equivalents', () => {
+  expect(escapeHTML('ABCD')).toBe('ABCD');
+  expect(escapeHTML('<3')).toBe('&lt;3');
+  expect(escapeHTML('<p>This is cool</p>')).toBe('&lt;p&gt;This is cool&lt;/p&gt;');
+  expect(escapeHTML('<div>"Rise" & \'Shine\'</div>')).toBe(
+    '&lt;div&gt;&quot;Rise&quot; &amp; &#039;Shine&#039;&lt;/div&gt;'
+  );
+});
diff --git a/packages/plexis/test/__snapshots__/index.js.snap b/packages/plexis/test/__snapshots__/index.js.snap
index d11c642..ef0675b 100644
--- a/packages/plexis/test/__snapshots__/index.js.snap
+++ b/packages/plexis/test/__snapshots__/index.js.snap
@@ -39,5 +39,6 @@ Array [
   "isString",
   "toPascalCase",
   "classify",
+  "escapeHTML",
 ]
 `;