From 09c58c2ba842213278c34f637d8bdc0f72f9f573 Mon Sep 17 00:00:00 2001 From: Matthias Wilhelm Date: Mon, 27 Jan 2020 15:48:13 +0100 Subject: [PATCH 1/3] Migrate get_sort.js test from mocha to TypeScript --- .../__tests__/doc_table/lib/get_sort.js | 104 ------------------ .../discover/np_ready/angular/discover.js | 11 +- .../angular/doc_table/lib/get_sort.d.ts | 13 ++- .../angular/doc_table/lib/get_sort.js | 4 +- 4 files changed, 17 insertions(+), 115 deletions(-) delete mode 100644 src/legacy/core_plugins/kibana/public/discover/__tests__/doc_table/lib/get_sort.js diff --git a/src/legacy/core_plugins/kibana/public/discover/__tests__/doc_table/lib/get_sort.js b/src/legacy/core_plugins/kibana/public/discover/__tests__/doc_table/lib/get_sort.js deleted file mode 100644 index d5485bca33cf5..0000000000000 --- a/src/legacy/core_plugins/kibana/public/discover/__tests__/doc_table/lib/get_sort.js +++ /dev/null @@ -1,104 +0,0 @@ -/* - * Licensed to Elasticsearch B.V. under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch B.V. licenses this file to you 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 expect from '@kbn/expect'; -import ngMock from 'ng_mock'; - -import { getSort } from '../../../np_ready/angular/doc_table/lib/get_sort'; -import FixturesStubbedLogstashIndexPatternProvider from 'fixtures/stubbed_logstash_index_pattern'; - -let indexPattern; - -describe('docTable', function() { - beforeEach(ngMock.module('kibana')); - - beforeEach( - ngMock.inject(function(Private) { - indexPattern = Private(FixturesStubbedLogstashIndexPatternProvider); - }) - ); - - describe('getSort function', function() { - it('should be a function', function() { - expect(getSort).to.be.a(Function); - }); - - it('should return an array of objects', function() { - expect(getSort([['bytes', 'desc']], indexPattern)).to.eql([{ bytes: 'desc' }]); - - delete indexPattern.timeFieldName; - expect(getSort([['bytes', 'desc']], indexPattern)).to.eql([{ bytes: 'desc' }]); - }); - - it('should passthrough arrays of objects', () => { - expect(getSort([{ bytes: 'desc' }], indexPattern)).to.eql([{ bytes: 'desc' }]); - }); - - it('should return an empty array when passed an unsortable field', function() { - expect(getSort(['non-sortable', 'asc'], indexPattern)).to.eql([]); - expect(getSort(['lol_nope', 'asc'], indexPattern)).to.eql([]); - - delete indexPattern.timeFieldName; - expect(getSort(['non-sortable', 'asc'], indexPattern)).to.eql([]); - }); - - it('should return an empty array ', function() { - expect(getSort([], indexPattern)).to.eql([]); - expect(getSort(['foo'], indexPattern)).to.eql([]); - expect(getSort({ foo: 'bar' }, indexPattern)).to.eql([]); - }); - - it('should return an empty array on non-time patterns', function() { - delete indexPattern.timeFieldName; - - expect(getSort([], indexPattern)).to.eql([]); - expect(getSort(['foo'], indexPattern)).to.eql([]); - expect(getSort({ foo: 'bar' }, indexPattern)).to.eql([]); - }); - }); - - describe('getSort.array function', function() { - it('should have an array method', function() { - expect(getSort.array).to.be.a(Function); - }); - - it('should return an array of arrays for sortable fields', function() { - expect(getSort.array([['bytes', 'desc']], indexPattern)).to.eql([['bytes', 'desc']]); - }); - - it('should return an array of arrays from an array of elasticsearch sort objects', function() { - expect(getSort.array([{ bytes: 'desc' }], indexPattern)).to.eql([['bytes', 'desc']]); - }); - - it('should sort by an empty array when an unsortable field is given', function() { - expect(getSort.array([{ 'non-sortable': 'asc' }], indexPattern)).to.eql([]); - expect(getSort.array([{ lol_nope: 'asc' }], indexPattern)).to.eql([]); - - delete indexPattern.timeFieldName; - expect(getSort.array([{ 'non-sortable': 'asc' }], indexPattern)).to.eql([]); - }); - - it('should return an empty array when passed an empty sort array', () => { - expect(getSort.array([], indexPattern)).to.eql([]); - - delete indexPattern.timeFieldName; - expect(getSort.array([], indexPattern)).to.eql([]); - }); - }); -}); diff --git a/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/discover.js b/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/discover.js index 2383e58a7201b..0bb073decac6e 100644 --- a/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/discover.js +++ b/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/discover.js @@ -27,7 +27,7 @@ import '../components/field_chooser/field_chooser'; // doc table import './doc_table'; -import { getSort } from './doc_table/lib/get_sort'; +import { getSortArray } from './doc_table/lib/get_sort'; import { getSortForSearchSource } from './doc_table/lib/get_sort_for_search_source'; import * as columnActions from './doc_table/actions/columns'; @@ -522,7 +522,7 @@ function discoverController( language: localStorage.get('kibana.userQueryLanguage') || config.get('search:queryLanguage'), }, - sort: getSort.array(savedSearch.sort, $scope.indexPattern), + sort: getSortArray(savedSearch.sort, $scope.indexPattern), columns: savedSearch.columns.length > 0 ? savedSearch.columns : config.get('defaultColumns').slice(), index: $scope.indexPattern.id, @@ -534,7 +534,7 @@ function discoverController( } $state.index = $scope.indexPattern.id; - $state.sort = getSort.array($state.sort, $scope.indexPattern); + $state.sort = getSortArray($state.sort, $scope.indexPattern); $scope.getBucketIntervalToolTipText = () => { return i18n.translate('kbn.discover.bucketIntervalTooltip', { @@ -615,10 +615,7 @@ function discoverController( if (!sort) return; // get the current sort from searchSource as array of arrays - const currentSort = getSort.array( - $scope.searchSource.getField('sort'), - $scope.indexPattern - ); + const currentSort = getSortArray($scope.searchSource.getField('sort'), $scope.indexPattern); // if the searchSource doesn't know, tell it so if (!angular.equals(sort, currentSort)) $scope.fetch(); diff --git a/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/doc_table/lib/get_sort.d.ts b/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/doc_table/lib/get_sort.d.ts index 0bf8a93a88367..5665c5ddde564 100644 --- a/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/doc_table/lib/get_sort.d.ts +++ b/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/doc_table/lib/get_sort.d.ts @@ -20,8 +20,17 @@ import { IIndexPattern } from '../../../../../../../../../plugins/data/public'; import { SortOrder } from '../components/table_header/helpers'; +export type SortInput = + | Record + | SortOrder + | SortOrder[] + | Array> + | string[]; + export function getSort( - sort?: SortOrder[], + sort?: SortInput, indexPattern?: IIndexPattern, - defaultSortOrder?: SortOrder + defaultSortOrder?: string ): any; + +export function getSortArray(sort: SortInput, indexPattern?: IIndexPattern): any; diff --git a/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/doc_table/lib/get_sort.js b/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/doc_table/lib/get_sort.js index ce32fdaeda237..b9896f0f798f2 100644 --- a/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/doc_table/lib/get_sort.js +++ b/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/doc_table/lib/get_sort.js @@ -53,10 +53,10 @@ export function getSort(sort, indexPattern) { return []; } -getSort.array = function(sort, indexPattern) { +export function getSortArray(sort, indexPattern) { return getSort(sort, indexPattern).map(sortPair => _(sortPair) .pairs() .pop() ); -}; +} From 708de0573eabff98369fb83450999ff63abdf740 Mon Sep 17 00:00:00 2001 From: Matthias Wilhelm Date: Mon, 27 Jan 2020 16:14:02 +0100 Subject: [PATCH 2/3] Add jest test --- .../angular/doc_table/lib/get_sort.test.ts | 99 +++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 src/legacy/core_plugins/kibana/public/discover/np_ready/angular/doc_table/lib/get_sort.test.ts diff --git a/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/doc_table/lib/get_sort.test.ts b/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/doc_table/lib/get_sort.test.ts new file mode 100644 index 0000000000000..87567ee08aad9 --- /dev/null +++ b/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/doc_table/lib/get_sort.test.ts @@ -0,0 +1,99 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you 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 { getSort, getSortArray } from './get_sort'; +// @ts-ignore +import FixturesStubbedLogstashIndexPatternProvider from 'fixtures/stubbed_logstash_index_pattern'; +import { IIndexPattern } from '../../../../kibana_services'; + +describe('docTable', function() { + let indexPattern: IIndexPattern; + + beforeEach(() => { + indexPattern = FixturesStubbedLogstashIndexPatternProvider() as IIndexPattern; + }); + + describe('getSort function', function() { + test('should be a function', function() { + expect(typeof getSort === 'function').toBeTruthy(); + }); + + test('should return an array of objects', function() { + expect(getSort([['bytes', 'desc']], indexPattern)).toEqual([{ bytes: 'desc' }]); + + delete indexPattern.timeFieldName; + expect(getSort([['bytes', 'desc']], indexPattern)).toEqual([{ bytes: 'desc' }]); + }); + + test('should passthrough arrays of objects', () => { + expect(getSort([{ bytes: 'desc' }], indexPattern)).toEqual([{ bytes: 'desc' }]); + }); + + test('should return an empty array when passed an unsortable field', function() { + expect(getSort(['non-sortable', 'asc'], indexPattern)).toEqual([]); + expect(getSort(['lol_nope', 'asc'], indexPattern)).toEqual([]); + + delete indexPattern.timeFieldName; + expect(getSort(['non-sortable', 'asc'], indexPattern)).toEqual([]); + }); + + test('should return an empty array ', function() { + expect(getSort([], indexPattern)).toEqual([]); + expect(getSort(['foo'], indexPattern)).toEqual([]); + expect(getSort({ foo: 'bar' }, indexPattern)).toEqual([]); + }); + + test('should return an empty array on non-time patterns', function() { + delete indexPattern.timeFieldName; + + expect(getSort([], indexPattern)).toEqual([]); + expect(getSort(['foo'], indexPattern)).toEqual([]); + expect(getSort({ foo: 'bar' }, indexPattern)).toEqual([]); + }); + }); + + describe('getSortArray function', function() { + test('should have an array method', function() { + expect(getSortArray).toBeInstanceOf(Function); + }); + + test('should return an array of arrays for sortable fields', function() { + expect(getSortArray([['bytes', 'desc']], indexPattern)).toEqual([['bytes', 'desc']]); + }); + + test('should return an array of arrays from an array of elasticsearch sort objects', function() { + expect(getSortArray([{ bytes: 'desc' }], indexPattern)).toEqual([['bytes', 'desc']]); + }); + + test('should sort by an empty array when an unsortable field is given', function() { + expect(getSortArray([{ 'non-sortable': 'asc' }], indexPattern)).toEqual([]); + expect(getSortArray([{ lol_nope: 'asc' }], indexPattern)).toEqual([]); + + delete indexPattern.timeFieldName; + expect(getSortArray([{ 'non-sortable': 'asc' }], indexPattern)).toEqual([]); + }); + + test('should return an empty array when passed an empty sort array', () => { + expect(getSortArray([], indexPattern)).toEqual([]); + + delete indexPattern.timeFieldName; + expect(getSortArray([], indexPattern)).toEqual([]); + }); + }); +}); From 891c3f751c6074788bf0328b996151eefd2738ee Mon Sep 17 00:00:00 2001 From: Matthias Wilhelm Date: Tue, 28 Jan 2020 10:05:48 +0100 Subject: [PATCH 3/3] TypeScriptify get_sort.js to get_sort.ts --- .../data/public/search/search_source/types.ts | 7 +- .../angular/doc_table/lib/get_sort.d.ts | 36 ---------- .../angular/doc_table/lib/get_sort.js | 62 ---------------- .../angular/doc_table/lib/get_sort.test.ts | 24 +++---- .../angular/doc_table/lib/get_sort.ts | 71 +++++++++++++++++++ .../lib/get_sort_for_search_source.ts | 10 +-- 6 files changed, 90 insertions(+), 120 deletions(-) delete mode 100644 src/legacy/core_plugins/kibana/public/discover/np_ready/angular/doc_table/lib/get_sort.d.ts delete mode 100644 src/legacy/core_plugins/kibana/public/discover/np_ready/angular/doc_table/lib/get_sort.js create mode 100644 src/legacy/core_plugins/kibana/public/discover/np_ready/angular/doc_table/lib/get_sort.ts diff --git a/src/legacy/core_plugins/data/public/search/search_source/types.ts b/src/legacy/core_plugins/data/public/search/search_source/types.ts index 9c5b57519d75f..416b811c95ebf 100644 --- a/src/legacy/core_plugins/data/public/search/search_source/types.ts +++ b/src/legacy/core_plugins/data/public/search/search_source/types.ts @@ -26,7 +26,12 @@ export enum SortDirection { desc = 'desc', } -export type EsQuerySortValue = Record; +export interface SortDirectionNumeric { + order: SortDirection; + numeric_type?: 'double' | 'long' | 'date' | 'date_nanos'; +} + +export type EsQuerySortValue = Record; export interface SearchSourceFields { type?: string; diff --git a/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/doc_table/lib/get_sort.d.ts b/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/doc_table/lib/get_sort.d.ts deleted file mode 100644 index 5665c5ddde564..0000000000000 --- a/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/doc_table/lib/get_sort.d.ts +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Licensed to Elasticsearch B.V. under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch B.V. licenses this file to you 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 { IIndexPattern } from '../../../../../../../../../plugins/data/public'; -import { SortOrder } from '../components/table_header/helpers'; - -export type SortInput = - | Record - | SortOrder - | SortOrder[] - | Array> - | string[]; - -export function getSort( - sort?: SortInput, - indexPattern?: IIndexPattern, - defaultSortOrder?: string -): any; - -export function getSortArray(sort: SortInput, indexPattern?: IIndexPattern): any; diff --git a/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/doc_table/lib/get_sort.js b/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/doc_table/lib/get_sort.js deleted file mode 100644 index b9896f0f798f2..0000000000000 --- a/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/doc_table/lib/get_sort.js +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Licensed to Elasticsearch B.V. under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch B.V. licenses this file to you 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 _ from 'lodash'; - -export function isSortable(field, indexPattern) { - return indexPattern.fields.getByName(field) && indexPattern.fields.getByName(field).sortable; -} - -function createSortObject(sortPair, indexPattern) { - if (Array.isArray(sortPair) && sortPair.length === 2 && isSortable(sortPair[0], indexPattern)) { - const [field, direction] = sortPair; - return { [field]: direction }; - } else if (_.isPlainObject(sortPair) && isSortable(Object.keys(sortPair)[0], indexPattern)) { - return sortPair; - } else { - return undefined; - } -} - -/** - * 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 {object} a sort object suitable for returning to elasticsearch - */ -export function getSort(sort, indexPattern) { - let sortObjects; - if (Array.isArray(sort)) { - sortObjects = _.compact(sort.map(sortPair => createSortObject(sortPair, indexPattern))); - } - - if (!_.isEmpty(sortObjects)) { - return sortObjects; - } - return []; -} - -export function getSortArray(sort, indexPattern) { - return getSort(sort, indexPattern).map(sortPair => - _(sortPair) - .pairs() - .pop() - ); -} diff --git a/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/doc_table/lib/get_sort.test.ts b/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/doc_table/lib/get_sort.test.ts index 87567ee08aad9..c9cbad245f5e4 100644 --- a/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/doc_table/lib/get_sort.test.ts +++ b/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/doc_table/lib/get_sort.test.ts @@ -20,13 +20,13 @@ import { getSort, getSortArray } from './get_sort'; // @ts-ignore import FixturesStubbedLogstashIndexPatternProvider from 'fixtures/stubbed_logstash_index_pattern'; -import { IIndexPattern } from '../../../../kibana_services'; +import { IndexPattern } from '../../../../kibana_services'; describe('docTable', function() { - let indexPattern: IIndexPattern; + let indexPattern: IndexPattern; beforeEach(() => { - indexPattern = FixturesStubbedLogstashIndexPatternProvider() as IIndexPattern; + indexPattern = FixturesStubbedLogstashIndexPatternProvider() as IndexPattern; }); describe('getSort function', function() { @@ -46,25 +46,17 @@ describe('docTable', function() { }); test('should return an empty array when passed an unsortable field', function() { - expect(getSort(['non-sortable', 'asc'], indexPattern)).toEqual([]); - expect(getSort(['lol_nope', 'asc'], indexPattern)).toEqual([]); + expect(getSort([['non-sortable', 'asc']], indexPattern)).toEqual([]); + expect(getSort([['lol_nope', 'asc']], indexPattern)).toEqual([]); delete indexPattern.timeFieldName; - expect(getSort(['non-sortable', 'asc'], indexPattern)).toEqual([]); + expect(getSort([['non-sortable', 'asc']], indexPattern)).toEqual([]); }); test('should return an empty array ', function() { expect(getSort([], indexPattern)).toEqual([]); - expect(getSort(['foo'], indexPattern)).toEqual([]); - expect(getSort({ foo: 'bar' }, indexPattern)).toEqual([]); - }); - - test('should return an empty array on non-time patterns', function() { - delete indexPattern.timeFieldName; - - expect(getSort([], indexPattern)).toEqual([]); - expect(getSort(['foo'], indexPattern)).toEqual([]); - expect(getSort({ foo: 'bar' }, indexPattern)).toEqual([]); + expect(getSort([['foo', 'bar']], indexPattern)).toEqual([]); + expect(getSort([{ foo: 'bar' }], indexPattern)).toEqual([]); }); }); diff --git a/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/doc_table/lib/get_sort.ts b/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/doc_table/lib/get_sort.ts new file mode 100644 index 0000000000000..a8dbaa50e5aa8 --- /dev/null +++ b/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/doc_table/lib/get_sort.ts @@ -0,0 +1,71 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you 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 _ from 'lodash'; +import { IndexPattern } from '../../../../../../../../../plugins/data/public'; + +export type SortPairObj = Record; +export type SortPairArr = [string, string]; +export type SortPair = SortPairArr | SortPairObj; +export type SortInput = SortPair | SortPair[]; + +export function isSortable(fieldName: string, indexPattern: IndexPattern) { + const field = indexPattern.getFieldByName(fieldName); + return field && field.sortable; +} + +function createSortObject( + sortPair: SortInput, + indexPattern: IndexPattern +): SortPairObj | undefined { + if ( + Array.isArray(sortPair) && + sortPair.length === 2 && + isSortable(String(sortPair[0]), indexPattern) + ) { + const [field, direction] = sortPair as SortPairArr; + return { [field]: direction }; + } else if (_.isPlainObject(sortPair) && isSortable(Object.keys(sortPair)[0], indexPattern)) { + return sortPair as SortPairObj; + } +} + +/** + * 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[] { + if (Array.isArray(sort)) { + return sort + .map((sortPair: SortPair) => createSortObject(sortPair, indexPattern)) + .filter(sortPairObj => typeof sortPairObj === 'object') as SortPairObj[]; + } + return []; +} + +/** + * compared to getSort it doesn't return an array of objects, it returns an array of arrays + * [[fieldToSort: directionToSort]] + */ +export function getSortArray(sort: SortPair[], indexPattern: IndexPattern) { + return getSort(sort, indexPattern).map(sortPair => Object.entries(sortPair).pop()); +} diff --git a/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/doc_table/lib/get_sort_for_search_source.ts b/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/doc_table/lib/get_sort_for_search_source.ts index 62a44d30adfd5..6721f7a03584c 100644 --- a/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/doc_table/lib/get_sort_for_search_source.ts +++ b/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/doc_table/lib/get_sort_for_search_source.ts @@ -16,7 +16,7 @@ * specific language governing permissions and limitations * under the License. */ -import { IndexPattern } from '../../../../kibana_services'; +import { EsQuerySortValue, IndexPattern } from '../../../../kibana_services'; import { SortOrder } from '../components/table_header/helpers'; import { getSort } from './get_sort'; import { getDefaultSort } from './get_default_sort'; @@ -31,8 +31,8 @@ import { getDefaultSort } from './get_default_sort'; export function getSortForSearchSource( sort?: SortOrder[], indexPattern?: IndexPattern, - defaultDirection: 'asc' | 'desc' = 'desc' -) { + defaultDirection: string = 'desc' +): EsQuerySortValue[] { if (!sort || !indexPattern) { return []; } else if (Array.isArray(sort) && sort.length === 0) { @@ -46,8 +46,8 @@ export function getSortForSearchSource( order: sortPair[timeFieldName], numeric_type: 'date_nanos', }, - }; + } as EsQuerySortValue; } - return sortPair; + return sortPair as EsQuerySortValue; }); }