Repository: plexis-js/plexis
Pull request #42 (2019-10-22 04:32:06)

feat: startsWith
Hi.

This is for #12. 

Brandon 


Patch:
diff --git a/docs/pages/api.md b/docs/pages/api.md
index 3c4e538..f72ac19 100644
--- a/docs/pages/api.md
+++ b/docs/pages/api.md
@@ -3,6 +3,7 @@
 - [compose](/packages/compose)
 - [isLowerCase](/packages/isLowerCase)
 - [isUpperCase](/packages/isUpperCase)
+- [startsWith](/packages/startsWith)
 - [toChicago](/packages/toChicago)
 - [toLower](/packages/toLower)
 - [toPred](/packages/toPred)
diff --git a/packages/plexis/package.json b/packages/plexis/package.json
index 201b0ac..d70abb7 100644
--- a/packages/plexis/package.json
+++ b/packages/plexis/package.json
@@ -12,6 +12,7 @@
     "@plexis/compose": "^0.0.13",
     "@plexis/is-lowercase": "^0.0.13",
     "@plexis/is-uppercase": "^0.0.13",
+    "@plexis/starts-with": "^0.0.13",
     "@plexis/to-chicago": "^0.0.13",
     "@plexis/to-lower": "^0.0.13",
     "@plexis/to-pred": "^0.0.13",
diff --git a/packages/plexis/src/index.js b/packages/plexis/src/index.js
index 822fc15..899d511 100644
--- a/packages/plexis/src/index.js
+++ b/packages/plexis/src/index.js
@@ -20,3 +20,4 @@ export {
   default as toChicagoStyle,
   default as toChicagoTitle
 } from '@plexis/to-chicago';
+export {default as startsWith} from '@plexis/starts-with';
diff --git a/packages/startsWith/.npmignore b/packages/startsWith/.npmignore
new file mode 100644
index 0000000..552cf85
--- /dev/null
+++ b/packages/startsWith/.npmignore
@@ -0,0 +1,17 @@
+.DS_Store
+*.test.js
+.prettierrc
+.github
+.template
+.package-template
+.nyc_output
+.circleci
+.commitlintrc.js
+coverage
+coverage.lcov
+docs
+babel.config.js
+test
+CONTRIBUTING.md
+CODE_OF_CONDUCT.md
+LICENCE
\ No newline at end of file
diff --git a/packages/startsWith/README.md b/packages/startsWith/README.md
new file mode 100644
index 0000000..ead4d80
--- /dev/null
+++ b/packages/startsWith/README.md
@@ -0,0 +1,16 @@
+## `@plexis/startsWith`
+
+Checks whether the input text starts with the given pattern.
+
+`npm i @plexis/starts-with`
+
+**Usage**
+
+```javascript
+import startsWith from '@plexis/starts-with';
+
+startsWith('This is me', 'This is'); // returns true
+startsWith('This is me', 'This is', 1); // returns false
+startsWith(/[@!#$]/, '@plexis is great!'); // returns true
+startsWith(/[A-Z]/, '@plexis is great!'); // returns false
+```
diff --git a/packages/startsWith/package.json b/packages/startsWith/package.json
new file mode 100644
index 0000000..feac7eb
--- /dev/null
+++ b/packages/startsWith/package.json
@@ -0,0 +1,23 @@
+{
+  "name": "@plexis/starts-with",
+  "version": "0.0.13",
+  "main": "dist/index.js",
+  "module": "dist/index.esm.js",
+  "jsnext:main": "./src/index.js",
+  "author": "Brandon Skinner <brandon.m.skinner@gmail.com>",
+  "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/startsWith/src/index.js b/packages/startsWith/src/index.js
new file mode 100644
index 0000000..73cc927
--- /dev/null
+++ b/packages/startsWith/src/index.js
@@ -0,0 +1,21 @@
+/**
+ * @description Checks whether the input text starts with the given pattern.
+ * @param {String} text
+ * @param {String|Regex} pattern
+ * @param {Number} startingPosition, optional
+ * @example
+ * startsWith('This is me', 'This is'); // returns true
+ * startsWith('This is me', 'This is', 1); // returns false
+ * startsWith(/[@!#$]/, '@plexis is great!'); // returns true
+ */
+const startsWith = (text, pattern, startingPosition) => {
+  try {
+    if (typeof pattern === 'object') {
+      return text.slice(startingPosition).search(new RegExp(pattern)) === 0 ? true : false;
+    }
+  } catch (e) {}
+
+  return text.slice(startingPosition).indexOf(pattern) === 0 ? true : false;
+};
+
+export default startsWith;


Test patch:
diff --git a/packages/plexis/test/__snapshots__/index.js.snap b/packages/plexis/test/__snapshots__/index.js.snap
index 70fb638..b848be0 100644
--- a/packages/plexis/test/__snapshots__/index.js.snap
+++ b/packages/plexis/test/__snapshots__/index.js.snap
@@ -25,5 +25,6 @@ Array [
   "toChicagoManualStyle",
   "toChicagoStyle",
   "toChicagoTitle",
+  "startsWith",
 ]
 `;
diff --git a/packages/startsWith/test/index.js b/packages/startsWith/test/index.js
new file mode 100644
index 0000000..1646718
--- /dev/null
+++ b/packages/startsWith/test/index.js
@@ -0,0 +1,41 @@
+import startsWith from '../src';
+
+it('Matches single characters', () => {
+  expect(startsWith('Hello', 'H')).toBe(true);
+});
+
+it("Doesn't match single characters not at front of string", () => {
+  expect(startsWith('Hello', 'l')).toBe(false);
+});
+
+it('Matches single characters with startingPosition set at character index', () => {
+  expect(startsWith('Help', 'l', 2)).toBe(true);
+});
+
+it('Matches single word strings', () => {
+  expect(startsWith('Hello there champ!', 'Hello')).toBe(true);
+});
+
+it("Doesn't match single word strings not at front of string", () => {
+  expect(startsWith('Hello there champ!', 'there')).toBe(false);
+});
+
+it('Matches single word strings with startingPosition set at word index', () => {
+  expect(startsWith('Hello there champ!', 'there', 6)).toBe(true);
+});
+
+it('Matches single character regex', () => {
+  expect(startsWith('@plexis is great!', /[@]/)).toBe(true);
+});
+
+it("Doesn't match single character regex not at front of string", () => {
+  expect(startsWith('@plexis is great!', /[!]/)).toBe(false);
+});
+
+it('Matches single word string', () => {
+  expect(startsWith('@plexis is great!', /is/, 8)).toBe(true);
+});
+
+it('Matches complex regex(HTML tag)', () => {
+  expect(startsWith('<td>Table Cell</td>', /<([\w]+).*>(.*?)<\/\1>/)).toBe(true);
+});