Skip to content

Commit e575c1b

Browse files
committed
feat: 🎸 add equal fucntion
1 parent aaea326 commit e575c1b

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

‎src/equal/index.ts‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*******************************************************************************
2+
* (c) 2023 unipackage
3+
*
4+
* Licensed under either the MIT License (the "MIT License") or the Apache License, Version 2.0
5+
* (the "Apache License"). You may not use this file except in compliance with one of these
6+
* licenses. You may obtain a copy of the MIT License at
7+
*
8+
* https://opensource.org/licenses/MIT
9+
*
10+
* Or the Apache License, Version 2.0 at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the MIT License or the Apache License for the specific language governing permissions and
18+
* limitations under the respective licenses.
19+
********************************************************************************/
20+
21+
import { isEqual } from "lodash"
22+
23+
/**
24+
* Compares two objects for equality based on specified or all properties.
25+
* @param one - The first object.
26+
* @param another - The second object.
27+
* @param fields - Optional parameter specifying properties to compare. If not provided, compares all properties.
28+
* @returns True if the objects are equal, false otherwise.
29+
*/
30+
export function equal<T extends Object>(
31+
one: T,
32+
another: T,
33+
fields?: Array<keyof T>
34+
): boolean {
35+
// If no specific properties are specified, use all properties of the object
36+
const properties = fields ?? (Object.keys(one) as Array<keyof T>)
37+
38+
// Iterate through each specified property and compare their values
39+
for (const property of properties) {
40+
if (!isEqual(one[property], another[property])) {
41+
// If any property is not equal, return false
42+
return false
43+
}
44+
}
45+
46+
// All properties are equal, return true
47+
return true
48+
}

‎test/equal/index.test.ts‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*******************************************************************************
2+
* (c) 2023 unipackage
3+
*
4+
* Licensed under either the MIT License (the "MIT License") or the Apache License, Version 2.0
5+
* (the "Apache License"). You may not use this file except in compliance with one of these
6+
* licenses. You may obtain a copy of the MIT License at
7+
*
8+
* https://opensource.org/licenses/MIT
9+
*
10+
* Or the Apache License, Version 2.0 at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the MIT License or the Apache License for the specific language governing permissions and
18+
* limitations under the respective licenses.
19+
********************************************************************************/
20+
21+
import { expect } from "chai"
22+
import { describe, it } from "mocha"
23+
import { equal } from "../../src/equal"
24+
25+
// Sample entity class for testing
26+
class SampleEntity {
27+
constructor(public id: number, public name: string) {}
28+
}
29+
30+
describe("equal function", () => {
31+
it("should return true for equal objects", () => {
32+
const obj1 = { id: 1, name: "John" }
33+
const obj2 = { id: 1, name: "John" }
34+
35+
// Using the equal function to compare objects
36+
const result = equal(obj1, obj2)
37+
38+
expect(result).to.be.true
39+
})
40+
41+
it("should return false for non-equal objects", () => {
42+
const obj1 = { id: 1, name: "John" }
43+
const obj2 = { id: 2, name: "Jane" }
44+
45+
// Using the equal function to compare objects
46+
const result = equal(obj1, obj2)
47+
48+
expect(result).to.be.false
49+
})
50+
51+
it("should compare specified fields only", () => {
52+
const entity1 = new SampleEntity(1, "John")
53+
const entity2 = new SampleEntity(1, "Jane")
54+
55+
// Using the equal function to compare objects with specified fields
56+
const result = equal(entity1, entity2, ["id"])
57+
58+
expect(result).to.be.true // Only ID is compared, so it should be considered equal
59+
})
60+
})

0 commit comments

Comments
 (0)