Socket
Socket
Sign inDemoInstall

pretty-json-stringify

Package Overview
Dependencies
0
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    pretty-json-stringify

A function that adds customizeable indents to JSON data


Version published
Weekly downloads
1.1K
decreased by-57.04%
Maintainers
1
Install size
9.31 kB
Created
Weekly downloads
 

Readme

Source

pretty-json-stringify Build Status npm module

A naive function that adds customizeable indents to JSON data (pretty-prints JSON whitespace). You can define whether any object or array should either be collapsed to one line or expanded with one item on each line.

Syntax

var JSONString = prettyJSONStringify(objectToStringify, params);

Example

var prettyJSONStringify = require('pretty-json-stringify');

prettyJSONStringify({
    simple : [1, 2, 3],
    tooShort : [4],
    doNotExpand : [5, 6, 7],
    level1 : { 
        level2_1 : {
            a : 1
        },
        level2_2containsExpanded : {
            a : 1,
            doExpand : ['expanded'] // If any portion of object contains something told to be expanded, parent
                                    // object is expanded regardless of whether it is told to be expanded itself
        },
        level2_3 : {
            a : 1
        },
    }
}, {
    shouldExpand : function(object, level, key) {
        if (key == 'doNotExpand') return false;
        if (key == 'doExpand') return true;
        if (Array.isArray(object) && object.length < 2) return false;
        if (level >= 2) return false;
        return true;
    }
});

This returns

{
    "simple" : [
        1,
        2,
        3
    ],
    "tooShort" : [4],
    "doNotExpand" : [5, 6, 7],
    "level1" : {
        "level2_1" : { "a" : 1 },
        "level2_2containsExpanded" : {
            "a" : 1,
            "doExpand" : [
                "expanded"
            ]
        },
        "level2_3" : { "a" : 1 }
    }
}

Also following "code-style" parameters can be applied (any passed string is inserted as is, it is user's responsibility to keep it valid whitespace):

  • tab (default: 4 spaces)
  • spaceBeforeColon (default: 1 space)
  • spaceAfterColon (default: 1 space)
  • spaceAfterComma (default: 1 space)
  • spaceInsideObject (inserted after opening and before closing braces, default: 1 space)
  • spaceInsideArray (inserted after opening and before closing braces, default: empty string).

If shouldExpand parameter is not passed, it expands every object whose plain JSON is longer that 25 characters.

Keywords

FAQs

Last updated on 01 Nov 2016

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc