Repository: plexis-js/plexis
Pull request #62 (2019-10-23 19:09:25)

add withoutIndent mentioned in #52
Implements #52 


Patch:
diff --git a/docs/pages/api.md b/docs/pages/api.md
index 4ec41de..c338e34 100644
--- a/docs/pages/api.md
+++ b/docs/pages/api.md
@@ -12,4 +12,5 @@
 - [toSucc](/packages/toSucc)
 - [toTitle](/packages/toTitle)
 - [withoutDiacritics](/packages/withoutDiacritics)
+- [withoutIndent](/packages/withoutIndent)
 - [isString](/packages/isString)
diff --git a/packages/plexis/package.json b/packages/plexis/package.json
index 36331e3..32b58ae 100644
--- a/packages/plexis/package.json
+++ b/packages/plexis/package.json
@@ -24,7 +24,8 @@
     "@plexis/to-pred": "^0.0.15",
     "@plexis/to-succ": "^0.0.15",
     "@plexis/to-title": "^0.0.15",
-    "@plexis/without-diacritics": "^0.0.15"
+    "@plexis/without-diacritics": "^0.0.15",
+    "@plexis/without-indent": "^0.0.1"
   },
   "keywords": [
     "utility",
diff --git a/packages/plexis/src/index.js b/packages/plexis/src/index.js
index 802c19b..43e59da 100644
--- a/packages/plexis/src/index.js
+++ b/packages/plexis/src/index.js
@@ -21,6 +21,11 @@ export {
   default as toChicagoTitle
 } from '@plexis/to-chicago';
 export {default as startsWith} from '@plexis/starts-with';
+export {
+  default as withoutIndent,
+  default as removeIndent,
+  default as dedent
+} from '@plexis/without-indent';
 export {
   default as distance,
   default as calcLevenshtein,
diff --git a/packages/withoutIndent/README.md b/packages/withoutIndent/README.md
new file mode 100644
index 0000000..7a037ba
--- /dev/null
+++ b/packages/withoutIndent/README.md
@@ -0,0 +1,21 @@
+## `withoutIndent`
+
+Removes leading whitespaces from each line in a string
+
+`npm i @plexis/without-indent`
+
+**Usage**
+
+```javascript
+import withoutIndent from '@plexis/without-indent';
+
+withoutIndent('\t\t\tHello World'); // returns 'Hello World'
+withoutIndent('\n\tHello\n\tWorld'); // returns '\nHello\nWorld'
+```
+
+**Aliases**
+
+```javascript
+import {removeIndent} from 'plexis';
+import {dedent} from 'plexis';
+```
diff --git a/packages/withoutIndent/package.json b/packages/withoutIndent/package.json
new file mode 100644
index 0000000..f865de8
--- /dev/null
+++ b/packages/withoutIndent/package.json
@@ -0,0 +1,23 @@
+{
+  "name": "@plexis/without-indent",
+  "version": "0.0.1",
+  "main": "dist/index.js",
+  "module": "dist/index.esm.js",
+  "jsnext:main": "./src/index.js",
+  "files": ["dist", "src"],
+  "license": "MIT",
+  "keywords": [
+    "utility",
+    "string",
+    "parse",
+    "format",
+    "validate"
+  ],
+  "repository": {
+    "type": "git",
+    "url": "git+https://github.com/plexis-js/plexis.git"
+  },
+  "publishConfig": {
+    "access": "public"
+  }
+}
\ No newline at end of file
diff --git a/packages/withoutIndent/src/index.js b/packages/withoutIndent/src/index.js
new file mode 100644
index 0000000..796f9ff
--- /dev/null
+++ b/packages/withoutIndent/src/index.js
@@ -0,0 +1,18 @@
+/**
+ * @description Removes leading whitespaces from every line in the given string
+ * @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'
+ */
+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,'');
+}
+
+export default withoutIndent;


Test patch:
diff --git a/packages/plexis/test/__snapshots__/index.js.snap b/packages/plexis/test/__snapshots__/index.js.snap
index b101624..221ba28 100644
--- a/packages/plexis/test/__snapshots__/index.js.snap
+++ b/packages/plexis/test/__snapshots__/index.js.snap
@@ -26,6 +26,9 @@ Array [
   "toChicagoStyle",
   "toChicagoTitle",
   "startsWith",
+  "withoutIndent",
+  "removeIndent",
+  "dedent",
   "distance",
   "calcLevenshtein",
   "levenshtein",
diff --git a/packages/withoutIndent/test/index.js b/packages/withoutIndent/test/index.js
new file mode 100644
index 0000000..a122071
--- /dev/null
+++ b/packages/withoutIndent/test/index.js
@@ -0,0 +1,44 @@
+import withoutIndent from '../src';
+
+it('removes an indent from the front', () => {
+  expect(withoutIndent('\tHello')).toBe('Hello');
+  expect(withoutIndent('\rHello')).toBe('Hello');
+  expect(withoutIndent(' Hello')).toBe('Hello');
+});
+
+it("doesn't remove newline characters", () => {
+    expect(withoutIndent('\nHello')).toBe('\nHello');
+});
+
+it("doesn't remove anything when there's no indent", () => {
+  expect(withoutIndent('Hello')).toBe('Hello');
+});
+
+it('removes multiple indents.', () => {
+  expect(withoutIndent('\t\t\tHello')).toBe('Hello');
+});
+
+it('removes indents on multiple lines', () => {
+  expect(withoutIndent('\t\tHello\n\t\tWorld')).toBe('Hello\nWorld');
+});
+
+it("doesn't remove indents that aren't at the beginning of a line.", () => {
+  expect(withoutIndent('Hello\t')).toBe('Hello\t');
+});
+
+it('removes only specified amount of indents', () => {
+  expect(withoutIndent('\t\t\t\tHello', 3)).toBe('\tHello');
+});
+
+it("doesn't remove extra characters when amount specified exceeds indents", () => {
+  expect(withoutIndent('\t\tHello', 3)).toBe('Hello');
+});
+
+it("doesn't remove indents when 0 is given as amount", () => {
+  expect(withoutIndent('\tHello', 0)).toBe('\tHello');
+});
+
+it('removes a mix of different indent characters', () => {
+  expect(withoutIndent('\t \rHello')).toBe('Hello');
+});
+