Created
April 19, 2012 14:31
-
-
Save bobtfish/2421325 to your computer and use it in GitHub Desktop.
BSON packing javascript
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 compress_simple(s) { | |
| var i=0, l, out=''; | |
| if (s.length % 2 !== 0) s += ' '; | |
| for (l = s.length; i < l; i+=2) { | |
| out += String.fromCharCode((s.charCodeAt(i)*256) + s.charCodeAt(i+1)); | |
| } | |
| return out; | |
| } | |
| function uncompress_simple(s) { | |
| var i=0, l, out=''; | |
| for (l = s.length; i < l; i+=1) { | |
| var ch = s.charCodeAt(i); | |
| var remainder = ch % 256; | |
| ch = (ch - remainder)/256; | |
| out += String.fromCharCode(ch) + String.fromCharCode(remainder); | |
| } | |
| return out; | |
| } | |
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
| <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" | |
| "http://www.w3.org/TR/html4/loose.dtd"> | |
| <html> | |
| <head> | |
| <script src="http://code.jquery.com/jquery-latest.js"></script> | |
| <link rel="stylesheet" href="http://code.jquery.com/qunit/git/qunit.css" type="text/css" media="screen" /> | |
| <script type="text/javascript" src="http://code.jquery.com/qunit/git/qunit.js"></script> | |
| <script type="text/javascript" src="code.js"></script> | |
| <script type="text/javascript" src="tests.js"></script> | |
| </head> | |
| <body> | |
| <h1 id="qunit-header">QUnit example</h1> | |
| <h2 id="qunit-banner"></h2> | |
| <div id="qunit-testrunner-toolbar"></div> | |
| <h2 id="qunit-userAgent"></h2> | |
| <ol id="qunit-tests"></ol> | |
| <div id="qunit-fixture">test markup, will be hidden</div> | |
| </body> | |
| </html> | |
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
| $(document).ready(function(){ | |
| test("compress_simple", function() { | |
| var out = compress_simple("simple"); | |
| equal( out.charCodeAt(0), "29545", "First UTF-16 char ok"); | |
| equal( out.charCodeAt(1), "28016", "Second UTF-16 char ok"); | |
| equal( out.charCodeAt(2), "27749", "Third UTF-16 char ok"); | |
| ok( !out.charCodeAt(3), "End"); | |
| }); | |
| test("uncompress_simple", function() { | |
| var out = uncompress_simple(compress_simple("simple")); | |
| equal( out, "simple" ); | |
| }); | |
| test("uneven bytes", function() { | |
| // FIXME - This fails as we always pack to an even number of bytes.. | |
| equal( uncompress_simple(compress_simple("s")), "s" ); | |
| }); | |
| // FIXME - further, if we pack JSON, then that's horribly inefficient due to all | |
| // the brackets! | |
| // We could pack into BSON which would be much more efficient for JSON | |
| // storage, and there's a demon implementation here: | |
| // https://github.com/muhmi/javascript-bson/blob/master/lib/bson.js | |
| // And then subsequently pack those bytes into UTF-16, in a similar | |
| // way to above, which would give us massive savings when packing JSON | |
| // into localstorage... | |
| }); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment