mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Data: frame compare refactor, drop unused skipProperties (#32700)
This commit is contained in:
parent
c0d83fc01e
commit
3f97b9972b
@ -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 = {
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user