ljharb / json-stable-stringify

MIT License
52 stars 11 forks source link

key alignment option? #2

Open arthurblake opened 1 year ago

arthurblake commented 1 year ago

I would really like to see a new option that aligns the keys for even more readability when pretty printing... e.g.

stringify({"a": 5, "foo": true, "glue": "hello"}, {space: '  ', alignKeys: true});

produces something like

{
  "a"    : 5,
  "foo"  : true,
  "glue" : "hello"
}

Should be an easy option to add. Maybe I'll submit a PR if I get less lazy...

ljharb commented 1 year ago

I'm not sure I see the value; alignment like that in my experience isn't actually valuable or more readable, even in code - humans read code left to right, not top to bottom.

arthurblake commented 1 year ago

I certainly read top to bottom quite a lot (esp. with large config files and log files) and value such a feature. At any rate if you don't think it's a worthy feature I understand. Easy enough to implement in my own little corner of the world. :)

ljharb commented 1 year ago

Is there even anything in the eslint ecosystem that autoformats in this way?

The biggest problem with it is that it adds lots of diff churn - as soon as the longest key changes length, a ton of lines have to change.

arthurblake commented 1 year ago

Most definitely. For example: https://www.npmjs.com/package/prettyjson I don't like this particular one because it converts to YAML. There is also https://www.npmjs.com/package/json-align but it's 8 years old. Also wanted to point out that JSON is a data format (and so can be used for many many purposes, not just "code") So it's nice to have options. I won't belabor the point though as there are many options and quite easy for me to implement on my own. I have used json-stable-stringify a lot and really like it. So thanks for that!

arthurblake commented 1 year ago

Regarding diff churn. Easy to do it in one pass (we are already doing one pass on the sort) if implemented correctly. If I submit a high quality PR that proves it would you consider?

arthurblake commented 1 year ago

By one pass, what I really meant was no extra pass. The same pass that is sorting can compute maximum key length on the object keys and then at output do the padding.

ljharb commented 1 year ago

I mean, when someone code reviews the formatting changes, a bunch of irrelevant lines will be changed. That makes code reviewing harder. (I don't care about the complexity of the autofix itself)

arthurblake commented 1 year ago

Can you clarify that last comment? What code review are you talking about? My main use case here is logging REST API JSON objects that can get quite large. Not JSON stored as code.

ljharb commented 1 year ago

Ah, fair, diff churn is more of a concern about formatting code, not JSON.

I suppose it'd be worth reviewing a PR implementation to see how it looks?

arthurblake commented 1 year ago

I see where you are coming from. Just consider people use this lib for all kinds of use cases that might be different from what you think. I use JSON to store a lot of configuration and often as a protocol format in different APIs. Not just stored as static code. And I think I may be in the majority even. Although a lot of people have migrated to YAML for configuration for good reason too.