Repository: plexis-js/plexis
Pull request #69 (2019-10-26 11:51:40)

Feature: `toSnakeCase / underscored`


Patch:
diff --git a/packages/plexis/package.json b/packages/plexis/package.json
index b40bc1f..dbe566e 100644
--- a/packages/plexis/package.json
+++ b/packages/plexis/package.json
@@ -26,6 +26,7 @@
     "@plexis/to-chunks": "^0.0.19",
     "@plexis/to-lower": "^0.0.19",
     "@plexis/to-pascal-case": "^0.0.19",
+    "@plexis/to-snake-case": "^0.0.19",
     "@plexis/to-pred": "^0.0.19",
     "@plexis/to-succ": "^0.0.19",
     "@plexis/to-title": "^0.0.19",
diff --git a/packages/plexis/src/index.js b/packages/plexis/src/index.js
index bf5eaa3..c5332d6 100644
--- a/packages/plexis/src/index.js
+++ b/packages/plexis/src/index.js
@@ -26,6 +26,7 @@ export {
   default as toChicagoStyle,
   default as toChicagoTitle
 } 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 _when} from '@plexis/when';
diff --git a/packages/toSnakeCase/README.md b/packages/toSnakeCase/README.md
new file mode 100644
index 0000000..a3ed63a
--- /dev/null
+++ b/packages/toSnakeCase/README.md
@@ -0,0 +1,25 @@
+# `@plexis/toSnakeCase`
+
+Converts the input text to camelCase.
+
+**Installation**
+`npm i @plexis/to-snake-case`
+
+**Usage**
+
+```javascript
+import toSnakeCase from '@plexis/to-snake-case';
+
+toSnakeCase('Cool'); // => 'cool'
+toSnakeCase('cool mate'); // => 'cool_mate'
+toSnakeCase('Hey how are you today?'); // => 'hey_how_are_you_today'
+toSnakeCase('PascalCase'); // => 'pascal_case'
+toSnakeCase('kebab-case'); // => kebab_case
+```
+
+**Aliases**
+
+```javascript
+import {toSnakeCase} from 'plexis';
+import {snakeCase} from 'plexis';
+```
diff --git a/packages/toSnakeCase/package.json b/packages/toSnakeCase/package.json
new file mode 100644
index 0000000..b57d0be
--- /dev/null
+++ b/packages/toSnakeCase/package.json
@@ -0,0 +1,29 @@
+{
+  "name": "@plexis/to-snake-case",
+  "version": "0.0.19",
+  "main": "dist/index.js",
+  "module": "dist/index.esm.js",
+  "jsnext:main": "./src/index.js",
+  "files": [
+    "dist",
+    "src"
+  ],
+  "authors": [
+    "Gopi Krishna <gopiwayz@gmail.com> (https://bgopikrishna.me)"
+  ],
+  "license": "MIT",
+  "keywords": [
+    "utility",
+    "string",
+    "parse",
+    "format",
+    "validate"
+  ],
+  "repository": {
+    "type": "git",
+    "url": "git+https://github.com/plexis-js/plexis.git"
+  },
+  "publishConfig": {
+    "access": "public"
+  }
+}
diff --git a/packages/toSnakeCase/src/index.js b/packages/toSnakeCase/src/index.js
new file mode 100644
index 0000000..0746a54
--- /dev/null
+++ b/packages/toSnakeCase/src/index.js
@@ -0,0 +1,32 @@
+/**
+ * @description Converts a string to snake case
+ * @param {String} text
+ * @returns {String} snake case text
+ * @example
+ * toSnakeCase('Cool') => 'cool'
+ * toSnakeCase('cool mate') => 'cool_mate'
+ * toSnakeCase('Hey how are you today?') => 'hey_how_are_you_today'
+ * toSnakeCase('PascalCase') => 'pascal_case'
+ * toSnakeCase('kebab-case') => 'kebab_case'
+ */
+
+const toSnakeCase = string => {
+  const removeSym = (letter, index, string) => {
+    return string.length - 1 === index ? '' : '_';
+  };
+  const pascalAndCamelParser = l => {
+    return (l[0] + '_' + l[1]).toLowerCase();
+  };
+
+  const wordPatt = /[\w]([A-Z])/g;
+  const symbolPatt = /[^a-zA-Z0-9]/g;
+  let result = string
+    .trim()
+    .replace(wordPatt, pascalAndCamelParser)
+    .replace(symbolPatt, removeSym)
+    .toLowerCase();
+
+  return result;
+};
+
+export default toSnakeCase;
diff --git a/packages/withoutIndent/src/index.js b/packages/withoutIndent/src/index.js
index 796f9ff..e55c824 100644
--- a/packages/withoutIndent/src/index.js
+++ b/packages/withoutIndent/src/index.js
@@ -3,7 +3,7 @@
  * @param {String} text
  * @param {Number} [removeUpTo] - Specifies how many whitespaces to remove
  * @example
- * 
+ *
  * withoutIndent('\t\t\tHello World'); // returns 'Hello World'
  * withoutIndent('\n\tHello\n\tWorld'); // returns '\nHello\nWorld'
  * withoutIndent('\n\tHello\n\t\t\tWorld', 2); // returns '\nHello\n\tWorld'
@@ -12,7 +12,7 @@ const withoutIndent = (text, removeUpTo) => {
   if (removeUpTo <= 0) return text;
   const howMany = typeof removeUpTo === 'number' ? `{1,${removeUpTo}}` : '+';
   const regex = new RegExp(`^[ \\t\\r]${howMany}|(?<=\\n+)[ \\t\\r]${howMany}`, 'g');
-  return text.replace(regex,'');
-}
+  return text.replace(regex, '');
+};
 
 export default withoutIndent;


Test patch:
diff --git a/packages/plexis/test/__snapshots__/index.js.snap b/packages/plexis/test/__snapshots__/index.js.snap
index 8edc145..ebde18c 100644
--- a/packages/plexis/test/__snapshots__/index.js.snap
+++ b/packages/plexis/test/__snapshots__/index.js.snap
@@ -30,6 +30,8 @@ Array [
   "toChicagoManualStyle",
   "toChicagoStyle",
   "toChicagoTitle",
+  "toSnakeCase",
+  "snakeCase",
   "startsWith",
   "toChunks",
   "_when",
diff --git a/packages/toPascalCase/test/index.js b/packages/toPascalCase/test/index.js
index 0cb1c37..64f0089 100644
--- a/packages/toPascalCase/test/index.js
+++ b/packages/toPascalCase/test/index.js
@@ -4,7 +4,7 @@ it('Works with single characters', () => {
   expect(toPascalCase('h')).toBe('H');
 });
 
-it("Works will full words", () => {
+it('Works will full words', () => {
   expect(toPascalCase('hello')).toBe('Hello');
 });
 
@@ -16,18 +16,20 @@ it('Works with multiple words', () => {
   expect(toPascalCase('Hello there')).toBe('HelloThere');
 });
 
-it("Removes symbols", () => {
+it('Removes symbols', () => {
   expect(toPascalCase('@@@hello@@@')).toBe('Hello');
 });
 
-it("Works with long strings", () => {
-  expect(toPascalCase('This is a long string compared to the other tests')).toBe('ThisIsALongStringComparedToTheOtherTests')
-})
+it('Works with long strings', () => {
+  expect(toPascalCase('This is a long string compared to the other tests')).toBe(
+    'ThisIsALongStringComparedToTheOtherTests'
+  );
+});
 
-it("Converts camelCase properly", () => {
+it('Converts camelCase properly', () => {
   expect(toPascalCase('camelCase')).toBe('CamelCase');
-})
+});
 
-it("Converts kebab-case properly", () => {
+it('Converts kebab-case properly', () => {
   expect(toPascalCase('kebab-case')).toBe('KebabCase');
-})
\ No newline at end of file
+});
diff --git a/packages/toSnakeCase/test/index.js b/packages/toSnakeCase/test/index.js
new file mode 100644
index 0000000..07f9c98
--- /dev/null
+++ b/packages/toSnakeCase/test/index.js
@@ -0,0 +1,9 @@
+import toSnakeCase from '../src';
+
+it('converts a given string to snake case', () => {
+  expect(toSnakeCase('Cool')).toBe('cool');
+  expect(toSnakeCase('cool mate')).toBe('cool_mate');
+  expect(toSnakeCase('Hey how are you today?')).toBe('hey_how_are_you_today');
+  expect(toSnakeCase('PascalCase')).toBe('pascal_case');
+  expect(toSnakeCase('kebab-case')).toBe('kebab_case');
+});
diff --git a/packages/withoutIndent/test/index.js b/packages/withoutIndent/test/index.js
index a122071..f35c37a 100644
--- a/packages/withoutIndent/test/index.js
+++ b/packages/withoutIndent/test/index.js
@@ -7,7 +7,7 @@ it('removes an indent from the front', () => {
 });
 
 it("doesn't remove newline characters", () => {
-    expect(withoutIndent('\nHello')).toBe('\nHello');
+  expect(withoutIndent('\nHello')).toBe('\nHello');
 });
 
 it("doesn't remove anything when there's no indent", () => {
@@ -41,4 +41,3 @@ it("doesn't remove indents when 0 is given as amount", () => {
 it('removes a mix of different indent characters', () => {
   expect(withoutIndent('\t \rHello')).toBe('Hello');
 });
-