David Chambers avatar David Chambers committed 1ed19d1

invoke methods at any depth

Comments (0)

Files changed (4)

     "{name} was born in {dob.getFullYear}".format(me)
     # "David was born in 1984"
     
-    "{pop}{pop}{pop}".format(["one", "two", "three"])
-    # "threetwoone"
+    sheldon = quip: -> "Bazinga!"
+    
+    "I've always wanted to go to a goth club. {quip.toUpperCase}".format(sheldon)
+    # "I've always wanted to go to a goth club. BAZINGA!"
 
 ### String.prototype.format.transformers
 

spec/stringformat_spec.coffee

     '{0.toUpperCase}'.format('iii').should_be 'III'
     '{0.getFullYear}'.format(new Date '26 Apr 1984').should_be '1984'
     '{pop}{pop}{pop}'.format(['one', 'two', 'three']).should_be 'threetwoone'
+    '{quip.toUpperCase}'.format(quip: -> 'Bazinga!').should_be 'BAZINGA!'
 
   String::format.transformers.s = -> 's' unless +this is 1
 

string-format.coffee

   unless /^(\d+)([.]|$)/.test key
     key = '0.' + key
   while match = /(.+?)[.](.+)/.exec key
-    object = object[match[1]]
+    object = resolve object, match[1]
     key = match[2]
+  resolve object, key
+
+resolve = (object, key) ->
   value = object[key]
   if typeof value is 'function' then value.call object else value
 
 // Generated by CoffeeScript 1.3.1
 (function() {
-  var format, lookup,
+  var format, lookup, resolve,
     __slice = [].slice;
 
   format = String.prototype.format = function() {
   };
 
   lookup = function(object, key) {
-    var match, value;
+    var match;
     if (!/^(\d+)([.]|$)/.test(key)) {
       key = '0.' + key;
     }
     while (match = /(.+?)[.](.+)/.exec(key)) {
-      object = object[match[1]];
+      object = resolve(object, match[1]);
       key = match[2];
     }
+    return resolve(object, key);
+  };
+
+  resolve = function(object, key) {
+    var value;
     value = object[key];
     if (typeof value === 'function') {
       return value.call(object);
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.