chore(shared): move shallowCompare to object.ts

This commit is contained in:
Bowen Tan 2022-06-23 17:36:13 +08:00
parent 9cfb133adc
commit ea65948c6c
4 changed files with 16 additions and 16 deletions

View File

@ -1,4 +1,4 @@
import { shallowCompare } from '../src/utils/shallowCompare';
import { shallowCompare } from '../src/utils';
describe('shallowCompare function', () => {
it('compare values', () => {

View File

@ -1,4 +1,3 @@
export * from './shallowCompare';
export * from './number';
export * from './spec';
export * from './expression';

View File

@ -27,3 +27,18 @@ export function traverse(value: unknown, seen: Set<unknown> = new Set()) {
export function isPromise(value: object): value is Promise<unknown> {
return value instanceof Promise;
}
export function shallowCompare(obj1: any, obj2: any) {
if (typeof obj1 === 'object' && typeof obj2 === 'object') {
if (Object.keys(obj1).length !== Object.keys(obj2).length) return false;
for (const key in obj1) {
if (key in obj2 && obj1[key] === obj2[key]) {
continue;
} else {
return false;
}
}
return true;
}
return obj1 === obj2;
}

View File

@ -1,14 +0,0 @@
export function shallowCompare(obj1: any, obj2: any) {
if (typeof obj1 === 'object' && typeof obj2 === 'object') {
if (Object.keys(obj1).length !== Object.keys(obj2).length) return false;
for (const key in obj1) {
if (key in obj2 && obj1[key] === obj2[key]) {
continue;
} else {
return false;
}
}
return true;
}
return obj1 === obj2;
}