Skip to content

Commit

Permalink
[Discover] Convert legacy sort to be compatible with multi sort (#76986
Browse files Browse the repository at this point in the history
…) (#77334)
  • Loading branch information
kertal committed Sep 14, 2020
1 parent 02dac58 commit 2613af4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ describe('docTable', function () {
expect(getSort([['foo', 'bar']], indexPattern)).toEqual([]);
expect(getSort([{ foo: 'bar' }], indexPattern)).toEqual([]);
});

test('should convert a legacy sort to an array of objects', function () {
expect(getSort(['foo', 'desc'], indexPattern)).toEqual([{ foo: 'desc' }]);
expect(getSort(['foo', 'asc'], indexPattern)).toEqual([{ foo: 'asc' }]);
});
});

describe('getSortArray function', function () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,25 @@ function createSortObject(
}
}

export function isLegacySort(sort: SortPair[] | SortPair): sort is SortPair {
return (
sort.length === 2 && typeof sort[0] === 'string' && (sort[1] === 'desc' || sort[1] === 'asc')
);
}

/**
* Take a sorting array and make it into an object
* @param {array} sort two dimensional array [[fieldToSort, directionToSort]]
* or an array of objects [{fieldToSort: directionToSort}]
* @param {object} indexPattern used for determining default sort
* @returns Array<{object}> an array of sort objects
*/
export function getSort(sort: SortPair[], indexPattern: IndexPattern): SortPairObj[] {
export function getSort(sort: SortPair[] | SortPair, indexPattern: IndexPattern): SortPairObj[] {
if (Array.isArray(sort)) {
if (isLegacySort(sort)) {
// To stay compatible with legacy sort, which just supported a single sort field
return [{ [sort[0]]: sort[1] }];
}
return sort
.map((sortPair: SortPair) => createSortObject(sortPair, indexPattern))
.filter((sortPairObj) => typeof sortPairObj === 'object') as SortPairObj[];
Expand Down

0 comments on commit 2613af4

Please sign in to comment.