-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathbert.min.js
1 lines (1 loc) · 10.1 KB
/
bert.min.js
1
function BertClass(){this.BERT_START=String.fromCharCode(131);this.SMALL_ATOM=String.fromCharCode(115);this.ATOM=String.fromCharCode(100);this.BINARY=String.fromCharCode(109);this.SMALL_INTEGER=String.fromCharCode(97);this.INTEGER=String.fromCharCode(98);this.SMALL_BIG=String.fromCharCode(110);this.LARGE_BIG=String.fromCharCode(111);this.FLOAT=String.fromCharCode(99);this.STRING=String.fromCharCode(107);this.LIST=String.fromCharCode(108);this.SMALL_TUPLE=String.fromCharCode(104);this.LARGE_TUPLE=String.fromCharCode(105);this.NIL=String.fromCharCode(106);this.ZERO=String.fromCharCode(0)}function BertAtom(a){this.type="Atom";this.value=a;this.toString=function(){return a}}function BertBinary(a){this.type="Binary";this.value=a;this.toString=function(){return'<<"'+a+'">>'}}function BertTuple(b){this.type="Tuple";this.length=b.length;this.value=b;for(var a=0;a<b.length;a++){this[a]=b[a]}this.toString=function(){var c,d="";for(c=0;c<this.length;c++){if(d!==""){d+=", "}d+=this[c].toString()}return"{"+d+"}"}}BertClass.prototype.encode=function(a){return this.BERT_START+this.encode_inner(a)};BertClass.prototype.encode_to_bytearray=function(c){var a=this.encode(c);var e=a.length;var d=new Uint8Array(e);for(var b=0;b<e;b++){d[b]=a.charCodeAt(b)}return d};BertClass.prototype.encode_to_base64=function(b){var a=this.encode(b);return this.base64_encode(a)};BertClass.prototype.decode=function(b){if(b[0]!==this.BERT_START){throw ("Not a valid BERT.")}var a=this.decode_inner(b.substring(1));if(a.rest!==""){throw ("Invalid BERT.")}return a.value};BertClass.prototype.atom=function(a){return new BertAtom(a)};BertClass.prototype.binary=function(a){return new BertBinary(a)};BertClass.prototype.tuple=function(){return new BertTuple(arguments)};BertClass.prototype.key_encoding=BertClass.prototype.atom;BertClass.prototype.assoc_array_key_encoding=function(a){if(a=="atom"){this.key_encoding=this.atom}else{if(a=="binary"){this.key_encoding=this.binary}}};BertClass.prototype.encode_inner=function(a){var b="encode_"+typeof(a);return this[b](a)};BertClass.prototype.encode_string=function(a){if(a.length>65535||this.string_has_unicode(a)){return this.encode_unicode_string(a)}else{return this.encode_latin1_string(a)}};BertClass.prototype.string_has_unicode=function(a){for(i=0;i<a.length;i++){if(a.charCodeAt(i)>=256){return true}}return false};BertClass.prototype.encode_latin1_string=function(a){return this.STRING+this.int_to_bytes(a.length,2)+a};BertClass.prototype.encode_unicode_string=function(b){var a,c=this.LIST+this.int_to_bytes(b.length,4);for(a=0;a<b.length;a++){c+=this.encode_number(b.charCodeAt(a))}return c+this.NIL};BertClass.prototype.encode_boolean=function(a){if(a){return this.encode_inner(this.atom("true"))}else{return this.encode_inner(this.atom("false"))}};BertClass.prototype.encode_undefined=function(a){return this.encode_inner(this.atom("undefined"))};BertClass.prototype.encode_number=function(a){var b,c=(a%1===0);if(!c){return this.encode_float(a)}if(c&&a>=0&&a<256){return this.SMALL_INTEGER+this.int_to_bytes(a,1)}if(c&&a>=-134217728&&a<=134217727){return this.INTEGER+this.int_to_bytes(a,4)}b=this.bignum_to_bytes(a);if(b.length<256){return this.SMALL_BIG+this.int_to_bytes(b.length-1,1)+b}else{return this.LARGE_BIG+this.int_to_bytes(b.length-1,4)+b}};BertClass.prototype.encode_float=function(a){var b=a.toExponential();while(b.length<31){b+=this.ZERO}return this.FLOAT+b};BertClass.prototype.encode_object=function(a){if(a.type==="Atom"){return this.encode_atom(a)}if(a.type==="Binary"){return this.encode_binary(a)}if(a.type==="Tuple"){return this.encode_tuple(a)}if(a.constructor.toString().indexOf("Array")!==-1){return this.encode_array(a)}return this.encode_associative_array(a)};BertClass.prototype.encode_atom=function(a){return this.ATOM+this.int_to_bytes(a.value.length,2)+a.value};BertClass.prototype.encode_binary=function(a){return this.BINARY+this.int_to_bytes(a.value.length,4)+a.value};BertClass.prototype.encode_tuple=function(b){var a,c="";if(b.length<256){c+=this.SMALL_TUPLE+this.int_to_bytes(b.length,1)}else{c+=this.LARGE_TUPLE+this.int_to_bytes(b.length,4)}for(a=0;a<b.length;a++){c+=this.encode_inner(b[a])}return c};BertClass.prototype.encode_array=function(b){var a,c=this.LIST+this.int_to_bytes(b.length,4);for(a=0;a<b.length;a++){c+=this.encode_inner(b[a])}c+=this.NIL;return c};BertClass.prototype.encode_associative_array=function(b){var a,c=[];for(a in b){if(b.hasOwnProperty(a)){c.push(this.tuple(this.key_encoding(a),b[a]))}}return this.encode_array(c)};BertClass.prototype.decode_inner=function(b){var a=b[0];b=b.substring(1);switch(a){case this.SMALL_ATOM:return this.decode_atom(b,1);case this.ATOM:return this.decode_atom(b,2);case this.BINARY:return this.decode_binary(b);case this.SMALL_INTEGER:return this.decode_integer(b,1);case this.INTEGER:return this.decode_integer(b,4);case this.SMALL_BIG:return this.decode_big(b,1);case this.LARGE_BIG:return this.decode_big(b,4);case this.FLOAT:return this.decode_float(b);case this.STRING:return this.decode_string(b);case this.LIST:return this.decode_list(b);case this.SMALL_TUPLE:return this.decode_tuple(b,1);case this.LARGE_TUPLE:return this.decode_large_tuple(b,4);case this.NIL:return this.decode_nil(b);default:throw ("Unexpected BERT type: "+b.charCodeAt(0))}};BertClass.prototype.decode_atom=function(d,b){var a,c;a=this.bytes_to_int(d,b);d=d.substring(b);c=d.substring(0,a);if(c==="true"){c=true}else{if(c==="false"){c=false}}return{value:this.atom(c),rest:d.substring(a)}};BertClass.prototype.decode_binary=function(b){var a=this.bytes_to_int(b,4);b=b.substring(4);return{value:this.binary(b.substring(0,a)),rest:b.substring(a)}};BertClass.prototype.decode_integer=function(c,a){var b=this.bytes_to_int(c,a);c=c.substring(a);return{value:b,rest:c}};BertClass.prototype.decode_big=function(d,b){var a,c;a=this.bytes_to_int(d,b);d=d.substring(b);c=this.bytes_to_bignum(d,a);return{value:c,rest:d.substring(a+1)}};BertClass.prototype.decode_float=function(b){var a=31;return{value:parseFloat(b.substring(0,a)),rest:b.substring(a)}};BertClass.prototype.decode_string=function(b){var a=this.bytes_to_int(b,2);b=b.substring(2);return{value:b.substring(0,a),rest:b.substring(a)}};BertClass.prototype.decode_list=function(c){var a,b,f,d,e=[];a=this.bytes_to_int(c,4);c=c.substring(4);for(b=0;b<a;b++){f=this.decode_inner(c);e.push(f.value);c=f.rest}d=c[0];if(d!==this.NIL){throw ("List does not end with NIL!")}c=c.substring(1);return{value:e,rest:c}};BertClass.prototype.decode_tuple=function(d,b){var a,c,f,e=[];a=this.bytes_to_int(d,b);d=d.substring(b);for(c=0;c<a;c++){f=this.decode_inner(d);e.push(f.value);d=f.rest}return{value:this.tuple(e),rest:d}};BertClass.prototype.decode_nil=function(a){return{value:[],rest:a}};BertClass.prototype.int_to_bytes=function(g,d){var a,f,b,e,c="";a=(g<0);if(a){g=g*(0-1)}f=g;for(b=0;b<d;b++){e=g%256;if(a){e=255-e}c=String.fromCharCode(e)+c;g=Math.floor(g/256)}if(g>0){throw ("Argument out of range: "+f+" (Max Allowable Length: "+d+")")}return c};BertClass.prototype.bytes_to_int=function(c,d){var a,b,f,e=0;a=(c.charCodeAt(0)>128);for(b=0;b<d;b++){f=c.charCodeAt(b);if(a){f=255-f}if(e===0){e=f}else{e=e*256+f}}if(a){e=e*(0-1)}return e};BertClass.prototype.bignum_to_bytes=function(d){var a,c,b="";a=d<0;if(a){d*=-1;b+=String.fromCharCode(1)}else{b+=String.fromCharCode(0)}while(d!==0){c=d%256;b+=String.fromCharCode(c);d=Math.floor(d/256)}return b};BertClass.prototype.bytes_to_bignum=function(d,b){var a,c,f,e=0;a=(d.charCodeAt(0)===1);d=d.substring(1);for(c=b-1;c>=0;c--){f=d.charCodeAt(c);if(e===0){e=f}else{e=e*256+f}}if(a){return e*-1}return e};BertClass.prototype.bytes_to_string=function(c){var a,b="";for(a=0;a<c.length;a++){b+=String.fromCharCode(c[a])}return b};BertClass.prototype.pp_bytes=function(a){var b,c="";for(b=0;b<a.length;b++){if(c!==""){c+=","}c+=""+a.charCodeAt(b)}return"<<"+c+">>"};BertClass.prototype.pp_term=function(a){return a.toString()};BertClass.prototype.test_encode=function(){alert(this.pp_bytes(this.encode(this.atom("hello"))));alert(this.pp_bytes(this.encode(this.binary("hello"))));alert(this.pp_bytes(this.encode(true)));alert(this.pp_bytes(this.encode(42)));alert(this.pp_bytes(this.encode(5000)));alert(this.pp_bytes(this.encode(-5000)));alert(this.pp_bytes(this.encode(987654321)));alert(this.pp_bytes(this.encode(-987654321)));alert(this.pp_bytes(this.encode(3.14159)));alert(this.pp_bytes(this.encode(-3.14159)));alert(this.pp_bytes(this.encode([1,2,3])));alert(this.pp_bytes(this.encode({a:1,b:2,c:3})));alert(this.pp_bytes(this.encode(this.tuple("Hello",1))));alert(this.pp_bytes(this.encode([])));alert(this.pp_bytes(this.encode({a:this.tuple(1,2,3),b:[4,5,6]})))};BertClass.prototype.test_decode=function(){var d,c,b,a;d=this.bytes_to_string([131,108,0,0,0,4,104,2,100,0,4,97,116,111,109,100,0,6,109,121,65,116,111,109,104,2,100,0,6,98,105,110,97,114,121,109,0,0,0,9,77,121,32,66,105,110,97,114,121,104,2,100,0,4,98,111,111,108,100,0,4,116,114,117,101,104,2,100,0,6,115,116,114,105,110,103,107,0,11,72,101,108,108,111,32,116,104,101,114,101,106]);alert(this.pp_term(this.decode(d)));c=this.bytes_to_string([131,108,0,0,0,5,104,2,100,0,13,115,109,97,108,108,95,105,110,116,101,103,101,114,97,42,104,2,100,0,8,105,110,116,101,103,101,114,49,98,0,0,19,136,104,2,100,0,8,105,110,116,101,103,101,114,50,98,255,255,236,120,104,2,100,0,8,98,105,103,95,105,110,116,49,110,4,0,177,104,222,58,104,2,100,0,8,98,105,103,95,105,110,116,50,110,4,1,177,104,222,58,106]);alert(this.pp_term(this.decode(c)));b=this.bytes_to_string([131,99,45,51,46,49,52,49,53,56,57,57,57,57,57,57,57,57,57,57,56,56,50,54,50,101,43,48,48,0,0,0,0]);alert(this.pp_term(this.decode(b)));a=this.bytes_to_string([131,106]);alert(this.pp_term(this.decode(a)))};BertClass.prototype.base64_encode=function(a){if(typeof(btoa)==="function"){return btoa(a)}else{return this._base64_encode(a)}};BertClass.prototype._base64_encode=function(j){var e="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";var d,c,b,n,m,l,k,o,h=0,p=0,g="",f=[];if(!j){return j}do{d=j.charCodeAt(h++);c=j.charCodeAt(h++);b=j.charCodeAt(h++);o=d<<16|c<<8|b;n=o>>18&63;m=o>>12&63;l=o>>6&63;k=o&63;f[p++]=e.charAt(n)+e.charAt(m)+e.charAt(l)+e.charAt(k)}while(h<j.length);g=f.join("");var a=j.length%3;return(a?g.slice(0,a-3):g)+"===".slice(a||3)};var Bert=new BertClass();