Permalink
Browse files

fixed formatting bug

  • Loading branch information...
1 parent 1254f05 commit cf34e275addadcb8bbe46745390af10e976cb0f5 @mafintosh committed Aug 29, 2012
Showing with 11 additions and 8 deletions.
  1. +11 −8 formatter.js
View
@@ -1,22 +1,25 @@
-var Param = function(item) {
- this.item = item;
+var param = function(val) {
+ return function(map) {
+ return map[val];
+ };
};
-
-Param.prototype.toString = function(params) {
- return params[this.item];
+var str = function(val) {
+ return function() {
+ return val;
+ };
};
module.exports = function(format) {
if (!format) return null;
format = format.replace(/\{\*\}/g, '*').replace(/\*/g, '{*}').replace(/:(\w+)/g, '{$1}'); // normalize
format = format.match(/(?:[^\{]+)|(?:{[^\}]+\})/g).map(function(item) {
- return item[0] !== '{' ? item : new Param(item.substring(1, item.length-1));
+ return item[0] !== '{' ? str(item) : param(item.substring(1, item.length-1));
});
return function(params) {
- return format.reduce(function(item, result) {
- return result+item.toString(params);
+ return format.reduce(function(result, item) {
+ return result+item(params);
}, '');
};
};

0 comments on commit cf34e27

Please sign in to comment.