Skip to content

Commit

Permalink
invoke methods at any depth
Browse files Browse the repository at this point in the history
  • Loading branch information
davidchambers committed Apr 17, 2012
1 parent 0c2ed5d commit e648f22
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,10 @@ me = name: "David", dob: new Date "26 Apr 1984"
"{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
Expand Down
1 change: 1 addition & 0 deletions spec/stringformat_spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ describe 'String::format', ->
'{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

Expand Down
5 changes: 4 additions & 1 deletion string-format.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ lookup = (object, key) ->
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

Expand Down

0 comments on commit e648f22

Please sign in to comment.