Skip to content

Commit

Permalink
TypeScriptify get_sort.js to get_sort.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
kertal committed Jan 28, 2020
1 parent 708de05 commit 891c3f7
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 120 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ export enum SortDirection {
desc = 'desc',
}

export type EsQuerySortValue = Record<string, SortDirection>;
export interface SortDirectionNumeric {
order: SortDirection;
numeric_type?: 'double' | 'long' | 'date' | 'date_nanos';
}

export type EsQuerySortValue = Record<string, SortDirection | SortDirectionNumeric>;

export interface SearchSourceFields {
type?: string;
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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([]);
});
});

Expand Down
Original file line number Diff line number Diff line change
@@ -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<string, string>;
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());
}
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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) {
Expand All @@ -46,8 +46,8 @@ export function getSortForSearchSource(
order: sortPair[timeFieldName],
numeric_type: 'date_nanos',
},
};
} as EsQuerySortValue;
}
return sortPair;
return sortPair as EsQuerySortValue;
});
}

0 comments on commit 891c3f7

Please sign in to comment.