Skip to content

Instantly share code, notes, and snippets.

@nblackburn
Created May 15, 2024 08:28
Show Gist options
  • Save nblackburn/e5fc690e4ee080591056efdcf3b21f2b to your computer and use it in GitHub Desktop.
Save nblackburn/e5fc690e4ee080591056efdcf3b21f2b to your computer and use it in GitHub Desktop.
Query String
export const encodeQueryString = (params) => {
const keys = Object.keys(params);
const encode = (param) => {
const value = params[param];
const pair = [param];
if (value) {
pair.push(value);
}
return pair.join('=');
};
return keys.map(encode).join('&');
};
export const decodeQueryString = (query) => {
const pairs = {};
const parts = query.split('&').map((part) => part.split('='));
parts.forEach(([key, value]) => {
pairs[key] = value;
});
return pairs;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment