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: ensure temporal field propagates to time expression #8757

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/compile/format.ts
Expand Up @@ -95,14 +95,25 @@ export function formatSignalRef({
}
}

function getTimeDef(def: FieldDef<string> | DatumDef<string>) {
if (!isFieldDef(def)) {
return {
unit: undefined,
utc: undefined
};
}
return normalizeTimeUnit(def.timeUnit) || {};
}

if (isFieldOrDatumDefForTimeFormat(fieldOrDatumDef)) {
const {unit: timeUnit, utc: isUTCUnit} = getTimeDef(fieldOrDatumDef);
const signal = timeFormatExpression({
field,
timeUnit: isFieldDef(fieldOrDatumDef) ? normalizeTimeUnit(fieldOrDatumDef.timeUnit)?.unit : undefined,
timeUnit,
format,
formatType: config.timeFormatType,
rawTimeFormat: config.timeFormat,
isUTCScale: isScaleFieldDef(fieldOrDatumDef) && fieldOrDatumDef.scale?.type === ScaleType.UTC
isUTCScale: isUTCUnit || (isScaleFieldDef(fieldOrDatumDef) && fieldOrDatumDef.scale?.type === ScaleType.UTC)
});
return signal ? {signal} : undefined;
}
Expand Down
24 changes: 24 additions & 0 deletions test/compile/format.test.ts
Expand Up @@ -70,6 +70,17 @@ describe('Format', () => {
expect(expression).toBe(`utcFormat(datum["yearmonth_a"], '%Y')`);
});

it('should get the right time expression for utcyearmonth', () => {
const fieldDef = {timeUnit: 'utcyearmonth', field: 'a', type: TEMPORAL} as const;
const expression = timeFormatExpression({
field: vgField(fieldDef, {expr: 'datum'}),
timeUnit: 'month',
format: '%Y',
isUTCScale: true
});
expect(expression).toBe(`utcFormat(datum["utcyearmonth_a"], '%Y')`);
});

it('should get the right time expression for with a custom timeFormatType', () => {
const fieldDef = {field: 'a', type: TEMPORAL} as const;
const expression = timeFormatExpression({
Expand Down Expand Up @@ -157,6 +168,19 @@ describe('Format', () => {
});

describe('formatSignalRef()', () => {
it('should derive the correct temporal unit', () => {
expect(
formatSignalRef({
fieldOrDatumDef: {field: 'foo', type: TEMPORAL, timeUnit: 'utcdate'},
format: undefined,
formatType: undefined,
config: {}
})
).toEqual({
signal:
'utcFormat(utcdate_foo, timeUnitSpecifier(["date"], {"year-month":"%b %Y ","year-month-date":"%b %d, %Y "}))'
});
});
it('should format ordinal field defs if format is present', () => {
expect(
formatSignalRef({
Expand Down