diff --git a/.gitignore b/.gitignore
index 637d92c..d911e9b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,50 @@
+# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
+# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
+
+# User-specific stuff:
.idea/
-node_modules/
\ No newline at end of file
+.idea/**/workspace.xml
+.idea/**/tasks.xml
+.idea/dictionaries
+
+# Sensitive or high-churn files:
+.idea/**/dataSources/
+.idea/**/dataSources.ids
+.idea/**/dataSources.xml
+.idea/**/dataSources.local.xml
+.idea/**/sqlDataSources.xml
+.idea/**/dynamic.xml
+.idea/**/uiDesigner.xml
+
+# Gradle:
+.idea/**/gradle.xml
+.idea/**/libraries
+
+# CMake
+cmake-build-debug/
+
+# Mongo Explorer plugin:
+.idea/**/mongoSettings.xml
+
+## File-based project format:
+*.iws
+
+## Plugin-specific files:
+
+# IntelliJ
+out/
+
+# mpeltonen/sbt-idea plugin
+.idea_modules/
+
+# JIRA plugin
+atlassian-ide-plugin.xml
+
+# Cursive Clojure plugin
+.idea/replstate.xml
+
+# Crashlytics plugin (for Android Studio and IntelliJ)
+com_crashlytics_export_strings.xml
+crashlytics.properties
+crashlytics-build.properties
+fabric.properties
\ No newline at end of file
diff --git a/README.markdown b/README.markdown
deleted file mode 100644
index 554d4d5..0000000
--- a/README.markdown
+++ /dev/null
@@ -1,98 +0,0 @@
-
BERT-JS
-
-What is BERT?
-BERT (Binary ERlang Term) is a format created by the Erlang development team for serializing Erlang terms, and promoted by Tom Preston-Werner as a way for different languages to communicate in a simple and efficient manner.
-
-Read Tom's Slides
-
-
-What is BERT JS?
-
-BERT-JS is a first cut Javascript implementation of the BERT protocol. In other words, using BERT-JS, you can serialize data into a binary format that can then be de-serialized by Erlang directly into an Erlang term. (Or, by Ruby, as Tom has written a BERT library for Ruby.)
-
-Limitations
-
-* Decoding floats is not yet supported.
-
-Interface
-
-* Bert.encode(Object) - Encode a Javascript object into BERT, return a String. The object can be a Boolean, Integer, Float, String, Array, Associative Array, or an Atom, Binary, or Tuple. (with the help of Bert.atom(), Bert.binary(), or Bert.tuple(), respectively).
-* Bert.decode(String) - Decode a BERT string into a Javascript object. Atoms, Binaries, and Tuples are special objects. See code for structure.
-* Bert.atom(String) - Create a Javascript object that will be encoded to an Atom.
-* Bert.binary(String) - Create a Javascript object that will be encoded to an Binary.
-* Bert.tuple(Element1, Element2, Element3, ...) - Create a Javascript object that will be encoded to a Tuple.
-Examples
-
-Note, below the return value is given in the form of an Erlang binary which can be fed into Erlang's binary_to_term/1. In reality, this returns a Javascript String with the ASCII values of the binary.
-
- Bert.encode(Bert.atom("hello"));
- Returns: <<131,100,0,5,104,101,108,108,111>>
- Erlang: hello
-
- Bert.encode(Bert.binary("hello"));
- Returns: <<131,109,0,0,0,5,104,101,108,108,111>>
- Erlang: <<"hello">>
-
- Bert.encode(true);
- Returns: <<131,100,0,4,116,114,117,101>>
- Erlang: true
-
- Bert.encode(42);
- Returns: <<131,97,42>>
- Erlang: 42
-
- Bert.encode(5000);
- Returns: <<131,98,0,0,19,136>>
- Erlang: 5000
-
- Bert.encode(-5000);
- Returns: <<131,98,255,255,236,120>>
- Erlang: -5000
-
- Bert.encode(987654321);
- Returns: <<131,110,4,0,177,104,222,58>>
- Erlang: 987654321
-
- Bert.encode(-987654321);
- Returns: <<131,110,4,1,177,104,222,58>>
- Erlang: -987654321
-
- Bert.encode(3.14159);
- Returns: <<131,99,51,46,49,52,49,53,57,101,43,48,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0>>
- Erlang: 3.14159
-
- Bert.encode(-3.14159);
- Returns: <<131,99,45,51,46,49,52,49,53,57,101,43,48,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0>>
- Erlang: -3.14159
-
- Bert.encode([1, 2, 3]);
- Returns: <<131,108,0,0,0,3,97,1,97,2,97,3,106>>
- Erlang: [1,2,3]
-
- Bert.encode({a:1, b:2, c:3});
- Returns: <<131,108,0,0,0,3,104,2,100,0,1,97,97,1,104,2,100,0,1,98,97,2,104,2,100,0,1,99,97,3,106>>
- Erlang: [{a,1},{b,2},{c,3}]
-
- Bert.encode(Bert.tuple("Hello", 1));
- Returns: <<131,104,2,107,0,5,72,101,108,108,111,97,1>>
- Erlang: {"Hello",1}
-
- Bert.encode({
- a : Bert.tuple(1, 2, 3),
- b : [4,5,6]
- });
- Returns: <<131,108,0,0,0,2,104,2,100,0,1,97,104,3,97,1,97,2,97,3,104,2,100,0,1,98,108,0,0,0,3,97,4,97,5,97,6,106,106>>
- Erlang: [{a,{1,2,3}},{b,[4,5,6]}]
-
- var S = Bert.bytes_to_string([131,108,0,0,0,3,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,106]);
- var Obj = Bert.decode(S);
- Object is equiv to: [{atom,myAtom},{binary,<<"My Binary">>},{bool,true},{string,"Hello there"}]
-
- var S = Bert.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]);
- var Obj = Bert.decode(S);
- Object is equiv to: [{small_integer,42},{integer1,5000},{integer2,-5000},{big_int1,987654321},{big_int2,-987654321}]
\ No newline at end of file
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..085340a
--- /dev/null
+++ b/README.md
@@ -0,0 +1,79 @@
+# BERT-JS
+
+For Other Languages
+
+* [Swift](https://github.com/softwarejoint/SwiftBert)
+* [Android](https://github.com/softwarejoint/AndroidBert)
+* [JavaScript](https://github.com/softwarejoint/JavaScriptBert)
+
+### What is BERT?
+BERT (Binary Erlang Term) is a format created by the Erlang development team for serializing Erlang terms, and promoted by Tom Preston-Werner as a way for different languages to communicate in a simple and efficient manner.
+
+[Erlang External Term Format](http://erlang.org/doc/apps/erts/erl_ext_dist.html)
+
+### What is BERT JS?
+
+BERT-JS is a first cut Javascript implementation of the BERT protocol. In other words, using BERT-JS, you can serialize data into a binary format that can then be de-serialized by Erlang directly into an Erlang term.
+
+### Limitations
+
+* Decoding floats is not yet supported.
+
+### Usage With WebSockets
+
+SetUp Bert
+
+```
+ function setUpBert() {
+ bert = new BertClass();
+ bert.encodeObjectKeysAsNumber = true;
+ bert.encodeStringAsBinary = true;
+
+
+ //decodes erlang map binary value as String for 'key'
+ bert.markDecodeBinaryAsStringForKey(key);
+ }
+
+```
+
+```
+ var WS = false;
+ if (window.WebSocket) WS = WebSocket;
+ if (!WS && window.MozWebSocket) WS = MozWebSocket;
+
+ if (!WS) { return; }
+
+ var websocket = new WS(serverUrl);
+ websocket.binaryType = "arraybuffer";
+ websocket.onmessage = onMessage;
+
+```
+
+Receive Json
+
+```
+ function onMessage(msg) {
+
+ if (typeof msg.data == "string") {
+ console.log("received data: ", msg.data);
+ return;
+ }
+
+ var array = new Uint8Array(msg.data);
+ var charData = bert.bytes_to_string(array);
+ var json = bert.decode(charData);
+
+ console.log("decoded", json);
+ }
+```
+
+Send Json
+```
+ //json = {}; //json is object
+
+ function send(json) {
+ var encoded = bert.encode(Obj);
+ var byteArray = bert.binary_to_list(encoded);
+ websocket.send(new Uint8Array(byteArray));
+ }
+```
diff --git a/bert.js b/bert.js
index fa22c24..be1fda8 100644
--- a/bert.js
+++ b/bert.js
@@ -3,14 +3,7 @@
// Contributions by Ben Browning (@bbrowning)
// See MIT-LICENSE for licensing information.
-
-// BERT-JS is a Javascript implementation of Binary Erlang Term Serialization.
-// - http://github.com/rklophaus/BERT-JS
-//
-// References:
-// - http://www.erlang-factory.com/upload/presentations/36/tom_preston_werner_erlectricity.pdf
-// - http://www.erlang.org/doc/apps/erts/erl_ext_dist.html#8
-
+// Updated for map support
// - CLASSES -
@@ -25,12 +18,15 @@ function BertClass() {
this.LARGE_BIG = String.fromCharCode(111);
this.FLOAT = String.fromCharCode(99);
this.STRING = String.fromCharCode(107);
+ this.MAP = String.fromCharCode(116);
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);
- this.ZERO_CHAR = String.fromCharCode(48);
+ this.encodeObjectKeysAsNumber = false;
+ this.encodeStringAsBinary = false;
+ this.decodeBinaryAsStringForKeys = [];
}
function BertAtom(Obj) {
@@ -49,6 +45,14 @@ function BertBinary(Obj) {
};
}
+function BertNumber(Obj) {
+ this.type = "Number";
+ this.value = Obj;
+ this.toString = function () {
+ return "<<\"" + Obj + "\">>";
+ };
+}
+
function BertTuple(Arr) {
this.type = "Tuple";
this.length = Arr.length;
@@ -70,7 +74,6 @@ function BertTuple(Arr) {
}
-
// - INTERFACE -
BertClass.prototype.encode = function (Obj) {
@@ -96,32 +99,40 @@ BertClass.prototype.binary = function (Obj) {
return new BertBinary(Obj);
};
+BertClass.prototype.number = function (Obj) {
+ return new BertNumber(Obj);
+};
+
BertClass.prototype.tuple = function () {
return new BertTuple(arguments);
};
-
-
// - ENCODING -
BertClass.prototype.encode_inner = function (Obj) {
- if (Obj === undefined) throw new Error("Cannot encode undefined values.")
+ if (Obj === undefined) throw new Error("Cannot encode undefined values.")
var func = 'encode_' + typeof(Obj);
return this[func](Obj);
};
BertClass.prototype.encode_string = function (Obj) {
- return this.STRING + this.int_to_bytes(Obj.length, 2) + Obj;
+ if (this.encodeStringAsBinary) {
+ return this.encode_binary(Obj);
+ } else {
+ return this.STRING + this.int_to_bytes(Obj.length, 2) + Obj;
+ }
+};
+
+BertClass.prototype.encode_binary = function(Obj) {
+ return this.BINARY + this.int_to_bytes(Obj.length, 4) + Obj;
};
BertClass.prototype.encode_boolean = function (Obj) {
if (Obj) {
- return this.encode_inner(
- this.tuple(this.atom("bert"), this.atom("true")));
+ return this.encode_inner(this.atom("true"));
}
else {
- return this.encode_inner(
- this.tuple(this.atom("bert"), this.atom("false")));
+ return this.encode_inner(this.atom("false"));
}
};
@@ -155,15 +166,15 @@ BertClass.prototype.encode_number = function (Obj) {
BertClass.prototype.encode_float = function (Obj) {
// float...
var s = Obj.toExponential(20);
- var match = /([^e]+)(e[+-])(\d+)/.exec(s);
- var a = match[1];
- var b = match[2];
- var c = match[3];
- var exponentialPart = c;
- if ( exponentialPart.length == 1 ) {
- exponentialPart = '0' + exponentialPart;
- }
- s = a+b+exponentialPart;
+ var match = /([^e]+)(e[+-])(\d+)/.exec(s);
+ var a = match[1];
+ var b = match[2];
+ var c = match[3];
+ var exponentialPart = c;
+ if ( exponentialPart.length == 1 ) {
+ exponentialPart = '0' + exponentialPart;
+ }
+ s = a+b+exponentialPart;
while (s.length < 31) {
s += this.ZERO;
}
@@ -173,16 +184,19 @@ BertClass.prototype.encode_float = function (Obj) {
BertClass.prototype.encode_object = function (Obj) {
// Check if it's an atom, binary, or tuple...
if (Obj === null){
- return this.encode_inner(this.atom("null"));
- }
- if (Obj.type === "Atom") {
- return this.encode_atom(Obj);
- }
- if (Obj.type === "Binary") {
- return this.encode_binary(Obj);
+ return this.encode_inner(this.atom("null"));
}
- if (Obj.type === "Tuple") {
- return this.encode_tuple(Obj);
+ switch (Obj.type) {
+ case "Atom":
+ return this.encode_atom(Obj);
+ case "Binary":
+ return this.encode_binary(Obj.value);
+ case "Tuple":
+ return this.encode_tuple(Obj);
+ case "Number":
+ return this.encode_number(Obj.value);
+ default:
+ break
}
// Check if it's an array...
@@ -191,7 +205,7 @@ BertClass.prototype.encode_object = function (Obj) {
}
// Treat the object as an associative array...
- return this.encode_associative_array(Obj);
+ return this.encode_map(Obj);
};
BertClass.prototype.encode_atom = function (Obj) {
@@ -199,7 +213,7 @@ BertClass.prototype.encode_atom = function (Obj) {
};
BertClass.prototype.encode_binary = function (Obj) {
- return this.BINARY + this.int_to_bytes(Obj.value.length, 4) + Obj.value;
+ return this.BINARY + this.int_to_bytes(Obj.length, 4) + Obj;
};
BertClass.prototype.encode_tuple = function (Obj) {
@@ -227,52 +241,69 @@ BertClass.prototype.encode_array = function (Obj) {
return s;
};
-BertClass.prototype.encode_associative_array = function (Obj) {
- var key, Arr = [];
- for (key in Obj) {
+BertClass.prototype.encode_map = function (Obj) {
+ var keys = 0;
+ var mapData = '';
+ for (var key in Obj) {
if (Obj.hasOwnProperty(key)) {
- Arr.push(this.tuple(this.atom(key), Obj[key]));
+ if (this.encodeObjectKeysAsNumber) {
+ mapData += this.encode_number(key);
+ } else {
+ mapData += this.encode_inner(key);
+ }
+
+ mapData += this.encode_inner(Obj[key]);
+ keys++;
}
}
- return this.encode_array(Arr);
+
+ return this.MAP + this.int_to_bytes(keys, 4) + mapData;
};
+// - DECODING -
+BertClass.prototype.decodeBinaryAsStringForKey = function(key) {
+ return this.decodeBinaryAsStringForKeys.indexOf(key) != -1;
+};
-// - DECODING -
+BertClass.prototype.markDecodeBinaryAsStringForKey = function (key) {
+ this.decodeBinaryAsStringForKeys.push(key);
+};
BertClass.prototype.decode_inner = function (S) {
var Type = S[0];
S = S.substring(1);
switch (Type) {
- case this.SMALL_ATOM:
- return this.decode_atom(S, 1);
- case this.ATOM:
- return this.decode_atom(S, 2);
- case this.BINARY:
- return this.decode_binary(S);
- case this.SMALL_INTEGER:
- return this.decode_small_integer(S);
- case this.INTEGER:
- return this.decode_integer(S, 4);
- case this.SMALL_BIG:
- return this.decode_big(S, 1);
- case this.LARGE_BIG:
- return this.decode_big(S, 4);
- case this.FLOAT:
- return this.decode_float(S);
- case this.STRING:
- return this.decode_string(S);
- case this.LIST:
- return this.decode_list(S);
- case this.SMALL_TUPLE:
- return this.decode_tuple(S, 1);
- case this.LARGE_TUPLE:
- return this.decode_large_tuple(S, 4);
- case this.NIL:
- return this.decode_nil(S);
- default:
- throw ("Unexpected BERT type: " + S.charCodeAt(0));
+ case this.SMALL_ATOM:
+ return this.decode_atom(S, 1);
+ case this.ATOM:
+ return this.decode_atom(S, 2);
+ case this.BINARY:
+ return this.decode_binary(S);
+ case this.SMALL_INTEGER:
+ return this.decode_small_integer(S);
+ case this.INTEGER:
+ return this.decode_integer(S, 4);
+ case this.SMALL_BIG:
+ return this.decode_big(S, 1);
+ case this.LARGE_BIG:
+ return this.decode_big(S, 4);
+ case this.FLOAT:
+ return this.decode_float(S);
+ case this.STRING:
+ return this.decode_string(S);
+ case this.LIST:
+ return this.decode_list(S);
+ case this.SMALL_TUPLE:
+ return this.decode_tuple(S, 1);
+ case this.LARGE_TUPLE:
+ return this.decode_tuple(S, 4);
+ case this.MAP:
+ return this.decode_map(S);
+ case this.NIL:
+ return this.decode_nil(S);
+ default:
+ throw ("Unexpected BERT type: " + S.charCodeAt(0));
}
};
@@ -281,8 +312,15 @@ BertClass.prototype.decode_atom = function (S, Count) {
Size = this.bytes_to_int(S, Count);
S = S.substring(Count);
Value = S.substring(0, Size);
+
+ if (Value === 'true') {
+ Value = true;
+ } else if (Value === 'false') {
+ Value = false;
+ }
+
return {
- value: this.atom(Value),
+ value: Value,
rest: S.substring(Size)
};
};
@@ -374,25 +412,25 @@ BertClass.prototype.decode_tuple = function (S, Count) {
if (Size >= 2) {
var Head = Arr[0];
if (typeof Head === 'object' && Head.type === 'Atom'
- && Head.value === "bert") {
+ && Head.value === "bert") {
var Kind = Arr[1];
if (typeof Kind !== 'object' || Kind.type !== 'Atom') {
throw ("Invalid {bert, _} tuple!");
}
switch (Kind.value) {
- case "true":
- return {value: true, rest: S};
- case "false":
- return {value: false, rest: S};
- case "nil":
- return {value: [], rest: S};
- case "time":
- case "dict":
- case "regex":
- throw ("TODO: decode " + Kind.Value);
- default:
- throw ("Invalid {bert, " +
- Kind.Value.toString() + "} tuple!");
+ case "true":
+ return {value: true, rest: S};
+ case "false":
+ return {value: false, rest: S};
+ case "nil":
+ return {value: [], rest: S};
+ case "time":
+ case "dict":
+ case "regex":
+ throw ("TODO: decode " + Kind.Value);
+ default:
+ throw ("Invalid {bert, " +
+ Kind.Value.toString() + "} tuple!");
}
}
}
@@ -402,6 +440,32 @@ BertClass.prototype.decode_tuple = function (S, Count) {
};
};
+BertClass.prototype.decode_map = function(S) {
+ var Size = this.bytes_to_int(S, 4);
+ S = S.substring(4);
+ var map = {};
+
+ for (var i = 0; i < Size; i++) {
+ var KeyEl = this.decode_inner(S);
+ S = KeyEl.rest;
+ var ValEl = this.decode_inner(S);
+ S = ValEl.rest;
+ var key = KeyEl.value;
+ var parsedVal = ValEl.value;
+
+ if (parsedVal.type === 'Binary' && this.decodeBinaryAsStringForKey(key)) {
+ parsedVal = parsedVal.value;
+ }
+
+ map[key] = parsedVal;
+ }
+
+ return {
+ value: map,
+ rest: S
+ };
+};
+
BertClass.prototype.decode_nil = function (S) {
// nil is an empty list
return {
@@ -410,8 +474,6 @@ BertClass.prototype.decode_nil = function (S) {
};
};
-
-
// - UTILITY FUNCTIONS -
// Encode an integer to a big-endian byte-string of length Length.
@@ -533,10 +595,8 @@ BertClass.prototype.pp_term = function (Obj) {
};
BertClass.prototype.binary_to_list = function (Str){
- var ret = [];
- for (var i = 0; i < Str.length; i++)
- ret.push(Str.charCodeAt(i));
- return ret;
-};
-
-module.exports = new BertClass();
+ var ret = [];
+ for (var i = 0; i < Str.length; i++)
+ ret.push(Str.charCodeAt(i));
+ return ret;
+};
\ No newline at end of file
diff --git a/package.json b/package.json
deleted file mode 100644
index 2304951..0000000
--- a/package.json
+++ /dev/null
@@ -1,22 +0,0 @@
-{
- "name": "bert-js",
- "version": "1.0.0",
- "description": "Erlang BERT format encoder/decoder for javascript",
- "main": "bert.js",
- "scripts": {
- "test": "mocha tests.js"
- },
- "repository": {
- "type": "git",
- "url": "git+https://github.com/stjepano/BERT-JS.git"
- },
- "author": "",
- "license": "ISC",
- "bugs": {
- "url": "https://github.com/stjepano/BERT-JS/issues"
- },
- "homepage": "https://github.com/stjepano/BERT-JS#readme",
- "dependencies": {
- "chai": "^3.5.0"
- }
-}
diff --git a/test.html b/test.html
deleted file mode 100644
index b2a6177..0000000
--- a/test.html
+++ /dev/null
@@ -1,11 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
diff --git a/tests.js b/tests.js
deleted file mode 100644
index 3d44b39..0000000
--- a/tests.js
+++ /dev/null
@@ -1,163 +0,0 @@
-var Bert = require('./bert.js');
-var chai = require('chai');
-
-var should = chai.should();
-var expect = chai.expect;
-
-describe('Bert', function() {
-
- it ('should encode atom', function() {
- Bert.binary_to_list(Bert.encode(Bert.atom('hello'))).should.deep.equal([
- 131,100,0,5,104,101,108,108,111
- ]);
- });
-
- it ('should encode binary', function() {
- Bert.binary_to_list(Bert.encode(Bert.binary("hello"))).should.deep.equal([
- 131,109,0,0,0,5,104,101,108,108,111
- ]);
- });
-
- it ('should encode boolean', function() {
- Bert.binary_to_list(Bert.encode(true)).should.deep.equal([
- 131,104,2,100,0,4,98,101,114,116,100,0,4,116,114,117,101
- ]);
-
- Bert.binary_to_list(Bert.encode(false)).should.deep.equal([
- 131,104,2,100,0,4,98,101,114,116,100,0,5,102,97,108,115,101
- ]);
- });
-
- it ('should encode ints', function() {
- expect(Bert.binary_to_list(Bert.encode(0))).to.deep.equal([
- 131,97,0
- ]);
-
- expect(Bert.binary_to_list(Bert.encode(-1))).to.deep.equal([
- 131,98,255,255,255,255
- ]);
-
- expect(Bert.binary_to_list(Bert.encode(42))).to.deep.equal([
- 131,97,42
- ]);
-
- expect(Bert.binary_to_list(Bert.encode(5000))).to.deep.equal([
- 131,98,0,0,19,136
- ]);
-
- expect(Bert.binary_to_list(Bert.encode(-5000))).to.deep.equal([
- 131,98,255,255,236,120
- ]);
-
- expect(Bert.binary_to_list(Bert.encode(987654321))).to.deep.equal([
- 131,110,4,0,177,104,222,58
- ]);
-
- expect(Bert.binary_to_list(Bert.encode(-987654321))).to.deep.equal([
- 131,110,4,1,177,104,222,58
- ]);
- });
-
- it ('should encode null', function() {
- expect(Bert.binary_to_list(Bert.encode(null))).to.deep.equal([
- 131,100,0,4,110,117,108,108
- ]);
- });
-
- it ('should not encode undefined', function() {
- expect(function(){
- Bert.binary_to_list(Bert.encode(undefined))
- }).to.throw("Cannot encode undefined values.")
- });
-
- it ('should encode floats', function() {
- expect(Bert.binary_to_list(Bert.encode(2.5))).to.deep.equal([
- 131,99,50,46,53,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,101,
- 43,48,48,0,0,0,0,0
- ], 'failed 2.5');
-
- expect(Bert.binary_to_list(Bert.encode(3.14159))).to.deep.equal([
- 131,99,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,0
- ], 'failed 3.14158');
-
- expect(Bert.binary_to_list(Bert.encode(-3.14159))).to.deep.equal([
- 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
- ], 'failed -3.14159');
-
- expect(Bert.binary_to_list(Bert.encode(0.0012))).to.deep.equal([
- 131,99,49,46,49,57,57,57,57,57,57,57,57,57,57,57,57,57,57,56,57,52,56,56,101,
- 45,48,51,0,0,0,0,0
- ]);
-
- expect(Bert.binary_to_list(Bert.encode(-0.0012))).to.deep.equal([
- 131,99,45,49,46,49,57,57,57,57,57,57,57,57,57,57,57,57,57,57,56,57,52,56,56,
- 101,45,48,51,0,0,0,0
- ]);
- });
-
- it ('should encode arrays', function() {
- expect(Bert.binary_to_list(Bert.encode(["1","2","3"]))).to.deep.equal([
- 131,108,0,0,0,3,107,0,1,49,107,0,1,50,107,0,1,51,106
- ]);
- });
-
- it ('should encode assoc arrays', function(){
- expect(Bert.binary_to_list(Bert.encode({a : 1, b : 2, c : 3}))).to.deep.equal([
- 131,108,0,0,0,3,104,2,100,0,1,97,97,1,104,2,100,0,1,98,97,2,104,2,100,0,1,99,97,3,106
- ])
- });
- it ('should encode tuple', function(){
- expect(Bert.binary_to_list(Bert.encode(Bert.tuple("Hello", 1)))).to.deep.equal([
- 131,104,2,107,0,5,72,101,108,108,111,97,1
- ])
- });
- it ('should encode empty list', function(){
- expect(Bert.binary_to_list(Bert.encode([]))).to.deep.equal([
- 131,104,2,100,0,4,98,101,114,116,100,0,3,110,105,108
- ])
- });
- it ('should encode complex', function(){
- expect(Bert.binary_to_list(Bert.encode({
- a : Bert.tuple(1, 2, 3),
- b : [400, 5, 6]
- }))).to.deep.equal([
- 131,108,0,0,0,2,104,2,100,0,1,97,104,3,97,1,97,2,97,3,104,2,100,0,1,98,108,0,0,0,3,98,0,0,1,144,97,5,97,6,106,106
- ])
- });
- it ('should decode complex', function(){
-
- var term = Bert.decode(Bert.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]))
- expect(Bert.pp_term(term)).to.equal('{atom, myAtom},{binary, <<"My Binary">>},{bool, true},{string, Hello there}')
-
- });
- it ('should decode small ints', function(){
- expect(Bert.decode(Bert.bytes_to_string([131,97,130]))).to.equal(130)
- })
- it ('should decode negative ints', function(){
- expect(Bert.decode(Bert.bytes_to_string([131,98,255,255,255,255]))).to.equal(-1)
- });
- it ('should decode ints', function(){
- var term = Bert.decode(Bert.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]));
- expect(Bert.pp_term(term)).to.equal('{small_integer, 42},{integer1, 5000},{integer2, -5000},{big_int1, 987654321},{big_int2, -987654321}')
- });
- it ('should decode floats', function(){
- // Try decoding this: -3.14159
- var term = Bert.decode(Bert.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]));
- expect(term).to.equal(-3.14159);
- });
- it ('should decode empty list', function(){
- var term = Bert.decode(Bert.bytes_to_string([131, 106]));
- expect(term).to.deep.equal([]);
- });
- it ('should decode true', function(){
- var term = Bert.decode(Bert.bytes_to_string([131,104,2,100,0,4,98,101,114,116,100,0,4,116,114,117,101]));
- expect(term).to.equal(true);
- })
- it ('should decode false', function(){
- var term = Bert.decode(Bert.bytes_to_string([131,104,2,100,0,4,98,101,114,116,100,0,5,102,97,108,115,101]));
- expect(term).to.equal(false);
- })
-
-});