8000 make string val type more strict: · rowhit/plotly.js@0b3040b · GitHub
[go: up one dir, main page]

Skip to content

Commit 0b3040b

Browse files
committed
make string val type more strict:
- now only coerce numbers, dates and boolean to string
1 parent 9814edf commit 0b3040b

File tree

2 files changed

+22
-21
lines changed

2 files changed

+22
-21
lines changed

src/lib/coerce.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,16 +99,18 @@ exports.valObjects = {
9999
// TODO 'values shouldn't be in there (edge case: 'dash' in Scatter)
100100
otherOpts: ['dflt', 'noBlank', 'strict', 'arrayOk', 'values'],
101101
coerceFunction: function(v, propOut, dflt, opts) {
102-
if(opts.strict === true && typeof v !== 'string') {
103-
propOut.set(dflt);
104-
return;
105-
}
102+
if(typeof v !== 'string') {
103+
var okToCoerce = (
104+
typeof v === 'number' ||
105+
v instanceof Date ||
106+
typeof v === 'boolean'
107+
);
106108

107-
var s = String(v);
108-
if(v === undefined || (opts.noBlank === true && !s)) {
109-
propOut.set(dflt);
109+
if(opts.strict === true || !okToCoerce) propOut.set(dflt);
110+
else propOut.set(String(v));
110111
}
111-
else propOut.set(s);
112+
else if(opts.noBlank && !v) propOut.set(dflt);
113+
else propOut.set(v);
112114
}
113115
},
114116
color: {

test/jasmine/tests/lib_test.js

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -526,13 +526,13 @@ describe('Test lib.js:', function() {
526526
.toEqual('42');
527527

528528
expect(coerce({s: [1, 2, 3]}, {}, stringAttrs, 's'))
529-
.toEqual('1,2,3');
529+
.toEqual(dflt);
530530

531531
expect(coerce({s: true}, {}, stringAttrs, 's'))
532532
.toEqual('true');
533533

534534
expect(coerce({s: {1: 2}}, {}, stringAttrs, 's'))
535-
.toEqual('[object Object]'); // useless, but that's what it does!!
535+
.toEqual(dflt);
536536
});
537537
});
538538

@@ -861,27 +861,26 @@ describe('Test lib.js:', function() {
861861
});
862862

863863
it('should work for valType \'string\' where', function() {
864+
var date = new Date(2016, 1, 1);
864865

865-
// This fails as the coerceFunction coerce all values
866-
// (except when 'strict' is true) using String()
867-
//
868-
// Things like undefined, null, {}, [] should be considered valid!
869-
//
870-
// The question becomes how the change this while not
871-
// scarifying perf??
872-
873-
assert(['3', '4', 'a'], [undefined, {}, [], null], {
866+
assert(['3', '4', 'a', 3, 1.2113, '', date, false], [undefined, {}, [], null], {
874867
valType: 'string',
875868
dflt: 'a'
876869
});
877870

878-
assert(['3', '4', 'a'], [undefined, {}, [], null, ''], {
871+
assert(['3', '4', 'a', 3, 1.2113, date, true], ['', undefined, {}, [], null], {
879872
valType: 'string',
880873
dflt: 'a',
881874
noBlank: true
882875
});
883876

884-
assert(['3', '4'], [undefined, 1, {}, [], null, ''], {
877+
assert(['3', '4', ''], [undefined, 1, {}, [], null, date, true, false], {
878+
valType: 'string',
879+
dflt: 'a',
880+
strict: true
881+
});
882+
883+
assert(['3', '4'], [undefined, 1, {}, [], null, date, '', true, false], {
885884
valType: 'string',
886885
dflt: 'a',
887886
strict: true,

0 commit comments

Comments
 (0)
0