npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

json-truncator

v1.0.3

Published

A utility to truncate JSON arrays or objects.

Downloads

132

Readme

JsonTruncator

JsonTruncator is a utility class designed for truncating JSON objects or arrays to a specified maximum length. This tool is particularly useful for managing large JSON data by limiting the amount of data displayed, which can help in debugging and logging scenarios. JsonTruncator supports both objects and arrays, including nested structures, ensuring that even deeply nested JSON data can be truncated efficiently.

Table of Contents

Installation

To use JsonTruncator, install the package via NPM, PNPM or YARN. You can install it locally within your project or globally if you intend to use it as a CLI tool.

1. Install dependencies:

  • NPM
npm install json-truncator
  • PNPM
pnpm install json-truncator
  • YARN
yarn add json-truncator

2.Install the package globally (optional, for CLI usage):

  • NPM
yarn install -g json-truncator
  • PNPM
pnpm install -g json-truncator
  • YARN
yarn global add json-truncator

Installing the package globally allows you to use the truncate-json command directly from the command line.

Usage

Importing the Class

To utilize the JsonTruncator class in your project, import it as follows:

import { JsonTruncator } from "json-truncator";

Using the truncate Method

The truncate method allows you to truncate JSON objects or arrays to a maximum number of items. Here is how you can use it:

Example

import { JsonTruncator } from "json-truncator";

const inputArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const truncatedArray = JsonTruncator.truncate(inputArray, 5);

console.log(truncatedArray);
// Output: [1, 2, 3, 4, 5, '...']

const inputObject = { a: 1, b: 2, c: 3, d: 4, e: 5 };
const truncatedObject = JsonTruncator.truncate(inputObject, 3);

console.log(truncatedObject);
// Output: { a: 1, b: 2, c: 3, etc: '...' }

CLI Usage

If the package is installed globally, you can use it directly from the command line to truncate JSON data.

Commands

  • Truncate a JSON Array or Object using a predefined command :
json-truncator -c deepArrayObject -l 5
  • Truncate a JSON Array by providing custom data :
json-truncator -c -c customObject -l 2 -d '{"name": "John", "age": 30, "address": "123 Street"}'
  • Truncate a JSON Object by providing custom data :
json-truncator -c customObject -l 2 -d '{"name": "John", "age": 30, "address": "123 Street"}'

Options

  • -c, --command <command>: Command to execute (Available commands: deepArrayObject, arrayObject, notArrayObject, mixedArray, deepObject, object, mixedObject, customArray, customObject).

  • -l, --length <number>: Maximum number of items to be displayed, default 3.

  • -d, --data <json>: JSON data to be truncated (used with customArray or customObject).

Author

Garsetayusuf

Email: [email protected]