jorinvo / edn-data

EDN parser and generator that works with plain JS data, with support for TS and node streams
MIT License
96 stars 5 forks source link

Encoding a nested map? #13

Closed dharrigan closed 2 years ago

dharrigan commented 2 years ago

Hi,

I hope you don't mind me reaching out via github issues, but I'm lost as to how to encode a javascript structure (below), into edn (as a vector of vectors, with each inner vector containing a key/map pair):

javascript:

{"foo/bar": {message: "wibble"}}

expected edn:

[[:foo/bar {:message "wibble"]]

Experimenting with your library, I've got close with:

const encoded = toEDNString(
    [[
        {key: "foo/bar"},
        [{key: "message"}, "wibble"]
    ]]
);

to give:

[[:foo/bar: [:message "wibble"}]]]

I've tried a few things, with no joy, even trying this:

const encoded = toEDNString(
    [[
        {key: "foo/bar"},
        toEDNStringFromSimpleObject({message: "wibble"})
    ]]
);

to give:

[[:foo/bar "{:message \"wibble\"}"]]

And finally:

const encoded = toEDNString(
    [[
        {key: "foo/bar"},
        {map: [{key: "message"}, "wibble"]}
    ]]
);

throws an error:

node_modules/edn-data/dist/generate.js:69
            .map(([k, v]) => `${exports.toEDNString(k)} ${exports.toEDNString(v)}`)
                 ^

TypeError: object is not iterable (cannot read property Symbol(Symbol.iterator))

Would you have any suggestions on how this might be done?

Thank you for your time.

dharrigan commented 2 years ago

Hi,

After some more experimentation, figured it out:

const encoded = toEDNString(
    [[
        {key: "foo/bar"}, {map: [[{key: "message"}, "wibble"]] }
    ]]
);

😃

jorinvo commented 2 years ago

Hey @dharrigan merry christmas :tada: and great you figured out a way to transform the data structure with edn-data!

It is awesome to see people actually using the library :slightly_smiling_face: Let me know if you run into other issues!

I am closing this for now if that is alright. Otherwise just reopen.