Skip to content

Commit e2539b4

Browse files
Added findOne method. Added tests for it. Readme badges added.
1 parent a7c965d commit e2539b4

File tree

3 files changed

+132
-1
lines changed

3 files changed

+132
-1
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22

33
Useful functions to help working with Javascript Array of Objects
44

5+
<br />
6+
7+
<a href="https://www.npmjs.com/package/array-of-objects-functions">
8+
<img src="https://user-images.githubusercontent.com/15264441/76154412-cfddb180-60d3-11ea-8c90-4c3c8f21f690.png" />
9+
</a>
10+
11+
<a href="https://www.npmjs.com/package/array-of-objects-functions">
12+
<img src="https://user-images.githubusercontent.com/15264441/76154414-d0764800-60d3-11ea-8e2e-ee42b5b56e69.png" />
13+
</a>
14+
515
## Usage
616

717
### Install the package

src/array-of-objects.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,29 @@ export function getUniqueValues(arrayOfObjects: Record<string, any>[], propertyN
33

44
arrayOfObjects.map(obj => {
55
if (obj[propertyName] !== undefined) {
6-
if (output.find(val => obj[propertyName] === val) === undefined) {
6+
if (output.find(val => JSON.stringify(obj[propertyName]) === JSON.stringify(val)) === undefined) {
77
output.push(obj[propertyName])
88
}
99
}
1010
})
1111

1212
return output
1313
}
14+
15+
export function findOne(
16+
arrayOfObjects: Record<string, any>[],
17+
propertyName: any,
18+
propertyValue: any
19+
): Record<string, any> | null {
20+
let output: Record<string, any> | null = null
21+
22+
arrayOfObjects.map(obj => {
23+
if (obj[propertyName] !== undefined) {
24+
if (JSON.stringify(obj[propertyName]) === JSON.stringify(propertyValue)) {
25+
output = obj
26+
}
27+
}
28+
})
29+
30+
return output
31+
}

tests/findOne.spec.ts

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import { findOne } 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: 19,
20+
data: {
21+
title: "Test 2",
22+
description: "Lorem ipsum 2"
23+
}
24+
}
25+
]
26+
27+
describe("Find One - Get the only result", () => {
28+
const result = findOne(arrayOfObjects, "data", {
29+
title: "Test 1",
30+
description: "Lorem ipsum 1"
31+
})
32+
const expectedResult = {
33+
id: 7,
34+
data: {
35+
title: "Test 1",
36+
description: "Lorem ipsum 1"
37+
}
38+
}
39+
const notExpectedResult = null
40+
41+
it("Equal", () => {
42+
expect(result).toEqual(expectedResult)
43+
})
44+
it("Not equal", () => {
45+
expect(result).not.toEqual(notExpectedResult)
46+
})
47+
})
48+
49+
const arrayOfObjects2: Record<string, any>[] = [
50+
{
51+
id: 7,
52+
data: {
53+
title: "Test 1",
54+
description: "Lorem ipsum 1"
55+
}
56+
},
57+
{
58+
id: 11
59+
},
60+
{
61+
id: 15,
62+
data: {}
63+
},
64+
{
65+
id: 19,
66+
data: {
67+
title: "Test 1",
68+
description: "Lorem ipsum 1"
69+
}
70+
}
71+
]
72+
73+
describe("Find One - Get the last result", () => {
74+
const result = findOne(arrayOfObjects2, "data", {
75+
title: "Test 1",
76+
description: "Lorem ipsum 1"
77+
})
78+
const expectedResult = {
79+
id: 19,
80+
data: {
81+
title: "Test 1",
82+
description: "Lorem ipsum 1"
83+
}
84+
}
85+
const notExpectedResult = null
86+
const notExpectedResult2 = {
87+
id: 7,
88+
data: {
89+
title: "Test 1",
90+
description: "Lorem ipsum 1"
91+
}
92+
}
93+
94+
it("Equal", () => {
95+
expect(result).toEqual(expectedResult)
96+
})
97+
it("Not equal", () => {
98+
expect(result).not.toEqual(notExpectedResult)
99+
})
100+
it("Not equal", () => {
101+
expect(result).not.toEqual(notExpectedResult2)
102+
})
103+
})

0 commit comments

Comments
 (0)