Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] Updated type data-utils getColumnFormatter method #2640

Merged
merged 1 commit into from
Sep 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) : '');
}
Loading