GithubHelp home page GithubHelp logo

waldyrious / print Goto Github PK

View Code? Open in Web Editor NEW

This project forked from alhadis/print

0.0 2.0 0.0 77 KB

Generate a diff-friendly, human-readable representation of a value.

Home Page: https://npmjs.com/package/print

License: ISC License

JavaScript 100.00%

print's Introduction

Print

Generate a human-readable representation of a value. Focussed on producing clean and accurate depictions of data. Suitable for debugging and generating diffs.

Usage

const print = require("print");
let output = print({
    foo: "bar",
    baz: "quux",
    list: [1, 2, 3, 4, "five"]
});
console.log(output);

print.out(obj); // Shortcut for console.log(print(obj));

Comparison with built-in functions

print                JSON.stringify          util.inspect

{                    {                       { foo: 'bar',
    baz: "quux"          "foo": "bar",         baz: 'quux',
    foo: "bar"           "baz": "quux",        list:
    list: [              "list": [             [ 1,
        1                    1,                  2,
        2                    2,                  3,
        3                    3,                  4,
        4                    4,                  'five' ] }
        "five"               "five"
    ]                    ]
}                    }

print also handles circular references by showing a -> pointing to where the object was first mentioned. For example, the following code:

const A = {};
const B = {foo: A};
print({A, B});

Will produce:

{
    A: {}
    B: {
        foo: -> A
    }
}

Options

An optional second parameter can be passed to refine print's output.

Available options and their default values are listed below:

print(input, {
    ampedSymbols:     true,
    escapeChars:      /(?!\x20)\s|\\/g,
    invokeGetters:    false,
    maxArrayLength:   100,
    showAll:          false,
    showArrayIndices: false,
    showArrayLength:  false,
    sortProps:        true
});

ampedSymbols

Boolean
Prefix Symbol-keyed properties with @@. Disable to show Symbol(…) instead.

escapeChars

RegExp | Function
What characters, if any, are escaped in string values.

By default, anything that alters the output's meaning or layout is escaped:

\f \n \r \t \v \\

This can be overridden with a custom expression or callback, the latter of which receives the entire string as an argument.

Passing falsey values to escapeChars disables escaping altogether, which isn't recommended if your input contains line-breaks or tabulation.

invokeGetters

Boolean
Permit print to call a property getter to display its computed value.

Invoking a getter can have unwanted side-effects, so this option is disabled by default.

maxArrayLength

Number
Maximum number of array values to show before truncating them:

[
    1
    2
    3

    … 7 more values not shown
]

Note this excludes any named properties stored on the Array object:

const input = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
input.foo = "bar";
print(input, { maxArrayLength: 3 })
[
    1
    2
    3
	
    … 7 more values not shown

    foo: "bar"
]

showAll

Boolean
Include non-enumerable properties when printing an object.

Note that inherited properties are always hidden.

showArrayIndices

Boolean
Show the index of each element in an array.

[                  [
    "A"                0: "A"
    "B"      ->        1: "B"
    "C"                2: "C"
]                  ]

showArrayLength

Boolean
Display an array's length property after its values:

[
    "A"
    "B"
    "C"
    length: 3
]

sortProps

Boolean
Alphabetise the properties of printed objects.

To display properties in the order they were assigned, set this to false.

Note that alphabetisation is case-insensitive.

print's People

Contributors

alhadis avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.