Skip to content

Commit 6428ec2

Browse files
authored
Merge pull request #6 from hckrnews/feature/array-with-object-diff
Now you can also check the diff of arrays with objects
2 parents 492fd42 + 25bd9dd commit 6428ec2

File tree

8 files changed

+106
-42
lines changed

8 files changed

+106
-42
lines changed

.github/workflows/nodejs.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99

1010
strategy:
1111
matrix:
12-
node-version: [10.x, 12.x, 14.x, 16.x]
12+
node-version: [10.x, 12.x, 14.x, 16.x, 18.x]
1313

1414
steps:
1515
- uses: actions/checkout@v1

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ node_js:
77
- "12"
88
- "14"
99
- "16"
10+
- "18"
1011
before_script:
1112
- npm run lint
1213
- npm test

package-lock.json

+39-31
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@hckrnews/arrays",
3-
"version": "2.0.3",
3+
"version": "2.1.0",
44
"description": "Usefull array helpers.",
55
"files": [
66
"src/helpers.js",

src/modules/diff.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ class Diff {
77

88
get differenceArray() {
99
return this.currentArray.filter(
10-
(value) => this.otherArray.indexOf(value) < 0
10+
(value) =>
11+
this.otherArray
12+
.map((item) => JSON.stringify(item))
13+
.indexOf(JSON.stringify(value)) < 0
1114
);
1215
}
1316

@@ -17,7 +20,10 @@ class Diff {
1720
}
1821

1922
return this.otherArray.filter(
20-
(value) => this.currentArray.indexOf(value) < 0
23+
(value) =>
24+
this.currentArray
25+
.map((item) => JSON.stringify(item))
26+
.indexOf(JSON.stringify(value)) < 0
2127
);
2228
}
2329

src/modules/intersect.js

+13-7
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
export default function intersect(original, array, multi) {
22
return original.filter((value) => {
33
if (multi) {
4-
const found = array.reduce((accumulator, currentValue) => {
5-
if (currentValue.indexOf(value) >= 0) {
6-
return accumulator + 1;
7-
}
4+
const found = array
5+
.map((item) => JSON.stringify(item))
6+
.reduce((accumulator, currentValue) => {
7+
if (currentValue.indexOf(JSON.stringify(value)) >= 0) {
8+
return accumulator + 1;
9+
}
810

9-
return accumulator;
10-
}, 0);
11+
return accumulator;
12+
}, 0);
1113

1214
return found === array.length;
1315
}
1416

15-
return array.indexOf(value) >= 0;
17+
return (
18+
array
19+
.map((item) => JSON.stringify(item))
20+
.indexOf(JSON.stringify(value)) >= 0
21+
);
1622
});
1723
}

test/diff-object.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { expect, describe, it } from '@jest/globals';
2+
import { Arr } from '../src/helpers.js';
3+
4+
const a = new Arr([{ name: 'John' }, { name: 'Peter' }, { name: 'Luke' }]);
5+
const b = [{ name: 'Peter' }, { name: 'Luke' }, { name: 'Paul' }];
6+
7+
describe('Diff array with objects', () => {
8+
describe('One side', () => {
9+
it('Should return the difference, in this example should it be John.', () => {
10+
expect([{ name: 'John' }]).toEqual(a.diff(b));
11+
});
12+
});
13+
14+
describe('Average both sides', () => {
15+
it('Should return the difference of both sides, in this example should it be John and Paul.', () => {
16+
expect([{ name: 'John' }, { name: 'Paul' }]).toEqual(
17+
a.diff(b, true)
18+
);
19+
});
20+
});
21+
});

test/intersect-object.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { expect, describe, it } from '@jest/globals';
2+
import { Arr } from '../src/helpers.js';
3+
4+
const a = new Arr([{ name: 'John' }, { name: 'Peter' }, { name: 'Luke' }]);
5+
const b = [{ name: 'Peter' }, { name: 'Luke' }, { name: 'Paul' }];
6+
const c = [{ name: 'Luke' }, { name: 'Paul' }, { name: 'John' }];
7+
8+
describe('Intersect array with objects', () => {
9+
describe('One side', () => {
10+
it('Should return the intersect, in this example should it be Peter and Luke.', () => {
11+
expect([{ name: 'Peter' }, { name: 'Luke' }]).toEqual(
12+
a.intersect(b)
13+
);
14+
});
15+
});
16+
17+
describe('Average both sides', () => {
18+
it('Should return the intersect of more than 2 arrays, in this example should it be Luke.', () => {
19+
expect([{ name: 'Luke' }]).toEqual(a.intersect([b, c], true));
20+
});
21+
});
22+
});

0 commit comments

Comments
 (0)