Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
A ridiculously useful javascript string function
JavaScript
branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
test
.gitignore
.travis.yml
README.md
dotformat.js
dotformat.min.js
package.json

README.md

Build Status

Pretty much I was tired of creating string like this:

"Hello " + name + " today is "+ dayOfWeek;

Being inspired by Scala's String.format() method I decided to port that to Javascript. So now it reads like:

"Hello %s today is %s".format( name, dayOfWeek ); //Hello Emmett today is April 27, 2012

or you could use an Array:

"Hello %s today is %s".format( [name, dayOfWeek] ); //Hello Emmett today is April 27, 2012

Going one step beyond that the String.format() function also accepts objects so that same string could read:

"Hello %name% today is %dayOfWeek%".format( {name:"Emmett", dayOfWeek:"April 27, 2012"} ); //Hello Emmett today is April 27, 2012

I've found it useful for url patterns also:

"http://www.locationinsight.com?&q=%query%".format({query:"Hello+World:") // http://www.locationinsight.com?&q=Hello+World
Something went wrong with that request. Please try again.