Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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