From ed9209663b350879611043e6855c1d9d20935a2d Mon Sep 17 00:00:00 2001 From: Adrian Lee Date: Tue, 3 Dec 2013 13:41:53 -0800 Subject: [PATCH] feature(util): Added trim to util library --- src/util.coffee | 13 ++++++++++--- test/unit/util.spec.coffee | 5 +++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/util.coffee b/src/util.coffee index 9ba9cc8..bb3f85e 100644 --- a/src/util.coffee +++ b/src/util.coffee @@ -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 @@ -86,6 +86,13 @@ 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) @@ -93,9 +100,9 @@ angular.module("Mac.Util", []).factory "util", [ 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() diff --git a/test/unit/util.spec.coffee b/test/unit/util.spec.coffee index 89b02eb..529ceb1 100644 --- a/test/unit/util.spec.coffee +++ b/test/unit/util.spec.coffee @@ -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"