Skip to content

Commit

Permalink
isolate type
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasneirynck committed Sep 15, 2020
1 parent 51b376e commit d570f9a
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 21 deletions.
4 changes: 4 additions & 0 deletions x-pack/plugins/maps/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,3 +262,7 @@ export enum MB_LOOKUP_FUNCTION {
GET = 'get',
FEATURE_STATE = 'feature-state',
}

export type RawValue = string | number | boolean | undefined | null;

export type FieldFormatter = (value: RawValue) => string | number;
4 changes: 1 addition & 3 deletions x-pack/plugins/maps/public/classes/sources/source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { Adapters } from 'src/plugins/inspector/public';
import { copyPersistentState } from '../../reducers/util';

import { IField } from '../fields/field';
import { MAX_ZOOM, MIN_ZOOM } from '../../../common/constants';
import { FieldFormatter, MAX_ZOOM, MIN_ZOOM } from '../../../common/constants';
import { AbstractSourceDescriptor } from '../../../common/descriptor_types';
import { OnSourceChangeArgs } from '../../connected_components/layer_panel/view';

Expand All @@ -37,8 +37,6 @@ export type PreIndexedShape = {
path: string;
};

export type FieldFormatter = (value: string | number | null | undefined | boolean) => string;

export interface ISource {
destroy(): void;
getDisplayName(): Promise<string>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { shallow } from 'enzyme';
import { Feature, Point } from 'geojson';

import { DynamicColorProperty } from './dynamic_color_property';
import { COLOR_MAP_TYPE, VECTOR_STYLES } from '../../../../../common/constants';
import { COLOR_MAP_TYPE, RawValue, VECTOR_STYLES } from '../../../../../common/constants';
import { mockField, MockLayer, MockStyle } from './__tests__/test_util';
import { ColorDynamicOptions } from '../../../../../common/descriptor_types';
import { IVectorLayer } from '../../../layers/vector_layer/vector_layer';
Expand All @@ -28,7 +28,7 @@ const makeProperty = (options: ColorDynamicOptions, style?: MockStyle, field?: I
field ? field : mockField,
(new MockLayer(style ? style : new MockStyle()) as unknown) as IVectorLayer,
() => {
return (value: string | number | undefined) => value + '_format';
return (value: RawValue) => value + '_format';
}
);
};
Expand Down Expand Up @@ -273,7 +273,7 @@ describe('supportsFieldMeta', () => {
null,
(new MockLayer(new MockStyle()) as unknown) as IVectorLayer,
() => {
return (value: string | number | undefined) => value + '_format';
return (value: RawValue) => value + '_format';
}
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jest.mock('../components/vector_style_editor', () => ({
}));

import React from 'react';
import { VECTOR_STYLES } from '../../../../../common/constants';
import { RawValue, VECTOR_STYLES } from '../../../../../common/constants';
// @ts-ignore
import { DynamicIconProperty } from './dynamic_icon_property';
import { mockField, MockLayer } from './__tests__/test_util';
Expand All @@ -33,7 +33,7 @@ const makeProperty = (options: Partial<IconDynamicOptions>, field: IField = mock
field,
mockVectorLayer,
() => {
return (value: string | number | undefined) => value + '_format';
return (value: RawValue) => value + '_format';
}
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import { Map as MbMap } from 'mapbox-gl';
import { DynamicStyleProperty, getNumericalMbFeatureStateValue } from './dynamic_style_property';
import { OrientationDynamicOptions } from '../../../../../common/descriptor_types';
import { RawValue } from './style_property';
import { RawValue } from '../../../../../common/constants';

export class DynamicOrientationProperty extends DynamicStyleProperty<OrientationDynamicOptions> {
syncIconRotationWithMb(symbolLayerId: string, mbMap: MbMap) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { shallow } from 'enzyme';

// @ts-ignore
import { DynamicSizeProperty } from './dynamic_size_property';
import { VECTOR_STYLES } from '../../../../../common/constants';
import { RawValue, VECTOR_STYLES } from '../../../../../common/constants';
import { IField } from '../../../fields/field';
import { Map as MbMap } from 'mapbox-gl';
import { SizeDynamicOptions } from '../../../../../common/descriptor_types';
Expand Down Expand Up @@ -48,7 +48,7 @@ const makeProperty = (
field,
(new MockLayer(mockStyle) as unknown) as IVectorLayer,
() => {
return (value: string | number | undefined) => value + '_format';
return (value: RawValue) => value + '_format';
},
false
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import _ from 'lodash';
import React from 'react';
import { Map as MbMap } from 'mapbox-gl';
import { DynamicStyleProperty, FieldFormatter } from './dynamic_style_property';
import { DynamicStyleProperty } from './dynamic_style_property';
import { OrdinalLegend } from '../components/legend/ordinal_legend';
import { makeMbClampedNumberExpression } from '../style_util';
import {
Expand All @@ -16,7 +16,7 @@ import {
SMALL_MAKI_ICON_SIZE,
// @ts-expect-error
} from '../symbol_utils';
import { MB_LOOKUP_FUNCTION, VECTOR_STYLES } from '../../../../../common/constants';
import { FieldFormatter, MB_LOOKUP_FUNCTION, VECTOR_STYLES } from '../../../../../common/constants';
import { SizeDynamicOptions } from '../../../../../common/descriptor_types';
import { IField } from '../../../fields/field';
import { IVectorLayer } from '../../../layers/vector_layer/vector_layer';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@
import _ from 'lodash';
import React from 'react';
import { Feature } from 'geojson';
import { AbstractStyleProperty, IStyleProperty, RawValue } from './style_property';
import { AbstractStyleProperty, IStyleProperty } from './style_property';
import { DEFAULT_SIGMA } from '../vector_style_defaults';
import {
FIELD_ORIGIN,
MB_LOOKUP_FUNCTION,
SOURCE_META_DATA_REQUEST_ID,
STYLE_TYPE,
VECTOR_STYLES,
RawValue,
FieldFormatter,
} from '../../../../../common/constants';
import { OrdinalFieldMetaPopover } from '../components/field_meta/ordinal_field_meta_popover';
import { CategoricalFieldMetaPopover } from '../components/field_meta/categorical_field_meta_popover';
Expand Down Expand Up @@ -58,8 +60,6 @@ export interface IDynamicStyleProperty<T> extends IStyleProperty<T> {
getMbPropertyValue(value: RawValue): RawValue;
}

export type FieldFormatter = (value: string | number | undefined) => string | number;

export class DynamicStyleProperty<T>
extends AbstractStyleProperty<T>
implements IDynamicStyleProperty<T> {
Expand Down Expand Up @@ -327,7 +327,7 @@ export class DynamicStyleProperty<T>
if (this.getField()) {
const fieldName = this.getFieldName();
const fieldFormatter = this._getFieldFormatter(fieldName);
return fieldFormatter && value !== null ? fieldFormatter(value) : super.formatField(value);
return fieldFormatter ? fieldFormatter(value) : super.formatField(value);
} else {
return super.formatField(value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import { Map as MbMap } from 'mapbox-gl';
import { DynamicStyleProperty } from './dynamic_style_property';
import { LabelDynamicOptions } from '../../../../../common/descriptor_types';
import { RawValue } from './style_property';
import { RawValue } from '../../../../../common/constants';

export class DynamicTextProperty extends DynamicStyleProperty<LabelDynamicOptions> {
syncTextFieldWithMb(mbLayerId: string, mbMap: MbMap) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import { ReactElement } from 'react';
// @ts-ignore
import { getVectorStyleLabel } from '../components/get_vector_style_label';
import { FieldMetaOptions } from '../../../../../common/descriptor_types';
import { VECTOR_STYLES } from '../../../../../common/constants';
export type RawValue = string | number | undefined | null;
import { RawValue, VECTOR_STYLES } from '../../../../../common/constants';

export type LegendProps = {
isPointsOnly: boolean;
Expand Down Expand Up @@ -55,7 +54,13 @@ export class AbstractStyleProperty<T> implements IStyleProperty<T> {
}

formatField(value: RawValue): string | number {
return typeof value === 'undefined' || value === null ? '' : value;
if (typeof value === 'undefined' || value === null) {
return '';
} else if (typeof value === 'boolean') {
return value.toString();
} else {
return value;
}
}

getStyleName(): VECTOR_STYLES {
Expand Down

0 comments on commit d570f9a

Please sign in to comment.