Elixir → JavaScript Porting Initiative

Ah, Type.encodeMapKey is something I overlooked. That may address some of my issues. Thanks! I’ll make some progress this week and see where it takes me. I’ll keep you in the loop @Lucassifoni.

I’d be glad if you looked (when you get the time) at the current test helpers I added in the open PR, because on one hand I don’t really like adding helpers, on the other hand I’m not for shimming Elixir modules at test time to make them conform to Hologram’s behaviour (not supporting v1 sets) just for the sake of mirroring js/elixir tests.. that would amount to testing test code IMO since this limitation does not really exist elixir-side.

In retrospect I did not choose the simplest module but it’s the one I truly need to port my app to Hologram x) .

Hologram preserves the original key : (see type.mjs)

 static map(data = []) {
    const hashTableWithMetadata = data.reduce((acc, [key, value]) => {
      acc[Type.encodeMapKey(key)] = [key, value];
      return acc;
    }, {});

    return {type: "map", data: hashTableWithMetadata};
  }

So you should be good to go by going through Type @tenkiller

2 Likes

I noticed that Type.encodeMapKey() does not support string data types. Is this intentional? I can extend it to support them in a separate PR, or would you rather I include that change in my upcoming :sets.to_list/1 PR?

For reference, here is the test I would add to type_test.mjs after extending it:

it("encodes boxed string value as map key", () => {
  const string = Type.string("Hologram");
  const result = Type.encodeMapKey(string);

  assert.equal(result, "string(Hologram)");
});

Looking at your PRs now, @Lucassifoni

That’s intentional - there’s no separate primitive “string” type in Elixir at the VM level. Elixir strings are UTF-8 encoded binaries. Type.string() is only an internal helper Hologram uses for text segments inside bitstrings, not a public string type. You can use Type.bitstring() helper for text - it will convert the text to boxed bitstring type (which includes binaries and therefore UTF-8 strings).