Skip to content

Commit daa2c7e

Browse files
committed
templateFormatString: do not error out when lookup objects are undefined
1 parent db87e62 commit daa2c7e

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

Diff for: src/lib/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -1031,6 +1031,7 @@ var TEMPLATE_STRING_FORMAT_SEPARATOR = /^[:|\|]/;
10311031
function templateFormatString(string, labels, d3locale) {
10321032
var opts = this;
10331033
var args = arguments;
1034+
if(!labels) labels = {};
10341035
// Not all that useful, but cache nestedProperty instantiation
10351036
// just in case it speeds things up *slightly*:
10361037
var getterCache = {};
@@ -1039,6 +1040,7 @@ function templateFormatString(string, labels, d3locale) {
10391040
var obj, value, i;
10401041
for(i = 3; i < args.length; i++) {
10411042
obj = args[i];
1043+
if(!obj) continue;
10421044
if(obj.hasOwnProperty(key)) {
10431045
value = obj[key];
10441046
break;

Diff for: test/jasmine/tests/lib_test.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -2250,14 +2250,24 @@ describe('Test lib.js:', function() {
22502250
}
22512251
expect(Lib.warn.calls.count()).toBe(10);
22522252
});
2253+
2254+
it('does not error out when arguments are undefined', function() {
2255+
expect(function() {
2256+
Lib.hovertemplateString('y: %{y}', undefined, locale, undefined);
2257+
}).not.toThrow();
2258+
});
22532259
});
22542260

22552261
describe('texttemplateString', function() {
2262+
var locale = false;
22562263
it('evaluates attributes', function() {
2257-
var locale = false;
22582264
expect(Lib.texttemplateString('foo %{bar}', {}, locale, {bar: 'baz'})).toEqual('foo baz');
22592265
});
22602266

2267+
it('looks for default label if no format is provided', function() {
2268+
expect(Lib.texttemplateString('y: %{y}', {yLabel: '0.1'}, locale, {y: 0.123})).toEqual('y: 0.1');
2269+
});
2270+
22612271
it('warns user up to 10 times if a variable cannot be found', function() {
22622272
spyOn(Lib, 'warn').and.callThrough();
22632273
Lib.texttemplateString('%{idontexist}', {});

0 commit comments

Comments
 (0)