Skip to content

Commit

Permalink
Serialize JS functions
Browse files Browse the repository at this point in the history
  • Loading branch information
bartblast committed Oct 18, 2024
1 parent 9e5c410 commit 54ff6e3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
4 changes: 4 additions & 0 deletions assets/js/serializer.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ export default class Serializer {
return `__bigint__:${value.toString()}`;
}

if (typeof value === "function") {
return `__function__:${value.toString()}`;
}

return value;
});

Expand Down
20 changes: 20 additions & 0 deletions test/javascript/serializer_test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,26 @@ describe("Serializer", () => {
});
});

describe("function", () => {
it("top-level", () => {
// prettier-ignore
const term = (param1, param2) => { const integer = Type.integer(param1); const binary = Type.bitstring('a"bc'); return Type.list([integer, binary, param2]); };

const expected = `[1,"__function__:(param1, param2) => { const integer = Type.integer(param1); const binary = Type.bitstring('a\\"bc'); return Type.list([integer, binary, param2]); }"]`;

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

it("nested", () => {
// prettier-ignore
const term = {a: (param1, param2) => { const integer = Type.integer(param1); const binary = Type.bitstring('a"bc'); return Type.list([integer, binary, param2]); }, b: 2};

const expected = `[1,{"a":"__function__:(param1, param2) => { const integer = Type.integer(param1); const binary = Type.bitstring('a\\"bc'); return Type.list([integer, binary, param2]); }","b":2}]`;

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

describe("integer", () => {
it("top-level", () => {
const term = 234;
Expand Down

0 comments on commit 54ff6e3

Please sign in to comment.