Skip to content

frontend: convert utils/equal in typescript #3344

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 0 additions & 70 deletions frontends/web/src/utils/equal.js

This file was deleted.

28 changes: 28 additions & 0 deletions frontends/web/src/utils/equal.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,5 +114,33 @@ describe('equal', () => {
expect(equal(a, null)).toBeFalsy();
expect(equal(null, a)).toBeFalsy();
});

it('deep compares nested structures', () => {
const a = { foo: [1, { bar: 'baz' }] };
const b = { foo: [1, { bar: 'baz' }] };
expect(equal(a, b)).toBeTruthy();
});

});

describe('RegExp, functions and dates are currently not supported', () => {

it('compares RegExp objects correctly', () => {
expect(equal(/foo/g, /foo/g)).toBeTruthy();
expect(equal(/foo/g, /bar/g)).toBeFalsy();
});

it('compares Date objects correctly', () => {
expect(equal(new Date('2020-01-01'), new Date('2020-01-01'))).toBeTruthy();
expect(equal(new Date('2020-01-01'), new Date('2021-01-01'))).toBeFalsy();
});

it('does not consider functions equal', () => {
const a = () => {};
const b = () => {};
expect(equal(a, b)).toBeFalsy();
});

});

});
95 changes: 95 additions & 0 deletions frontends/web/src/utils/equal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/**
* Copyright 2018 Shift Devices AG
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

const isArray = Array.isArray;
const hasProp = Object.prototype.hasOwnProperty;
const typedKeys = <T extends object>(obj: Readonly<T>): readonly (keyof T)[] => {
return Object.keys(obj) as (keyof T)[];
};

/**
* Performs a deep equality check between two values.
*
* This function compares primitive types, arrays, plain objects, Date instances,
* and RegExp objects. It returns true if the values are deeply equal, false otherwise.
*
* - Uses `Object.is` for primitive comparison (handles `NaN`, `-0`, etc.)
* - Recursively checks array contents and object properties
* - Properly compares Date and RegExp objects
* - Returns false for functions, symbols, maps, sets, or class instances (not handled)
*
* @param a - The first value to compare.
* @param b - The second value to compare.
* @returns `true` if values are deeply equal, `false` otherwise.
*/
export const equal = (a: unknown, b: unknown): boolean => {
if (Object.is(a, b)) {
return true;
}

if (a instanceof Date && b instanceof Date) {
return a.getTime() === b.getTime();
}

if (a instanceof RegExp && b instanceof RegExp) {
return a.toString() === b.toString();
}

if (
(a instanceof Date) !== (b instanceof Date)
|| (a instanceof RegExp) !== (b instanceof RegExp)
) {
return false;
}

if (a && b && typeof a === 'object' && typeof b === 'object') {
if (isArray(a) && isArray(b)) {
if (a.length !== b.length) {
return false;
}
for (let i = 0; i < a.length; i++) {
if (!equal(a[i], b[i])) {
return false;
}
}
return true;
}

if (isArray(a) !== isArray(b)) {
return false;
}

const aKeys = typedKeys(a);
const bKeys = typedKeys(b);

if (aKeys.length !== bKeys.length) {
return false;
}

for (const key of aKeys) {
if (!hasProp.call(b, key)) {
return false;
}
if (!equal(a[key], b[key])) {
return false;
}
}

return true;
}

return false;
};