Skip to content

Commit

Permalink
fix PR commetns
Browse files Browse the repository at this point in the history
  • Loading branch information
alexwizp committed Dec 23, 2019
1 parent acbba4a commit 3f07aba
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import { memoize, noop } from 'lodash';
import moment from 'moment-timezone';
import { KBN_FIELD_TYPES } from '../../kbn_field_types/types';
import { FieldFormat } from '../field_format';
import { FieldFormat, IFieldFormatMetaParams } from '../field_format';
import { TextContextTypeConvert, FIELD_FORMAT_IDS } from '../types';

export class DateFormat extends FieldFormat {
Expand All @@ -32,7 +32,7 @@ export class DateFormat extends FieldFormat {
private memoizedPattern: string = '';
private timeZone: string = '';

constructor(params: Record<string, any>, getConfig: Function) {
constructor(params: IFieldFormatMetaParams, getConfig: Function) {
super(params, getConfig);

this.memoizedConverter = memoize((val: any) => {
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/data/common/field_formats/converters/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { i18n } from '@kbn/i18n';
import { escape, memoize } from 'lodash';
import { getHighlightHtml } from '../utils';
import { KBN_FIELD_TYPES } from '../../kbn_field_types/types';
import { FieldFormat } from '../field_format';
import { FieldFormat, IFieldFormatMetaParams } from '../field_format';
import { TextContextTypeConvert, HtmlContextTypeConvert, FIELD_FORMAT_IDS } from '../types';

const templateMatchRE = /{{([\s\S]+?)}}/g;
Expand Down Expand Up @@ -64,7 +64,7 @@ export class UrlFormat extends FieldFormat {
];
static urlTypes = URL_TYPES;

constructor(params: Record<string, any>) {
constructor(params: IFieldFormatMetaParams) {
super(params);
this.compileTemplate = memoize(this.compileTemplate);
}
Expand Down
11 changes: 10 additions & 1 deletion src/plugins/data/common/field_formats/field_format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ import { HtmlContextTypeConvert, TextContextTypeConvert } from './types';

const DEFAULT_CONTEXT_TYPE = TEXT_CONTEXT_TYPE;

export interface IFieldFormatMetaParams {
[key: string]: any;
parsedUrl?: {
origin: string;
pathname?: string;
basePath?: string;
};
}

export abstract class FieldFormat {
/**
* @property {string} - Field Format Id
Expand Down Expand Up @@ -90,7 +99,7 @@ export abstract class FieldFormat {
protected readonly _params: any;
protected getConfig: Function | undefined;

constructor(_params: Record<string, any> = {}, getConfig?: Function) {
constructor(_params: IFieldFormatMetaParams = {}, getConfig?: Function) {
this._params = _params;

if (getConfig) {
Expand Down
7 changes: 6 additions & 1 deletion src/plugins/data/common/field_formats/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@
*/

export { HTML_CONTEXT_TYPE, TEXT_CONTEXT_TYPE } from './content_types';
export { FieldFormat, IFieldFormatType, IFieldFormatId } from './field_format';
export {
FieldFormat,
IFieldFormatType,
IFieldFormatId,
IFieldFormatMetaParams,
} from './field_format';
export { getHighlightRequest, asPrettyString, getHighlightHtml } from './utils';
export * from './converters';
export * from './constants';
25 changes: 15 additions & 10 deletions src/plugins/data/public/field_formats_provider/field_formats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
IFieldFormatType,
IFieldFormatId,
FieldFormat,
IFieldFormatMetaParams,
} from '../../common';
import { FieldType } from './types';

Expand Down Expand Up @@ -76,9 +77,9 @@ export class FieldFormatRegisty {
* Get a derived FieldFormat class by its id.
*
* @param {IFieldFormatId} formatId - the format id
* @return {FieldFormat | void}
* @return {FieldFormat | undefined}
*/
getType = (formatId: IFieldFormatId): IFieldFormatType | void => {
getType = (formatId: IFieldFormatId): IFieldFormatType | undefined => {
const fieldFormat = this.fieldFormats.get(formatId);

if (fieldFormat) {
Expand All @@ -88,6 +89,8 @@ export class FieldFormatRegisty {
return decoratedFieldFormat as IFieldFormatType;
}
}

return undefined;
};

/**
Expand All @@ -97,12 +100,12 @@ export class FieldFormatRegisty {
*
* @param {KBN_FIELD_TYPES} fieldType
* @param {ES_FIELD_TYPES[]} esTypes - Array of ES data types
* @return {FieldFormat | void}
* @return {FieldFormat | undefined}
*/
getDefaultType = (
fieldType: KBN_FIELD_TYPES,
esTypes: ES_FIELD_TYPES[]
): IFieldFormatType | void => {
): IFieldFormatType | undefined => {
const config = this.getDefaultConfig(fieldType, esTypes);

return this.getType(config.id);
Expand All @@ -113,11 +116,11 @@ export class FieldFormatRegisty {
* using the format:defaultTypeMap config map
*
* @param {ES_FIELD_TYPES[]} esTypes - Array of ES data types
* @return {ES_FIELD_TYPES | void}
* @return {ES_FIELD_TYPES | undefined}
*/
getTypeNameByEsTypes = (esTypes: ES_FIELD_TYPES[] | undefined): ES_FIELD_TYPES | void => {
getTypeNameByEsTypes = (esTypes: ES_FIELD_TYPES[] | undefined): ES_FIELD_TYPES | undefined => {
if (!Array.isArray(esTypes)) {
return;
return undefined;
}

return esTypes.find(type => this.defaultMap[type] && this.defaultMap[type].es);
Expand Down Expand Up @@ -241,11 +244,11 @@ export class FieldFormatRegisty {
*
* @private
* @param {IFieldFormatType} fieldFormat - field format type
* @return {FieldFormat | void}
* @return {FieldFormat | undefined}
*/
private fieldFormatMetaParamsDecorator = (
fieldFormat: IFieldFormatType
): IFieldFormatType | void => {
): IFieldFormatType | undefined => {
const getMetaParams = (customParams: Record<string, any>) => this.buildMetaParams(customParams);

if (fieldFormat) {
Expand All @@ -258,6 +261,8 @@ export class FieldFormatRegisty {
}
};
}

return undefined;
};

/**
Expand All @@ -267,7 +272,7 @@ export class FieldFormatRegisty {
* @param {Record<string, any>} custom params
* @return {Record<string, any>}
*/
private buildMetaParams = <T extends ...>(customParams: T = {}): T & {parsedUrl: ParsedUrl} => ({
private buildMetaParams = <T extends IFieldFormatMetaParams>(customParams: T): T => ({
parsedUrl: {
origin: window.location.origin,
pathname: window.location.pathname,
Expand Down

0 comments on commit 3f07aba

Please sign in to comment.