Skip to content

Commit

Permalink
Implemented the ReducesUrls class.
Browse files Browse the repository at this point in the history
  • Loading branch information
searls committed Apr 23, 2011
1 parent dabe9cf commit 4fdb872
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 7 deletions.
1 change: 1 addition & 0 deletions public/SpecRunner.html
Expand Up @@ -8,6 +8,7 @@
<script type="text/javascript" src="lib/jasmine-1.0.2/jasmine-html.js"></script>

<!-- include source files here... -->
<script type="text/javascript" src="src/vendor/underscore-min.js"></script>
<script type="text/javascript" src="src/reduces-urls.js"></script>

<!-- include spec files here... -->
Expand Down
66 changes: 60 additions & 6 deletions public/spec/reduces-urls-spec.coffee
Expand Up @@ -3,14 +3,68 @@ describe 'ReducesUrls - A class that extracts all the fun out of your URLs', ->
beforeEach -> subject = new ReducesUrls

describe '#reduce', ->
context 'a URL with a protocol and host', ->
result = null
beforeEach ->
result = subject.reduce 'http://www.pillartechnology.com'
result = null
context 'a fully loaded URL', ->
beforeEach ->
result = subject.reduce 'http://www.pillartechnology.com:4567/clients?delighted=true&really=YA_RLY'

it 'identifies the protocol', ->
expect(result.protocol).toBe('http')

it 'identifies the host', ->
expect(result.host).toBe('www.pillartechnology.com')



it 'identifies the port', ->
expect(result.port).toBe(4567)

it 'identifies the path', ->
expect(result.path).toBe('/clients')

it 'identifies the query string', ->
expect(result.queryString).toBe('delighted=true&really=YA_RLY')

describe 'the param object translated from the query string', ->
params = null
beforeEach -> params = result.params

it 'populates the first param', ->
expect(params.delighted).toBe('true')

it 'populates the second param', ->
expect(params.really).toBe('YA_RLY')

context 'a URL without a port', ->
beforeEach -> result = subject.reduce 'http://www.pillartechnology.com/index.html'

it 'defaults to port 80', ->
expect(result.port).toBe(80)

context 'a URL with a query string but no path', ->
beforeEach -> result = subject.reduce 'http://www.pillartechnology.com?you=winnar'

it 'identifies no path', ->
expect(result.path).not.toBeDefined()

it 'identifies the query string', ->
expect(result.queryString).toBe('you=winnar')

it 'translates the query string to a param object', ->
expect(result.params.you).toBe('winnar')

context 'a URL with an empty query string', ->
beforeEach -> result = subject.reduce 'http://www.pillartechnology.com/path?'

it 'identifies the query string', ->
expect(result.queryString).toBe('')

it 'builds an empty params object', ->
expect(result.params).toEqual({})

context 'a query param with no value', ->
beforeEach -> result = subject.reduce 'http://www.pillartechnology.com/path?a=&b=foo'

it 'the empty param is null on the params object', ->
expect(result.params.a).toBe(null)

it 'a subsequent param is populated', ->
expect(result.params.b).toBe('foo')
30 changes: 29 additions & 1 deletion public/src/reduces-urls.coffee
@@ -1,2 +1,30 @@
class window.ReducesUrls
reduce: -> ''
constructor: ->
@URL_REGEX = ///
(.*):// #protocol
([^:\?]*) #host
(?::([\d]*))? #[port]
(/[^\?]*)? #[path]
(?:\?(.*))? #[query string]
///

reduce: (url) ->
components = url.match(@URL_REGEX)
{
protocol: components[1],
host: components[2],
port: parseInt(components[3]) || 80
path: components[4],
queryString: components[5],
params: this._parameterize(components[5])
}

_parameterize: (queryString) ->
params = {}
if queryString then _(queryString.split('&')).each((field) ->
console.log(field)
pair = field.split('=')
params[pair[0]] = pair[1] || null
)
params

26 changes: 26 additions & 0 deletions public/src/vendor/underscore-min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4fdb872

Please sign in to comment.