Skip to content

Commit

Permalink
Serialize boxed maps
Browse files Browse the repository at this point in the history
  • Loading branch information
bartblast committed Oct 17, 2024
1 parent dfcc1b2 commit 0791b3a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
4 changes: 4 additions & 0 deletions assets/js/serializer.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ export default class Serializer {
return `__integer__:${value.value.toString()}`;
}

if (value?.type === "map") {
return {...value, data: Object.values(value.data)};
}

if (typeof value === "bigint") {
return `__bigint__:${value.toString()}`;
}
Expand Down
31 changes: 30 additions & 1 deletion test/javascript/serializer_test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Type from "../../assets/js/type.mjs";

defineGlobalErlangAndElixirModules();

describe.only("Serializer", () => {
describe("Serializer", () => {
describe("serialize()", () => {
const serialize = Serializer.serialize;

Expand Down Expand Up @@ -120,6 +120,35 @@ describe.only("Serializer", () => {
assert.equal(serialize(term), expected);
});
});

describe("map", () => {
it("top-level", () => {
const term = Type.map([
[Type.atom("x"), Type.integer(1)],
[Type.bitstring("y"), Type.float(1.23)],
]);

const expected =
'[1,{"type":"map","data":[["__atom__:x","__integer__:1"],["__binary__:y","__float__:1.23"]]}]';

assert.equal(serialize(term), expected);
});

it("nested", () => {
const term = {
a: Type.map([
[Type.atom("x"), Type.integer(1)],
[Type.bitstring("y"), Type.float(1.23)],
]),
b: 2,
};

const expected =
'[1,{"a":{"type":"map","data":[["__atom__:x","__integer__:1"],["__binary__:y","__float__:1.23"]]},"b":2}]';

assert.equal(serialize(term), expected);
});
});
});

describe("JS terms", () => {
Expand Down

0 comments on commit 0791b3a

Please sign in to comment.