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

Commit

Permalink
refactor(util): Added default values to pluralize, toCamelCase and to…
Browse files Browse the repository at this point in the history
…SnakeCase
  • Loading branch information
adrianlee44 committed Sep 16, 2013
1 parent 4f79765 commit 166bf16
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
10 changes: 5 additions & 5 deletions src/util.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ angular.module("Mac.Util", []).factory "util", [
@description
Pluralize string based on the count
@param {String} string String to pluralize
@param {String} string String to pluralize (default "")
@param {Integer} count Object counts
@param {Boolean} includeCount Include the number or not (default false)
@returns {String} Pluralized string based on the count
###
pluralize: (string, count, includeCount = false) ->
pluralize: (string = "", count, includeCount = false) ->
# If our string has no length, return without further processing
return string unless string?.trim().length > 0
return string if not angular.isString(string) or string.trim().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 @@ -92,9 +92,9 @@ angular.module("Mac.Util", []).factory "util", [
uncapitalize: (string) ->
str = String(string) or ""
return str.charAt(0).toLowerCase() + str[1..]
toCamelCase: (string) ->
toCamelCase: (string = "") ->
string.trim().replace /[-_\s]+(.)?/g, (match, c) -> c.toUpperCase()
toSnakeCase: (string) ->
toSnakeCase: (string = "") ->
string.trim().
replace(/([a-z\d])([A-Z]+)/g, "$1_$2").
replace(/[-\s]+/g, "_").
Expand Down
5 changes: 4 additions & 1 deletion test/unit/util.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ describe "Mac Util", ->

it "should not break when pluralizing empty strings or null", ->
expect(util.pluralize "").toBe ""
expect(util.pluralize null).toBe null
expect(util.pluralize null).toBe ""

it "should not break when passing parameter other than string", ->
expect(util.pluralize 23).toBe 23

it "should convert strings to camel case", ->
expect(util.toCamelCase "my_cool_string").toBe "myCoolString"
Expand Down

0 comments on commit 166bf16

Please sign in to comment.