Created
June 10, 2014 18:10
-
-
Save jkodroff/ef7372406aaeba70e511 to your computer and use it in GitHub Desktop.
Some potentially useful handlebars helpers.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Handlebars.registerHelper('json', function(obj) { | |
return JSON.stringify(obj); | |
}); | |
Handlebars.registerHelper('fixed', function(obj) { | |
return obj.toFixed(2); | |
}); | |
Handlebars.registerHelper('encode', function(obj) { | |
return encodeURIComponent(obj); | |
}); | |
Handlebars.registerHelper("shortDate", function (datetime) { | |
if (!datetime) | |
return datetime; | |
// the default MVC JSON serializer does not format dates correctly: http://www.devcurry.com/2013/04/json-dates-are-different-in-aspnet-mvc.html#.Uk2xG4bXa_l | |
var parsedValue = new Date(parseInt(datetime.replace(/\/Date\((.*?)\)\//gi, "$1"))); | |
return (parsedValue.getMonth() + 1) + '/' + ("00" + parsedValue.getDate()).substr(-2) + '/' + parsedValue.getFullYear(); | |
}); | |
Handlebars.registerHelper('phoneNumber', function(obj) { | |
if (!obj) | |
return obj; | |
var digits = obj.replace(/[^\d.]/g, ""); | |
if (!digits || (digits.length != 10 && digits.length != 11)) | |
return obj; | |
if (digits.length == 11) | |
digits = digits.substring(1, 11); | |
return '(' + digits.substring(0, 3) + ') ' + digits.substring(3, 6) + '-' + digits.substring(6); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment