0.2.3 • Published 8 years ago
human-stringify v0.2.3
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.
humanStringifysummarizes 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 ofundefined.undefinedresults in the string"undefined"instead of the valueundefined.humanStringifyalways returns a string. If it encounters a value it doesn't understand (probably a bug), it returns"<unknown>".
Install
$ npm install human-stringifyUsage
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 installRunning Tests
$ npm run testLicense
MIT. No dependencies.