Created
March 18, 2019 05:50
-
-
Save Abdillah/87b584829522bbadcda201b3c03fe7a4 to your computer and use it in GitHub Desktop.
Nice to have independent functions
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
| function objectToQueryStr(object) { | |
| if (typeof object !== 'object') { | |
| return ''; | |
| } | |
| return Object.keys(object).reduce(function (acc, key) { | |
| var item = key + '=' + encodeURI(object[key].toString()); | |
| if (acc.length) { | |
| return acc + '&' + item; | |
| } | |
| return acc + item; | |
| }, ''); | |
| } | |
| function queryStrToObject(qs) { | |
| if (!qs.length && typeof qs !== 'string') { | |
| return {}; | |
| } | |
| return qs.split('&').reduce(function (acc, item) { | |
| var key = item.split('=', 2)[0]; | |
| var val = item.split('=', 2)[1] || item; | |
| acc[key] = val; | |
| return acc; | |
| }, {}); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment