human-stringify
TypeScript icon, indicating that this package has built-in type declarations

0.2.3 • Public • Published

Build Status Coverage Status FOSSA Status

human-stringify

Using JSON.stringify for debugging is very convenient, but risky. You never know if the argument might be a large array, or have thousands of keys. Using a debugger to inspect values avoids these problems, but then values are only available at a point in time.

JSON.stringify is for machines. humanStringify is for humans:

  • Automatically elides large strings, large arrays, and large objects. Humans can't consume huge amounts of data anyway. humanStringify summarizes existence of a large amount of data, not to present all of the values.
humanStringify(longString); // `...string of len ${longString.length}`
humanStringify(bigArray); // `[...${bigArray.length} elements]`
humanStringify(bigObject); // `{...large object}`
  • Tells you about Uint8Arrays, Uint8ClampedArrays, and ArrayBuffer instances and their sizes, but skips their contents.
humanStringify(uint8Array); // `Uint8Array(len: ${uint8Array.length})`
humanStringify(uint8ClampedArray); // `Uint8ClampedArray(len: ${uint8ClampedArray.length})`
humanStringify(arrayBuffer); // `ArrayBuffer(byteLength: ${arrayBuffer.byteLength}`
  • Outputs function(){...} for Functions, instead of undefined.

  • undefined results in the string "undefined" instead of the value undefined.

  • humanStringify always returns a string. If it encounters a value it doesn't understand (probably a bug), it returns "<unknown>".

Install

$ npm install human-stringify

Usage

import humanStringify from "human-stringify";
console.log(`obj is ${humanStringify(obj)}`);

Options:

humanStringify(obj, { limit: 100, compact: true, maxDepth: 5 });
  • limit elides contents of arrays and objects with more than this number of keys. Defaults to 100.
  • compact If true, return a single line output. If false, return formatted multiline output. Defaults to false.
  • maxDepth Limit the depth of nested arrays and objects. This is helpful for avoiding stack overflow with cyclical structures. It can also help avoid overly noisy output. Defaults to 3.

Building

$ npm install

Running Tests

$ npm run test

License

MIT. No dependencies.

FOSSA Status

Readme

Keywords

none

Package Sidebar

Install

npm i human-stringify

Weekly Downloads

1

Version

0.2.3

License

MIT

Unpacked Size

46.1 kB

Total Files

12

Last publish

Collaborators

  • acchou