From e5e7ae8c8ad8c7f9fd234d7919d3ae08f4f46c9e Mon Sep 17 00:00:00 2001 From: yazeedb Date: Wed, 18 Apr 2018 20:45:32 -0400 Subject: [PATCH 1/5] add snippet --- snippets/when.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 snippets/when.md diff --git a/snippets/when.md b/snippets/when.md new file mode 100644 index 000000000..16dbe9a54 --- /dev/null +++ b/snippets/when.md @@ -0,0 +1,18 @@ +### when + +Tests a value, `x`, against a predicate function. If `true`, return `fn(x)`. Else, return `x`. +`when(pred, whenTrue)` returns another function expecting `x` for better composability. + +```js +const when = (pred, whenTrue) => (x) => pred(x) ? whenTrue(x) : x; +``` + +```js +const doubleEvenNumbers = when( + (x) => x % 2 === 0, + (x) => x * 2 +); + +doubleEvenNumbers(2) // 4 +doubleEvenNumbers(1) // 1 +``` From eea09ac10689efe557001794c80c5fb030702246 Mon Sep 17 00:00:00 2001 From: yazeedb Date: Wed, 18 Apr 2018 20:45:58 -0400 Subject: [PATCH 2/5] update tag_database --- tag_database | 1 + 1 file changed, 1 insertion(+) diff --git a/tag_database b/tag_database index 3a9b2cb60..68504d62a 100644 --- a/tag_database +++ b/tag_database @@ -291,6 +291,7 @@ URLJoin:string,utility,regexp UUIDGeneratorBrowser:browser,utility,random UUIDGeneratorNode:node,utility,random validateNumber:utility,math +when:function without:array words:string,regexp xProd:array,math From 35c0412b404c4706f2256cb02eea4838b6769f4b Mon Sep 17 00:00:00 2001 From: yazeedb Date: Wed, 18 Apr 2018 20:47:57 -0400 Subject: [PATCH 3/5] word choice --- snippets/when.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/when.md b/snippets/when.md index 16dbe9a54..fd36e1a20 100644 --- a/snippets/when.md +++ b/snippets/when.md @@ -1,7 +1,7 @@ ### when Tests a value, `x`, against a predicate function. If `true`, return `fn(x)`. Else, return `x`. -`when(pred, whenTrue)` returns another function expecting `x` for better composability. +`when(pred, whenTrue)` returns a function expecting `x` for better composability. ```js const when = (pred, whenTrue) => (x) => pred(x) ? whenTrue(x) : x; From 3116d19997aca3a11d82bb49ba1411f6a262da3c Mon Sep 17 00:00:00 2001 From: yazeedb Date: Wed, 18 Apr 2018 20:49:43 -0400 Subject: [PATCH 4/5] add tests --- test/when/when.js | 2 ++ test/when/when.test.js | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 test/when/when.js create mode 100644 test/when/when.test.js diff --git a/test/when/when.js b/test/when/when.js new file mode 100644 index 000000000..3ce465630 --- /dev/null +++ b/test/when/when.js @@ -0,0 +1,2 @@ +const when = (pred, whenTrue) => (x) => pred(x) ? whenTrue(x) : x; +module.exports = when; \ No newline at end of file diff --git a/test/when/when.test.js b/test/when/when.test.js new file mode 100644 index 000000000..1a814190f --- /dev/null +++ b/test/when/when.test.js @@ -0,0 +1,18 @@ +const test = require('tape'); +const when = require('./when.js'); + +test('Testing when', (t) => { + //For more information on all the methods supported by tape + //Please go to https://github.com/substack/tape + t.true(typeof when === 'function', 'when is a Function'); + + const doubleEvenNumbers = when( + (x) => x % 2 === 0, + (x) => x * 2 + ); + + t.true(doubleEvenNumbers(2) === 4); + t.true(doubleEvenNumbers(1) === 1); + + t.end(); +}); From a9a09a746c405daa60d24df8637ac2e00c4d4ad8 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 19 Apr 2018 20:01:20 +0300 Subject: [PATCH 5/5] Update when.md --- snippets/when.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/snippets/when.md b/snippets/when.md index fd36e1a20..3ad0ceab7 100644 --- a/snippets/when.md +++ b/snippets/when.md @@ -1,7 +1,8 @@ ### when -Tests a value, `x`, against a predicate function. If `true`, return `fn(x)`. Else, return `x`. -`when(pred, whenTrue)` returns a function expecting `x` for better composability. +Tests a value, `x`, against a predicate function. If `true`, return `fn(x)`. Else, return `x`. + +Return a function expecting a single value, `x`, that returns the appropriate value based on `pred`. ```js const when = (pred, whenTrue) => (x) => pred(x) ? whenTrue(x) : x; @@ -12,7 +13,6 @@ const doubleEvenNumbers = when( (x) => x % 2 === 0, (x) => x * 2 ); - -doubleEvenNumbers(2) // 4 -doubleEvenNumbers(1) // 1 +doubleEvenNumbers(2); // 4 +doubleEvenNumbers(1); // 1 ```