0

So, I have to functions to turn a string to an object and an object to a string, however I need to account for an except and I am not sure how. Let me show you what I have

     parseObjectToUrl: function (obj){
            var myStr = "";
            var first_iteration = true;
            for (var p in obj) {

                if(first_iteration){
                    myStr += p + "=";
                   first_iteration = false; 
                }else{
                    myStr += "&" + p + "=";
                }

                tObj = obj[p];
                var first_inner = true;
                 for(t in tObj){
                    if(first_inner){
                        myStr +=  t;   
                        first_inner = false;
                      }else{
                        myStr += "," + t; 
                      }

                        yObj = tObj[t];
                       for( y in yObj){
                            myStr += "/" + yObj[y];
                       }
                 }
            }
           return myStr;
       },
       parseObjectFromUrl : function(url){
            var builtObj = {};
            //remove first slash
            url = url.slice(0, 0) + url.slice(1);
            var ch = url.split('&');
            var tempParent = {};
            for (var p in ch) {

                var tempSub = {};
                var arr = ch[p].split('=');
                var keyParent = arr[0];
                var splitInside = arr[1].split(",");

                for (var i in splitInside) {
                    var sub = splitInside[i].split('/');
                    var subKey = sub[0];
                    tempSub[subKey] = sub.slice(1);

                } 
                tempParent[keyParent] = tempSub;
            }
            return tempParent
        }

So these the string looks like

 /module1=mod1/2/3/4,mod2/2/3/4&module2=mod2/3/4/5

and the object looks like

myObj = 
{ 
 module1 : { mod1 : [2,3,4] , mod2 [2,3,4]} ,
module2 : { mod2 : [3,4,5]} 

}

So these functions work fine for me however I (unfortunately) need to be able to handle the case when the user adds an "/" into the options like -

 myObj = 
{ 
 module1 : { mod1 : [2/,3/,4/] , mod2 [2,3,4]} ,
module2 : { mod2 : [3,4,5]} 

}

I'm sure it's going to throw a wrench in my function because i'm splitting by the "/", so I'm not sure how to get around this. Would i escape the slash? How would that fit into the functions if so? Looking for any advice on this issue. Thanks!

Edit:

I was able to encode the escaped url like :

 obj.replace(/([/-])/g, "%2F");

to an escaped url, hoever I am having trouble doing the reverse of this. here is my attempt.

 obj.replace(/(%2F)/g, "/");
  • 1
    _“functions to turn a string to an object and an object to a string”_ – for what purpose? What are you trying to achieve with this eventually? Are you trying to create a “poor man’s JSON” here …? – CBroe Mar 11 '15 at 23:52
  • @CBroe a custom routing system to hold a state via the url so a user can copy/paste/bookmark the url that will save states across multiple separate modules (angular) –  Mar 11 '15 at 23:54
  • 1
    Well you could try and URL-encode the slash I suppose … – CBroe Mar 12 '15 at 00:07
  • @CBroe that's what ended up working, thanks for the advice! i am still trying to figure out "decoding" it. –  Mar 12 '15 at 00:29
  • 1
    what about urlencode & urldecode: https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/encodeURI – ChaosClown Mar 12 '15 at 00:34

1 Answers1

0

in my opinion it would be better to use url arrays, but keep in mind the characters for your url could be limited:

maximum length of HTTP GET request?

having said that one could do something like this:

module1[]=1&module1[]=2&module2[]=4&module2[]=3

this is equal to the following pseudo code:

$_GET["module1"] = array(1,2);
$_GET["module2"] = array(4,3);

and use encodeURIComponent & decodeURIComponent for your values

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent

Community
  • 1
  • 1
ChaosClown
  • 367
  • 2
  • 16