hjson / hjson-js

Hjson for JavaScript
https://hjson.github.io/
MIT License
417 stars 49 forks source link

New option to format arrays of simple values (strings, numbers, bools) on a single line. #21

Closed kay999 closed 7 years ago

kay999 commented 7 years ago

I added a simple option "inlineSimple" which formats arrays of simple values comma-separated on a single line. This can improve readability in some cases.

This

[
   "value1"
   "value2"
   "value3"
  10
  11
]

with inlineSimple=true is formatted as:

[ "value1", "value2", "value2", 10, 11 ]
laktak commented 7 years ago

Nice idea and thanks for the PR!

I think this would be really great if we could also

kay999 commented 7 years ago

Maybe rename it to "inlineArrayElements" and allow using a number which is the maximum number of elements which are inlined:

inlineArrayElements= true    // always inline
inlineArrayElements= 5        // only inline up to 5 elements

(or use the number of characters instead of the element count)

"only force quoted strings inside the array" would require a 2-pass algorithm to first determine if all array elements are simple values because otherwise it would also force it for non-inlined elements.

laktak commented 7 years ago

(or use the number of characters instead of the element count)

I'd go with max-line-length - indent as the main goal is readability.

"only force quoted strings inside the array" would require a 2-pass algorithm to first determine if all array elements are simple values because otherwise it would also force it for non-inlined elements.

It might make more sense to start by creating an inline array (because you need to know the final length of the elements) and only restart with a normal array if it does not fit/qualify.

laktak commented 7 years ago

Thanks - I'm closing this in favor of the new condense api that also works on objects.