Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue #27 #32

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,17 @@ let deepFlattenedArray3 = helpful.deepFlattenArray([[0, 1], [2, [3, 4, [5, [6]]]

**Return Type:** Array (`Array(5) [0, 1, 2, 3, 4]`)

### differenceOfObjects
Finds the difference of two objects (any keys that are present in the second object are removed from the first)
```javascript
let differenceOfObjs = helpful.differenceOfObjects({"a": 1, "b": 2}, {"b": 3, "c": 4}); // {"a": 1}
```
**Parameters:**
- object1: Object (`{"a": 1, "b": 2}`)
- object2: Object (`{"b": 3, "c": 4}`)

**Return Type:** Object (`{"a": 1}`)

## Hex

### hex.convertFromString
Expand Down
16 changes: 16 additions & 0 deletions helpful.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,22 @@
return res;
}

helpful.differenceOfObjects = function(object1, object2){
if(Object.keys(object1).length === 0 || Object.keys(object2).length === 0) {
return [];
}
let array1 = Object.keys(object1);
let array2 = Object.keys(object2);
for(let i = 0; i < array1.length; i++){
for(let j = 0; j < array2.length; j++){
if(array1[i] == array2[j]){
delete object1[array1[i]];
}
}
}
return object1;
}

helpful.hex = {};

/* Modified from https://github.com/TogaTech/tEnvoy */
Expand Down
7 changes: 7 additions & 0 deletions test/tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,13 @@ describe("Tests", function() {
let actual = helpful.deepFlattenArray([[0, 1], [2, [3, 4, [5, [6]]]]]);
assert.deepEqual(expected, actual);
});

i++;
it(`${i}: differenceOfObjects - Should remove any keys from the first object that are presented in the second object`, function() {
let expected = {"a": 1};
let actual = helpful.differenceOfObjects({"a": 1, "b": 2}, {"b": 3, "c": 4});
assert.deepEqual(expected, actual);
});

});
describe("Hex", function() {
Expand Down