Skip to content

Instantly share code, notes, and snippets.

@rofrol
Last active June 3, 2020 13:04
Show Gist options
  • Save rofrol/e226e396d7dda5d0d566e0abb305ae2c to your computer and use it in GitHub Desktop.
Save rofrol/e226e396d7dda5d0d566e0abb305ae2c to your computer and use it in GitHub Desktop.
Why it does not console.log cf_callType?
// Because URLSearchParams iteration omits cf_callType key when iterating, I am using this function:
// Bug reproduction: https://gist.github.com/rofrol/e226e396d7dda5d0d566e0abb305ae2c#file-urlsearchparams_confused-js
// https://stackoverflow.com/questions/8648892/how-to-convert-url-parameters-to-a-javascript-object/8649003#8649003
class URLSearchParams {
constructor(search) { this.searchParams = this.searchToSearchParams(search); }
searchToSearchParams(search) { return JSON.parse('{"' + search.replace(/^\?/, '').replace(/&/g, '","').replace(/=/g, '":"') + '"}', (key, value) => key === "" ? value : decodeURIComponent(value)) }
has(key) { return key in this.searchParams; }
get(key) { return this.searchParams[key]; }
set(key, value) { this.searchParams[key] = value; }
delete(key) { delete this.searchParams[key]; }
toString() {
return Object.entries(this.searchParams).map(([key, value]) => {
return encodeURIComponent(key) + '=' + encodeURIComponent(value)
}).join('&');
}
*[Symbol.iterator]() {
for (let tuple of Object.entries(this.searchParams)) {
yield tuple;
}
}
}
{
const callFormPrefix = 'cf_';
let searchParams = new URLSearchParams("MDN=12341234&cf_mdn=12341234&cf_callReason=R3&cf_callType=Escalation");
console.log(searchParams.toString());
for (let [key] of searchParams) {
console.log('key', key)
if (key.startsWith(callFormPrefix)) {
console.log('starts2:', key)
searchParams.delete(key);
}
}
'cf_callType'.startsWith(callFormPrefix)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment