Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for checkForFunctionProperty so that order does not matter #6248

Merged
merged 11 commits into from
Apr 9, 2024
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- [BUG][Multiple Datasource] Fix data source filter bug and add tests ([#6152](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6152))
- [BUG][Multiple Datasource] Fix obsolete snapshots for test within data source management plugin ([#6185](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6185))
- [Workspace] Add base path when parse url in http service ([#6233](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6233))
- [BUG] Fix for checkForFunctionProperty so that order does not matter ([#6248](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6248))

### 🚞 Infrastructure

Expand Down
51 changes: 45 additions & 6 deletions src/plugins/vis_type_vega/public/data_model/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ describe('Utils.handleInvalidQuery', () => {
test('should return null if input object has function as property', async () => {
const input = {
key1: 'value1',
key2: () => {
alert('Hello!');
},
key2: () => jest.fn(),
};

expect(Utils.handleInvalidQuery(input)).toBe(null);
Expand All @@ -71,9 +69,7 @@ describe('Utils.handleInvalidQuery', () => {
const input = {
key1: 'value1',
key2: {
func: () => {
alert('Hello!');
},
func: () => jest.fn(),
},
};
expect(Utils.handleInvalidQuery(input)).toBe(null);
Expand All @@ -91,4 +87,47 @@ describe('Utils.handleInvalidQuery', () => {
}).toThrowError();
});
});

test('should identify object contains function properties', async () => {
cwperks marked this conversation as resolved.
Show resolved Hide resolved
const input1 = {
key1: 'value1',
key2: () => jest.fn(),
};

expect(Utils.checkForFunctionProperty(input1)).toBe(true);

const input2 = {
key1: () => jest.fn(),
key2: 'value1',
};

expect(Utils.checkForFunctionProperty(input2)).toBe(true);

const nestedInput = {
key1: {
nestedKey1: 'nestedValue1',
nestedKey2: () => jest.fn(),
},
key2: 'value1',
};

expect(Utils.checkForFunctionProperty(nestedInput)).toBe(true);

const inputWithoutFn = {
key1: 'value1',
key2: 'value2',
};

expect(Utils.checkForFunctionProperty(inputWithoutFn)).toBe(false);

const nestedInputWithoutFn = {
key1: {
nestedKey1: 'nestedValue1',
nestedKey2: 'nestedValue2',
},
key2: 'value2',
};

expect(Utils.checkForFunctionProperty(nestedInputWithoutFn)).toBe(false);
});
});
16 changes: 8 additions & 8 deletions src/plugins/vis_type_vega/public/data_model/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,14 @@ export class Utils {
}

static checkForFunctionProperty(object: object): boolean {
let result = false;
Object.values(object).forEach((value) => {
result =
typeof value === 'function'
? true
: Utils.isObject(value) && Utils.checkForFunctionProperty(value);
});
return result;
for (const value of Object.values(object)) {
if (typeof value === 'function') {
return true;
} else if (Utils.isObject(value) && Utils.checkForFunctionProperty(value)) {
return true;
}
}
return false;
Comment on lines +82 to +89
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cwperks could we refactory code a little


    for (const value of Object.values(object)) {
      if (typeof value === 'function') return true;
      if (Utils.isObject(value) && Utils.checkForFunctionProperty(value)) return true;
    }
    return false;

}

static handleInvalidDate(date: unknown): number | string | Date | null {
Expand Down
Loading