Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

from the coding point of view I'm looking for way to pass an bit array as url parameter in a small javascript application and I want to keep the parameter length as small as possible. I have an idea how to solve it, but that seems rather complicated for a problem that seems like a common one to me.

The context is the following: I'm currently trying out angularjs and wrote a simple app that is a bit like a shopping cart / configurator. I have a fairly small number of objects (>1024) and each object has a small number of configuration options (less than 6). Someone using the app might pick ~20 of these objects in a typical scenario and configure these with the tool. I'd like to make that configuration 'bookmarkable'. The App stores this configuration as an array.

I could jsonify that array I guess and pass it as url parameter, but this would be horribly long. As indicated above - all I need for each object would be 2 bytes (10 bit for the object ID and 6 bit for its configuration). Writing a function that translates my configuration array into a bit array shouldn't be difficult. If I'm not mistaken im restricted to 32 bit integers in javascript, but that shouldn't be too much trouble either. Eventually I could translate that bitarray into an integer version and use this as url parameter, but this would still a fairly long parameter and my guess is that I could make it much shorter if I'd not only use numbers in the url parameter, but as well letters.

Since this problem more or less sounds to me like a fairly common one, I'd be very happy if you could give me snippets or hints where I could look to solve the problem. If there are more effecient ways than my idea to use an array of bit arrays that's great too. I just want to avoid creating a really clumsy solution if there should be an elegant best practice version so to speak. The whole point is to get a bit more familar with javascript / angularjs.

Cheers

share|improve this question
    
How are you getting the 10 bits of the object id and the 6 configuration bits, how are they represented before "urlifying"? –  Bergi Jan 8 '14 at 13:26
    
So far it's all in json object .. something along the lines of: var myConfig = [{133,0,0,1,0,1,0},{254,0,1,1,0,1,0}],{...} .. next I'd convert those into 16 bit arrays as described here: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… –  io_berlin Jan 8 '14 at 13:53
    
With the bit shift operations it should be fairly simple. –  io_berlin Jan 8 '14 at 14:00
    
That's not a valid json object, could it be an array? It would be nice if you could include the code. –  Bergi Jan 8 '14 at 14:01
    
I'll try to make a small version that I could post on fiddle - the code is pretty messy (I use it to test many different things). If it helps - the json object (array) is defined like that: `$scope.units = [ {'id': 123, 'option 1': 0, 'oprion 2' : 1}, {'id': 3, 'option 1': 1, 'option 2': 2} ]; –  io_berlin Jan 8 '14 at 14:19

3 Answers 3

You can try converting your numbers to hex:

> [2,10,20,30,245].map(function(x) { return (x < 16 ? "0" : "") + x.toString(16) }).join("")
"020a141ef5"

and vice versa:

> "020a141ef5".match(/../g).map(function(x) { return parseInt(x, 16) })
[2, 10, 20, 30, 245]

This isn't the most compact coding, but the easiest one to implement.

share|improve this answer
    
Thank you for the idea - not as compact as I'd like it to be, but a nice and simple starting point. –  io_berlin Jan 8 '14 at 13:24

I have a fairly small number of objects and each object has a small number of configuration options ...

For me, the model is too complicated to put all of this on URL rails.

I would create some Json object , store it into cache / localStorage with unique ID and pass in URL ID only.

On receive side, extract all stored data by ID you passed with URL.

By this way t will be easy to maintain the code and debug in case of failure.

share|improve this answer
    
I see your point - I'd like to avoid any DB for this project however and see how much / what I can do with pure a javascript solution. If I should learn that attempting such is a stupid idea - better now in such test project. –  io_berlin Jan 8 '14 at 13:39

When representing the bytes as letters, you probably will need percent encoding anyway. However, you can simply use String.fromCharCode before:

> encodeURIComponent(String.fromCharCode.apply(String, [2, 10, 20, 30, 245]))
%02%0A%14%1E%C3%B5
share|improve this answer
    
Thank you - helped me a lot. Now I know better where to look. I sometimes simply don't know the proper keywords to search for. –  io_berlin Jan 8 '14 at 14:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.