Sha256: 527233717ce46de16c45fabde0a0753b67bba841fdca439fd18a6d180e7f8f20

Contents?: true

Size: 767 Bytes

Versions: 4

Compression:

Stored size: 767 Bytes

Contents

$.buildPath = (path, params...) ->
  extraParams = {}
  paramsHash = {}

  if $.isPlainObject(lastParam = params.pop()) then paramsHash = lastParam
  else params.push(lastParam)

  $.each params, (_, value) ->
    replacement = "/#{value}"
    path = path.replace /\/:[\w_]+/, replacement

  # try to insert parameters
  $.each paramsHash, (name, value) ->
    if path.match(new RegExp(":#{name}"))
      pattern = "(.*?)\\(?([\\.\\/]?):#{name}(\\(.*?\\)|[^\\)]*)?\\)?(.*)"
      replacement = "$1$2#{value}$3$4"
      path = path.replace new RegExp(pattern), replacement
    else extraParams[name] = value

  # cleanup
  path = path.replace(/\(.*\)/, '')

  # add extra params
  path += "?#{$.param(extraParams)}" unless $.isEmptyObject(extraParams)
  
  return path

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
js_routes-0.1.5 lib/assets/javascripts/jquery.pathBuilder.js.coffee
js_routes-0.1.4 lib/assets/javascripts/jquery.pathBuilder.js.coffee
js_routes-0.1.3 lib/assets/javascripts/jquery.pathBuilder.js.coffee
js_routes-0.1.1 lib/assets/javascripts/jquery.pathBuilder.js.coffee