Skip to content

Commit

Permalink
Merge pull request #1412 from grid-js/fix-search
Browse files Browse the repository at this point in the history
Fix deepEqual helper function
  • Loading branch information
afshinm committed Jan 15, 2024
2 parents ea1eedb + 054c961 commit 2d337e6
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 26 deletions.
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;
}
for (let i = 0; i < obj1.length; i++) {
if (!deepEqual(obj1[i], obj2[i])) {
return false;
}
}
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();
});
});

0 comments on commit 2d337e6

Please sign in to comment.