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 pyth, degrees, radian and hex2rgb
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianlee44 committed Sep 24, 2013
1 parent 61c9549 commit a3bea8b
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 2 deletions.
27 changes: 25 additions & 2 deletions src/util.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ angular.module("Mac.Util", []).factory "util", [

capitalize: (string) ->
str = String(string) or ""
return str.charAt(0).toUpperCase() + str[1..]
return str.charAt(0).toUpperCase() + str.substring(1)
uncapitalize: (string) ->
str = String(string) or ""
return str.charAt(0).toLowerCase() + str[1..]
return str.charAt(0).toLowerCase() + str.substring(1)
toCamelCase: (string = "") ->
string.trim().replace /[-_\s]+(.)?/g, (match, c) -> c.toUpperCase()
toSnakeCase: (string = "") ->
Expand Down Expand Up @@ -120,6 +120,29 @@ angular.module("Mac.Util", []).factory "util", [

result

pyth: (a,b) -> Math.sqrt a*a + b*b
degrees: (radian) -> (radian * 180) / Math.PI
radian: (degrees) -> (degrees * Math.PI) / 180

hex2rgb: (hex) ->
hex = hex.substring(1) if hex.indexOf('#') is 0
hex = hex.toLowerCase()
rgb = {}

if hex.length is 3
rgb.r = hex.charAt(0) + hex.charAt(0)
rgb.g = hex.charAt(1) + hex.charAt(1)
rgb.b = hex.charAt(2) + hex.charAt(2)
else
rgb.r = hex.substring(0, 2)
rgb.g = hex.substring(2, 4)
rgb.b = hex.substring(4)

for color, value of rgb
rgb[color] = parseInt value, 16

return rgb

_urlRegex: /(?:(?:(http[s]{0,1}:\/\/)(?:(www|[\d\w\-]+)\.){0,1})|(www|[\d\w\-]+)\.)([\d\w\-]+)\.([A-Za-z]{2,6})(:[\d]*){0,1}(\/?[\d\w\-\?\,\'\/\\\+&%\$#!\=~\.]*){0,1}/i

validateUrl: (url) ->
Expand Down
37 changes: 37 additions & 0 deletions src/util.jade
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ block append content
li(mac-scroll-spy-target="convertKeysToSnakeCase"): a(href="#convertKeysToSnakeCase")
i.icon-chevron-right
| convertKeysToSnakeCase
li(mac-scroll-spy-target="pythagoras"): a(href="#pythagoras")
i.icon-chevron-right
| Pythagoras
li(mac-scroll-spy-target="degrees"): a(href="#degrees")
i.icon-chevron-right
| Radian to Degrees
li(mac-scroll-spy-target="radian"): a(href="#radian")
i.icon-chevron-right
| Degrees to Radian
li(mac-scroll-spy-target="hex2rgb"): a(href="#hex2rgb")
i.icon-chevron-right
| HEX to RGB
li(mac-scroll-spy-target="validateUrl"): a(href="#validateUrl")
i.icon-chevron-right
| validateUrl
Expand Down Expand Up @@ -93,6 +105,31 @@ block append content
| util.toSnakeCase({'helloWorld': 'test'})
| => {'hello_world': 'test'}

section#pythagoras(mac-scroll-spy-anchor)
h3 Pythagoras
pre.prettyprint(ng-non-bindable)
| util.pyth(3, 4)
| => 5

section#degrees(mac-scroll-spy-anchor)
h3 Radian to Degrees
pre.prettyprint(ng-non-bindable)
| util.degrees(Math.PI)
| => 180

section#radian(mac-scroll-spy-anchor)
h3 Degrees to Radian
pre.prettyprint(ng-non-bindable)
| util.radian(180)
| => Math.PI

section#hex2rgb(mac-scroll-spy-anchor)
h3 HEX to RGB
p Convert HEX color into RGB values
pre.prettyprint(ng-non-bindable)
| util.hex2rgb("#5331DE")
| => {r: 83, g: 49, b: 222}

section#validateUrl(mac-scroll-spy-anchor)
h3 validateUrl
p Parse url and break it into different components
Expand Down
25 changes: 25 additions & 0 deletions test/unit/util.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,29 @@ describe "Mac Util", ->
expect(util.extendAttributes "macGyver", defaults, attrsWithPrefix).toEqual
width: 30, height:20

describe "pythagoras function", ->
it "should calculate value correctly", ->
expect(util.pyth(3, 4)).toBe 5

describe "radian to degree", ->
it "should convert radian to degree correctly", ->
expect(util.degrees Math.PI).toBe 180

describe "degree to radian", ->
it "should convert degree to radian correctly", ->
expect(util.radian(180)).toBe Math.PI

describe "hex to rgb", ->

hexValues =
"3D9AEB": {r: 61, g: 154, b: 235}
"#3D9AEB": {r: 61, g: 154, b: 235}
"BAC": {r: 187, g: 170, b: 204}
"#BAC": {r: 187, g: 170, b: 204}

for hex, rgb of hexValues
it "should convert #{hex} correctly", ->
testRGB = util.hex2rgb hex
for rgbKey, rgbValue of testRGB
expect(rgbValue).toBe rgb[rgbKey]

0 comments on commit a3bea8b

Please sign in to comment.