[Pkg-javascript-commits] [leaflet] 74/301: '#1968 compiled interpolation'
Jonas Smedegaard
js at moszumanska.debian.org
Mon Jan 27 22:22:42 UTC 2014
This is an automated email from the git hooks/post-receive script.
js pushed a commit to branch master
in repository leaflet.
commit b2b25c8941918ee697425e6134ab0862fcbcf453
Author: Calvin Metcalf <calvin.metcalf at state.ma.us>
Date: Tue Aug 13 08:05:41 2013 -0400
'#1968 compiled interpolation'
---
spec/suites/core/UtilSpec.js | 33 +++++++++++++++++++++++++++++++--
src/core/Util.js | 36 ++++++++++++++++++++++++++----------
2 files changed, 57 insertions(+), 12 deletions(-)
diff --git a/spec/suites/core/UtilSpec.js b/spec/suites/core/UtilSpec.js
index 62d487d..7ec46d9 100644
--- a/spec/suites/core/UtilSpec.js
+++ b/spec/suites/core/UtilSpec.js
@@ -189,16 +189,45 @@ describe('Util', function() {
describe('#template', function () {
it('evaluates templates with a given data object', function () {
- var tpl = 'Hello {foo} and {bar}!';
+ var tpl = 'Hello {foo} and {baz }!';
var str = L.Util.template(tpl, {
foo: 'Vlad',
- bar: 'Dave'
+ bar: 'Dave',
+ baz:function(o){
+ return o.bar;
+ }
});
expect(str).to.eql('Hello Vlad and Dave!');
});
+ it('check the cache', function () {
+ var tpl = 'Hello {foo} and {baz }!';
+
+ var str = L.Util.templateCache[tpl]({
+ foo: 'ladies',
+ baz: function(){
+ return 'gentlemen';
+ }
+ });
+
+ expect(str).to.eql('Hello ladies and gentlemen!');
+ });
+ it('evaluates templates with a function', function () {
+ var tpl = L.Util.compileTemplate('Hello { foo } and { bar}!',{});
+ var str1 = tpl({
+ foo: 'Vlad',
+ bar: 'Dave'
+ });
+ var str2 = tpl({
+ foo: '{Calvin}',
+ bar: '{Simon}'
+ });
+
+ expect(str1).to.eql('Hello Vlad and Dave!');
+ expect(str2).to.eql('Hello {Calvin} and {Simon}!');
+ });
it('does not modify text without a token variable', function () {
expect(L.Util.template('foo', {})).to.eql('foo');
});
diff --git a/src/core/Util.js b/src/core/Util.js
index 8fb9947..49117e5 100644
--- a/src/core/Util.js
+++ b/src/core/Util.js
@@ -104,17 +104,33 @@ L.Util = {
}
return ((!existingUrl || existingUrl.indexOf('?') === -1) ? '?' : '&') + params.join('&');
},
-
+
+ compileTemplate: function (str, data) {
+ /*jslint evil: true */
+ //from https://gist.github.com/padolsey/6008842
+ return new Function(
+ 'o',
+ 'return "' + (
+ str.replace(/\"/g, '\\"').replace(/\{ *([\w_]+) *\}/g, function (_, $1) {
+ if (typeof data[$1] === 'function') {
+ return '" + o["' + $1 + '"](o) + "';
+ } else {
+ return '" + o["' + $1 + '"] + "';
+ }
+ })
+ ) + '";'
+ );
+ },
+
+ templateCache: {},
+
template: function (str, data) {
- return str.replace(/\{ *([\w_]+) *\}/g, function (str, key) {
- var value = data[key];
- if (value === undefined) {
- throw new Error('No value provided for variable ' + str);
- } else if (typeof value === 'function') {
- value = value(data);
- }
- return value;
- });
+ if (str in this.templateCache) {
+ return this.templateCache[str](data);
+ } else {
+ this.templateCache[str] = this.compileTemplate(str, data);
+ return this.templateCache[str](data);
+ }
},
isArray: function (obj) {
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/leaflet.git
More information about the Pkg-javascript-commits
mailing list