diff --git a/helpful.js b/helpful.js index d39db91..8c6986a 100644 --- a/helpful.js +++ b/helpful.js @@ -338,6 +338,19 @@ return [pass, notPass]; } + helpful.partitionObject = function (obj, func) { + let obj1 = {}; + let obj2 = {}; + for(let key in obj) { + if (func(key, obj[key])) { + obj1[key] = obj[key]; + }else { + obj2[key] = obj[key]; + } + } + return [obj1, obj2]; + } + helpful.hex = {}; /* Modified from https://github.com/TogaTech/tEnvoy */ diff --git a/test/tester.js b/test/tester.js index 200e993..f31f07e 100644 --- a/test/tester.js +++ b/test/tester.js @@ -318,6 +318,21 @@ describe("Tests", function() { assert.deepEqual(expected, actual); }); + i++; + it(`${i}: PartitionObject - Should partition an object based on a certain condition`, function() { + let expected = [{"a": 1}, {"b": 2, "c": 3, "d": 4}]; + let actual = helpful.partitionObject({"a": 1, "b": 2, "c": 3, "d": 4}, (key, value) => key == "a"); + ; + assert.deepEqual(expected, actual); + }); + + i++; + it(`${i}: PartitionObject - Should partition an object based on a certain condition`, function() { + let expected = [{"c": 3, "d": 4}, {"a": 1, "b": 2}]; + let actual = helpful.partitionObject({"a": 1, "b": 2, "c": 3, "d": 4}, (key, value) => value > 2); + assert.deepEqual(expected, actual); + }) + }); describe("Hex", function() { let i = 0;