summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--luigi-template.js35
-rw-r--r--test.js12
2 files changed, 28 insertions, 19 deletions
diff --git a/luigi-template.js b/luigi-template.js
index 2073a0f..df7e150 100644
--- a/luigi-template.js
+++ b/luigi-template.js
@@ -116,9 +116,7 @@ LuigiTemplate = (function() {
return a.reduce(fn, iv);
};
} else {
- return function(a, fn, iv) {
- var r = iv;
-
+ return function(a, fn, r) {
each(a, function(v, i) {
r = fn(r, v, i, a);
});
@@ -132,7 +130,7 @@ LuigiTemplate = (function() {
var trim = (function() {
if (String.prototype.trim) {
return function(s) {
- return s.trim();
+ return (s || '').trim();
};
} else {
var re = /^\s+|\s+$/g;
@@ -197,8 +195,8 @@ LuigiTemplate = (function() {
};
var RES = {
- actions: /%\{\s*([^\s\|\}]+)\s*((\s*\|(\s*[^\s\|\}]+)+)*)\s*\}|([^%]|%)/g,
- filter: /(\S+)((\s*\S+)*)\s*/g,
+ actions: /%\{\s*([^\s\|\}]+)\s*((\s*\|(\s*[^\s\|\}]+)+)*)\s*\}|([^%]+|%)/g,
+ filter: /(\S+)((\s*\S+)*)\s*/,
delim_filters: /\s*\|\s*/,
delim_args: /\s+/
};
@@ -237,9 +235,11 @@ LuigiTemplate = (function() {
if (!m)
throw new Error('invalid filter: ' + f);
+ var as = trim(m[2]);
+
r.push({
name: m[1],
- args: trim(m[2]).split(RES.delim_args)
+ args: as.length ? as.split(RES.delim_args) : []
});
});
@@ -255,13 +255,22 @@ LuigiTemplate = (function() {
function run(o) {
var i, l, f, fs, me = this;
- return map(this.actions, function(row) {
- if (!row.key in o)
- throw new Error('missing key: ' + row.key)
+ print(JSON.stringify(this.actions));
- return reduce(row.filters, function(r, f) {
- return FILTERS[f.name](r, f.args, o, this);
- }, o[row.key]);
+ return map(this.actions, function(row) {
+ if (row.type == 'text') {
+ return row.text;
+ } else if (row.type == 'action') {
+ if (!row.key in o)
+ throw new Error('missing key: ' + row.key)
+
+ return reduce(row.filters, function(r, f) {
+ return FILTERS[f.name](r, f.args, o, this);
+ }, o[row.key]);
+ } else {
+ /* never reached */
+ throw new Error('BUG: invalid type: ' + row.type);
+ }
}).join('');
}
diff --git a/test.js b/test.js
index afbad18..0fedfcd 100644
--- a/test.js
+++ b/test.js
@@ -25,21 +25,21 @@ var template_str = [
"%{greet}, %{name}!",
// test filters and filters with parameters
- "Your name hashes to: %{name|hash(sha1)|uc}",
+ "Your name uppercase is: %{name|uc}",
// test custom filter
"Your custom filtered name is: %{name|custom}",
// test custom filter with arguments
- "Your custom_args name is: %{name|custom_args(foo,bar,baz)}",
+ "Your custom_args name is: %{name|custom_args foo bar baz}",
// test whitespace in filters
- "random test: %{name | hash( sha512 ) | uc }",
+ "random test: %{name | lc }",
// test pluralize filter
- 'pluralize test (0): %{count_0} %{count_0 | pluralize(item)}',
- 'pluralize test (1): %{count_1} %{count_1 | pluralize(item)}',
- 'pluralize test (10): %{count_10} %{count_10 | pluralize(item)}',
+ 'pluralize test (0): %{count_0} item%{count_0 | s}',
+ 'pluralize test (1): %{count_1} item%{count_1 | s}',
+ 'pluralize test (10): %{count_10} item%{count_10 | s}',
// terminating newline
''