Skip to content

Instantly share code, notes, and snippets.

@oelmekki
Created April 3, 2014 15:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oelmekki/9956259 to your computer and use it in GitHub Desktop.
Save oelmekki/9956259 to your computer and use it in GitHub Desktop.
class App.Url
parser_regexp: /^(.*:\/\/)([^:\/]+)(:[^\/]+)?(.*?)(\?.*?)?(#.*)?$/
constructor: ( initial ) ->
initial.replace( @parser_regexp, ( match, @_protocol, @_domain, @_port, @_path, @_params, @_anchor ) => )
@protocol = if @_protocol then @_protocol.replace( /:\/\//, '' ) else ''
@domain = @_domain or ''
@port = if @_port then @_port.replace( /:/, '' ) else ''
@path = @_path or ''
@params = if @_params then @_params.replace( /^\?/, '' ) else ''
@anchor = if @_anchor then @_anchor.replace( /#/, '' ) else ''
get_params: ->
params = {}
$.each( @params.split( '&' ), ( i, pair ) ->
pair.replace( /(.*)=(.*)/, ( match, name, value ) -> ( params[ name ] = value ) )
)
params
set_params: ( params ) ->
@params = ''
@params = "#{@params}#{name}=#{value}&" for own name, value of params
@params = @params.replace( /&$/, '' )
@
to_s: ->
port = if @port then ":#{@port}" else ''
params = if @params then "?#{@params}" else ''
anchor = if @anchor then "##{@anchor}" else ''
"#{@protocol}://#{@domain}#{port}#{@path}#{params}#{anchor}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment