module Rack
module Utils
def append_to_query_string(url, params = {})
base, query = url.split('?')
params = parse_query(query).merge(params.stringify_keys)
params = params.sort_by { |k, _v| k.to_s }.to_h
query = build_query(params)
[base, query].reject(&:blank?).join('?')
end
module_function :append_to_query_string
end
end
window.BLAHUtils = {
isPresent: function(object) {
return object != undefined && object.toString().trim() !== ''
},
presense: function(object) {
return this.isPresent(object) ? object : null
},
appendToQueryString: function(url, params = {}) {
[base, query] = url.split('?')
params = {
...this.parseQuery(query),
...params
}
query = this.buildQuery(params)
return [base, query].filter(v => this.isPresent(v)).join('?')
},
parseQuery: function(query) {
if (!this.isPresent(query)) {
return {}
}
return query.split('&').reduce((h, pair) => {
[k, v] = pair.split('=')
h[k] = decodeURIComponent(v)
return h
}, {})
},
buildQuery: function(params) {
return Object.entries(params).map(([k, v]) => {
return [k, encodeURIComponent(v)].join('=')
}).join('&')
}
}