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(); ).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', () => { describe('custom config comparison', () => {
it('handles custom config shallow equality', () => { it('handles custom config shallow equality', () => {
const a = { const a = {

View File

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