Repository: sanctuary-js/sanctuary (js)

Issue:
Add S.when and S.unless
Create versions of [R.when](http://ramdajs.com/0.23.0/docs/#when) and [R.unless](http://ramdajs.com/0.23.0/docs/#unless).

Pull request description:
logic: add S.when and S.unless
Closes #347

Patch:
diff --git a/index.js b/index.js
index 52d5f30..442c0d2 100644
--- a/index.js
+++ b/index.js
@@ -2613,6 +2613,42 @@
   }
   S.ifElse = def('ifElse', {}, [Pred(a), Fn(a, b), Fn(a, b), a, b], ifElse);
 
+  //# when :: (a -> Boolean) -> (a -> a) -> a -> a
+  //.
+  //. Takes a unary predicate, a unary function, and a value of any type, and
+  //. returns the result of applying the function to the value if the value
+  //. satisfies the predicate; the value otherwise.
+  //.
+  //. ```javascript
+  //. > S.when(x => x >= 0, Math.sqrt, 16)
+  //. 4
+  //.
+  //. > S.when(x => x >= 0, Math.sqrt, -1)
+  //. -1
+  //. ```
+  function when(pred, f, x) {
+    return ifElse(pred, f, I, x);
+  }
+  S.when = def('when', {}, [Pred(a), Fn(a, a), a, a], when);
+
+  //# unless :: (a -> Boolean) -> (a -> a) -> a -> a
+  //.
+  //. Takes a unary predicate, a unary function, and a value of any type, and
+  //. returns the result of applying the function to the value if the value
+  //. does not satisfy the predicate; the value otherwise.
+  //.
+  //. ```javascript
+  //. > S.unless(x => x < 0, Math.sqrt, 16)
+  //. 4
+  //.
+  //. > S.unless(x => x < 0, Math.sqrt, -1)
+  //. -1
+  //. ```
+  function unless(pred, f, x) {
+    return ifElse(pred, I, f, x);
+  }
+  S.unless = def('unless', {}, [Pred(a), Fn(a, a), a, a], unless);
+
   //# allPass :: Array (a -> Boolean) -> a -> Boolean
   //.
   //. Takes an array of unary predicates and a value of any type


Test patch:
diff --git a/test/unless.js b/test/unless.js
new file mode 100644
index 0000000..93f5ba4
--- /dev/null
+++ b/test/unless.js
@@ -0,0 +1,19 @@
+'use strict';
+
+var S = require('..');
+
+var eq = require('./internal/eq');
+
+
+test('unless', function() {
+
+  eq(typeof S.unless, 'function');
+  eq(S.unless.length, 3);
+  eq(S.unless.toString(), 'unless :: (a -> Boolean) -> (a -> a) -> a -> a');
+
+  function lt0(x) { return x < 0; }
+
+  eq(S.unless(lt0, Math.sqrt, 16), 4);
+  eq(S.unless(lt0, Math.sqrt, -1), -1);
+
+});
diff --git a/test/when.js b/test/when.js
new file mode 100644
index 0000000..ee59419
--- /dev/null
+++ b/test/when.js
@@ -0,0 +1,19 @@
+'use strict';
+
+var S = require('..');
+
+var eq = require('./internal/eq');
+
+
+test('when', function() {
+
+  eq(typeof S.when, 'function');
+  eq(S.when.length, 3);
+  eq(S.when.toString(), 'when :: (a -> Boolean) -> (a -> a) -> a -> a');
+
+  function gte0(x) { return x >= 0; }
+
+  eq(S.when(gte0, Math.sqrt, 16), 4);
+  eq(S.when(gte0, Math.sqrt, -1), -1);
+
+});