Created
July 10, 2022 07:56
-
-
Save c80609a/3b696546e0567cf725117fb1dd7d6f14 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
re = /([^&=]+)=?([^&]*)/g | |
decodeRE = /\+/g | |
decode = (str) -> decodeURIComponent(str.replace(decodeRE, " ")) | |
assign = (obj, keyPath, value) -> | |
for i in [0...keyPath.length] | |
key = keyPath[i] | |
obj[key] = {} if !(key in obj) | |
obj = obj[key] | |
obj[keyPath[lastKeyIndex]] = value | |
return | |
window.getQueryParamsAsObject = (query = window.location.search) -> | |
query = query.substring(query.indexOf('?') + 1) | |
params = {} | |
e | |
while e = re.exec(query) | |
k = decode(e[1]) | |
v = decode(e[2]) | |
if k.substring(k.length - 2) == '[]' | |
k = k.substring(0, k.length - 2) | |
(params[k] || (params[k] = [])).push(v) | |
else | |
params[k] = v | |
for prop in params | |
structure = prop.split('[') | |
if structure.length > 1 | |
levels = [] | |
structure.forEach((item, i) -> | |
key = item.replace(/[?[\]\\ ]/g, '') | |
levels.push(key) | |
) | |
assign(params, levels, params[prop]) | |
delete(params[prop]) | |
return params | |
# query = "?utf8=%E2%9C%93&q%5Bdate_range%5D=01.01.2022+-+10.07.2022&q%5Buser_id_eq%5D=&by_login=&by_name=&q%5Bprogram_id_eq%5D=&q%5Bcountry_id_in%5D%5B%5D=&q%5Bclick_type%5D=&q%5Bwith_leads%5D=0&q%5Bshow_with_negative_leads%5D=0&q%5Bonly_with_data%5D=0&q%5Bpreland_shown%5D=0&commit=Find&q%5Buniq_id_eq%5D=&q%5Bip_s%5D=&q%5Bcpa_id_eq%5D=&q%5Bhostname_cont%5D=&q%5Bpreland_template_id_eq%5D=&q%5Bpreland_id_eq%5D=&q%5Bsmart_template_id_eq%5D=&by_clicked=&by_webmaster_click_uid=&by_source=&by_sub1=&by_sub2=&by_sub3=&by_sub4=&by_sub5=&by_preland_locale_id%5B%5D=1&by_preland_locale_id%5B%5D=2&q%5Boperator_id_in%5D%5B%5D=&q%5Bnode_id_eq%5D=" | |
# result = getQueryParamsAsObject(query) | |
# console.log(JSON.stringify(result, null, 2) | |
# { | |
# "utf8": "✓", | |
# "q[date_range]": "01.01.2022 - 10.07.2022", | |
# "q[user_id_eq]": "", | |
# "by_login": "", | |
# "by_name": "", | |
# "q[program_id_eq]": "", | |
# "q[country_id_in]": [ | |
# "" | |
# ], | |
# "q[click_type]": "", | |
# "q[with_leads]": "0", | |
# "q[show_with_negative_leads]": "0", | |
# "q[only_with_data]": "0", | |
# "q[preland_shown]": "0", | |
# "commit": "Find", | |
# "q[uniq_id_eq]": "", | |
# "q[ip_s]": "", | |
# "q[cpa_id_eq]": "", | |
# "q[hostname_cont]": "", | |
# "q[preland_template_id_eq]": "", | |
# "q[preland_id_eq]": "", | |
# "q[smart_template_id_eq]": "", | |
# "by_clicked": "", | |
# "by_webmaster_click_uid": "", | |
# "by_source": "", | |
# "by_sub1": "", | |
# "by_sub2": "", | |
# "by_sub3": "", | |
# "by_sub4": "", | |
# "by_sub5": "", | |
# "by_preland_locale_id": [ | |
# "1", | |
# "2" | |
# ], | |
# "q[operator_id_in]": [ | |
# "" | |
# ], | |
# "q[node_id_eq]": "" | |
# } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment