Skip to content

Commit

Permalink
[fix] Updated type data-utils getColumnFormatter method (#2640)
Browse files Browse the repository at this point in the history
Signed-off-by: Ihor Dykhta <[email protected]>
  • Loading branch information
igorDykhta authored Sep 12, 2024
1 parent 5d77b7a commit 56c9c3e
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions src/utils/src/data-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
TOOLTIP_KEY,
TooltipFormat
} from '@kepler.gl/constants';
import {Millisecond, Field, ColMetaProps} from '@kepler.gl/types';
import {Millisecond, Field} from '@kepler.gl/types';

import {snapToMarks} from './plot';
import {isPlainObject} from './utils';
Expand Down Expand Up @@ -311,18 +311,20 @@ export function getFormatter(
return defaultFormatter;
}

export function getColumnFormatter(colMeta: ColMetaProps): FieldFormatter {
const {format, displayFormat} = colMeta;
export function getColumnFormatter(
field: Pick<Field, 'type'> & Partial<Pick<Field, 'format' | 'displayFormat'>>
): FieldFormatter {
const {format, displayFormat} = field;

if (!format && !displayFormat) {
return FIELD_DISPLAY_FORMAT[colMeta.type];
return FIELD_DISPLAY_FORMAT[field.type];
}
const tooltipFormat = Object.values(TOOLTIP_FORMATS).find(f => f[TOOLTIP_KEY] === displayFormat);

if (tooltipFormat) {
return applyDefaultFormat(tooltipFormat);
} else if (typeof displayFormat === 'string' && colMeta) {
return applyCustomFormat(displayFormat, colMeta);
} else if (typeof displayFormat === 'string' && field) {
return applyCustomFormat(displayFormat, field);
} else if (typeof displayFormat === 'object') {
return applyValueMap(displayFormat);
}
Expand Down Expand Up @@ -365,7 +367,7 @@ export function getBooleanFormatter(format: string): FieldFormatter {
}
}
// Allow user to specify custom tooltip format via config
export function applyCustomFormat(format, field): FieldFormatter {
export function applyCustomFormat(format, field: {type?: string}): FieldFormatter {
switch (field.type) {
case ALL_FIELD_TYPES.real:
case ALL_FIELD_TYPES.integer:
Expand Down Expand Up @@ -421,7 +423,11 @@ export function datetimeFormatter(
timezone?: string | null
): (format?: string) => (ts: number) => string {
return timezone
? format => ts => moment.utc(ts).tz(timezone).format(format)
? format => ts =>
moment
.utc(ts)
.tz(timezone)
.format(format)
: // return empty string instead of 'Invalid date' if ts is undefined/null
format => ts => ts ? moment.utc(ts).format(format) : '';
format => ts => (ts ? moment.utc(ts).format(format) : '');
}

0 comments on commit 56c9c3e

Please sign in to comment.