Class PrintOptions

PrintOptions, when printMode="print".

Note: these work only with printMode="print", ignored otherwise

Note: it is a class only for validation purposes via class-validator, but it's not meant to be instantiated.

Constructors

Properties

useToString?: boolean | "quoted"

Whether to use the toString() method, but only if a custom one exists on objects.

@default: undefined, i.e false

functionOrClass?: "body" | "name" | "inspect"

Whether to print the function 'body' (using toString()), just the name, or the default inspect message [Function: functionName] as a string.

@default: undefined / hard coded behaviour is 'inspect'

nesting?: boolean

Whether to use nesting of each item in new line, when printing objects / arrays

@default: undefined, i.e false

stringify?: boolean

Whether to use stringify on objects i.e convert them to JSON in a safe way, using 'json-stringify-safe'.

@default: undefined, i.e false

depth?: number

Max depth level of object/array nesting to consider, all levels higher are ignored.

A string to detail this is added in the place - see inspect!

maxItems?: number

Max number of items to consider in Arrays / Sets (at their root level). Remaining items are omitted (use omitted: true to print comment)

maxProps?: number

Max number of props to consider in Objects/Maps (at their root level). Props order is as retrieved by JS, there's no sorting. Remaining props are omitted (use omitted: true to print comment)

omitted?: boolean
mapAsObject?: boolean

Print Map as an Object, or as a string

true: print an object, that can be copy-pasted eg

   `new Map(Object.entries({ aa: 11, bb: 4 }))`

false: print a string, using inspect eg

   `Map(2) { 'aa' => 11, 'bb' => 4 }`
setAsArray?: boolean

Print Set as an Array, or as a string

true: print an Array that can de dehydrated, eg new Set([1, 2, 3]) /* OMITTED 1 Set items - maxItems = 3... false: print as a string, using inspect eg Set(3) { 1, 2, 3 }` /* OMITTED 1 Set items - maxItems = 3...

instanceClass?: string | boolean | ((instance: object) => string)

Print the class of the instance (i.e the constructor), as an artificial key (a.k.a discriminator).

  • false / undefined (default) - do not add a fake object key to indicate class

  • true: use "class" as prop key, the class / constructor itself as value, eg { class: Person name: 'Angelos }

  • string, eg "kind" or "constructor": use this string as the prop key, as above, eg:

With options: { print: { instanceClass: 'kind' } }

We print { kind: Person name: 'Angelos' }

  • function, accepting (instance, constructor, object, prop), and producing the whole line of the class info.

For example with options:

{ print: { instanceClass: (__, constructor) => __theClass__: '${constructor.name}' } }

We print

{ theClass: 'Person' name: 'Angelos' }

undefinedInJSON?: string

What to replace undefined, when outputing JSON-like via print.stringify

For example 'null' or '[Undefined]'. If it is 'null' then no quotes are added.

@default: '[Undefined]'

symbolFormat?:
    | "for"
    | "outside"
    | "inside"
    | "none"

Selects how to format a Symbol.

For a Symbol('label') we can print:

  • 'for' (default): Symbol.for('label') (or "Symbol.for('label')") in stringify
  • 'outside': 'Symbol(label)' (or "Symbol(label)") in stringify
  • 'inside': Symbol('label') (or "Symbol('label')") in stringify
  • 'none': Symbol(label) (or "Symbol(label)") in stringify

If 'label' is a number, the inside quotes are omitted.

dateFormat?: string

How to format Date object values

With new Date(2023, 11, 31, 23, 58, 59, 250)

  • 'new' - outputs new Date(2023, 11, 31, 23, 58, 59, 250)
  • any other string, eg 'toLocaleString' or 'toISOString' - call this method on the date object!

Start any of these with '@' (eg '@toISOString') to adjust timezone minutes offset (@see https://stackoverflow.com/questions/43591771/new-date-has-wrong-time-in-node-js/71262913#71262913)

@default: 'new'

emptyItem?: string | false

What to print instead of null, for empty items in sparse arrays, when stringify: true

It receives no quotes, so use "myEmptyLabel" (with double quotes) for strings, since this is meant for JSON.

If it is false, nothing is printed, which will make your Arrays indexes correspond to the wrong value (but they look better!). Also keep in mind that comments on {stringify: false} are off, so with false no information will convey the Array was sparse!

@default: "[Empty item]"

argsFormat?: "object" | "array"

How to format arguments pseudo-array (received in function () {})

Eg for value (function(a, b, c) { return arguments })(1, 'foo', {prop: 'val'})

  • 'array' - as array eg [1, 'foo', {prop: 'val'}] - default
  • 'object' - as object eg {'0': 1, '1': 'foo', '2': { prop: 'val'}}
objectProp?: "object" | "toString"
colors?: false | {
    level?: StyleFunction;
    number?: StyleFunction;
    regexp?: StyleFunction;
    boolean?: StyleFunction;
    string?: StyleFunction;
    quote?: StyleFunction;
    squareBracket?: StyleFunction;
    curlyBracket?: StyleFunction;
    comment?: StyleFunction;
    function?: StyleFunction;
    class?: StyleFunction;
    circular?: StyleFunction;
    comma?: StyleFunction;
    propKey?: StyleFunction;
    map?: StyleFunction;
    set?: StyleFunction;
    symbol?: StyleFunction;
    bigint?: StyleFunction;
    date?: StyleFunction;
    arguments?: StyleFunction;
}
inherited?: boolean

@default: false

transform?: TprintTransformFunction

A callback to transform a value or filter object props / array items.

Its behavior depends on the returned value from transform():

  • string: use this string as the printed result for this value, instead of the builtin way. It doesn't include quotes, so anything can go here. If you need quotes (returning an actual string), use 'quote' param that caters for JSON
  • false: omit value completely, prints empty string in its place. If value is also an object property, the prop also is omitted (i.e filtering props / values)
  • true/anything else: use default/builtin handling for this value