0.2.3 • Published 6 years ago

human-stringify v0.2.3

Weekly downloads
1
License
MIT
Repository
github
Last release
6 years ago

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

0.2.3

6 years ago

0.2.2

6 years ago

0.2.1

6 years ago

0.2.0

6 years ago

0.1.4

6 years ago

0.1.3

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago