How do I string.format() or sprintf() in coffeescript?
This seems to do the trick:
String.prototype.format = ->
args = arguments
return this.replace /{(\d+)}/g, (match, number) ->
return if typeof args[number] isnt 'undefined' then args[number] else match
Translated using this javascript from fearphage
Which can then be used like this:
fmt = "<p>{0} {1} (<a href='mailto:{2}'>{2}</a>)</p>"
mystring = fmt.format "Fred", "Flinstone", "[email protected]"
mystring would then be:
<p>Fred Flinstone (<a href='mailto:[email protected]'>[email protected]</a>)</p>
Using the #{var} approach (while perfect for example given) doesn't work with a string that needs to be recycled several times. In a looping situation for example.
HTML_header = fs.readFileSync('includes/notify/header.html').toString()
HTML_managerOpen = fs.readFileSync('includes/notify/managerOpen.html').toString()
HTML_student = fs.readFileSync('includes/notify/student.html').toString()
HTML_managerClose = fs.readFileSync('includes/notify/managerClose.html').toString()
HTML_footer = fs.readFileSync('includes/notify/footer.html').toString()
HTML_final = HTML_header
getter2 = (r, callback) ->
HTML_final += HTML_managerOpen.format r.EMAIL, r.FNAME, r.LNAME, r.STUDENTS.length, r.PHONE, r.MEMAIL, r.MFNAME, r.MLNAME
async.forEachSeries r.STUDENTS, getter3, (err) ->
HTML_final += HTML_managerClose
callback null
getter3 = (r, callback) ->
HTML_final += HTML_student.format r.EMAIL, r.FNAME, r.LNAME, r.PHONE, r.DESCRIPTION, r.ENROLLED, "", "", "", "", "", "", r.CERTEXAMSCORE, r.COIKEY
callback null
async.forEachSeries results, getter2, (err) ->
cback null, HTML_final + HTML_footer