Created
February 28, 2014 17:46
-
-
Save benjamincharity/9275860 to your computer and use it in GitHub Desktop.
Sentance case filter for Angular. Solution below requires Underscore.js
This file contains hidden or 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
myModule.filter "sentence_case", -> | |
_.memoize (x) -> | |
return unless angular.isString(x) | |
x = x.toLowerCase() | |
capitalize = (str) -> | |
str += '' | |
return str.charAt(0).toUpperCase() + str.slice(1) | |
fmt = (y) -> | |
capitalized = capitalize($.trim(y)) | |
x = _.map(x.split("."), (z) -> fmt(z)).join(". ") | |
x = _.map(x.split("!"), (z) -> fmt(z)).join("! ") | |
x = _.map(x.split(","), (z) -> z).join(", ") |
This file contains hidden or 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
myModule.filter("sentence_case", function() { | |
return _.memoize(function(x) { | |
var capitalize, fmt; | |
if (!angular.isString(x)) { | |
return; | |
} | |
x = x.toLowerCase(); | |
capitalize = function(str) { | |
str += ''; | |
return str.charAt(0).toUpperCase() + str.slice(1); | |
}; | |
fmt = function(y) { | |
var capitalized; | |
return capitalized = capitalize($.trim(y)); | |
}; | |
x = _.map(x.split("."), function(z) { | |
return fmt(z); | |
}).join(". "); | |
x = _.map(x.split("!"), function(z) { | |
return fmt(z); | |
}).join("! "); | |
return x = _.map(x.split(","), function(z) { | |
return z; | |
}).join(", "); | |
}); | |
}); |
This file contains hidden or 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
<p>{{ myScopeVariable | sentence_case }}</p> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment