DS Picker: Ignore capitalization when sorting by name (#72665)

This commit is contained in:
Ivan Ortega Alba 2023-08-01 13:20:59 +02:00 committed by GitHub
parent 7469f58709
commit 148b6186b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 7 deletions

View File

@ -32,15 +32,27 @@ describe('isDataSourceMatch', () => {
describe('getDataSouceCompareFn', () => {
const dataSources = [
{ uid: 'c', name: 'c', meta: { builtIn: false } },
{ uid: 'D', name: 'D', meta: { builtIn: false } },
{ uid: 'a', name: 'a', meta: { builtIn: true } },
{ uid: 'b', name: 'b', meta: { builtIn: false } },
] as DataSourceInstanceSettings[];
it('sorts data sources alphabetically ignoring captitalization', () => {
dataSources.sort(getDataSourceCompareFn(undefined, [], []));
expect(dataSources).toEqual([
{ uid: 'b', name: 'b', meta: { builtIn: false } },
{ uid: 'c', name: 'c', meta: { builtIn: false } },
{ uid: 'D', name: 'D', meta: { builtIn: false } },
{ uid: 'a', name: 'a', meta: { builtIn: true } },
] as DataSourceInstanceSettings[]);
});
it('sorts built in datasources last and other data sources alphabetically', () => {
dataSources.sort(getDataSourceCompareFn(undefined, [], []));
expect(dataSources).toEqual([
{ uid: 'b', name: 'b', meta: { builtIn: false } },
{ uid: 'c', name: 'c', meta: { builtIn: false } },
{ uid: 'D', name: 'D', meta: { builtIn: false } },
{ uid: 'a', name: 'a', meta: { builtIn: true } },
] as DataSourceInstanceSettings[]);
});
@ -50,6 +62,7 @@ describe('getDataSouceCompareFn', () => {
expect(dataSources).toEqual([
{ uid: 'c', name: 'c', meta: { builtIn: false } },
{ uid: 'b', name: 'b', meta: { builtIn: false } },
{ uid: 'D', name: 'D', meta: { builtIn: false } },
{ uid: 'a', name: 'a', meta: { builtIn: true } },
] as DataSourceInstanceSettings[]);
});
@ -60,6 +73,7 @@ describe('getDataSouceCompareFn', () => {
{ uid: 'a', name: 'a', meta: { builtIn: true } },
{ uid: 'c', name: 'c', meta: { builtIn: false } },
{ uid: 'b', name: 'b', meta: { builtIn: false } },
{ uid: 'D', name: 'D', meta: { builtIn: false } },
] as DataSourceInstanceSettings[]);
});
@ -68,6 +82,7 @@ describe('getDataSouceCompareFn', () => {
expect(dataSources).toEqual([
{ uid: 'b', name: 'b', meta: { builtIn: false } },
{ uid: 'c', name: 'c', meta: { builtIn: false } },
{ uid: 'D', name: 'D', meta: { builtIn: false } },
{ uid: 'a', name: 'a', meta: { builtIn: true } },
] as DataSourceInstanceSettings[]);
});
@ -78,7 +93,7 @@ describe('getDataSouceCompareFn', () => {
{ uid: 'b', name: 'b', meta: { builtIn: false } },
{ uid: 'c', name: 'c', meta: { builtIn: false } },
{ uid: 'e', name: 'e', meta: { builtIn: false } },
{ uid: 'd', name: 'd', meta: { builtIn: false } },
{ uid: 'D', name: 'D', meta: { builtIn: false } },
{ uid: 'f', name: 'f', meta: { builtIn: false } },
] as DataSourceInstanceSettings[];
@ -87,7 +102,7 @@ describe('getDataSouceCompareFn', () => {
{ uid: 'c', name: 'c', meta: { builtIn: false } },
{ uid: 'e', name: 'e', meta: { builtIn: false } },
{ uid: 'b', name: 'b', meta: { builtIn: false } },
{ uid: 'd', name: 'd', meta: { builtIn: false } },
{ uid: 'D', name: 'D', meta: { builtIn: false } },
{ uid: 'f', name: 'f', meta: { builtIn: false } },
{ uid: 'a', name: 'a', meta: { builtIn: true } },
] as DataSourceInstanceSettings[]);

View File

@ -44,6 +44,9 @@ export function getDataSourceCompareFn(
dataSourceVariablesIDs: string[]
) {
const cmpDataSources = (a: DataSourceInstanceSettings, b: DataSourceInstanceSettings) => {
const nameA = a.name.toUpperCase();
const nameB = b.name.toUpperCase();
// Sort the current ds before everything else.
if (current && isDataSourceMatch(a, current)) {
return -1;
@ -68,8 +71,6 @@ export function getDataSourceCompareFn(
return -1;
} else if (bIsVariable && !aIsVariable) {
return 1;
} else if (bIsVariable && aIsVariable) {
return a.name < b.name ? -1 : 1;
}
// Sort built in DataSources to the bottom and alphabetically by name.
@ -77,12 +78,10 @@ export function getDataSourceCompareFn(
return 1;
} else if (b.meta.builtIn && !a.meta.builtIn) {
return -1;
} else if (a.meta.builtIn && b.meta.builtIn) {
return a.name < b.name ? -1 : 1;
}
// Sort the rest alphabetically by name.
return a.name < b.name ? -1 : 1;
return nameA < nameB ? -1 : 1;
};
return cmpDataSources;