Skip to content

Commit

Permalink
feat: update isEqual helper
Browse files Browse the repository at this point in the history
  • Loading branch information
mlmoravek committed Feb 28, 2024
1 parent ada4b49 commit fe42369
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 17 deletions.
1 change: 0 additions & 1 deletion packages/oruga/src/components/dropdown/DropdownItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ const rootClasses = defineClasses(
:tabindex="tabindex"
data-oruga="dropdown-item"
@click="selectItem">
{{ isActive }}
<!--
@slot Override the label, default is label prop
-->
Expand Down
54 changes: 38 additions & 16 deletions packages/oruga/src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,24 +126,46 @@ export function clone<T extends object>(obj: T): T {
return Object.assign({}, obj);
}

export function isEqual(object1: unknown, object2: unknown): boolean {
const keys1 = Object.keys(object1);
const keys2 = Object.keys(object2);

if (keys1.length !== keys2.length) return false;

for (const key of keys1) {
const val1 = object1[key];
const val2 = object2[key];
const areObjects = isObject(val1) && isObject(val2);
if (
(areObjects && !isEqual(val1, val2)) ||
(!areObjects && val1 !== val2)
)
return false;
export function isEqual(valueA: unknown, valueB: unknown): boolean {
// Check if only one value is empty.
if ((!valueA && !!valueB) || (!!valueA && !valueB)) return false;

// Check if both values are objecs.
if (isObject(valueA) && isObject(valueB)) {
// Get the keys of both objects.
const keys1 = Object.keys(valueA);
const keys2 = Object.keys(valueB);

Check warning on line 137 in packages/oruga/src/utils/helpers.ts

View check run for this annotation

Codecov / codecov/patch

packages/oruga/src/utils/helpers.ts#L136-L137

Added lines #L136 - L137 were not covered by tests

// Check if the number of keys is the same.
if (keys1.length !== keys2.length) return false;

Check warning on line 140 in packages/oruga/src/utils/helpers.ts

View check run for this annotation

Codecov / codecov/patch

packages/oruga/src/utils/helpers.ts#L140

Added line #L140 was not covered by tests

// Iterate through the keys and compare their values recursively.
for (const key of keys1) {
const val1 = valueA[key];
const val2 = valueB[key];
const areObjects = isObject(val1) && isObject(val2);
if (
(areObjects && !isEqual(val1, val2)) ||
(!areObjects && val1 !== val2)

Check warning on line 149 in packages/oruga/src/utils/helpers.ts

View check run for this annotation

Codecov / codecov/patch

packages/oruga/src/utils/helpers.ts#L143-L149

Added lines #L143 - L149 were not covered by tests
)
return false;

Check warning on line 151 in packages/oruga/src/utils/helpers.ts

View check run for this annotation

Codecov / codecov/patch

packages/oruga/src/utils/helpers.ts#L151

Added line #L151 was not covered by tests
}
// If all checks pass, the objects are deep equal.
return true;

Check warning on line 154 in packages/oruga/src/utils/helpers.ts

View check run for this annotation

Codecov / codecov/patch

packages/oruga/src/utils/helpers.ts#L154

Added line #L154 was not covered by tests
}

// Check if both values are arrays.
else if (Array.isArray(valueA) && Array.isArray(valueB)) {
// Check if the number of keys is the same.
if (valueA.length !== valueB.length) return false;

Check warning on line 160 in packages/oruga/src/utils/helpers.ts

View check run for this annotation

Codecov / codecov/patch

packages/oruga/src/utils/helpers.ts#L160

Added line #L160 was not covered by tests
// Check if each value of the array is the same.
if (!valueA.every((val, index) => val === valueB[index])) return false;

Check warning on line 162 in packages/oruga/src/utils/helpers.ts

View check run for this annotation

Codecov / codecov/patch

packages/oruga/src/utils/helpers.ts#L162

Added line #L162 was not covered by tests
// If all checks pass, the arrays are deep equal.
return true;

Check warning on line 164 in packages/oruga/src/utils/helpers.ts

View check run for this annotation

Codecov / codecov/patch

packages/oruga/src/utils/helpers.ts#L164

Added line #L164 was not covered by tests
}

return true;
// If both objects are identical, return true.
else return valueA === valueB;
}

/**
Expand Down

0 comments on commit fe42369

Please sign in to comment.