# pretty-print-object

> Function to pretty-print an object with an ability to annotate every value.

Latest version **1.1.0** (published 2015-12-13) · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install pretty-print-object
pnpm add pretty-print-object
yarn add pretty-print-object
bun add pretty-print-object
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2015-12-13 |
| First published | 2015-12-12 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Node | ^5.0.0 |
| Dependencies | 4 |
| Known vulnerabilities | 0 (+5 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 7 |
| Author | Gajus Kuizinas |
| Maintainers | gajus |
| Keywords | prettyprint, pretty-print, object |

## Links

- npm: https://www.npmjs.com/package/pretty-print-object
- Repository: https://github.com/gajus/pretty-print-object
- Homepage: https://github.com/gajus/pretty-print-object#readme
- Issues: https://github.com/gajus/pretty-print-object/issues
- npm.io page: https://npm.io/package/pretty-print-object

## Dependencies (4)

- [lodash](https://npm.io/package/lodash.md) ^3.10.1
- [is-arrow-function](https://npm.io/package/is-arrow-function.md) ^2.0.3
- [source-map-support](https://npm.io/package/source-map-support.md) ^0.4.0
- [get-parameter-names](https://npm.io/package/get-parameter-names.md) ^0.2.0

## Recent versions

- 1.1.0 (latest) — 2015-12-13
- 1.0.1 — 2015-12-13
- 1.0.0 — 2015-12-13
- 0.0.1 — 2015-12-12

## README

# Pretty-print Object

[![Travis build status](http://img.shields.io/travis/gajus/pretty-print-object/master.svg?style=flat-square)](https://travis-ci.org/gajus/pretty-print-object)
[![NPM version](http://img.shields.io/npm/v/pretty-print-object.svg?style=flat-square)](https://www.npmjs.com/package/pretty-print-object)
[![js-canonical-style](https://img.shields.io/badge/code%20style-canonical-brightgreen.svg?style=flat-square)](https://github.com/gajus/canonical)

Function to pretty-print an object with an ability to annotate every value.

## API

```js
/**
 * @typedef {Object} optionsType
 * @property {string} indentTemplate String used to indent one level of code (default: '    ').
 * @property {valueIndex|null} valueIndex A function used to index values in the object, the line of declaration in the output and the internal type of the value.
 */

/**
 * @param {Object} subject
 * @param {optionsType} options
 * @returns {string}
 */
prettyPrintObject;
```

## Use

```js
import prettyPrintObject from 'pretty-print-object';
```

### Format Object for `console.log`

```js
console.log(prettyPrintObject({
    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
    ]
}));
```

```
{
    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`.

```js
import {
    createValueIndex
} from './../prettyPrintObject';
```

`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.

```js
{
    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.

```js
import _ from 'lodash';
import prettyPrintObject, {
    createValueIndex
} from './../prettyPrintObject';

let formatAnnotatedObject;

formatAnnotatedObject = (subject) => {
    let formattedValue,
        valueIndex,
        valueIndexData;

    valueIndex = createValueIndex();

    formattedValue = prettyPrintObject(subject, {
        valueIndex: valueIndex
    });
    valueIndexData = valueIndex.getValueIndexData();

    return formattedValue = _.map(formattedValue.split('\n'), (line, linuNumber) => {
        if (_.has(valueIndexData, linuNumber)) {
            line += ' : ' + valueIndexData[linuNumber].type;
        }

        return line;
    }).join('\n');
};
```

We are going to annotate data from the previous example:

```js
console.log(formatAnnotatedObject({
    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
    ]
}));
```

```
{
    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

```sh
npm install pretty-print-object
```

---
_Source: https://npm.io/package/pretty-print-object · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
