From de77807dc998b0d3a9bde32640dfbf8c5d871fa2 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Wed, 14 Feb 2018 12:13:07 +0200 Subject: [PATCH] Add bifurcate, bifurcateBy --- snippets/bifurcate.md | 17 +++++++++++++++++ snippets/bifurcateBy.md | 17 +++++++++++++++++ tag_database | 2 ++ test/bifurcate/bifurcate.js | 6 ++++++ test/bifurcate/bifurcate.test.js | 14 ++++++++++++++ test/bifurcateBy/bifurcateBy.js | 6 ++++++ test/bifurcateBy/bifurcateBy.test.js | 14 ++++++++++++++ test/testlog | 18 ++++++++++++++---- 8 files changed, 90 insertions(+), 4 deletions(-) create mode 100644 snippets/bifurcate.md create mode 100644 snippets/bifurcateBy.md create mode 100644 test/bifurcate/bifurcate.js create mode 100644 test/bifurcate/bifurcate.test.js create mode 100644 test/bifurcateBy/bifurcateBy.js create mode 100644 test/bifurcateBy/bifurcateBy.test.js diff --git a/snippets/bifurcate.md b/snippets/bifurcate.md new file mode 100644 index 000000000..4177c9483 --- /dev/null +++ b/snippets/bifurcate.md @@ -0,0 +1,17 @@ +### bifurcate + +Splits values into two groups. If an element in `filter` is truthy, the corresponding element in the collection belongs to the first group; otherwise, it belongs to the second group. + +Use `Array.reduce()` and `Array.push()` to add elements to groups, based on `filter`. + +```js +const bifurcate = (arr, filter) => + arr.reduce((acc, val, i) => (acc[filter[i] ? 0 : 1].push(val), acc), [ + [], + [], + ]); +``` + +```js +bifurcate([ 'beep', 'boop', 'foo', 'bar' ], [ true, true, false, true ]); // [ ['beep', 'boop', 'bar'], ['foo'] ] +``` diff --git a/snippets/bifurcateBy.md b/snippets/bifurcateBy.md new file mode 100644 index 000000000..23f81b06f --- /dev/null +++ b/snippets/bifurcateBy.md @@ -0,0 +1,17 @@ +### bifurcateBy + +Splits values into two groups according to a predicate function, which specifies which group an element in the input collection belongs to. If the predicate function returns a truthy value, the collection element belongs to the first group; otherwise, it belongs to the second group. + +Use `Array.reduce()` and `Array.push()` to add elements to groups, based on the value returned by `fn` for each element. + +```js +const bifurcateBy = (arr, fn) => + arr.reduce((acc, val, i) => (acc[fn(val, i) ? 0 : 1].push(val), acc), [ + [], + [], + ]); +``` + +```js +bifurcateBy([ 'beep', 'boop', 'foo', 'bar' ], x => x[0] === 'b'); // [ ['beep', 'boop', 'bar'], ['foo'] ] +``` diff --git a/tag_database b/tag_database index 6459c8aae..c69f793c6 100644 --- a/tag_database +++ b/tag_database @@ -5,6 +5,8 @@ atob:node,string,utility attempt:function average:math,array averageBy:math,array,function +bifurcate:array +bifurcateBy:array,function bind:function,object bindAll:object,function bindKey:function,object diff --git a/test/bifurcate/bifurcate.js b/test/bifurcate/bifurcate.js new file mode 100644 index 000000000..86fc9f7c1 --- /dev/null +++ b/test/bifurcate/bifurcate.js @@ -0,0 +1,6 @@ +const bifurcate = (arr, filter) => +arr.reduce((acc, val, i) => (acc[filter[i] ? 0 : 1].push(val), acc), [ +[], +[], +]); +module.exports = bifurcate; \ No newline at end of file diff --git a/test/bifurcate/bifurcate.test.js b/test/bifurcate/bifurcate.test.js new file mode 100644 index 000000000..c65333ee0 --- /dev/null +++ b/test/bifurcate/bifurcate.test.js @@ -0,0 +1,14 @@ +const test = require('tape'); +const bifurcate = require('./bifurcate.js'); + +test('Testing bifurcate', (t) => { + //For more information on all the methods supported by tape + //Please go to https://github.com/substack/tape + t.true(typeof bifurcate === 'function', 'bifurcate is a Function'); + t.deepEqual(bifurcate([ 'beep', 'boop', 'foo', 'bar' ], [ true, true, false, true ]), [ ['beep', 'boop', 'bar'], ['foo'] ], 'Splits the collection into two groups'); + //t.deepEqual(bifurcate(args..), 'Expected'); + //t.equal(bifurcate(args..), 'Expected'); + //t.false(bifurcate(args..), 'Expected'); + //t.throws(bifurcate(args..), 'Expected'); + t.end(); +}); diff --git a/test/bifurcateBy/bifurcateBy.js b/test/bifurcateBy/bifurcateBy.js new file mode 100644 index 000000000..fb153edd7 --- /dev/null +++ b/test/bifurcateBy/bifurcateBy.js @@ -0,0 +1,6 @@ +const bifurcateBy = (arr, fn) => +arr.reduce((acc, val, i) => (acc[fn(val, i) ? 0 : 1].push(val), acc), [ +[], +[], +]); +module.exports = bifurcateBy; \ No newline at end of file diff --git a/test/bifurcateBy/bifurcateBy.test.js b/test/bifurcateBy/bifurcateBy.test.js new file mode 100644 index 000000000..a6c293638 --- /dev/null +++ b/test/bifurcateBy/bifurcateBy.test.js @@ -0,0 +1,14 @@ +const test = require('tape'); +const bifurcateBy = require('./bifurcateBy.js'); + +test('Testing bifurcateBy', (t) => { + //For more information on all the methods supported by tape + //Please go to https://github.com/substack/tape + t.true(typeof bifurcateBy === 'function', 'bifurcateBy is a Function'); + t.deepEqual(bifurcateBy([ 'beep', 'boop', 'foo', 'bar' ], x => x[0] === 'b'), [ ['beep', 'boop', 'bar'], ['foo'] ], 'Splits the collection into two groups'); + //t.deepEqual(bifurcateBy(args..), 'Expected'); + //t.equal(bifurcateBy(args..), 'Expected'); + //t.false(bifurcateBy(args..), 'Expected'); + //t.throws(bifurcateBy(args..), 'Expected'); + t.end(); +}); diff --git a/test/testlog b/test/testlog index 9ffd675a9..f7972c818 100644 --- a/test/testlog +++ b/test/testlog @@ -1,4 +1,4 @@ -Test log for: Wed Feb 14 2018 11:56:24 GMT+0200 (GTB Standard Time) +Test log for: Wed Feb 14 2018 12:12:48 GMT+0200 (GTB Standard Time) > 30-seconds-of-code@0.0.1 test G:\My Files\git Repositories\30-seconds-of-code > tape test/**/*.test.js | tap-spec @@ -53,6 +53,16 @@ Test log for: Wed Feb 14 2018 11:56:24 GMT+0200 (GTB Standard Time) √ Produces the right result with a function √ Produces the right result with a property name + Testing bifurcate + + √ bifurcate is a Function + √ Splits the collection into two groups + + Testing bifurcateBy + + √ bifurcateBy is a Function + √ Splits the collection into two groups + Testing binarySearch √ binarySearch is a Function @@ -1803,15 +1813,15 @@ Test log for: Wed Feb 14 2018 11:56:24 GMT+0200 (GTB Standard Time) Testing zipWith √ zipWith is a Function - √ Runs the function provided √ Sends a GET request + √ Runs the function provided √ Runs promises in series √ Sends a POST request √ Works with multiple promises - total: 905 - passing: 905 + total: 909 + passing: 909 duration: 2.4s