Skip to content
This repository has been archived by the owner on May 30, 2022. It is now read-only.

Commit

Permalink
feature(util): Added trim to util library
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianlee44 committed Dec 4, 2013
1 parent 84e04da commit ed92096
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/util.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ angular.module("Mac.Util", []).factory "util", [
###
pluralize: (string = "", count, includeCount = false) ->
# If our string has no length, return without further processing
return string if not angular.isString(string) or string.trim().length is 0
return string if not angular.isString(string) or @trim(string).length is 0

# If the user is expecting count to be anything other
# than the default, check if it is actually a number
Expand Down Expand Up @@ -86,16 +86,23 @@ angular.module("Mac.Util", []).factory "util", [
pluralizedString = string[0...-word.length] + pluralizedWord
if includeCount then "#{$filter("number") count} #{pluralizedString}" else pluralizedString

trim: (string) ->
str = String(string) or ""
if String::trim?
str.trim()
else
str.replace /^\s+|\s+$/gm, ""

capitalize: (string) ->
str = String(string) or ""
return str.charAt(0).toUpperCase() + str.substring(1)
uncapitalize: (string) ->
str = String(string) or ""
return str.charAt(0).toLowerCase() + str.substring(1)
toCamelCase: (string = "") ->
string.trim().replace /[-_\s]+(.)?/g, (match, c) -> c.toUpperCase()
@trim(string).replace /[-_\s]+(.)?/g, (match, c) -> c.toUpperCase()
toSnakeCase: (string = "") ->
string.trim().
@trim(string).
replace(/([a-z\d])([A-Z]+)/g, "$1_$2").
replace(/[-\s]+/g, "_").
toLowerCase()
Expand Down
5 changes: 5 additions & 0 deletions test/unit/util.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ describe "Mac Util", ->
inject ($injector) ->
util = $injector.get "util"

it "should trim string", ->
expect(util.trim " test").toBe "test"
expect(util.trim "test ").toBe "test"
expect(util.trim " test ").toBe "test"

it "should capitalize words", ->
expect(util.capitalize "hamburger").toBe "Hamburger"
expect(util.capitalize "dog house").toBe "Dog house"
Expand Down

0 comments on commit ed92096

Please sign in to comment.