Skip to content
Browse files

simpler regexes for minimal

  • Loading branch information...
1 parent 21054f5 commit 0d13144421f61deda9f6aa08793237837e08b10f @chjj committed Sep 24, 2011
Showing with 17 additions and 14 deletions.
  1. +17 −14 lib/liquor_minimal.js
View
31 lib/liquor_minimal.js
@@ -8,7 +8,7 @@ var liquor = (function() {
var each = function(obj, func) {
if (!obj) return;
- if (typeof obj.length === 'number'
+ if (typeof obj.length === 'number'
&& typeof obj !== 'function') {
var i = 0
, l = obj.length;
@@ -31,26 +31,29 @@ var liquor = (function() {
}
};
- var iterate = /([ \t]*)@:([^\s]+)[ \t]*([^\n]*(?:\n+\1(?:[ ]{2}|\t)[^\n]+)*)/
- , condition = /([ \t]*)(?:\?|(!)):([^\s]+)[ \t]*([^\n]*(?:\n+\1(?:[ ]{2}|\t)[^\n]+)*)/;
+ var iterate = /( *)@:([^\s]+) *([^\n]*(?:\n+\1 {2}[^\n]+)*)/
+ , condition = /( *)(?:\?|(!)):([^\s]+) *([^\n]*(?:\n+\1 {2}[^\n]+)*)/;
return function(str, opt) {
- // normalize newlines
+ // normalize whitespace
// escape double quotes
str = str.replace(/\r\n/g, '\n')
.replace(/\r/g, '\n')
+ .replace(/\t/g, ' ')
.replace(/"/g, '\\"');
- // pre-preprocessing for shorthand
+ // pre-preprocessing for shorthand
// notations and sig-whitespace here
- while (iterate.test(str)) str = str.replace(iterate,
- '\n$1`each($2, function(v) {`$3 $1\n$1`})`'
- );
+ while (iterate.test(str))
+ str = str.replace(iterate,
+ '\n$1`each($2, function(v) {`$3 $1\n$1`})`'
+ );
- while (condition.test(str)) str = str.replace(condition,
- '\n$1`if ($2(typeof $3 !== "undefined" && $3)) \
- {`$4 $1\n$1`}`'
- );
+ while (condition.test(str))
+ str = str.replace(condition,
+ '\n$1`if ($2(typeof $3 !== "undefined" && $3)) \
+ {`$4 $1\n$1`}`'
+ );
// evaluate and interpolate
str = str.replace(/`([^`]+)`/g, '"); $1; __out.push("')
@@ -89,7 +92,7 @@ if (typeof module !== 'undefined' && module.exports) {
*/
if (!Object.keys) Object.keys = function(o) {
- var k, c = [];
+ var k, c = [];
if (o) for (k in o) if (c.hasOwnProperty.call(o, k)) c.push(k);
return c;
-};
+};

0 comments on commit 0d13144

Please sign in to comment.
Something went wrong with that request. Please try again.