Socket
Socket
Sign inDemoInstall

json-truncate

Package Overview
Dependencies
0
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    json-truncate

A way to truncate a json object.


Version published
Weekly downloads
5.1K
decreased by-3.33%
Maintainers
1
Install size
14.2 kB
Created
Weekly downloads
 

Readme

Source

json-truncate

A way to truncate a json object. Useful for circular referenced objects.

Status

npm Main Dependency Status devDependency Status js-standard-style semantic-release npm Greenkeeper badge

About

If you need to write data to a file or output an object to an api endpoint that has circular references I recommend you give json-truncate a try.

By removing deeply nested data to maintain simple copies of the circular references you can keep most of the data you might be interested in.

Install

npm install json-truncate --save

Usage

Below are examples of how to use json-truncate

Including

You can include with regular node require:

JSON.truncate = require('json-truncate')
Usage

Figure 1.0 - A basic example with default options.

JSON.truncate(SomeDeepObject)

Figure 1.1 - An example of setting the maxDepth property.

JSON.truncate(SomeDeepObject, 5)

Figure 1.2 - An example of all configurable options.

console.log(JSON.truncate({
  data: 'foo',
  level1: {
    data: 'bar',
    level2: {
      level3: {}
    }
  }
}, {
  maxDepth: 2,
  replace: '[Truncated]'
}))
/**
 * Output:
{
  "data": "foo",
  "level1": {
    "data": "bar",
    "level2": "[Truncated]"
  }
}
 **/

Configuration

By default, there are two configurable variables to keep in mind when using json-truncate:

  1. maxDepth (Number) = 10
  2. replace (Any) = undefined

If you would you can configure these either individually with each request, or globally with the configuration function. The following example mimics figure 1.2 above.

JSON.truncate.configure({
  maxDepth: 2,
  replace: '[Truncated]'
})
Arguments
  • obj - The Object that will be truncated.
  • options - (optional) An option object to customize the behavior of the utility. Defaults to {}.

Current Option Properties

OptionDescription
maxDepthThe default maxDepth to use for nested and deep properties on an object. Defaults to 10
:--:--
replaceA string value that is used to replace all truncated values. If this value is not a string then all truncated values will be replaced with undefined

Licence

MIT

Keywords

FAQs

Last updated on 05 Apr 2021

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