maybeStringify(val, indent)

If the provided value is a string, return it unchanged. Otherwise encode it with JSON.stringify and return the result.

  • val any — 

    value to be stringified (maybe)

  • indent number — 

    number of spaces used to indent the JSON object

    (default: 2)
returns string|undefined

See

  • isString — 

    used by this function to determine whether or not to process the value

  • acEx — 

    uses this function to produce the string representation of the actual and expected values

  • Tap — 

    uses this function as the default implementation of the Tap.messageToString method

Examples

const {maybeStringify} = require('spooning');

maybeStringify('foo'); // returns 'foo'

maybeStringify({foo: 'bar'}); // returns '{\n  "foo": "bar"\n}'

maybeStringify(); // returns undefined

maybeStringify(null); // returns 'null'

maybeStringify(true); //returns 'true'

maybeStringify(123); //returns '123'

maybeStringify(new Date(Date.UTC(2018, 9, 7, 23, 11, 43, 81))); // returns '"2018-10-07T23:11:43.081Z"'