Last active
July 23, 2019 17:01
-
-
Save emersondemetrio/7f1b0f08eac67b41c41d414db1d4cbdb to your computer and use it in GitHub Desktop.
Convert URLs to JSON
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
const url2JSON = source => { | |
const obj = { | |
source, | |
props: {}, | |
keysList: [], | |
valuesList: [] | |
}; | |
const arr = source.split('?'); | |
if (arr.length > 1) { | |
const parts = arr[1].split('&'); | |
parts.forEach(part => { | |
const splitted = part.split('='); | |
obj.keysList.push(splitted[0]); | |
obj.valuesList.push(splitted[1]); | |
obj.props[splitted[0]] = decodeURIComponent(splitted[1]); | |
}); | |
} | |
return obj; | |
}; | |
const source = `https://www.google.com/es/?sc_src=email_3187475&sc_lid=1111&sc_uid=272222&sc_llid=11111&sc_eh=asdasdasdasd&utm_source=sysmara&utm_medium=email&utm_campaign=Linkin%20Park&utm_content=-NUEVAS%20FUNCIONES%26nbsp;-&utm_term=2019-07-16`; | |
console.log('Example', url2JSON(source)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment