Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

code formatting

  • Loading branch information...
commit 3db3bddb7c6004cd4019d04a7cc687e10133bb5a 1 parent 95035e4
@olado authored
Showing with 93 additions and 86 deletions.
  1. +43 −40 benchmarks/templating/doT.js
  2. +43 −40 doT.js
  3. +7 −6 doT.min.js
View
83 benchmarks/templating/doT.js
@@ -7,35 +7,52 @@
(function() {
"use strict";
- var doT = { version : '0.2.0' };
+ var doT = {
+ version: '0.2.0',
+ templateSettings: {
+ evaluate: /\{\{([\s\S]+?)\}\}/g,
+ interpolate: /\{\{=([\s\S]+?)\}\}/g,
+ encode: /\{\{!([\s\S]+?)\}\}/g,
+ use: /\{\{#([\s\S]+?)\}\}/g,
+ define: /\{\{##\s*([\w\.$]+)\s*(\:|=)([\s\S]+?)#\}\}/g,
+ conditional: /\{\{\?(\?)?\s*([\s\S]*?)\s*\}\}/g,
+ iterate: /\{\{~\s*(?:\}\}|([\s\S]+?)\s*\:\s*([\w$]+)\s*(?:\:\s*([\w$]+))?\s*\}\})/g,
+ varname: 'it',
+ strip: true,
+ append: true,
+ selfcontained: false
+ },
+ template: undefined, //fn, compile template
+ compile: undefined //fn, for express
+ };
- var global = (function () { return this || (0 || eval)('this'); }());
+ var global = (function(){ return this || (0||eval)('this'); }());
if (typeof module !== 'undefined' && module.exports) {
module.exports = doT;
} else if (typeof define === 'function' && define.amd) {
- define(function() { return doT; });
+ define(function(){return doT;});
} else {
global.doT = doT;
}
- doT.templateSettings = {
- evaluate: /\{\{([\s\S]+?)\}\}/g,
- interpolate: /\{\{=([\s\S]+?)\}\}/g,
- encode: /\{\{!([\s\S]+?)\}\}/g,
- use: /\{\{#([\s\S]+?)\}\}/g,
- define: /\{\{##\s*([\w\.$]+)\s*(\:|=)([\s\S]+?)#\}\}/g,
- conditional: /\{\{\?(\?)?\s*([\s\S]*?)\s*\}\}/g,
- iterate: /\{\{~\s*(?:\}\}|([\s\S]+?)\s*\:\s*([\w$]+)\s*(?:\:\s*([\w$]+))?\s*\}\})/g,
- varname: 'it',
- strip : true,
- append: true,
- selfcontained: false
- };
+ function encodeHTMLSource() {
+ var encodeHTMLRules = { "&": "&#38;", "<": "&#60;", ">": "&#62;", '"': '&#34;', "'": '&#39;', "/": '&#47;' },
+ matchHTML = /&(?!\\w+;)|<|>|\"|'|\//g;
+ return function(code) {
+ return code ? code.toString().replace(matchHTML, function(m) {return encodeHTMLRules[m] || m;}) : code;
+ };
+ }
+ global.encodeHTML = encodeHTMLSource();
+
+ var startend = {
+ append: { start: "'+(", end: ")+'", startencode: "'+encodeHTML(" },
+ split: { start: "';out+=(", end: ");out+='", startencode: "';out+=encodeHTML("}
+ }, skip = /$^/;
function resolveDefs(c, block, def) {
return ((typeof block === 'string') ? block : block.toString())
- .replace(c.define, function (m, code, assign, value) {
+ .replace(c.define || skip, function(m, code, assign, value) {
if (code.indexOf('def.') === 0) {
code = code.substring(4);
}
@@ -48,30 +65,16 @@
}
return '';
})
- .replace(c.use, function(m, code) {
+ .replace(c.use || skip, function(m, code) {
var v = eval(code);
return v ? resolveDefs(c, v, def) : v;
});
}
function unescape(code) {
- return code.replace(/\\'/g, "'").replace(/\\\\/g,"\\").replace(/[\r\t\n]/g, ' ');
+ return code.replace(/\\'/g, "'").replace(/\\\\/g,"\\").replace(/[\r\t\n]/g, ' ');
}
- function encodeHTMLSource() {
- var encodeHTMLRules = { "&": "&#38;", "<": "&#60;", ">": "&#62;", '"': '&#34;', "'": '&#39;', "/": '&#47;' },
- matchHTML = /&(?!\\w+;)|<|>|\"|'|\//g;
- return function(code) {
- return code ? code.toString().replace(matchHTML, function(m) { return encodeHTMLRules[m] || m; }) : code;
- };
- }
- global.encodeHTML = encodeHTMLSource();
-
- var startend = { // optimal choice depends on platform/size of templates
- append: { start: "'+(", end: ")+'", startencode: "'+encodeHTML(" },
- split: { start: "';out+=(", end: ");out+='", startencode: "';out+=encodeHTML("}
- };
-
doT.template = function(tmpl, c, def) {
c = c || doT.templateSettings;
var cse = c.append ? startend.append : startend.split, str, needhtmlencode, sid=0, indv;
@@ -85,25 +88,25 @@
str = ("var out='" + ((c.strip) ? str.replace(/\s*<!\[CDATA\[\s*|\s*\]\]>\s*|[\r\n\t]|(\/\*[\s\S]*?\*\/)/g, ''): str)
.replace(/\\/g, '\\\\')
.replace(/'/g, "\\'")
- .replace(c.interpolate, function(m, code) {
+ .replace(c.interpolate || skip, function(m, code) {
return cse.start + unescape(code) + cse.end;
})
- .replace(c.encode, function(m, code) {
+ .replace(c.encode || skip, function(m, code) {
needhtmlencode = true;
return cse.startencode + unescape(code) + cse.end;
})
- .replace(c.conditional, function(m, elsecase, code) {
+ .replace(c.conditional || skip, function(m, elsecase, code) {
return elsecase ?
(code ? "';}else if(" + unescape(code) + "){out+='" : "';}else{out+='") :
(code ? "';if(" + unescape(code) + "){out+='" : "';}out+='");
})
- .replace(c.iterate, function(m, iterate, vname, iname) {
+ .replace(c.iterate || skip, function(m, iterate, vname, iname) {
if (!iterate) return "'} };out+='";
- sid+=1;indv = iname || "i"+sid;iterate=unescape(iterate);
+ sid+=1; indv=iname || "i"+sid; iterate=unescape(iterate);
return "';var arr"+sid+"="+iterate+";if(arr"+sid+"){var "+indv+"=-1,l"+sid+"=arr"+sid+".length-1;while("+indv+"<l"+sid+"){"
+vname+"=arr"+sid+"["+indv+"+=1];out+='";
})
- .replace(c.evaluate, function(m, code) {
+ .replace(c.evaluate || skip, function(m, code) {
return "';" + unescape(code) + "out+='";
})
+ "';return out;")
@@ -114,7 +117,7 @@
.split("var out='';out+=").join('var out=');
if (needhtmlencode && c.selfcontained) {
- str = "var encodeHTML=("+ encodeHTMLSource.toString()+"());"+str;
+ str = "var encodeHTML=(" + encodeHTMLSource.toString() + "());" + str;
}
try {
return new Function(c.varname, str);
View
83 doT.js
@@ -7,35 +7,52 @@
(function() {
"use strict";
- var doT = { version : '0.2.0' };
+ var doT = {
+ version: '0.2.0',
+ templateSettings: {
+ evaluate: /\{\{([\s\S]+?)\}\}/g,
+ interpolate: /\{\{=([\s\S]+?)\}\}/g,
+ encode: /\{\{!([\s\S]+?)\}\}/g,
+ use: /\{\{#([\s\S]+?)\}\}/g,
+ define: /\{\{##\s*([\w\.$]+)\s*(\:|=)([\s\S]+?)#\}\}/g,
+ conditional: /\{\{\?(\?)?\s*([\s\S]*?)\s*\}\}/g,
+ iterate: /\{\{~\s*(?:\}\}|([\s\S]+?)\s*\:\s*([\w$]+)\s*(?:\:\s*([\w$]+))?\s*\}\})/g,
+ varname: 'it',
+ strip: true,
+ append: true,
+ selfcontained: false
+ },
+ template: undefined, //fn, compile template
+ compile: undefined //fn, for express
+ };
- var global = (function () { return this || (0 || eval)('this'); }());
+ var global = (function(){ return this || (0||eval)('this'); }());
if (typeof module !== 'undefined' && module.exports) {
module.exports = doT;
} else if (typeof define === 'function' && define.amd) {
- define(function() { return doT; });
+ define(function(){return doT;});
} else {
global.doT = doT;
}
- doT.templateSettings = {
- evaluate: /\{\{([\s\S]+?)\}\}/g,
- interpolate: /\{\{=([\s\S]+?)\}\}/g,
- encode: /\{\{!([\s\S]+?)\}\}/g,
- use: /\{\{#([\s\S]+?)\}\}/g,
- define: /\{\{##\s*([\w\.$]+)\s*(\:|=)([\s\S]+?)#\}\}/g,
- conditional: /\{\{\?(\?)?\s*([\s\S]*?)\s*\}\}/g,
- iterate: /\{\{~\s*(?:\}\}|([\s\S]+?)\s*\:\s*([\w$]+)\s*(?:\:\s*([\w$]+))?\s*\}\})/g,
- varname: 'it',
- strip : true,
- append: true,
- selfcontained: false
- };
+ function encodeHTMLSource() {
+ var encodeHTMLRules = { "&": "&#38;", "<": "&#60;", ">": "&#62;", '"': '&#34;', "'": '&#39;', "/": '&#47;' },
+ matchHTML = /&(?!\\w+;)|<|>|\"|'|\//g;
+ return function(code) {
+ return code ? code.toString().replace(matchHTML, function(m) {return encodeHTMLRules[m] || m;}) : code;
+ };
+ }
+ global.encodeHTML = encodeHTMLSource();
+
+ var startend = {
+ append: { start: "'+(", end: ")+'", startencode: "'+encodeHTML(" },
+ split: { start: "';out+=(", end: ");out+='", startencode: "';out+=encodeHTML("}
+ }, skip = /$^/;
function resolveDefs(c, block, def) {
return ((typeof block === 'string') ? block : block.toString())
- .replace(c.define, function (m, code, assign, value) {
+ .replace(c.define || skip, function(m, code, assign, value) {
if (code.indexOf('def.') === 0) {
code = code.substring(4);
}
@@ -48,30 +65,16 @@
}
return '';
})
- .replace(c.use, function(m, code) {
+ .replace(c.use || skip, function(m, code) {
var v = eval(code);
return v ? resolveDefs(c, v, def) : v;
});
}
function unescape(code) {
- return code.replace(/\\'/g, "'").replace(/\\\\/g,"\\").replace(/[\r\t\n]/g, ' ');
+ return code.replace(/\\'/g, "'").replace(/\\\\/g,"\\").replace(/[\r\t\n]/g, ' ');
}
- function encodeHTMLSource() {
- var encodeHTMLRules = { "&": "&#38;", "<": "&#60;", ">": "&#62;", '"': '&#34;', "'": '&#39;', "/": '&#47;' },
- matchHTML = /&(?!\\w+;)|<|>|\"|'|\//g;
- return function(code) {
- return code ? code.toString().replace(matchHTML, function(m) { return encodeHTMLRules[m] || m; }) : code;
- };
- }
- global.encodeHTML = encodeHTMLSource();
-
- var startend = { // optimal choice depends on platform/size of templates
- append: { start: "'+(", end: ")+'", startencode: "'+encodeHTML(" },
- split: { start: "';out+=(", end: ");out+='", startencode: "';out+=encodeHTML("}
- };
-
doT.template = function(tmpl, c, def) {
c = c || doT.templateSettings;
var cse = c.append ? startend.append : startend.split, str, needhtmlencode, sid=0, indv;
@@ -85,25 +88,25 @@
str = ("var out='" + ((c.strip) ? str.replace(/\s*<!\[CDATA\[\s*|\s*\]\]>\s*|[\r\n\t]|(\/\*[\s\S]*?\*\/)/g, ''): str)
.replace(/\\/g, '\\\\')
.replace(/'/g, "\\'")
- .replace(c.interpolate, function(m, code) {
+ .replace(c.interpolate || skip, function(m, code) {
return cse.start + unescape(code) + cse.end;
})
- .replace(c.encode, function(m, code) {
+ .replace(c.encode || skip, function(m, code) {
needhtmlencode = true;
return cse.startencode + unescape(code) + cse.end;
})
- .replace(c.conditional, function(m, elsecase, code) {
+ .replace(c.conditional || skip, function(m, elsecase, code) {
return elsecase ?
(code ? "';}else if(" + unescape(code) + "){out+='" : "';}else{out+='") :
(code ? "';if(" + unescape(code) + "){out+='" : "';}out+='");
})
- .replace(c.iterate, function(m, iterate, vname, iname) {
+ .replace(c.iterate || skip, function(m, iterate, vname, iname) {
if (!iterate) return "'} };out+='";
- sid+=1;indv = iname || "i"+sid;iterate=unescape(iterate);
+ sid+=1; indv=iname || "i"+sid; iterate=unescape(iterate);
return "';var arr"+sid+"="+iterate+";if(arr"+sid+"){var "+indv+"=-1,l"+sid+"=arr"+sid+".length-1;while("+indv+"<l"+sid+"){"
+vname+"=arr"+sid+"["+indv+"+=1];out+='";
})
- .replace(c.evaluate, function(m, code) {
+ .replace(c.evaluate || skip, function(m, code) {
return "';" + unescape(code) + "out+='";
})
+ "';return out;")
@@ -114,7 +117,7 @@
.split("var out='';out+=").join('var out=');
if (needhtmlencode && c.selfcontained) {
- str = "var encodeHTML=("+ encodeHTMLSource.toString()+"());"+str;
+ str = "var encodeHTML=(" + encodeHTMLSource.toString() + "());" + str;
}
try {
return new Function(c.varname, str);
View
13 doT.min.js
@@ -1,6 +1,7 @@
-(function(){function n(b,a,f){return(typeof a==="string"?a:a.toString()).replace(b.define,function(g,e,c,i){if(e.indexOf("def.")===0)e=e.substring(4);if(!(e in f))if(c===":")f[e]=i;else eval("def['"+e+"']="+i);return""}).replace(b.use,function(g,e){var c=eval(e);return c?n(b,c,f):c})}function j(b){return b.replace(/\\'/g,"'").replace(/\\\\/g,"\\").replace(/[\r\t\n]/g," ")}function o(){var b={"&":"&#38;","<":"&#60;",">":"&#62;",'"':"&#34;","'":"&#39;","/":"&#47;"},a=/&(?!\\w+;)|<|>|\"|'|\//g;return function(f){return f?
-f.toString().replace(a,function(g){return b[g]||g}):f}}var h={version:"0.2.0"},k=function(){return this||(0,eval)("this")}();if(typeof module!=="undefined"&&module.exports)module.exports=h;else if(typeof define==="function"&&define.amd)define(function(){return h});else k.doT=h;h.templateSettings={evaluate:/\{\{([\s\S]+?)\}\}/g,interpolate:/\{\{=([\s\S]+?)\}\}/g,encode:/\{\{!([\s\S]+?)\}\}/g,use:/\{\{#([\s\S]+?)\}\}/g,define:/\{\{##\s*([\w\.$]+)\s*(\:|=)([\s\S]+?)#\}\}/g,conditional:/\{\{\?(\?)?\s*([\s\S]*?)\s*\}\}/g,
-iterate:/\{\{~\s*(?:\}\}|([\s\S]+?)\s*\:\s*([\w$]+)\s*(?:\:\s*([\w$]+))?\s*\}\})/g,varname:"it",strip:true,append:true,selfcontained:false};k.encodeHTML=o();var p={append:{start:"'+(",end:")+'",startencode:"'+encodeHTML("},split:{start:"';out+=(",end:");out+='",startencode:"';out+=encodeHTML("}};h.template=function(b,a,f){a=a||h.templateSettings;var g=a.append?p.append:p.split,e,c=0,i;if(a.use||a.define){var q=k.def;k.def=f||{};b=n(a,b,k.def);k.def=q}b=("var out='"+(a.strip?b.replace(/\s*<!\[CDATA\[\s*|\s*\]\]>\s*|[\r\n\t]|(\/\*[\s\S]*?\*\/)/g,
-""):b).replace(/\\/g,"\\\\").replace(/'/g,"\\'").replace(a.interpolate,function(m,d){return g.start+j(d)+g.end}).replace(a.encode,function(m,d){e=true;return g.startencode+j(d)+g.end}).replace(a.conditional,function(m,d,l){return d?l?"';}else if("+j(l)+"){out+='":"';}else{out+='":l?"';if("+j(l)+"){out+='":"';}out+='"}).replace(a.iterate,function(m,d,l,r){if(!d)return"'} };out+='";c+=1;i=r||"i"+c;d=j(d);return"';var arr"+c+"="+d+";if(arr"+c+"){var "+i+"=-1,l"+c+"=arr"+c+".length-1;while("+i+"<l"+c+
-"){"+l+"=arr"+c+"["+i+"+=1];out+='"}).replace(a.evaluate,function(m,d){return"';"+j(d)+"out+='"})+"';return out;").replace(/\n/g,"\\n").replace(/\t/g,"\\t").replace(/\r/g,"\\r").split("out+='';").join("").split("var out='';out+=").join("var out=");if(e&&a.selfcontained)b="var encodeHTML=("+o.toString()+"());"+b;try{return new Function(a.varname,b)}catch(s){typeof console!=="undefined"&&console.log("Could not create a template function: "+b);throw s;}};h.compile=function(b,a){return h.template(b,null,
-a)}})();
+// Laura Doktorova https://github.com/olado/doT
+(function(){function o(){var b={"&":"&#38;","<":"&#60;",">":"&#62;",'"':"&#34;","'":"&#39;","/":"&#47;"},a=/&(?!\\w+;)|<|>|\"|'|\//g;return function(f){return f?f.toString().replace(a,function(g){return b[g]||g}):f}}function p(b,a,f){return(typeof a==="string"?a:a.toString()).replace(b.define||h,function(g,e,c,i){if(e.indexOf("def.")===0)e=e.substring(4);if(!(e in f))if(c===":")f[e]=i;else eval("def['"+e+"']="+i);return""}).replace(b.use||h,function(g,e){var c=eval(e);return c?p(b,c,f):c})}function k(b){return b.replace(/\\'/g,
+"'").replace(/\\\\/g,"\\").replace(/[\r\t\n]/g," ")}var j={version:"0.2.0",templateSettings:{evaluate:/\{\{([\s\S]+?)\}\}/g,interpolate:/\{\{=([\s\S]+?)\}\}/g,encode:/\{\{!([\s\S]+?)\}\}/g,use:/\{\{#([\s\S]+?)\}\}/g,define:/\{\{##\s*([\w\.$]+)\s*(\:|=)([\s\S]+?)#\}\}/g,conditional:/\{\{\?(\?)?\s*([\s\S]*?)\s*\}\}/g,iterate:/\{\{~\s*(?:\}\}|([\s\S]+?)\s*\:\s*([\w$]+)\s*(?:\:\s*([\w$]+))?\s*\}\})/g,varname:"it",strip:true,append:true,selfcontained:false},template:undefined,compile:undefined},l=function(){return this||
+(0,eval)("this")}();if(typeof module!=="undefined"&&module.exports)module.exports=j;else if(typeof define==="function"&&define.amd)define(function(){return j});else l.doT=j;l.encodeHTML=o();var q={append:{start:"'+(",end:")+'",startencode:"'+encodeHTML("},split:{start:"';out+=(",end:");out+='",startencode:"';out+=encodeHTML("}},h=/$^/;j.template=function(b,a,f){a=a||j.templateSettings;var g=a.append?q.append:q.split,e,c=0,i;if(a.use||a.define){var r=l.def;l.def=f||{};b=p(a,b,l.def);l.def=r}b=("var out='"+
+(a.strip?b.replace(/\s*<!\[CDATA\[\s*|\s*\]\]>\s*|[\r\n\t]|(\/\*[\s\S]*?\*\/)/g,""):b).replace(/\\/g,"\\\\").replace(/'/g,"\\'").replace(a.interpolate||h,function(n,d){return g.start+k(d)+g.end}).replace(a.encode||h,function(n,d){e=true;return g.startencode+k(d)+g.end}).replace(a.conditional||h,function(n,d,m){return d?m?"';}else if("+k(m)+"){out+='":"';}else{out+='":m?"';if("+k(m)+"){out+='":"';}out+='"}).replace(a.iterate||h,function(n,d,m,s){if(!d)return"'} };out+='";c+=1;i=s||"i"+c;d=k(d);return"';var arr"+
+c+"="+d+";if(arr"+c+"){var "+i+"=-1,l"+c+"=arr"+c+".length-1;while("+i+"<l"+c+"){"+m+"=arr"+c+"["+i+"+=1];out+='"}).replace(a.evaluate||h,function(n,d){return"';"+k(d)+"out+='"})+"';return out;").replace(/\n/g,"\\n").replace(/\t/g,"\\t").replace(/\r/g,"\\r").split("out+='';").join("").split("var out='';out+=").join("var out=");if(e&&a.selfcontained)b="var encodeHTML=("+o.toString()+"());"+b;try{return new Function(a.varname,b)}catch(t){typeof console!=="undefined"&&console.log("Could not create a template function: "+
+b);throw t;}};j.compile=function(b,a){return j.template(b,null,a)}})();
Please sign in to comment.
Something went wrong with that request. Please try again.