Skip to content

Commit

Permalink
fix: handle null and undefined in @fluentui/utilities shallowCompare (m…
Browse files Browse the repository at this point in the history
  • Loading branch information
smhigley authored and NotWoods committed Nov 18, 2022
1 parent 67903bc commit fe76b11
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "fix: handle null and undefined in shallowCompare, add tests",
"packageName": "@fluentui/utilities",
"email": "sarah.higley@microsoft.com",
"dependentChangeType": "patch"
}
71 changes: 70 additions & 1 deletion packages/utilities/src/object.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,73 @@
import { assign, filteredAssign, mapEnumByName, values, omit } from './object';
import { assign, filteredAssign, mapEnumByName, values, omit, shallowCompare } from './object';

describe('shallowCompare', () => {
it('returns true for matching objects', () => {
const a = {
a: 1,
b: 'string',
c: {
d: 2,
},
};

const b = { ...a };

expect(shallowCompare(a, b)).toBeTruthy();
});

it('returns false when one object is a superset of the other', () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const a: { [key: string]: any } = {
a: 1,
b: 'string',
c: {
d: 2,
},
};

const b = { ...a, e: 'extra' };

expect(shallowCompare(a, b)).toBeFalsy();

a.e = 'extra';
a.f = 3;

expect(shallowCompare(a, b)).toBeFalsy();
});

it('returns false when nested objects are not strictly equal', () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const a: { [key: string]: any } = {
a: 1,
b: 'string',
c: {
d: 2,
},
};

const b = { ...a, c: { ...a.c } };

expect(shallowCompare(a, b)).toBeFalsy();
});

it('returns true for two empty objects', () => {
expect(shallowCompare({}, {})).toBeTruthy();
});

it('returns true for two falsy values', () => {
expect(shallowCompare(null, null)).toBeTruthy();
expect(shallowCompare(undefined, undefined)).toBeTruthy();
expect(shallowCompare(null, undefined)).toBeTruthy();
expect(shallowCompare(0, '')).toBeTruthy();
expect(shallowCompare(null, '')).toBeTruthy();
expect(shallowCompare(0, undefined)).toBeTruthy();
});

it('returns false when comparing null or undefined against an object', () => {
expect(shallowCompare(null, { a: 1 })).toBeFalsy();
expect(shallowCompare(undefined, { a: 1 })).toBeFalsy();
});
});

describe('assign', () => {
it('can copy an object', () => {
Expand Down
5 changes: 5 additions & 0 deletions packages/utilities/src/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function shallowCompare<TA extends any, TB extends any>(a: TA, b: TB): boolean {
if (!a || !b) {
// only return true if both a and b are falsy
return !a && !b;
}

for (let propName in a) {
if ((a as Object).hasOwnProperty(propName)) {
if (!(b as Object).hasOwnProperty(propName) || (b as { [key: string]: unknown })[propName] !== a[propName]) {
Expand Down

0 comments on commit fe76b11

Please sign in to comment.