Skip to content

Commit 5215f13

Browse files
committed
Always use fallback for missing values, except when fallback is false
1 parent 6a66148 commit 5215f13

File tree

3 files changed

+19
-20
lines changed

3 files changed

+19
-20
lines changed

src/lib/index.js

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,7 +1118,7 @@ var TEMPLATE_STRING_FORMAT_SEPARATOR = /^[:|\|]/;
11181118
*
11191119
* @param {object} options - Configuration object
11201120
* @param {array} options.data - Data objects containing substitution values
1121-
* @param {string} options.fallback - Fallback value when substitution fails
1121+
* @param {boolean|string} options.fallback - Fallback value when substitution fails. If false, the specifier is used.
11221122
* @param {object} options.labels - Data object containing fallback text when no formatting is specified, ex.: {yLabel: 'formattedYValue'}
11231123
* @param {object} options.locale - D3 locale for formatting
11241124
* @param {object} options.opts - Additional options
@@ -1151,47 +1151,43 @@ function templateFormatString({ data = [], locale, fallback, labels = {}, opts,
11511151
parsedNumber = _match.number;
11521152
}
11531153

1154-
let keyIsMissing = true;
11551154
let value = undefined;
11561155
if (hasOther) {
11571156
// 'other' specifiers that are undefined return an empty string by design
11581157
if (labels[key] === undefined) return '';
11591158
value = labels[key];
1160-
keyIsMissing = false;
11611159
} else {
11621160
for (const obj of data) {
11631161
if (!obj) continue;
11641162
if (obj.hasOwnProperty(key)) {
11651163
value = obj[key];
1166-
keyIsMissing = false;
11671164
break;
11681165
}
11691166

11701167
if (!SIMPLE_PROPERTY_REGEX.test(key)) {
11711168
// true here means don't convert null to undefined
11721169
value = lib.nestedProperty(obj, key).get(true);
1173-
keyIsMissing = false;
11741170
}
11751171
if (value !== undefined) break;
11761172
}
11771173
}
11781174

1179-
if (keyIsMissing) {
1175+
if (value === undefined) {
11801176
const { count, max, name } = opts;
1181-
if (count < max)
1177+
const fallbackValue = fallback === false ? match : fallback;
1178+
if (count < max) {
11821179
lib.warn(
11831180
[
11841181
`Variable '${key}' in ${name} could not be found!`,
1185-
'Please verify that the template is correct.'
1182+
'Please verify that the template is correct.',
1183+
`Using value: '${fallbackValue}'.`
11861184
].join(' ')
11871185
);
1186+
}
11881187
if (count === max) lib.warn(`Too many '${name}' warnings - additional warnings will be suppressed.`);
11891188
opts.count++;
11901189

1191-
return match;
1192-
} else if (value === undefined) {
1193-
// In this case, the actual value in the data set is 'undefined', so use fallback without warning
1194-
return fallback;
1190+
return fallbackValue;
11951191
}
11961192

11971193
if (parsedOp === '*') value *= parsedNumber;

src/plots/template_attributes.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,11 @@ exports.shapeTexttemplateAttrs = ({ editType = 'arraydraw', newshape } = {}, ext
9696
});
9797

9898
exports.templatefallbackAttrs = ({ editType = 'none' } = {}) => ({
99-
valType: 'string',
99+
valType: 'any',
100100
dflt: '-',
101101
editType,
102-
description: "Fallback value that's displayed when a variable referenced in a template has an undefined value."
102+
description: [
103+
"Fallback string that's displayed when a variable referenced in a template is missing.",
104+
"If the boolean value 'false' is passed in, the specifier with the missing variable will be displayed."
105+
].join(' ')
103106
});

test/jasmine/tests/lib_test.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2706,18 +2706,18 @@ describe('Test lib.js:', function () {
27062706
});
27072707

27082708
// This test must come after the warning count since it will affect the count
2709-
it('replaces template specifiers where the key is missing with the specifier', () => {
2709+
it('replaces template specifiers with the specifier when fallback is false', () => {
27102710
expect(
27112711
Lib.hovertemplateString({
27122712
data: [{ group: 1 }],
2713-
fallback: 'Planet Express',
2713+
fallback: false,
27142714
template: 'foo %{group} %{trace}'
27152715
})
27162716
).toEqual('foo 1 %{trace}');
27172717
});
27182718

27192719
// This test must come after the warning count since it will affect the count
2720-
it('replaces template specifiers where the value is undefined with the fallback value', () => {
2720+
it('replaces template specifiers with the fallback value when fallback is provided', () => {
27212721
expect(
27222722
Lib.hovertemplateString({
27232723
data: [{ group: 1, trace: undefined }],
@@ -2773,18 +2773,18 @@ describe('Test lib.js:', function () {
27732773
});
27742774

27752775
// This test must come after the warning count since it will affect the count
2776-
it('replaces template specifiers where the key is missing with the specifier', () => {
2776+
it('replaces template specifiers with the specifier when fallback is false', () => {
27772777
expect(
27782778
Lib.texttemplateString({
27792779
data: [{ group: 1 }],
2780-
fallback: 'Zoidberg',
2780+
fallback: false,
27812781
template: 'foo %{group} %{trace}'
27822782
})
27832783
).toEqual('foo 1 %{trace}');
27842784
});
27852785

27862786
// This test must come after the warning count since it will affect the count
2787-
it('replaces template specifiers where the value is undefined with the fallback value', () => {
2787+
it('replaces template specifiers with the fallback value when fallback is provided', () => {
27882788
expect(
27892789
Lib.texttemplateString({
27902790
data: [{ group: 1, trace: undefined }],

0 commit comments

Comments
 (0)