-
Notifications
You must be signed in to change notification settings - Fork 269
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AAE-21946 Support JSON paths with non-standard characters for data ta… (
#9571) * AAE-21946 Support JSON paths with non-standard characters for data table widget * add unit test * move path parse to extern helper class
- Loading branch information
Showing
5 changed files
with
182 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
.../src/lib/form/components/widgets/data-table/helpers/data-table-path-parser.helper.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/*! | ||
* @license | ||
* Copyright © 2005-2023 Hyland Software, Inc. and its affiliates. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { DataTablePathParserHelper } from './data-table-path-parser.helper'; | ||
import { | ||
mockEuropeCountriesData, | ||
mockJsonNestedResponseEuropeCountriesDataWithSeparatorInPropertyName, | ||
mockJsonNestedResponseEuropeCountriesDataWithMultipleSpecialCharacters, | ||
mockJsonNestedResponseEuropeCountriesData | ||
} from '../../../../mocks/data-table-widget.mock'; | ||
|
||
describe('DataTablePathParserHelper', () => { | ||
let helper: DataTablePathParserHelper; | ||
|
||
beforeEach(() => { | ||
helper = new DataTablePathParserHelper(); | ||
}); | ||
|
||
it('should return the correct data for path with separator in nested brackets', () => { | ||
const data = mockJsonNestedResponseEuropeCountriesDataWithSeparatorInPropertyName; | ||
const path = 'response.[my.data].[country[data].country]'; | ||
const result = helper.retrieveDataFromPath(data, path); | ||
expect(result).toEqual(mockEuropeCountriesData); | ||
}); | ||
|
||
it('should return the correct data for path with special characters except separator (.) in brackets', () => { | ||
const data = mockJsonNestedResponseEuropeCountriesDataWithMultipleSpecialCharacters; | ||
const path = 'response.[xyz:abc,xyz-abc,xyz_abc,abc+xyz]'; | ||
const result = helper.retrieveDataFromPath(data, path); | ||
expect(result).toEqual(mockEuropeCountriesData); | ||
}); | ||
|
||
it('should return the correct data for path with special characters except separator (.) without brackets', () => { | ||
const data = mockJsonNestedResponseEuropeCountriesDataWithMultipleSpecialCharacters; | ||
const path = 'response.xyz:abc,xyz-abc,xyz_abc,abc+xyz'; | ||
const result = helper.retrieveDataFromPath(data, path); | ||
expect(result).toEqual(mockEuropeCountriesData); | ||
}); | ||
|
||
it('should return the correct data for path without separator in brackets', () => { | ||
const data = mockJsonNestedResponseEuropeCountriesData; | ||
const path = '[response].[my-data]'; | ||
const result = helper.retrieveDataFromPath(data, path); | ||
expect(result).toEqual(mockEuropeCountriesData); | ||
}); | ||
|
||
it('should return an empty array if the path does not exist in the data', () => { | ||
const data = {}; | ||
const path = 'nonexistent.path'; | ||
const result = helper.retrieveDataFromPath(data, path); | ||
expect(result).toEqual([]); | ||
}); | ||
|
||
it('should return the correct data if the path is nested', () => { | ||
const data = { level1: { level2: { level3: { level4: ['parrot', 'fish'] } } } }; | ||
const path = 'level1.level2.level3.level4'; | ||
const result = helper.retrieveDataFromPath(data, path); | ||
expect(result).toEqual(['parrot', 'fish']); | ||
}); | ||
|
||
it('should return the correct data if the path is NOT nested', () => { | ||
const data = { pets: ['cat', 'dog'] }; | ||
const path = 'pets'; | ||
const result = helper.retrieveDataFromPath(data, path); | ||
expect(result).toEqual(['cat', 'dog']); | ||
}); | ||
|
||
it('should return the correct data if the path is NOT nested with separator (.) in property name', () => { | ||
const data = { 'my.pets': ['cat', 'dog'] }; | ||
const path = '[my.pets]'; | ||
const result = helper.retrieveDataFromPath(data, path); | ||
expect(result).toEqual(['cat', 'dog']); | ||
}); | ||
}); |
50 changes: 50 additions & 0 deletions
50
...cloud/src/lib/form/components/widgets/data-table/helpers/data-table-path-parser.helper.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/*! | ||
* @license | ||
* Copyright © 2005-2023 Hyland Software, Inc. and its affiliates. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
export class DataTablePathParserHelper { | ||
private readonly splitPathRegEx = /\.(?![^[]*\])/g; | ||
private readonly removeSquareBracketsRegEx = /^\[(.*)\]$/; | ||
|
||
retrieveDataFromPath(data: any, path: string): any[] { | ||
const properties = this.splitPathIntoProperties(path); | ||
const currentProperty = this.removeSquareBracketsFromProperty(properties.shift()); | ||
|
||
if (!this.isPropertyExistsInData(data, currentProperty)) { | ||
return []; | ||
} | ||
|
||
const nestedData = data[currentProperty]; | ||
|
||
if (Array.isArray(nestedData)) { | ||
return nestedData; | ||
} | ||
|
||
return this.retrieveDataFromPath(nestedData, properties.join('.')); | ||
} | ||
|
||
private splitPathIntoProperties(path: string): string[] { | ||
return path.split(this.splitPathRegEx); | ||
} | ||
|
||
private removeSquareBracketsFromProperty(property: string): string { | ||
return property.replace(this.removeSquareBracketsRegEx, '$1'); | ||
} | ||
|
||
private isPropertyExistsInData(data: any, property: string): boolean { | ||
return Object.prototype.hasOwnProperty.call(data, property); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters