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
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"'