Skip to content

Commit 6e2621d

Browse files
Added more functions. 100% test coverage.
1 parent 6ad3cc7 commit 6e2621d

9 files changed

+432
-9
lines changed

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "Useful functions to use with array of objects.",
55
"main": "lib/array-of-objects.js",
66
"repository": "git@github.com:viktor-maksimov/array-of-objects-functions.git",
7-
"author": "viktor-maksimov <viktor.slavov.maksimov@gmail.com>",
7+
"author": "viktor-maksimov",
88
"license": "MIT",
99
"private": false,
1010
"keywords": [
@@ -15,7 +15,8 @@
1515
"property",
1616
"object",
1717
"javascript",
18-
"typescript"
18+
"typescript",
19+
"find"
1920
],
2021
"homepage": "https://github.com/viktor-maksimov/array-of-objects-functions",
2122
"bugs": "https://github.com/viktor-maksimov/array-of-objects-functions/issues",

src/array-of-objects.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export function findLastOneMatching(
5757
return output
5858
}
5959

60-
export function findManyMatching(
60+
export function findAllMatching(
6161
arrayOfObjects: Record<string, any>[],
6262
propertyName: any,
6363
propertyValue: any
@@ -110,7 +110,13 @@ export function removeLastOneMatching(
110110
}
111111
})
112112

113-
return lastOneMatchingIndex != -1 ? arrayOfObjects.splice(lastOneMatchingIndex, 1) : arrayOfObjects
113+
let output = JSON.parse(JSON.stringify(arrayOfObjects))
114+
115+
if (lastOneMatchingIndex != -1) {
116+
output.splice(lastOneMatchingIndex, 1)
117+
}
118+
119+
return output
114120
}
115121

116122
export function removeAllMatching(

tests/findAllMatching.spec.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { findAllMatching } from "../src/array-of-objects"
2+
3+
const arrayOfObjects: Record<string, any>[] = [
4+
{
5+
id: 7,
6+
data: {
7+
title: "Test 1",
8+
description: "Lorem ipsum 1"
9+
}
10+
},
11+
{
12+
id: 11
13+
},
14+
{
15+
id: 15,
16+
data: {}
17+
},
18+
{
19+
id: 17,
20+
data: {
21+
title: "Test 2",
22+
description: "Lorem ipsum 2"
23+
}
24+
},
25+
{
26+
id: 19,
27+
data: {
28+
title: "Test 1",
29+
description: "Lorem ipsum 1"
30+
}
31+
}
32+
]
33+
34+
describe("Find All Matching - Get all matching", () => {
35+
const result = findAllMatching(arrayOfObjects, "data", {
36+
title: "Test 1",
37+
description: "Lorem ipsum 1"
38+
})
39+
const expectedResult = [
40+
{
41+
id: 7,
42+
data: {
43+
title: "Test 1",
44+
description: "Lorem ipsum 1"
45+
}
46+
},
47+
{
48+
id: 19,
49+
data: {
50+
title: "Test 1",
51+
description: "Lorem ipsum 1"
52+
}
53+
}
54+
]
55+
const notExpectedResult = null
56+
57+
it("Equal", () => {
58+
expect(result).toEqual(expectedResult)
59+
})
60+
it("Not equal", () => {
61+
expect(result).not.toEqual(notExpectedResult)
62+
})
63+
})

tests/findFirstOneMatching.spec.ts

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
import { findFirstOneMatching } from "../src/array-of-objects"
2+
3+
const arrayOfObjects: Record<string, any>[] = [
4+
{
5+
id: 7,
6+
data: {
7+
title: "Test 3",
8+
description: "Lorem ipsum 3"
9+
}
10+
},
11+
{
12+
id: 11
13+
},
14+
{
15+
id: 15,
16+
data: {}
17+
},
18+
{
19+
id: 17,
20+
data: {
21+
title: "Test 1",
22+
description: "Lorem ipsum 1"
23+
}
24+
},
25+
{
26+
id: 19,
27+
data: {
28+
title: "Test 2",
29+
description: "Lorem ipsum 2"
30+
}
31+
}
32+
]
33+
34+
describe("Find First One Matching - Get the only result", () => {
35+
const result = findFirstOneMatching(arrayOfObjects, "data", {
36+
title: "Test 1",
37+
description: "Lorem ipsum 1"
38+
})
39+
const expectedResult = {
40+
id: 17,
41+
data: {
42+
title: "Test 1",
43+
description: "Lorem ipsum 1"
44+
}
45+
}
46+
const notExpectedResult = null
47+
48+
it("Equal", () => {
49+
expect(result).toEqual(expectedResult)
50+
})
51+
it("Not equal", () => {
52+
expect(result).not.toEqual(notExpectedResult)
53+
})
54+
})
55+
56+
const arrayOfObjects2: Record<string, any>[] = [
57+
{
58+
id: 7,
59+
data: {
60+
title: "Test 1",
61+
description: "Lorem ipsum 1"
62+
}
63+
},
64+
{
65+
id: 11
66+
},
67+
{
68+
id: 15,
69+
data: {}
70+
},
71+
{
72+
id: 19,
73+
data: {
74+
title: "Test 1",
75+
description: "Lorem ipsum 1"
76+
}
77+
}
78+
]
79+
80+
describe("Find First One Matching - Get the first result", () => {
81+
const result = findFirstOneMatching(arrayOfObjects2, "data", {
82+
title: "Test 1",
83+
description: "Lorem ipsum 1"
84+
})
85+
const expectedResult = {
86+
id: 7,
87+
data: {
88+
title: "Test 1",
89+
description: "Lorem ipsum 1"
90+
}
91+
}
92+
const notExpectedResult = null
93+
const notExpectedResult2 = {
94+
id: 19,
95+
data: {
96+
title: "Test 1",
97+
description: "Lorem ipsum 1"
98+
}
99+
}
100+
101+
it("Equal", () => {
102+
expect(result).toEqual(expectedResult)
103+
})
104+
it("Not equal", () => {
105+
expect(result).not.toEqual(notExpectedResult)
106+
})
107+
it("Not equal", () => {
108+
expect(result).not.toEqual(notExpectedResult2)
109+
})
110+
})
111+
112+
describe("Find First One Matching - not finding property name", () => {
113+
const result = findFirstOneMatching(arrayOfObjects2, "parentId", {
114+
title: "Test 1",
115+
description: "Lorem ipsum 1"
116+
})
117+
const expectedResult = null
118+
119+
it("Equal", () => {
120+
expect(result).toEqual(expectedResult)
121+
})
122+
})
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { findOne } from "../src/array-of-objects"
1+
import { findLastOneMatching } from "../src/array-of-objects"
22

33
const arrayOfObjects: Record<string, any>[] = [
44
{
@@ -24,8 +24,8 @@ const arrayOfObjects: Record<string, any>[] = [
2424
}
2525
]
2626

27-
describe("Find One - Get the only result", () => {
28-
const result = findOne(arrayOfObjects, "data", {
27+
describe("Find Last One Matching - Get the only result", () => {
28+
const result = findLastOneMatching(arrayOfObjects, "data", {
2929
title: "Test 1",
3030
description: "Lorem ipsum 1"
3131
})
@@ -70,8 +70,8 @@ const arrayOfObjects2: Record<string, any>[] = [
7070
}
7171
]
7272

73-
describe("Find One - Get the last result", () => {
74-
const result = findOne(arrayOfObjects2, "data", {
73+
describe("Find Last One Matching - Get the last result", () => {
74+
const result = findLastOneMatching(arrayOfObjects2, "data", {
7575
title: "Test 1",
7676
description: "Lorem ipsum 1"
7777
})

tests/getUniqueValues.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,12 @@ describe("Get Unique Values", () => {
3535
expect(uniqueNames).not.toEqual(notExpectedUniqueNames)
3636
})
3737
})
38+
39+
describe("Get Unique Values - not finding property name", () => {
40+
const uniqueNames = getUniqueValues(arrayOfObjects, "address")
41+
const expectedUniqueNames = []
42+
43+
it("Equal", () => {
44+
expect(uniqueNames).toEqual(expectedUniqueNames)
45+
})
46+
})

tests/removeAllMatching.spec.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import { removeAllMatching } from "../src/array-of-objects"
2+
3+
const arrayOfObjects: Record<string, any>[] = [
4+
{
5+
id: 7
6+
},
7+
{
8+
id: 11,
9+
data: {}
10+
},
11+
{
12+
id: 15,
13+
data: {
14+
title: "Test 1",
15+
description: "Lorem ipsum 1"
16+
}
17+
},
18+
{
19+
id: 17,
20+
data: {
21+
title: "Test 2",
22+
description: "Lorem ipsum 2"
23+
}
24+
},
25+
{
26+
id: 19,
27+
data: {
28+
title: "Test 1",
29+
description: "Lorem ipsum 1"
30+
}
31+
}
32+
]
33+
34+
describe("Remove All Matching", () => {
35+
const result = removeAllMatching(arrayOfObjects, "data", {
36+
title: "Test 1",
37+
description: "Lorem ipsum 1"
38+
})
39+
const expectedResult = [
40+
{
41+
id: 7
42+
},
43+
{
44+
id: 11,
45+
data: {}
46+
},
47+
{
48+
id: 17,
49+
data: {
50+
title: "Test 2",
51+
description: "Lorem ipsum 2"
52+
}
53+
},
54+
]
55+
56+
it("Equal", () => {
57+
expect(result).toEqual(expectedResult)
58+
})
59+
it("Not equal", () => {
60+
expect(result).not.toEqual(arrayOfObjects)
61+
})
62+
})
63+
64+
//describe("Remove All Matching - not finding result", () => {
65+
// const result = removeAllMatching(arrayOfObjects, "data", {
66+
// title: "Test 3",
67+
// description: "Lorem ipsum 3"
68+
// })
69+
//
70+
// it("Equal", () => {
71+
// expect(result).toEqual(arrayOfObjects)
72+
// })
73+
//})

0 commit comments

Comments
 (0)