mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 01:38:56 -04:00
* add relative date field formatter * add unit test for relative_date field formatter
This commit is contained in:
parent
45c7cc0065
commit
68746400af
4 changed files with 55 additions and 1 deletions
|
@ -0,0 +1,26 @@
|
|||
import expect from 'expect.js';
|
||||
import moment from 'moment-timezone';
|
||||
import { RelativeDateFormat } from '../relative_date';
|
||||
|
||||
describe('Relative Date Format', function () {
|
||||
let convert;
|
||||
|
||||
beforeEach(function () {
|
||||
const relativeDate = new RelativeDateFormat({});
|
||||
convert = relativeDate.convert.bind(relativeDate);
|
||||
});
|
||||
|
||||
it('decoding an undefined or null date should return a "-"', function () {
|
||||
expect(convert(null)).to.be('-');
|
||||
expect(convert(undefined)).to.be('-');
|
||||
});
|
||||
|
||||
it('decoding invalid date should echo invalid value', function () {
|
||||
expect(convert('not a valid date')).to.be('not a valid date');
|
||||
});
|
||||
|
||||
it('should parse date values', function () {
|
||||
const val = '2017-08-13T20:24:09.904Z';
|
||||
expect(convert(val)).to.be(moment(val).fromNow());
|
||||
});
|
||||
});
|
|
@ -0,0 +1,25 @@
|
|||
import moment from 'moment';
|
||||
import { FieldFormat } from '../../../../../ui/field_formats/field_format';
|
||||
|
||||
export class RelativeDateFormat extends FieldFormat {
|
||||
constructor(params) {
|
||||
super(params);
|
||||
}
|
||||
|
||||
_convert(val) {
|
||||
if (val === null || val === undefined) {
|
||||
return '-';
|
||||
}
|
||||
|
||||
const date = moment(val);
|
||||
if (date.isValid()) {
|
||||
return date.fromNow();
|
||||
} else {
|
||||
return val;
|
||||
}
|
||||
}
|
||||
|
||||
static id = 'relative_date';
|
||||
static title = 'Relative Date';
|
||||
static fieldType = 'date';
|
||||
}
|
|
@ -20,7 +20,8 @@ const formatIds = [
|
|||
'url',
|
||||
'_source',
|
||||
'truncate',
|
||||
'boolean'
|
||||
'boolean',
|
||||
'relative_date'
|
||||
];
|
||||
|
||||
// eslint-disable-next-line kibana-custom/no-default-export
|
||||
|
|
|
@ -2,6 +2,7 @@ import { RegistryFieldFormatsProvider } from 'ui/registry/field_formats';
|
|||
import { UrlFormat } from '../../common/field_formats/types/url';
|
||||
import { BytesFormat } from '../../common/field_formats/types/bytes';
|
||||
import { DateFormat } from '../../common/field_formats/types/date';
|
||||
import { RelativeDateFormat } from '../../common/field_formats/types/relative_date';
|
||||
import { DurationFormat } from '../../common/field_formats/types/duration';
|
||||
import { IpFormat } from '../../common/field_formats/types/ip';
|
||||
import { NumberFormat } from '../../common/field_formats/types/number';
|
||||
|
@ -15,6 +16,7 @@ import { BoolFormat } from '../../common/field_formats/types/boolean';
|
|||
RegistryFieldFormatsProvider.register(() => UrlFormat);
|
||||
RegistryFieldFormatsProvider.register(() => BytesFormat);
|
||||
RegistryFieldFormatsProvider.register(() => DateFormat);
|
||||
RegistryFieldFormatsProvider.register(() => RelativeDateFormat);
|
||||
RegistryFieldFormatsProvider.register(() => DurationFormat);
|
||||
RegistryFieldFormatsProvider.register(() => IpFormat);
|
||||
RegistryFieldFormatsProvider.register(() => NumberFormat);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue