Data: frame compare refactor, drop unused skipProperties (#32700)

This commit is contained in:
Leon Sorokin 2021-04-05 17:29:58 -05:00 committed by GitHub
parent c0d83fc01e
commit 3f97b9972b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 56 deletions

View File

@ -93,39 +93,6 @@ describe('test comparisons', () => {
).toBeFalsy();
});
it('should skip provided properties', () => {
expect(
compareDataFrameStructures(
{
...frameB,
fields: [
field0,
{
...field1,
config: {
...field1.config,
},
},
],
},
{
...frameB,
fields: [
field0,
{
...field1,
config: {
...field1.config,
unit: 'rpm',
},
},
],
},
['unit']
)
).toBeTruthy();
});
describe('custom config comparison', () => {
it('handles custom config shallow equality', () => {
const a = {

View File

@ -15,10 +15,11 @@ import { DataFrame } from '../types/dataFrame';
*
* @beta
*/
export function compareDataFrameStructures(a: DataFrame, b: DataFrame, skipProperties?: string[]): boolean {
export function compareDataFrameStructures(a: DataFrame, b: DataFrame): boolean {
if (a === b) {
return true;
}
if (a?.fields?.length !== b?.fields?.length) {
return false;
}
@ -26,45 +27,36 @@ export function compareDataFrameStructures(a: DataFrame, b: DataFrame, skipPrope
for (let i = 0; i < a.fields.length; i++) {
const fA = a.fields[i];
const fB = b.fields[i];
if (fA.type !== fB.type) {
return false;
}
const cfgA = fA.config as any;
const cfgB = fB.config as any;
let aKeys = Object.keys(cfgA);
let bKeys = Object.keys(cfgB);
if (skipProperties) {
aKeys = aKeys.filter((k) => skipProperties.indexOf(k) < 0);
bKeys = aKeys.filter((k) => skipProperties.indexOf(k) < 0);
}
if (aKeys.length !== bKeys.length) {
return false;
}
for (const key of aKeys) {
if (skipProperties && skipProperties.indexOf(key) > -1) {
continue;
}
if (!cfgB.hasOwnProperty(key)) {
if (!(key in cfgB)) {
return false;
}
if (key === 'custom') {
if (!shallowCompare(cfgA[key], cfgB[key])) {
return false;
} else {
continue;
}
}
if (cfgA[key] !== cfgB[key]) {
} else if (cfgA[key] !== cfgB[key]) {
return false;
}
}
}
return true;
}
@ -88,29 +80,30 @@ export function compareArrayValues<T>(a: T[], b: T[], cmp: (a: T, b: T) => boole
return true;
}
type Cmp = (valA: any, valB: any) => boolean;
const defaultCmp: Cmp = (a, b) => a === b;
/**
* Checks if two objects are equal shallowly
*
* @beta
*/
export function shallowCompare<T extends {}>(a: T, b: T, cmp?: (valA: any, valB: any) => boolean) {
const aKeys = Object.keys(a);
const bKeys = Object.keys(b);
export function shallowCompare<T extends {}>(a: T, b: T, cmp: Cmp = defaultCmp) {
if (a === b) {
return true;
}
const aKeys = Object.keys(a);
const bKeys = Object.keys(b);
if (aKeys.length !== bKeys.length) {
return false;
}
for (let key of aKeys) {
if (cmp) {
//@ts-ignore
return cmp(a[key], b[key]);
}
//@ts-ignore
if (a[key] !== b[key]) {
if (!cmp(a[key], b[key])) {
return false;
}
}