From eb7e01397ae751431352c618df8c3800a07f4499 Mon Sep 17 00:00:00 2001 From: David Chambers Date: Mon, 3 Sep 2012 14:18:56 -0700 Subject: [PATCH] 0.2.0 --- package.json | 4 ++-- string-format.coffee | 2 +- string-format.js | 43 +++++++++++++++++++++++++++++++------------ 3 files changed, 34 insertions(+), 15 deletions(-) diff --git a/package.json b/package.json index a4c67ab..47d5953 100644 --- a/package.json +++ b/package.json @@ -10,9 +10,9 @@ }, "author": "David Chambers ", "main": "string-format.js", - "version": "0.1.0", + "version": "0.2.0", "devDependencies": { - "coffee-script": "1.2.x", + "coffee-script": "1.3.x", "jessie": "0.4.x" }, "scripts": { diff --git a/string-format.coffee b/string-format.coffee index dae0466..158dd78 100644 --- a/string-format.coffee +++ b/string-format.coffee @@ -39,4 +39,4 @@ resolve = (object, key) -> format.transformers = {} -format.version = '0.1.0' +format.version = '0.2.0' diff --git a/string-format.js b/string-format.js index 859020d..ebd475a 100644 --- a/string-format.js +++ b/string-format.js @@ -1,6 +1,7 @@ +// Generated by CoffeeScript 1.3.3 (function() { - var format, lookup, - __slice = Array.prototype.slice; + var format, lookup, resolve, + __slice = [].slice; format = String.prototype.format = function() { var args, error, explicit, idx, implicit, @@ -17,20 +18,26 @@ explicit = implicit = false; error = 'cannot switch from {} to {} numbering'.format(); return this.replace(/([{}])\1|[{](.*?)(?:!(.+?))?[}]/g, function(match, literal, key, transformer) { - var fn, value, _ref, _ref2, _ref3; - if (literal) return literal; + var fn, value, _ref, _ref1, _ref2; + if (literal) { + return literal; + } if (key.length) { explicit = true; - if (implicit) throw error('implicit', 'explicit'); + if (implicit) { + throw error('implicit', 'explicit'); + } value = (_ref = lookup(args, key)) != null ? _ref : ''; } else { implicit = true; - if (explicit) throw error('explicit', 'implicit'); - value = (_ref2 = args[idx++]) != null ? _ref2 : ''; + if (explicit) { + throw error('explicit', 'implicit'); + } + value = (_ref1 = args[idx++]) != null ? _ref1 : ''; } value = value.toString(); if (fn = format.transformers[transformer]) { - return (_ref3 = fn.call(value)) != null ? _ref3 : ''; + return (_ref2 = fn.call(value)) != null ? _ref2 : ''; } else { return value; } @@ -39,16 +46,28 @@ lookup = function(object, key) { var match; - if (!/^(\d+)([.]|$)/.test(key)) key = '0.' + key; + if (!/^(\d+)([.]|$)/.test(key)) { + key = '0.' + key; + } while (match = /(.+?)[.](.+)/.exec(key)) { - object = object[match[1]]; + object = resolve(object, match[1]); key = match[2]; } - return object[key]; + return resolve(object, key); + }; + + resolve = function(object, key) { + var value; + value = object[key]; + if (typeof value === 'function') { + return value.call(object); + } else { + return value; + } }; format.transformers = {}; - format.version = '0.1.0'; + format.version = '0.2.0'; }).call(this);