Created
May 15, 2024 08:28
-
-
Save nblackburn/e5fc690e4ee080591056efdcf3b21f2b to your computer and use it in GitHub Desktop.
Query String
This file contains hidden or 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
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