0

I am looking for (simple as possible) ways to compress a url that I am creating.

I am generating it off an object, and the object has only 2 items with values of objects with 2 items inside of that, and already it's getting pretty big. Here's what I have for reference:

For getting the url :

StateString.decode($location.search().state);

For setting it :

$location.search({state: StateString.encode(myObject)});

The Statestring just run then through this -

 return {
    encode: function(data) {
      return encodeURIComponent(JSON.stringify(data));
    },
    decode: function(searchString) {
        if(searchString){
            return JSON.parse(decodeURIComponent(searchString));
        }else{
            return
        }
    }

Just set in an angular factory for some re-use.

So - I am looking for ways to lighten the load a little with the url string, it's pretty long right now, even with just a few things inside of it. It would be nice to not have to rely on another js library for this (but if I have to, then I have to). Looking for any insight/tips on taking this on. Thanks for reading!

2
  • Is there a finite set of values that data can be? can you post an example of what data is? is the data going to a server or are you passing info around in the same application? Commented Mar 9, 2015 at 20:23
  • @Austin maybe 20-50, sorry it's hard to say. Max of like 80 possible (worst case), it is just for the front end, passing around the application via the url (I know there are better ways to do this but essentially I want the user to be able to copy a url to send another user their "state") Commented Mar 9, 2015 at 20:25

1 Answer 1

1

You could create an array where you have all the possible states and use the index as the key. To make it even smaller you can use the base 36 version of the index in the url parameter. so for 1 character (0-z) you can have 35 states and for 2 characters (0-zz) you can have 1295 states.

var encodingMap = {"{lorem:'ipsum','mykey':'myval'}","{'faefag':'veabvb'}"];
return {
    encode: function(data) {
      return encodeURIComponent(encodeMap.indexOf(JSON.stringify(data)).toString(36));
    },
    decode: function(searchString) {
        if(searchString){
            return JSON.parse(decodeURIComponent(decodeMap[parseInt(searchString,32)]));
        }else{
            return
        }
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Hmm, all of the states are dynamic though, so I might not know all possible

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.