Created
April 3, 2014 15:12
-
-
Save oelmekki/9956259 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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