borkdude / jet

CLI to transform between JSON, EDN, YAML and Transit using Clojure

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Remove , (comm) from edn

leandropincini opened this issue · comments

I'm wondering if maybe we could have a feature that removes the , (comm) while parsing to edn.
Like parse a json -> edn without commas.

Clojurian's slack thread: https://clojurians.slack.com/archives/CM5HRADAA/p1682017322255449

I've written a script once to remove commas:

borkdude@m1 ~/dev/babashka (master*) $ echo '{:a 1 :b 2}' | jet
{:a 1, :b 2}
borkdude@m1 ~/dev/babashka (master*) $ echo '{:a 1 :b 2}' | jet | /tmp/strip_commas.bb
{:a 1 :b 2}
borkdude@m1 ~/dev/babashka (master*) $ cat /tmp/strip_commas.bb
#!/usr/bin/env bb

(require '[rewrite-clj.zip :as z])

(-> (let [loc (z/of-string (slurp *in*))]
       (loop [loc loc]
         (let [next (z/next* loc)]
           (if (z/end? loc) (z/root loc)
               (if (= :comma (z/tag loc))
                 (recur (z/remove* loc))
                 (recur next))))))
    str
    (print))

(flush)

We could integrate this into jet, but I'll wait for more upvotes first.

Looks like a good code to be behind a flag to --remove-commas or something, we could even in the future consider doing it by default

Why would you do this by default?

Wow, where do all these upvotes come from, was there a campaign within nubank? :) Please explain more why this feature is considered important.

All the time I'd prefer to see a edn without commas, I suppose most people as well, but we could gather more feedback about that.

@borkdude we usually code edn without commas, like:

{:a 1
 :b 2
 :c 3}

And we use jet/jet.el to parse json<->edn to paste on code, and it's kind of inconvenient the need to keep removing commas after parsing it as we don't write:

{:a 1,
 :b 2,
 :c 3}

Alright. I'll hopefully get to it this week! :)

@ericdallo @leandropincini Is this mostly about commas between map entries? The library I'm using to colorize (puget) only allows stripping the comma between map entries, not generally. So what I could do:

If --no-commas is provided, I provide puget with the :map-delimiter "".
In other cases I can strip all commas myself manually.

IMO stripping all commas is a better choice, what we want most if not all the time, otherwise would be --no-commas-between-map-entries 😅

I know that's better but it's not possible in combination with colorization at the moment, unless I implement my own colorization.

Anyway, in emacs when calling jet, it won't produce colorized output anyway and then all commas would be removed.

I was wondering if it worths to add a know issue (breaks the color) for that and some ways to avoid it.

Published as 0.5.25 and the option is called --no-commas. It fully works with uncolored output, and only works inside maps with colored output, but in the future we can maybe improve this.