Skip to content

Commit 25a30a8

Browse files
committed
catch and handle bad d3 number format instead of throwing error
1 parent 6b93e61 commit 25a30a8

File tree

2 files changed

+30
-16
lines changed

2 files changed

+30
-16
lines changed

src/lib/index.js

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,33 @@ var BADNUM = numConstants.BADNUM;
1212

1313
var lib = module.exports = {};
1414

15-
lib.adjustFormat = function adjustFormat(a) {
15+
lib.adjustFormat = function adjustFormat(formatStr) {
1616
if(
17-
!a ||
18-
/^[0123456789].[0123456789]f/.test(a) ||
19-
/.[0123456789]%/.test(a)
20-
) return a;
17+
!formatStr ||
18+
/^[0123456789].[0123456789]f/.test(formatStr) ||
19+
/.[0123456789]%/.test(formatStr)
20+
) return formatStr;
2121

22-
if(a === '0.f') return '~f';
23-
if(/^[0123456789]%/.test(a)) return '~%';
24-
if(/^[0123456789]s/.test(a)) return '~s';
22+
if(formatStr === '0.f') return '~f';
23+
if(/^[0123456789]%/.test(formatStr)) return '~%';
24+
if(/^[0123456789]s/.test(formatStr)) return '~s';
2525

2626
// try adding tilde to the start of format in order to trim
27-
if(!(/^[~,.0$]/.test(a)) && /[&fps]/.test(a)) return '~' + a;
27+
if(!(/^[~,.0$]/.test(formatStr)) && /[&fps]/.test(formatStr)) return '~' + formatStr;
2828

29-
return a;
29+
return formatStr;
3030
};
3131

32-
lib.numberFormat = function(a) {
33-
return d3Format(lib.adjustFormat(a));
32+
lib.noFormat = function(value) { return value; };
33+
34+
lib.numberFormat = function(formatStr) {
35+
try {
36+
formatStr = d3Format(lib.adjustFormat(formatStr));
37+
} catch(e) {
38+
return lib.noFormat;
39+
}
40+
41+
return formatStr;
3442
};
3543

3644
lib.nestedProperty = require('./nested_property');

src/plots/plots.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -703,10 +703,16 @@ function getFormatter(formatObj, separators) {
703703
formatObj.thousands = separators.charAt(1);
704704

705705
return {
706-
numberFormat: function(a) {
707-
return formatLocale(formatObj).format(
708-
Lib.adjustFormat(a)
709-
);
706+
numberFormat: function(formatStr) {
707+
try {
708+
formatStr = formatLocale(formatObj).format(
709+
Lib.adjustFormat(formatStr)
710+
);
711+
} catch(e) {
712+
return Lib.noFormat;
713+
}
714+
715+
return formatStr;
710716
},
711717
timeFormat: timeFormatLocale(formatObj).utcFormat
712718
};

0 commit comments

Comments
 (0)