Skip to content
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

Fix deepEqual helper function #1412

Merged
merged 4 commits into from
Jan 15, 2024
Merged
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
2 changes: 2 additions & 0 deletions l10n/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import faIR from './fa_IR';
import nbNO from './nb_NO';
import uaUA from './ua_UA';
import csCZ from './cs_CZ';
import svSE from './sv_SE';

export {
esES,
Expand All @@ -34,4 +35,5 @@ export {
nbNO,
uaUA,
csCZ,
svSE,
};
42 changes: 21 additions & 21 deletions l10n/sv_SE.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
export default {
search: {
placeholder: 'Sök...',
},
sort: {
sortAsc: 'Sortera kolumn stigande',
sortDesc: 'Sortera kolumn fallande',
},
pagination: {
previous: 'Föregående',
next: 'Nästa',
navigate: (page, pages) => `Sida ${page} av ${pages}`,
page: (page) => `Sida ${page}`,
showing: 'Visar',
of: 'av',
to: 'till',
results: 'resultat',
},
loading: 'Laddar...',
noRecordsFound: 'Inga matchande poster hittades',
error: 'Ett fel uppstod vid hämtning av data',
};
search: {
placeholder: 'Sök...',
},
sort: {
sortAsc: 'Sortera kolumn stigande',
sortDesc: 'Sortera kolumn fallande',
},
pagination: {
previous: 'Föregående',
next: 'Nästa',
navigate: (page, pages) => `Sida ${page} av ${pages}`,
page: (page) => `Sida ${page}`,
showing: 'Visar',
of: 'av',
to: 'till',
results: 'resultat',
},
loading: 'Laddar...',
noRecordsFound: 'Inga matchande poster hittades',
error: 'Ett fel uppstod vid hämtning av data',
};
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gridjs",
"version": "6.1.0",
"version": "6.1.1",
"description": "Advanced table plugin",
"author": "Afshin Mehrabani <[email protected]>",
"license": "MIT",
Expand Down
42 changes: 40 additions & 2 deletions src/util/deepEqual.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,44 @@
* @param b right object
* @returns
*/
export function deepEqual<A, B>(a: A, b: B) {
return JSON.stringify(a) === JSON.stringify(b);
export function deepEqual<A, B>(obj1: A, obj2: B) {
// If objects are not the same type, return false
if (typeof obj1 !== typeof obj2) {
return false;
}
// If objects are both null or undefined, return true
if (obj1 === null && obj2 === null) {
return true;
}
// If objects are both primitive types, compare them directly
if (typeof obj1 !== 'object') {
// eslint-disable-next-line
// @ts-ignore
return obj1 === obj2;
}
// If objects are arrays, compare their elements recursively
if (Array.isArray(obj1) && Array.isArray(obj2)) {
if (obj1.length !== obj2.length) {
return false;

Check warning on line 25 in src/util/deepEqual.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

Check warning on line 26 in src/util/deepEqual.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
for (let i = 0; i < obj1.length; i++) {
if (!deepEqual(obj1[i], obj2[i])) {
return false;

Check warning on line 29 in src/util/deepEqual.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

Check warning on line 30 in src/util/deepEqual.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
}
return true;
}
// If objects are both objects, compare their properties recursively
const keys1 = Object.keys(obj1);
const keys2 = Object.keys(obj2);
if (keys1.length !== keys2.length) {
return false;
}
for (const key of keys1) {
// eslint-disable-next-line no-prototype-builtins
if (!obj2.hasOwnProperty(key) || !deepEqual(obj1[key], obj2[key])) {
return false;
}
}
return true;
}
34 changes: 34 additions & 0 deletions tests/jest/util/deepEqual.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { deepEqual } from '../../../src/util/deepEqual';

describe('deepEqual', () => {
it('should return true when objects are the same', () => {
const result = deepEqual({ a: 42 }, { a: 42 });
expect(result).toBeTrue();
});

it('should return false when objects are not the same', () => {
const result = deepEqual({ b: 42 }, { a: 42 });
expect(result).toBeFalse();
});

it('should return true when nested objects are the same', () => {
const result = deepEqual({ a: 42, c: { a: 24 } }, { a: 42, c: { a: 24 } });
expect(result).toBeTrue();
});

it('should return false when nested objects not are the same', () => {
const result = deepEqual({ a: 42, c: { x: 24 } }, { a: 42, c: { a: 24 } });
expect(result).toBeFalse();
});

it('should return false when objects have functions', () => {
const result = deepEqual({ a: 42, c: jest.fn() }, { a: 42, c: jest.fn() });
expect(result).toBeFalse();
});

it('should return true when objects have same functions', () => {
const fn = jest.fn();
const result = deepEqual({ a: 42, c: fn }, { a: 42, c: fn });
expect(result).toBeTrue();
});
});
Loading