summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpabs@pablotron.org <pabs@pablotron.org>2014-12-18 01:42:34 -0500
committerpabs@pablotron.org <pabs@pablotron.org>2014-12-18 01:42:34 -0500
commitb3c25342e7161bebfda5b3e6dae46b1243d91cdb (patch)
tree0e9df33e6f4c1eaad003ac985d15445d1cbbbbc2
parent28e962edf2d6212a85b9a68f3cf5bc74a1b3118b (diff)
downloadluigi-template-b3c25342e7161bebfda5b3e6dae46b1243d91cdb.tar.bz2
luigi-template-b3c25342e7161bebfda5b3e6dae46b1243d91cdb.zip
add each, map, and scan polyfills
-rw-r--r--luigi-template.js51
1 files changed, 50 insertions, 1 deletions
diff --git a/luigi-template.js b/luigi-template.js
index c787e6e..f36519c 100644
--- a/luigi-template.js
+++ b/luigi-template.js
@@ -74,6 +74,55 @@ LuigiTemplate = (function() {
var VERSION = '0.4.0';
+ // Array.each polyfill
+ var each = (function() {
+ if (Array.prototype.forEach) {
+ return function(a, fn) {
+ a.forEach(fn);
+ };
+ } else {
+ return function(a, fn) {
+ var i, l;
+
+ for (i = 0, l = a.length; i < l; i++)
+ fn(a[i], i, a);
+ };
+ }
+ })();
+
+ // Array.map polyfill
+ var map = (function() {
+ if (Array.prototype.map) {
+ return function(a, fn) {
+ return a.map(fn);
+ };
+ } else {
+ return function(a, fn) {
+ var r = new Array(a.length);
+
+ each(a, function(v, i) {
+ r[i] = v;
+ });
+
+ return r;
+ };
+ }
+ })();
+
+ // String.scan polyfill
+ function scan(s, re, fn) {
+ var m;
+
+ if (!re.global)
+ throw 'non-global regex';
+
+ while (m = re.exec(s)) {
+ m.shift();
+ fn(m);
+ }
+ };
+ }
+
// list of built-in filters
var FILTERS = {
uc: function(v) {
@@ -93,7 +142,7 @@ LuigiTemplate = (function() {
},
trim: function(v) {
- return (v || '').replace(/^\s+|\s+$/mg, '');
+ return (v || '').replace(/^\s+|\s+$/g, '');
},
h: (function() {