From 9679106c8ca17e057827c3aeeedc35430cc103d9 Mon Sep 17 00:00:00 2001 From: Kanit Wongsuphasawat Date: Wed, 15 Feb 2017 22:15:49 -0800 Subject: [PATCH] [WIP] Make `field()` correctly escape `[]` Fix #1949 --- src/fielddef.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/fielddef.ts b/src/fielddef.ts index 818bf43199..3a58a2b837 100644 --- a/src/fielddef.ts +++ b/src/fielddef.ts @@ -137,6 +137,14 @@ export interface FieldRefOption { export function field(fieldDef: FieldDef, opt: FieldRefOption = {}) { let field = fieldDef.field; + + const bracketsEscaped = field.charAt(0) === '[' + && field.charAt(field.length - 1) === ']'; + + if (bracketsEscaped) { + field = field.slice(1, field.length -1); + } + let prefix = opt.prefix; let suffix = opt.suffix; @@ -170,10 +178,11 @@ export function field(fieldDef: FieldDef, opt: FieldRefOption = {}) { } if (opt.datum) { - field = `datum["${field}"]`; + // for datum, we never have to wrap with [] + return `datum["${field}"]`; } - return field; + return bracketsEscaped ? `[${field}]` : field; } function _isFieldDimension(fieldDef: FieldDef) {