Advertisement
Guest User

Untitled

a guest
Jan 18th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.55 KB | None | 0 0
  1. function deserializeObjectInURI(obj) {
  2. return obj.map(function(value, key) {
  3. return [ key, (_.isArray(value) ? value.join(',') : value) ].join('=');
  4. }).join('&');
  5. }
  6.  
  7. function serializeObjectInURI(str) {
  8. try {
  9.  
  10. // Retrocomp
  11. if (str.substr(0,1) === '{') {
  12. return JSON.parse(str);
  13. }
  14.  
  15. var obj = {};
  16. _.each(str.split('&'), function(keyAndValue) {
  17. keyAndValue = keyAndValue.split('=');
  18. if (keyAndValue.length != 2) throw new Error();
  19. obj[ keyAndValue[0] ] = keyAndValue[1].split(',');
  20. });
  21. return obj;
  22.  
  23. } catch (err) {
  24. return null;
  25. }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement