Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions helpful.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,26 @@
}(typeof self !== 'undefined' ? self : this, function() {
const helpful = {};


// partitionObject #30

helpful.partitionObject = function(obj, predicate) {
return Object.entries(obj).reduce(
([pass, fail], [key, value]) => {
if (predicate(key, value)) {
pass[key] = value;
} else {
fail[key] = value;
}
return [pass, fail];
},
[{}, {}]
);
};




helpful.stringToArray = function(string) {
if(string == null) {
return "";
Expand Down
31 changes: 31 additions & 0 deletions test/tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,39 @@ const assert = require("assert");

describe("Tests", function() {
describe("General", function() {

let i = 0;


i++;
it(`${i}: partitionObject - Should partition object by value > 2`, function () {
let expected = [{ c: 3, d: 4 }, { a: 1, b: 2 }];
let actual = helpful.partitionObject({ a: 1, b: 2, c: 3, d: 4 }, (k, v) => v > 2);
assert.deepEqual(actual, expected);
});

i++;
it(`${i}: partitionObject - Should partition object by key === 'a'`, function () {
let expected = [{ a: 1 }, { b: 2, c: 3, d: 4 }];
let actual = helpful.partitionObject({ a: 1, b: 2, c: 3, d: 4 }, (k, v) => k === "a");
assert.deepEqual(actual, expected);
});

i++;
it(`${i}: partitionObject - Should return all in first if predicate always true`, function () {
let expected = [{ a: 1, b: 2 }, {}];
let actual = helpful.partitionObject({ a: 1, b: 2 }, () => true);
assert.deepEqual(actual, expected);
});

i++;
it(`${i}: partitionObject - Should return all in second if predicate always false`, function () {
let expected = [{}, { a: 1, b: 2 }];
let actual = helpful.partitionObject({ a: 1, b: 2 }, () => false);
assert.deepEqual(actual, expected);
});


i++;
it(`${i}: stringToArray - should convert string to array`, function() {
let expected = ["t", "e", "s", "t"];
Expand Down