Skip to content

Commit fa51e33

Browse files
authored
Merge pull request #4526 from plotly/allow-0-in-nameformat-templates
Allow 0 in grouby transform nameformat templates + fix Plotly.validate for valType:'any' attributes
2 parents 56f4945 + 098ced5 commit fa51e33

File tree

4 files changed

+49
-6
lines changed

4 files changed

+49
-6
lines changed

src/lib/index.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -989,11 +989,14 @@ lib.templateString = function(string, obj) {
989989
var getterCache = {};
990990

991991
return string.replace(lib.TEMPLATE_STRING_REGEX, function(dummy, key) {
992+
var v;
992993
if(SIMPLE_PROPERTY_REGEX.test(key)) {
993-
return obj[key] || '';
994+
v = obj[key];
995+
} else {
996+
getterCache[key] = getterCache[key] || lib.nestedProperty(obj, key).get;
997+
v = getterCache[key]();
994998
}
995-
getterCache[key] = getterCache[key] || lib.nestedProperty(obj, key).get;
996-
return getterCache[key]() || '';
999+
return lib.isValidTextValue(v) ? v : '';
9971000
});
9981001
};
9991002

src/plot_api/validate.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,14 @@ function crawl(objIn, objOut, schema, list, base, path) {
161161
var valOut = objOut[k];
162162

163163
var nestedSchema = getNestedSchema(schema, k);
164-
var isInfoArray = (nestedSchema || {}).valType === 'info_array';
165-
var isColorscale = (nestedSchema || {}).valType === 'colorscale';
164+
var nestedValType = (nestedSchema || {}).valType;
165+
var isInfoArray = nestedValType === 'info_array';
166+
var isColorscale = nestedValType === 'colorscale';
166167
var items = (nestedSchema || {}).items;
167168

168169
if(!isInSchema(schema, k)) {
169170
list.push(format('schema', base, p));
170-
} else if(isPlainObject(valIn) && isPlainObject(valOut)) {
171+
} else if(isPlainObject(valIn) && isPlainObject(valOut) && nestedValType !== 'any') {
171172
crawl(valIn, valOut, nestedSchema, list, base, p);
172173
} else if(isInfoArray && isArray(valIn)) {
173174
if(valIn.length > valOut.length) {

test/jasmine/tests/lib_test.js

+16
Original file line numberDiff line numberDiff line change
@@ -2229,6 +2229,14 @@ describe('Test lib.js:', function() {
22292229
it('replaces empty key with empty string', function() {
22302230
expect(Lib.templateString('foo %{} %{}', {})).toEqual('foo ');
22312231
});
2232+
2233+
it('should work with the number *0*', function() {
2234+
expect(Lib.templateString('%{group}', {group: 0})).toEqual('0');
2235+
});
2236+
2237+
it('should work with the number *0* (nested case)', function() {
2238+
expect(Lib.templateString('%{x.y}', {'x': {y: 0}})).toEqual('0');
2239+
});
22322240
});
22332241

22342242
describe('hovertemplateString', function() {
@@ -2249,6 +2257,14 @@ describe('Test lib.js:', function() {
22492257
expect(Lib.hovertemplateString('foo %{bar[0].baz}', {}, locale, {bar: [{baz: 'asdf'}]})).toEqual('foo asdf');
22502258
});
22512259

2260+
it('should work with the number *0*', function() {
2261+
expect(Lib.hovertemplateString('%{group}', {}, locale, {group: 0})).toEqual('0');
2262+
});
2263+
2264+
it('should work with the number *0* (nested case)', function() {
2265+
expect(Lib.hovertemplateString('%{x.y}', {}, locale, {'x': {y: 0}})).toEqual('0');
2266+
});
2267+
22522268
it('subtitutes multiple matches', function() {
22532269
expect(Lib.hovertemplateString('foo %{group} %{trace}', {}, locale, {group: 'asdf', trace: 'jkl;'})).toEqual('foo asdf jkl;');
22542270
});

test/jasmine/tests/validate_test.js

+23
Original file line numberDiff line numberDiff line change
@@ -640,4 +640,27 @@ describe('Plotly.validate', function() {
640640
}]);
641641
expect(out).toBeUndefined();
642642
});
643+
644+
it('should not attempt to crawl into nested objects of valType: \'any\' attributes', function() {
645+
var out = Plotly.validate([{
646+
mode: 'markers',
647+
x: ['a', 'b', 'c', 'a', 'b', 'c'],
648+
y: [1, 2, 3, 4, 5, 6],
649+
transforms: [{
650+
type: 'groupby',
651+
groups: ['a', 'b', 'c'],
652+
styles: [{
653+
target: 'a',
654+
value: {marker: {color: 'blue'}}
655+
}, {
656+
target: 'b',
657+
value: {marker: {color: 'red'}}
658+
}, {
659+
target: 'c',
660+
value: {marker: {color: 'black'}}
661+
}]
662+
}]
663+
}]);
664+
expect(out).toBeUndefined();
665+
});
643666
});

0 commit comments

Comments
 (0)