Skip to content

Commit d263cce

Browse files
committed
templateString evaluates attributes with a dot in their name
1 parent 1fde3aa commit d263cce

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

src/lib/index.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,11 +1005,12 @@ lib.templateString = function(string) {
10051005
var obj, value, i;
10061006
for(i = 1; i < args.length; i++) {
10071007
obj = args[i];
1008-
if(SIMPLE_PROPERTY_REGEX.test(key)) {
1009-
if(obj.hasOwnProperty(key)) {
1010-
value = obj[key];
1011-
}
1012-
} else {
1008+
if(obj.hasOwnProperty(key)) {
1009+
value = obj[key];
1010+
break;
1011+
}
1012+
1013+
if(!SIMPLE_PROPERTY_REGEX.test(key)) {
10131014
// getterCache[key] = getterCache[key] || lib.nestedProperty(obj, key).get;
10141015
// value = getterCache[key]();
10151016
value = getterCache[key] || lib.nestedProperty(obj, key).get();

test/jasmine/tests/lib_test.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2124,6 +2124,10 @@ describe('Test lib.js:', function() {
21242124
expect(Lib.templateString('foo %{bar}', {bar: 'baz'})).toEqual('foo baz');
21252125
});
21262126

2127+
it('evaluates attributes with a dot in their name', function() {
2128+
expect(Lib.templateString('%{marker.size}', {'marker.size': 12}, {marker: {size: 14}})).toEqual('12');
2129+
});
2130+
21272131
it('evaluates nested properties', function() {
21282132
expect(Lib.templateString('foo %{bar.baz}', {bar: {baz: 'asdf'}})).toEqual('foo asdf');
21292133
});
@@ -2144,7 +2148,7 @@ describe('Test lib.js:', function() {
21442148
expect(Lib.templateString('foo %{} %{}', {})).toEqual('foo ');
21452149
});
21462150

2147-
it('find the first object with a given key', function() {
2151+
it('uses the value from the first object with the specified key', function() {
21482152
var obj1 = {a: 'first'}, obj2 = {a: 'second', foo: {bar: 'bar'}};
21492153

21502154
// Simple key

0 commit comments

Comments
 (0)