Skip to content

Commit 419458c

Browse files
committed
manually stringify regex during PlotSchema.get()
1 parent 1bc95d5 commit 419458c

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

src/plot_api/plot_schema.js

+28-3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ var editTypes = require('./edit_types');
2525

2626
var extendFlat = Lib.extendFlat;
2727
var extendDeepAll = Lib.extendDeepAll;
28+
var isPlainObject = Lib.isPlainObject;
2829

2930
var IS_SUBPLOT_OBJ = '_isSubplotObj';
3031
var IS_LINKED_TO_ARRAY = '_isLinkedToArray';
@@ -140,7 +141,7 @@ exports.crawl = function(attrs, callback, specifiedLevel, attrString) {
140141

141142
if(exports.isValObject(attr)) return;
142143

143-
if(Lib.isPlainObject(attr) && attrName !== 'impliedEdits') {
144+
if(isPlainObject(attr) && attrName !== 'impliedEdits') {
144145
exports.crawl(attr, callback, level + 1, fullAttrString);
145146
}
146147
});
@@ -387,7 +388,7 @@ function recurseIntoValObject(valObject, parts, i) {
387388
// the innermost schema item we find.
388389
for(; i < parts.length; i++) {
389390
var newValObject = valObject[parts[i]];
390-
if(Lib.isPlainObject(newValObject)) valObject = newValObject;
391+
if(isPlainObject(newValObject)) valObject = newValObject;
391392
else break;
392393

393394
if(i === parts.length - 1) break;
@@ -565,6 +566,7 @@ function getFramesAttributes() {
565566
function formatAttributes(attrs) {
566567
mergeValTypeAndRole(attrs);
567568
formatArrayContainers(attrs);
569+
stringify(attrs);
568570

569571
return attrs;
570572
}
@@ -596,7 +598,7 @@ function mergeValTypeAndRole(attrs) {
596598
attrs[attrName + 'src'] = makeSrcAttr(attrName);
597599
}
598600
}
599-
else if(Lib.isPlainObject(attr)) {
601+
else if(isPlainObject(attr)) {
600602
// all attrs container objects get role 'object'
601603
attr.role = 'object';
602604
}
@@ -624,6 +626,29 @@ function formatArrayContainers(attrs) {
624626
exports.crawl(attrs, callback);
625627
}
626628

629+
// this can take around 10ms and should only be run from PlotSchema.get(),
630+
// to ensure JSON.stringify(PlotSchema.get()) gives the intended result.
631+
function stringify(attrs) {
632+
function walk(attr) {
633+
for(var k in attr) {
634+
if(isPlainObject(attr[k])) {
635+
walk(attr[k]);
636+
} else if(Array.isArray(attr[k])) {
637+
for(var i = 0; i < attr[k].length; i++) {
638+
walk(attr[k][i]);
639+
}
640+
} else {
641+
// as JSON.stringify(/test/) // => {}
642+
if(attr[k] instanceof RegExp) {
643+
attr[k] = attr[k].toString();
644+
}
645+
}
646+
}
647+
}
648+
649+
walk(attrs);
650+
}
651+
627652
function assignPolarLayoutAttrs(layoutAttributes) {
628653
extendFlat(layoutAttributes, {
629654
radialaxis: polarAxisAttrs.radialaxis,

test/jasmine/tests/plotschema_test.js

+9
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,15 @@ describe('plot schema', function() {
349349
expect(scatterglSchema.error_y.copy_ystyle).toBeUndefined();
350350
expect(scatterglSchema.error_y.copy_zstyle).toBeUndefined();
351351
});
352+
353+
it('should convert regex valObject fields to strings', function() {
354+
var splomAttrs = plotSchema.traces.splom.attributes;
355+
356+
expect(typeof splomAttrs.xaxes.items.regex).toBe('string');
357+
expect(splomAttrs.xaxes.items.regex).toBe('/^x([2-9]|[1-9][0-9]+)?$/');
358+
expect(typeof splomAttrs.yaxes.items.regex).toBe('string');
359+
expect(splomAttrs.yaxes.items.regex).toBe('/^y([2-9]|[1-9][0-9]+)?$/');
360+
});
352361
});
353362

354363
describe('getTraceValObject', function() {

0 commit comments

Comments
 (0)