simplify changes
This commit is contained in:
parent
6428455873
commit
1789afcdc9
|
@ -163,22 +163,4 @@ describe('FieldFormatsRegistry', () => {
|
|||
expect(params).toHaveProperty('parsedUrl');
|
||||
});
|
||||
});
|
||||
|
||||
describe('setCustomParams', () => {
|
||||
test('should provide custom setting to formatters: timezone', () => {
|
||||
fieldFormatsRegistry = new FieldFormatsRegistry();
|
||||
fieldFormatsRegistry.init(
|
||||
getConfig,
|
||||
{
|
||||
parsedUrl: {
|
||||
origin: '',
|
||||
pathname: '',
|
||||
basePath: '',
|
||||
},
|
||||
},
|
||||
[]
|
||||
);
|
||||
fieldFormatsRegistry.setCustomParams({ timezone: 'America/New_York' });
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
@ -40,7 +40,6 @@ export class FieldFormatsRegistry {
|
|||
protected defaultMap: Record<string, FieldFormatConfig> = {};
|
||||
protected metaParamsOptions: Record<string, any> = {};
|
||||
protected getConfig?: FieldFormatsGetConfigFn;
|
||||
protected customParams: Record<string, any> = {};
|
||||
// overriden on the public contract
|
||||
public deserialize: (mapping: SerializedFieldFormat) => IFieldFormat = () => {
|
||||
return new (FieldFormat.from(identity))();
|
||||
|
@ -58,13 +57,6 @@ export class FieldFormatsRegistry {
|
|||
this.metaParamsOptions = metaParamsOptions;
|
||||
}
|
||||
|
||||
/*
|
||||
* Allow use-case specific params that are reflected in getInstance / getDefaultInstancePlain
|
||||
*/
|
||||
setCustomParams(params: Record<string, any>) {
|
||||
this.customParams = params;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the id of the default type for this field type
|
||||
* using the format:defaultTypeMap config map
|
||||
|
@ -165,11 +157,7 @@ export class FieldFormatsRegistry {
|
|||
* @return {FieldFormat}
|
||||
*/
|
||||
getInstance = memoize(
|
||||
(formatId: FieldFormatId, instanceParams: Record<string, any> = {}): FieldFormat => {
|
||||
const params = {
|
||||
...instanceParams,
|
||||
...this.customParams,
|
||||
};
|
||||
(formatId: FieldFormatId, params: Record<string, any> = {}): FieldFormat => {
|
||||
const ConcreteFieldFormat = this.getType(formatId);
|
||||
|
||||
if (!ConcreteFieldFormat) {
|
||||
|
@ -192,14 +180,18 @@ export class FieldFormatsRegistry {
|
|||
* @param {ES_FIELD_TYPES[]} esTypes
|
||||
* @return {FieldFormat}
|
||||
*/
|
||||
getDefaultInstancePlain(fieldType: KBN_FIELD_TYPES, esTypes?: ES_FIELD_TYPES[]): FieldFormat {
|
||||
getDefaultInstancePlain(
|
||||
fieldType: KBN_FIELD_TYPES,
|
||||
esTypes?: ES_FIELD_TYPES[],
|
||||
params: Record<string, any> = {}
|
||||
): FieldFormat {
|
||||
const conf = this.getDefaultConfig(fieldType, esTypes);
|
||||
const defaultParams = {
|
||||
const instanceParams = {
|
||||
...conf.params,
|
||||
...this.customParams,
|
||||
...params,
|
||||
};
|
||||
|
||||
return this.getInstance(conf.id, defaultParams);
|
||||
return this.getInstance(conf.id, instanceParams);
|
||||
}
|
||||
/**
|
||||
* Returns a cache key built by the given variables for caching in memoized
|
||||
|
|
|
@ -1,129 +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 moment from 'moment-timezone';
|
||||
import { DateNanosFormat, analysePatternForFract, formatWithNanos } from './date_nanos_server';
|
||||
|
||||
describe('Date Nanos Format', () => {
|
||||
let convert: Function;
|
||||
let mockConfig: Record<string, any>;
|
||||
|
||||
beforeEach(() => {
|
||||
mockConfig = {};
|
||||
mockConfig.dateNanosFormat = 'MMMM Do YYYY, HH:mm:ss.SSSSSSSSS';
|
||||
mockConfig['dateFormat:tz'] = 'Browser';
|
||||
|
||||
const getConfig = (key: string) => mockConfig[key];
|
||||
const date = new DateNanosFormat({}, getConfig);
|
||||
|
||||
convert = date.convert.bind(date);
|
||||
});
|
||||
|
||||
test('should inject fractional seconds into formatted timestamp', () => {
|
||||
[
|
||||
{
|
||||
input: '2019-05-20T14:04:56.357001234Z',
|
||||
pattern: 'MMM D, YYYY @ HH:mm:ss.SSSSSSSSS',
|
||||
expected: 'May 20, 2019 @ 14:04:56.357001234',
|
||||
},
|
||||
{
|
||||
input: '2019-05-05T14:04:56.357111234Z',
|
||||
pattern: 'MMM D, YYYY @ HH:mm:ss.SSSSSSSSS',
|
||||
expected: 'May 5, 2019 @ 14:04:56.357111234',
|
||||
},
|
||||
{
|
||||
input: '2019-05-05T14:04:56.357Z',
|
||||
pattern: 'MMM D, YYYY @ HH:mm:ss.SSSSSSSSS',
|
||||
expected: 'May 5, 2019 @ 14:04:56.357000000',
|
||||
},
|
||||
{
|
||||
input: '2019-05-05T14:04:56Z',
|
||||
pattern: 'MMM D, YYYY @ HH:mm:ss.SSSSSSSSS',
|
||||
expected: 'May 5, 2019 @ 14:04:56.000000000',
|
||||
},
|
||||
{
|
||||
input: '2019-05-05T14:04:56.201900001Z',
|
||||
pattern: 'MMM D, YYYY @ HH:mm:ss SSSS',
|
||||
expected: 'May 5, 2019 @ 14:04:56 2019',
|
||||
},
|
||||
{
|
||||
input: '2019-05-05T14:04:56.201900001Z',
|
||||
pattern: 'SSSSSSSSS',
|
||||
expected: '201900001',
|
||||
},
|
||||
].forEach((fixture) => {
|
||||
const fracPattern = analysePatternForFract(fixture.pattern);
|
||||
const momentDate = moment(fixture.input).utc();
|
||||
const value = formatWithNanos(momentDate, fixture.input, fracPattern);
|
||||
expect(value).toBe(fixture.expected);
|
||||
});
|
||||
});
|
||||
|
||||
test('decoding an undefined or null date should return an empty string', () => {
|
||||
expect(convert(null)).toBe('-');
|
||||
expect(convert(undefined)).toBe('-');
|
||||
});
|
||||
|
||||
test('should clear the memoization cache after changing the date', () => {
|
||||
function setDefaultTimezone() {
|
||||
moment.tz.setDefault(mockConfig['dateFormat:tz']);
|
||||
}
|
||||
|
||||
const dateTime = '2019-05-05T14:04:56.201900001Z';
|
||||
|
||||
mockConfig['dateFormat:tz'] = 'America/Chicago';
|
||||
setDefaultTimezone();
|
||||
const chicagoTime = convert(dateTime);
|
||||
|
||||
mockConfig['dateFormat:tz'] = 'America/Phoenix';
|
||||
setDefaultTimezone();
|
||||
const phoenixTime = convert(dateTime);
|
||||
|
||||
expect(chicagoTime).not.toBe(phoenixTime);
|
||||
});
|
||||
|
||||
test('should return the value itself when it cannot successfully be formatted', () => {
|
||||
const dateMath = 'now+1M/d';
|
||||
expect(convert(dateMath)).toBe(dateMath);
|
||||
});
|
||||
});
|
||||
|
||||
describe('analysePatternForFract', () => {
|
||||
test('analysePatternForFract using timestamp format containing fractional seconds', () => {
|
||||
expect(analysePatternForFract('MMM, YYYY @ HH:mm:ss.SSS')).toMatchInlineSnapshot(`
|
||||
Object {
|
||||
"length": 3,
|
||||
"pattern": "MMM, YYYY @ HH:mm:ss.SSS",
|
||||
"patternEscaped": "MMM, YYYY @ HH:mm:ss.[SSS]",
|
||||
"patternNanos": "SSS",
|
||||
}
|
||||
`);
|
||||
});
|
||||
|
||||
test('analysePatternForFract using timestamp format without fractional seconds', () => {
|
||||
expect(analysePatternForFract('MMM, YYYY @ HH:mm:ss')).toMatchInlineSnapshot(`
|
||||
Object {
|
||||
"length": 0,
|
||||
"pattern": "MMM, YYYY @ HH:mm:ss",
|
||||
"patternEscaped": "",
|
||||
"patternNanos": "",
|
||||
}
|
||||
`);
|
||||
});
|
||||
});
|
|
@ -17,10 +17,9 @@
|
|||
* under the License.
|
||||
*/
|
||||
|
||||
import { coreMock } from '../../../../core/server/mocks';
|
||||
import { FIELD_FORMAT_IDS } from '../../common';
|
||||
import { DateFormat } from './converters/date_server';
|
||||
import { FieldFormatsService } from './field_formats_service';
|
||||
import { DateFormat } from './converters/date_server';
|
||||
import { coreMock } from '../../../../core/server/mocks';
|
||||
|
||||
describe('FieldFormatService', () => {
|
||||
test('DateFormat is server version', async () => {
|
||||
|
@ -33,22 +32,3 @@ describe('FieldFormatService', () => {
|
|||
expect(DateFormatFromRegsitry).toEqual(DateFormat);
|
||||
});
|
||||
});
|
||||
|
||||
describe('DateFormat with custom timezone', () => {
|
||||
test('should provide custom setting to formatters: timezone', async () => {
|
||||
const service = new FieldFormatsService();
|
||||
const fieldFormatsService = await service.start();
|
||||
const uiSettings = coreMock.createStart().uiSettings.asScopedToClient({} as any);
|
||||
const fieldFormatsRegistry = await fieldFormatsService.fieldFormatServiceFactory(uiSettings);
|
||||
|
||||
fieldFormatsRegistry.setCustomParams({ timezone: 'America/Phoenix' }); // set the timezone into the registry
|
||||
|
||||
const fieldFormats = fieldFormatsRegistry.getByFieldType(FIELD_FORMAT_IDS.DATE as any);
|
||||
const DateFormatInstance = fieldFormats.find((F) => F.id === 'date');
|
||||
expect(DateFormatInstance).toBeDefined();
|
||||
if (DateFormatInstance) {
|
||||
const formatter = new DateFormatInstance();
|
||||
// how to call the formatter?
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue