• Stars
    star
    232
  • Rank 172,847 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 10 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

The best of both `JSON.stringify(obj)` and `JSON.stringify(obj, null, indent)`.

json-stringify-pretty-compact

The output of JSON.stringify comes in two flavors: compact and pretty. The former is usually too compact to be read by humans, while the latter sometimes is too spacious. This module trades performance for a compromise between the two. The result is a pretty compact string, where “pretty” means both “kind of” and “nice”.

{
  "bool": true,
  "short array": [1, 2, 3],
  "long array": [
    {"x": 1, "y": 2},
    {"x": 2, "y": 1},
    {"x": 1, "y": 1},
    {"x": 2, "y": 2}
  ]
}

While the “pretty” mode of JSON.stringify puts every item of arrays and objects on its own line, this module puts the whole array or object on a single line, unless the line becomes too long (the default maximum is 80 characters). Making arrays and objects multi-line is the only attempt made to enforce the maximum line length; if that doesn’t help then so be it.

Installation

npm install json-stringify-pretty-compact
import stringify from "json-stringify-pretty-compact";

Note: This is an ESM only package. (I haven’t written that gist, but it’s a great resource.)

If you need CommonJS, install version 3.0.0. You won’t be missing out on anything: This package is done. No more features will be added, and no bugs have been found in years.

Want a CLI? Check out avantgardnerio/json-stringify-pretty-compact-cli!

stringify(obj, options = {})

It’s like JSON.stringify(obj, options.replacer, options.indent), except that objects and arrays are on one line if they fit (according to options.maxLength).

options:

  • indent: Defaults to 2. Works exactly like the third parameter of JSON.stringify.
  • maxLength: Defaults to 80. Lines will be tried to be kept at maximum this many characters long.
  • replacer: Defaults to undefined. Works exactly like the second parameter of JSON.stringify.

stringify(obj, {maxLength: 0, indent: indent}) gives the exact same result as JSON.stringify(obj, null, indent). (However, if you use a replacer, integer keys might be moved first.)

stringify(obj, {maxLength: Infinity}) gives the exact same result as JSON.stringify(obj), except that there are spaces after colons and commas.

Want more options? Check out @aitodotai/json-stringify-pretty-compact!

License

MIT.

More Repositories

1

eslint-plugin-simple-import-sort

Easy autofixable import sorting.
JavaScript
2,074
star
2

js-tokens

Tiny JavaScript tokenizer.
JavaScript
407
star
3

elm-watch

`elm make` in watch mode. Fast and reliable.
TypeScript
153
star
4

LinkHints

A browser extension that lets you click with your keyboard.
TypeScript
152
star
5

dual

[ABANDONED] Dual is an AutoHotkey script that lets you define dual-role modifier keys easily.
AutoHotkey
122
star
6

run-pty

Run several commands concurrently. Show output for one command at a time. Kill all at once.
JavaScript
100
star
7

source-map-resolve

[DEPRECATED] Resolve the source map and/or sources for a generated file.
JavaScript
86
star
8

spacefn-win

A Windows implementation of the SpaceFN keyboard layout.
AutoHotkey
71
star
9

urix

[DEPRECATED] Makes Windows-style paths more unix and URI friendly.
JavaScript
59
star
10

eslump

Fuzz testing JavaScript parsers and suchlike programs.
JavaScript
57
star
11

tiny-decoders

Type-safe data decoding for the minimalist.
TypeScript
52
star
12

resolve-url

[DEPRECATED] Like Node.js’ `path.resolve`/`url.resolve` for the browser.
JavaScript
52
star
13

source-map-visualize

Quickly open an online source map visualization with local files
JavaScript
50
star
14

source-map-url

[DEPRECATED] Tools for working with sourceMappingURL comments.
JavaScript
41
star
15

elm-app-url

URLs for applications
Elm
26
star
16

webextension-keyboard

⚠️ ABANDONED ⚠️ WebExtension API proposals related to keyboard handling.
JavaScript
20
star
17

line-numbers

[DEPRECATED] Add line numbers to a string.
JavaScript
16
star
18

anishtro

anishtro is a layout for the letters of the English alphabet, made for symmetrical keyboards with at least one main key per thumb.
JavaScript
15
star
19

dotfiles

Shell
13
star
20

source-map-concat

[DEPRECATED] Concatenate files with source maps.
JavaScript
13
star
21

vim-like-key-notation

Parse and generate vim-like key notation for modern browsers.
JavaScript
12
star
22

n-ary-huffman

An n-ary Huffman algorithm implementation.
CoffeeScript
9
star
23

frappe

JavaScript with some nice fluff on top of it.
7
star
24

elm-safe-virtual-dom

JavaScript
7
star
25

video-audio-sync

Fix videos where the audio is out of sync
Elm
7
star
26

elm-value-graph

Show how every value in an Elm application depend on each other, as a graph.
Elm
6
star
27

test-cli

[DEPRECATED] Test CLI applications (that are written a certain way).
JavaScript
5
star
28

VimFx-config

[DEPRECATED] VimFx Config template
JavaScript
5
star
29

keyboard

My personal keyboard layout.
Shell
5
star
30

strip-css-singleline-comments

Adds support for singleline comments in CSS.
JavaScript
5
star
31

elm-minification-benchmarks

A tool to help you evaluate what the best minifier for your Elm program is.
JavaScript
5
star
32

next-minimal-routes

[ABANDONED] Next.js dynamic URLs for the minimalist.
JavaScript
5
star
33

parse-stack

[DEPRECATED] Parses the stack property of errors. Cross-browser.
JavaScript
4
star
34

autoprefixer-brunch

[DEPRECATED] Adds autoprefixer support to brunch.
CoffeeScript
4
star
35

map-replace

[ABANDONED] A command line tool that applies replacements described in a JSON map to files.
CoffeeScript
3
star
36

macos-safari-overscroll-bugs

macOS Safari overscroll color bugs
HTML
3
star
37

elm-minesweeper

The classic game MineSweeper made with Elm.
Elm
3
star
38

hash-filename

[DEPRECATED] A command line tool that puts the hash of a file into its filename.
CoffeeScript
3
star
39

blog

JavaScript, the web and other programming languages from a JavaScripter's perspective.
3
star
40

css-tokens

[DEPRECATED] A regex that tokenizes CSS.
JavaScript
3
star
41

browser-tweaks

Personal browser extension containing a couple of tweaks.
JavaScript
3
star
42

hacking-elm

Live coding slides for a talk on how to do dirty tricks in Elm.
Elm
2
star
43

source-map-dummy

[DEPRECATED] Creates “dummy” source maps.
JavaScript
2
star
44

webextension-polyfill-messaging-issue

https://github.com/mozilla/webextension-polyfill/issues/130
JavaScript
2
star
45

elm-jason

elm/json re-implemented in Elm.
Elm
2
star
46

elm-old-binaries

Elm 0.15.1 – 0.18.0 binaries
2
star
47

eslint-config-lydell

[DEPRECATED] Kinda strict Prettier-friendly ESLint config.
JavaScript
2
star
48

image-upload

Test different `<input type="file">` variations for images
HTML
1
star
49

elm-version

Download and run Elm tooling from elm-tooling.json.
Shell
1
star
50

webext-message-issue

https://bugzilla.mozilla.org/show_bug.cgi?id=1369841
JavaScript
1
star
51

yaba

[DEPRECATED] Yet Another Better Assert. Shows the expression. Cross-browser.
JavaScript
1
star
52

microbit-simulator

Incomplete simulator for the Microbit.
Python
1
star
53

pegjs-each-code

[DEPRECATED] pegjs plugin helper: Run a function once for each code snippet in a grammar AST.
JavaScript
1
star
54

advent-of-code

My Advent of Code solutions.
Elm
1
star
55

climap

[DEPRECATED] Super simple source map generation for CLI tools.
JavaScript
1
star
56

elm-github-actions-test

1
star
57

cctop

[ABANDONED] Plain text configuration parser.
JavaScript
1
star
58

pegjs-each-node

[DEPRECATED] pegjs plugin helper: Run a function once for each node in a grammar AST.
JavaScript
1
star
59

elm-review-simplify-issue

Elm
1
star