Last Updated: February 25, 2016
·
3.175K
· benjamincharity

Sentence case filter for AngularJS.

   # in Coffeescript
   myAngularModule.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(", ")


// The Javascript Output
myAngularModule.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(", ");
  });
});

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside