Created
January 27, 2014 17:56
-
-
Save hayes/8653868 to your computer and use it in GitHub Desktop.
altr diff
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/lib/filters/add.js b/lib/filters/add.js | |
index 44d3f99..2c22e8a 100644 | |
--- a/lib/filters/add.js | |
+++ b/lib/filters/add.js | |
@@ -1,12 +1,21 @@ | |
var through = require('through') | |
-module.exports = function(num) { | |
- console.log(num) | |
- num = +num | |
+module.exports = function(expr) { | |
+ var stream = through(add_lhs) | |
+ , lhs = 0 | |
+ , rhs = 0 | |
- return through(add) | |
+ this.value_stream(expr).on('data', add_rhs) | |
- function add(val) { | |
- this.queue(+val + num) | |
+ return stream | |
+ | |
+ function add_lhs(val) { | |
+ lhs = +val | |
+ this.queue(lhs + rhs) | |
+ } | |
+ | |
+ function add_rhs(val) { | |
+ rhs = +val | |
+ stream.queue(lhs + rhs) | |
} | |
} | |
\ No newline at end of file | |
diff --git a/lib/index.js b/lib/index.js | |
index deaf225..5a01c8a 100644 | |
--- a/lib/index.js | |
+++ b/lib/index.js | |
@@ -3,6 +3,8 @@ var Template = module.exports = require('./main') | |
, if_handler = require('./tags/if') | |
, value = require('./tags/value') | |
, add = require('./filters/add') | |
+ , str = require('./filters/str') | |
+ , num = require('./filters/num') | |
, html = require('./tags/html') | |
, attr = require('./tags/attr') | |
@@ -13,3 +15,5 @@ Template.add_tag('altr-if', if_handler) | |
Template.add_tag('altr-for', for_handler) | |
Template.add_filter('add', add) | |
+Template.add_filter('str', str) | |
+Template.add_filter('num', num) | |
diff --git a/lib/main.js b/lib/main.js | |
index 64b3210..a78aa60 100644 | |
--- a/lib/main.js | |
+++ b/lib/main.js | |
@@ -2,6 +2,10 @@ var text_handler = require('./text') | |
, through = require('through') | |
, lookup = require('./lookup') | |
+var filter_regexp = /^([^(]+)(?:\((.*)\)$)/ | |
+ , num_regexp = /^\d/ | |
+ , str_regexp = /^'"/ | |
+ | |
function Template(root) { | |
if(!(this instanceof Template)) { | |
return new Template(root) | |
@@ -102,10 +106,10 @@ proto.value_stream = function(key, all) { | |
var parts = key.split('|') | |
, stream | |
- stream = this.make_filter(parts[0]) | |
+ stream = this.make_stream(parts[0]) | |
for(var i = 1, len = parts.length; i < len; ++i) { | |
- stream = stream.pipe(this.make_filter(parts[i])) | |
+ stream = stream.pipe(this.make_stream(parts[i])) | |
} | |
if(all) { | |
@@ -115,21 +119,43 @@ proto.value_stream = function(key, all) { | |
return stream.pipe(no_repeat()) | |
} | |
-proto.make_filter = function(str) { | |
- var parts = str.match(/^([^(]+)(?:\((.*)\)$)?/) | |
+proto.make_stream = function(str) { | |
+ var stream | |
+ , parts | |
- if(!parts) { | |
- throw new Error('could not parse string to stream') | |
- } else if(parts[2]) { | |
- if(!cons.filters[parts[1]]) { | |
- console.log(cons.filters) | |
- throw new Error('could not find filter: ' + parts[1]) | |
- } | |
+ if(parts = str.match(filter_regexp)) { | |
+ stream = this.make_filter(parts[1], parts[2]) | |
+ } else if(str.match(num_regexp)) { | |
+ stream = this.make_literal(+str) | |
+ } else if(str.match(str_regexp)) { | |
+ stream = this.make_literal(str.slice(1, -1)) | |
+ } else { | |
+ stream = this.lookup_stream(str) | |
+ } | |
+ | |
+ return stream | |
+} | |
+ | |
+proto.make_literal = function(val) { | |
+ var stream = through(echo) | |
+ | |
+ setTimeout(function() { | |
+ stream.queue(val) | |
+ }, 0) | |
+ | |
+ return stream | |
+ | |
+ function echo() { | |
+ this.queue(val) | |
+ } | |
+} | |
- return cons.filters[parts[1]].call(this, parts[2]) | |
+proto.make_filter = function(name, args) { | |
+ if(!cons.filters[name]) { | |
+ throw new Error('could not find filter: ' + name) | |
} | |
- return this.lookup_stream(parts[0]) | |
+ return cons.filters[parts[1]].call(this, args) | |
} | |
proto.lookup_stream = function(val) { | |
diff --git a/package.json b/package.json | |
index 4af9839..abca45c 100644 | |
--- a/package.json | |
+++ b/package.json | |
@@ -1,6 +1,6 @@ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment