From a8c985de6073dcef6a7e8dfe8ef81c302e6519be Mon Sep 17 00:00:00 2001 From: ryan Date: Tue, 12 Mar 2019 09:09:33 -0700 Subject: [PATCH] move sort to table processing --- .../grafana-ui/src/components/Table/Table.tsx | 2 +- .../grafana-ui/src/utils/processTableData.ts | 28 +++++++++++++++++-- .../grafana-ui/src/utils/processTimeSeries.ts | 23 +-------------- 3 files changed, 28 insertions(+), 25 deletions(-) diff --git a/packages/grafana-ui/src/components/Table/Table.tsx b/packages/grafana-ui/src/components/Table/Table.tsx index 7fb1a286e96..a551d6d221d 100644 --- a/packages/grafana-ui/src/components/Table/Table.tsx +++ b/packages/grafana-ui/src/components/Table/Table.tsx @@ -11,7 +11,7 @@ import { } from 'react-virtualized'; import { Themeable } from '../../types/theme'; -import { sortTableData } from '../../utils/processTimeSeries'; +import { sortTableData } from '../../utils/processTableData'; import { TableData, InterpolateFunction } from '@grafana/ui'; import { diff --git a/packages/grafana-ui/src/utils/processTableData.ts b/packages/grafana-ui/src/utils/processTableData.ts index b9fd0519565..d65e42827e0 100644 --- a/packages/grafana-ui/src/utils/processTableData.ts +++ b/packages/grafana-ui/src/utils/processTableData.ts @@ -1,7 +1,10 @@ -import { TableData, Column } from '../types/index'; - +// Libraries +import isNumber from 'lodash/isNumber'; import Papa, { ParseError, ParseMeta } from 'papaparse'; +// Types +import { TableData, Column } from '../types'; + // Subset of all parse options export interface TableParseOptions { headerIsFirstLine?: boolean; // Not a papa-parse option @@ -131,3 +134,24 @@ export function parseCSV(text: string, options?: TableParseOptions, details?: Ta columnMap: {}, }); } + +export function sortTableData(data: TableData, sortIndex?: number, reverse = false): TableData { + if (isNumber(sortIndex)) { + const copy = { + ...data, + rows: [...data.rows].sort((a, b) => { + a = a[sortIndex]; + b = b[sortIndex]; + // Sort null or undefined separately from comparable values + return +(a == null) - +(b == null) || +(a > b) || -(a < b); + }), + }; + + if (reverse) { + copy.rows.reverse(); + } + + return copy; + } + return data; +} diff --git a/packages/grafana-ui/src/utils/processTimeSeries.ts b/packages/grafana-ui/src/utils/processTimeSeries.ts index 3f2fded1da6..f5e9f96efba 100644 --- a/packages/grafana-ui/src/utils/processTimeSeries.ts +++ b/packages/grafana-ui/src/utils/processTimeSeries.ts @@ -4,7 +4,7 @@ import isNumber from 'lodash/isNumber'; import { colors } from './colors'; // Types -import { TimeSeries, TableData, TimeSeriesVMs, NullValueMode, TimeSeriesValue } from '../types'; +import { TimeSeries, TimeSeriesVMs, NullValueMode, TimeSeriesValue } from '../types'; interface Options { timeSeries: TimeSeries[]; @@ -173,24 +173,3 @@ export function processTimeSeries({ timeSeries, nullValueMode }: Options): TimeS return vmSeries; } - -export function sortTableData(data: TableData, sortIndex?: number, reverse = false): TableData { - if (isNumber(sortIndex)) { - const copy = { - ...data, - rows: [...data.rows].sort((a, b) => { - a = a[sortIndex]; - b = b[sortIndex]; - // Sort null or undefined separately from comparable values - return +(a == null) - +(b == null) || +(a > b) || -(a < b); - }), - }; - - if (reverse) { - copy.rows.reverse(); - } - - return copy; - } - return data; -}