Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
yhoonkim committed Dec 2, 2024
1 parent ac3f4fd commit fff4bef
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/compile/data/timeunit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
normalizeTimeUnit
} from '../../timeunit';
import {TimeUnitTransform} from '../../transform';
import {Dict, duplicate, entries, hash, isEmpty, replacePathInField, vals} from '../../util';
import {Dict, duplicate, entries, escapePathAccess, hash, isEmpty, replacePathInField, vals} from '../../util';
import {ModelWithField, isUnitModel} from '../model';
import {DataFlowNode} from './dataflow';
import {isRectBasedMark} from '../../mark';
Expand Down Expand Up @@ -218,7 +218,7 @@ function offsetExpr({timeUnit, field, reverse}: {timeUnit: TimeUnitParams; field
const smallestUnit = getSmallestTimeUnitPart(unit);
const {part, step} = getDateTimePartAndStep(smallestUnit, timeUnit.step);
const offsetFn = utc ? 'utcOffset' : 'timeOffset';
const expr = `${offsetFn}('${part}', datum['${field}'], ${reverse ? -step : step})`;
const expr = `${offsetFn}('${part}', datum['${escapePathAccess(field)}'], ${reverse ? -step : step})`;
return expr;
}

Expand All @@ -228,8 +228,8 @@ function offsetedRectFormulas(
timeUnit: TimeUnitParams
): VgFormulaTransform[] {
if (rectBandPosition !== undefined && rectBandPosition !== 0.5) {
const startExpr = `datum['${startField}']`;
const endExpr = `datum['${endField}']`;
const startExpr = `datum['${escapePathAccess(startField)}']`;
const endExpr = `datum['${escapePathAccess(endField)}']`;
return [
{
type: 'formula',
Expand Down
2 changes: 1 addition & 1 deletion src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ export function flatAccessWithDatum(path: string, datum: 'datum' | 'parent' | 'd
return `${datum}[${stringValue(splitAccessPath(path).join('.'))}]`;
}

function escapePathAccess(string: string) {
export function escapePathAccess(string: string) {
return string.replace(/(\[|\]|\.|'|")/g, '\\$1');
}

Expand Down
30 changes: 30 additions & 0 deletions test/compile/data/timeunit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,36 @@ describe('compile/data/timeunit', () => {
]);
});

it('should return a unit transform for bar with bandPosition=0 and escaped field name', () => {
const model = parseUnitModel({
data: {values: []},
mark: 'bar',
encoding: {
x: {field: "\\'a\\'", type: 'temporal', timeUnit: 'month', bandPosition: 0}
}
});

expect(assembleFromEncoding(model)).toEqual([
{
type: 'timeunit',
field: "\\'a\\'",
as: ["month_'a'", "month_'a'_end"],
units: ['month']
},
{
type: 'formula',
expr: "0.5 * timeOffset('month', datum['month_\\'a\\''], -1) + 0.5 * datum['month_\\'a\\'']",
as: `month_'a'_${OFFSETTED_RECT_START_SUFFIX}`
},

{
type: 'formula',
expr: "0.5 * datum['month_\\'a\\''] + 0.5 * datum['month_\\'a\\'_end']",
as: `month_'a'_${OFFSETTED_RECT_END_SUFFIX}`
}
]);
});

it('should return a timeunit transform with step for bar with bandPosition=0', () => {
const model = parseUnitModel({
data: {values: []},
Expand Down

0 comments on commit fff4bef

Please sign in to comment.