Repository: plexis-js/plexis
Pull request #55 (2019-10-22 21:36:25)

feat: add `_when` method (#2)
Implements #2 


Patch:
diff --git a/packages/plexis/package.json b/packages/plexis/package.json
index 1ec8a8a..efc6154 100644
--- a/packages/plexis/package.json
+++ b/packages/plexis/package.json
@@ -27,6 +27,7 @@
     "@plexis/to-pred": "^0.0.15",
     "@plexis/to-succ": "^0.0.15",
     "@plexis/to-title": "^0.0.15",
+    "@plexis/when": "^0.0.14",
     "@plexis/without-diacritics": "^0.0.15",
     "@plexis/without-indent": "^0.0.1"
   },
diff --git a/packages/plexis/src/index.js b/packages/plexis/src/index.js
index db2f877..b5734f2 100644
--- a/packages/plexis/src/index.js
+++ b/packages/plexis/src/index.js
@@ -26,6 +26,7 @@ export {
   default as toChicagoTitle
 } from '@plexis/to-chicago';
 export {default as startsWith} from '@plexis/starts-with';
+export {default as _when} from '@plexis/when';
 export {
   default as withoutIndent,
   default as removeIndent,
diff --git a/packages/when/README.md b/packages/when/README.md
new file mode 100644
index 0000000..528c2df
--- /dev/null
+++ b/packages/when/README.md
@@ -0,0 +1,36 @@
+## `when`
+
+`when` is used for checking and chaining within the composition for supercharged functions.
+
+`npm i @plexis/when`
+
+**Usage**
+
+```javascript
+import compose from '@plexis/compose';
+import when from '@plexis/when';
+
+const inc = x => x + 1;
+const add = (x, y) => x + y;
+const setToZero = () => 0;
+
+const compose = (...fns) => x => fns.reduce((r, f) => f(r), x);
+const when = (cond, f) => x => (cond(x) ? f(x) : x);
+
+const ops = compose(
+  inc,
+  x => add(x, 2),
+  when(num => num > 0, compose(setToZero))
+);
+
+ops(1); // returns 0
+ops(100); // returns 0
+ops(-100); // returns -97
+```
+
+**Aliases**
+
+```javascript
+import when from '@plexis/when';
+import {when} from 'plexis';
+```
diff --git a/packages/when/package.json b/packages/when/package.json
new file mode 100644
index 0000000..061a30f
--- /dev/null
+++ b/packages/when/package.json
@@ -0,0 +1,26 @@
+{
+  "name": "@plexis/when",
+  "version": "0.0.14",
+  "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"
+  }
+}
diff --git a/packages/when/src/index.js b/packages/when/src/index.js
new file mode 100644
index 0000000..a6e3630
--- /dev/null
+++ b/packages/when/src/index.js
@@ -0,0 +1,16 @@
+/**
+ * @description Is used for checking and chaining within the composition for supercharged functions.
+ * @param {Function|Boolean} [checking] A function or condition to check.
+ * @param {Function} [func] The function to invoke.
+ * @returns {Function} Returns the new composite function.
+ * @example
+ * _when(num => num < 0, () => 0)(-1) // returns 0
+ */
+const _when = (checking, func) => value => {
+  if (checking === true || (typeof checking === 'function' && checking(value))) {
+    return func(value);
+  }
+  return value;
+};
+
+export default _when;


Test patch:
diff --git a/packages/plexis/test/__snapshots__/index.js.snap b/packages/plexis/test/__snapshots__/index.js.snap
index 244d061..d11c642 100644
--- a/packages/plexis/test/__snapshots__/index.js.snap
+++ b/packages/plexis/test/__snapshots__/index.js.snap
@@ -29,6 +29,7 @@ Array [
   "toChicagoStyle",
   "toChicagoTitle",
   "startsWith",
+  "_when",
   "withoutIndent",
   "removeIndent",
   "dedent",
diff --git a/packages/when/test/index.js b/packages/when/test/index.js
new file mode 100644
index 0000000..2397c52
--- /dev/null
+++ b/packages/when/test/index.js
@@ -0,0 +1,27 @@
+import _when from '../src';
+
+it('executes the function if the condition is a boolean and true', () => {
+  const mock = jest.fn();
+  _when(true, mock)(1);
+  expect(mock).toHaveBeenCalledTimes(1);
+  expect(mock).toHaveBeenCalledWith(1);
+});
+
+it("doesn't execute the function if the condition is a boolean and false", () => {
+  const mock = jest.fn();
+  _when(false, mock)(1);
+  expect(mock).toHaveBeenCalledTimes(0);
+});
+
+it('executes the function if the condition is a function and evaluates to true', () => {
+  const mock = jest.fn();
+  _when(x => x > 0, mock)(1);
+  expect(mock).toHaveBeenCalledTimes(1);
+  expect(mock).toHaveBeenCalledWith(1);
+});
+
+it("doesn't execute the function if the condition is a function and evaluates to false", () => {
+  const mock = jest.fn();
+  _when(x => x > 0, mock)(-1);
+  expect(mock).toHaveBeenCalledTimes(0);
+});