Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Removed $last special variable, use reduce-join instead, or pre-bake …

…in data dictionary. Let's keep normal-template simple and fast.
  • Loading branch information...
commit 1a4943f7a129097139cf11759a3b6425db2a6a6d 1 parent e013c44
@gmosx authored
Showing with 4 additions and 8 deletions.
  1. +3 −7 lib/normal-template.js
  2. +1 −1  test/all-tests.js
View
10 lib/normal-template.js
@@ -6,10 +6,6 @@ var TOKEN_RE = new RegExp("(\{[\=\:\#\/].+?\})"),
COMMAND_RE = new RegExp("^\{[\:\/\=]");
var xpath = function (path) {
- if (path === '$last') { // special variable
- return "($last[$last.length-1])";
- }
-
if (/\||;|\$|~/.test(path)) {
throw new Error("Invalid characters in path '" + path + "'");
}
@@ -54,7 +50,7 @@ exports.filters = {
*/
exports.compile = function (src, options) {
// v = curent value, d = cursor, a = reduced array, df = default filter, res = result
- var code = ['var v,a,d = data,res = [],$last=[];'],
+ var code = ['var v,a,d = data,res = [];'],
stack = ["data"],
nesting = [], // handle tag nesting
nestingParam = [], // the arguments for the nested tags (used in reduce at the moment)
@@ -110,7 +106,7 @@ exports.compile = function (src, options) {
var depth = stack.length;
code.push('var a' + depth + ' = ' + val + ';if ((a' + depth + ' != undefined) && (a' + depth + '.length > 0)) ');
stack.unshift("a" + depth + "[i" + depth + "]");
- code.push('for (var i' + depth + ' = 0,l' + depth + ' = a' + depth + '.length; i' + depth + ' < l' + depth + '; i' + depth + '++) {$last['+(depth-1)+'] = (i' + depth + ' == l' + depth + '-1); d = a' + depth + '[i' + depth + '];');
+ code.push('for (var i' + depth + ' = 0,l' + depth + ' = a' + depth + '.length; i' + depth + ' < l' + depth + '; i' + depth + '++){d = a' + depth + '[i' + depth + '];');
continue;
case "else":
@@ -169,7 +165,7 @@ exports.compile = function (src, options) {
if (param) {
code.push('if (i' + depth + ' < (l' + depth + '-1)) res.push(' + param + ');');
}
- code.push('}; $last.pop(); d = ' + stack[0] + ';');
+ code.push('};d = ' + stack[0] + ';');
} else {
throw new Error("Unbalanced 'reduce' close tag" + (tag ? ", expecting '" + tag + "' close tag" : ""));
}
View
2  test/all-tests.js
@@ -1,6 +1,6 @@
exports.testNormalTemplate = require("./normal-template");
exports.testNormalTPP = require("./normal-template/tpp");
-exports.testLast = require("./last");
+//exports.testLast = require("./last");
if (module === require.main) {
require("test").run(exports);
Please sign in to comment.
Something went wrong with that request. Please try again.