Skip to content

Commit 3a99bac

Browse files
Merge pull request #1 from viktor-maksimov/feature/add-more-functions
Add more functions.
2 parents 57e9747 + 6e2621d commit 3a99bac

9 files changed

+525
-8
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: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,29 @@ export function getUniqueValues(
1717
return output
1818
}
1919

20-
export function findOne(
20+
export function findFirstOneMatching(
21+
arrayOfObjects: Record<string, any>[],
22+
propertyName: any,
23+
propertyValue: any
24+
): Record<string, any> | null {
25+
let output: Record<string, any> | null = null
26+
27+
arrayOfObjects.some(obj => {
28+
if (obj[propertyName] !== undefined) {
29+
if (JSON.stringify(obj[propertyName]) === JSON.stringify(propertyValue)) {
30+
output = obj
31+
32+
return true
33+
}
34+
}
35+
36+
return false
37+
})
38+
39+
return output
40+
}
41+
42+
export function findLastOneMatching(
2143
arrayOfObjects: Record<string, any>[],
2244
propertyName: any,
2345
propertyValue: any
@@ -34,3 +56,81 @@ export function findOne(
3456

3557
return output
3658
}
59+
60+
export function findAllMatching(
61+
arrayOfObjects: Record<string, any>[],
62+
propertyName: any,
63+
propertyValue: any
64+
): Record<string, any>[] {
65+
let output: Record<string, any>[] = []
66+
67+
arrayOfObjects.map(obj => {
68+
if (obj[propertyName] !== undefined) {
69+
if (JSON.stringify(obj[propertyName]) === JSON.stringify(propertyValue)) {
70+
output.push(obj)
71+
}
72+
}
73+
})
74+
75+
return output
76+
}
77+
78+
export function removeFirstOneMatching(
79+
arrayOfObjects: Record<string, any>[],
80+
propertyName: any,
81+
propertyValue: any
82+
): Record<string, any>[] {
83+
let flag: boolean = false
84+
85+
return arrayOfObjects.filter(obj => {
86+
if (obj[propertyName] !== undefined && !flag) {
87+
if (JSON.stringify(obj[propertyName]) === JSON.stringify(propertyValue)) {
88+
flag = true
89+
90+
return false
91+
}
92+
}
93+
94+
return true
95+
})
96+
}
97+
98+
export function removeLastOneMatching(
99+
arrayOfObjects: Record<string, any>[],
100+
propertyName: any,
101+
propertyValue: any
102+
): Record<string, any>[] {
103+
let lastOneMatchingIndex: number = -1
104+
105+
arrayOfObjects.map((obj, index) => {
106+
if (obj[propertyName] !== undefined) {
107+
if (JSON.stringify(obj[propertyName]) === JSON.stringify(propertyValue)) {
108+
lastOneMatchingIndex = index
109+
}
110+
}
111+
})
112+
113+
let output = JSON.parse(JSON.stringify(arrayOfObjects))
114+
115+
if (lastOneMatchingIndex != -1) {
116+
output.splice(lastOneMatchingIndex, 1)
117+
}
118+
119+
return output
120+
}
121+
122+
export function removeAllMatching(
123+
arrayOfObjects: Record<string, any>[],
124+
propertyName: any,
125+
propertyValue: any
126+
): Record<string, any>[] {
127+
return arrayOfObjects.filter(obj => {
128+
if (obj[propertyName] !== undefined) {
129+
if (JSON.stringify(obj[propertyName]) === JSON.stringify(propertyValue)) {
130+
return false
131+
}
132+
}
133+
134+
return true
135+
})
136+
}

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+
})

0 commit comments

Comments
 (0)