Skip to content

Commit

Permalink
map range converter
Browse files Browse the repository at this point in the history
  • Loading branch information
Liza K committed Oct 15, 2019
1 parent 1f2311d commit 15c3f38
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
4 changes: 3 additions & 1 deletion packages/kbn-es-query/src/filters/lib/meta_filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ export interface FilterState {
store: FilterStateStore;
}

type FilterFormatterFunction = (value: any) => string;
export interface FilterValueFormatter {
convert: (value: any) => string;
convert: FilterFormatterFunction;
getConverterFor: (type: string) => FilterFormatterFunction;
}

export interface FilterMeta {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
PhrasesFilter,
RangeFilter,
} from '@kbn/es-query';
import { omit } from 'lodash';
import { omit, get } from 'lodash';
import { Ipv4Address } from '../../../../../../../../plugins/kibana_utils/public';
import { Field, IndexPattern, isFilterable } from '../../../../index_patterns';
import { FILTER_OPERATORS, Operator } from './filter_operators';
Expand All @@ -45,15 +45,19 @@ export function getIndexPatternFromFilter(

function getValueFormatter(indexPattern?: IndexPattern, key?: string) {
if (!indexPattern || !key) return;
const field = indexPattern.fields.getByName(key);
return field && field.format;
let format = get(indexPattern, ['fields', 'byName', key, 'format']);
if (!format && indexPattern.fields.getByName) {
// TODO: Why is indexPatterns sometimes a map and sometimes an array?
format = (indexPattern.fields.getByName(key) as Field).format;
}
return format;
}

export function getDisplayValueFromFilter(filter: Filter, indexPatterns: IndexPattern[]): string {
const indexPattern = getIndexPatternFromFilter(filter, indexPatterns);
const valueFormatter: any = getValueFormatter(indexPattern, filter.meta.key);

if (typeof filter.meta.value === 'function') {
const valueFormatter: any = getValueFormatter(indexPattern, filter.meta.key);
return filter.meta.value(valueFormatter);
} else {
return filter.meta.value || '';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ const getFormattedValueFn = (left: any, right: any) => {
return (formatter?: FilterValueFormatter) => {
let displayValue = `${left} to ${right}`;
if (formatter) {
displayValue = `${formatter.convert(left)} to ${formatter.convert(right)}`;
const convert = formatter.getConverterFor('text');
displayValue = `${convert(left)} to ${convert(right)}`;
}
return displayValue;
};
Expand Down

0 comments on commit 15c3f38

Please sign in to comment.