Pretty-print Object
Function to pretty-print an object with an ability to annotate every value.
API
/** * @typedef * @property * @property */ /** * @param * @param * @returns */prettyPrintObject;
Use
;
console.log
Format Object for console;
{
foo: "FOO",
bar: "BAR",
emptyArray: [],
emptyObject: {},
arrayWithLiteralValues: [
1,
2,
3
],
objectWithLiteralValues: {
1: "foo",
2: "bar",
3: "baz"
},
types: [
undefined,
null,
function (a, b) { ... },
(a, b) => { ... },
NaN,
Infinity,
10.2,
true
]
}
Annotate Value Types
This library provides a method createValueIndex
.
;
createValueIndex
is a factory function that will produce an instance of valueIndex
. valueIndex
object implements methods add
and increment
. These methods are used internally to keep track of where and what values are added to the formatted object string.
getValueIndexData
method returns an object describing the collected data, e.g.
1: path: 'foo' value: 'foo' type: 'string'
For this example, we are going to build a helper function formatAnnotatedObject
that formats object output, indexes object values and annotates the formatted output with value types.
;; let formatAnnotatedObject; { let formattedValue valueIndex valueIndexData; valueIndex = ; formattedValue = ; valueIndexData = valueIndex; return formattedValue = _;};
We are going to annotate data from the previous example:
console;
{
foo: "FOO", : string
bar: "BAR", : string
emptyArray: [], : array
emptyObject: {}, : object
arrayWithLiteralValues: [
1, : number
2, : number
3 : number
], : array
objectWithLiteralValues: {
1: "foo", : string
2: "bar", : string
3: "baz" : string
}, : object
types: [
undefined, : undefined
null, : null
function (a, b) { ... }, : function
(a, b) => { ... }, : function
NaN, : nan
Infinity, : number
10.2, : number
true : boolean
] : array
} : object
Install
npm install pretty-print-object