Generates a diff-friendly, human-readable representation of a JavaScript object.
Ideal for preparing AssertionError
feedback, or for inspecting objects in
environments where Node's util.inspect()
is unavailable.
import print from "./print.mjs";
let result;
result = print(subject);
result = print(subject, options);
result = print(subject, "label");
result = print(subject, "label", options);
print()
returns a string containing an unambiguous representation of an object
or primitive, optionally prefixed with a label
. The output format depends
on the subject's type, the presence or absence of properties, and options
set by the user.
- Type: Any
- Default:
undefined
The JavaScript value being printed.
The name of a property (or variable) that subject
is associated with. This can
improve readability of object references (indicated by ->
), or clarify context
by prefixing output with a meaningful label.
const targetObject = {
foo: {},
bar: {},
};
targetObject.bar.referenceToFoo = targetObject.foo;
print(targetObject, "subject");
The example above returns:
subject: {
foo: {}
bar: {
referenceToFoo: -> subject.foo
}
}
- Type:
Object
- Default:
{}
Additional settings for refining output. Unless mentioned otherwise, all options
default to null
or false
.
Name | Type | Description |
---|---|---|
all |
Boolean |
Display non-enumerable properties |
attr |
Boolean |
Display property attributes as <W,E,C> |
followGetters |
Boolean |
Invoke getter functions |
indent |
String |
String (or num. of spaces) used to indent |
indexes |
Boolean |
Display the indexes of iterable entries |
maxDepth |
Number |
Hide object details after N recursions |
noAmp |
Boolean |
Don't format well-known symbols as @@… |
noHex |
Boolean |
Don't format byte-arrays as hexadecimal |
noSource |
Boolean |
Don't display function source code |
proto |
Boolean |
Show __proto__ properties if possible |
sort |
Boolean |
Sort properties alphabetically |
- Add options for…
- Annotating property attributes (writable, enumerable, configurable)
-
maxLines
/maxSize
for truncating length arrays or strings
- Document remaining options
- Migrate to GitHub Actions
- Shorten
sortProps
to justsort