1.0.0 • Published 7 years ago

maybe-stringify v1.0.0

Weekly downloads
1
License
MIT
Repository
github
Last release
7 years ago

maybe-stringify

A Node.js module which will convert data to JSON or, if the conversion is not possible, will return a fallback value.

Installation

npm install maybe-stringify --save

Usage

const maybeStringify = require('maybe-stringify')

maybeStringify(['test']) // returns '["test"]'

// Default fallback behavior is to return the value as-is,
// but this can be overridden.
maybeStringify(function () {}) // returns the function as-is since it can't stringify
maybeStringify(function () {}, {fallback: 'something else'}) // returns 'something else'

// Set `safe` to `false` to make recursive references trigger
// the fallback behavior.
const recursive = {}
recursive.prop = recursive
maybeStringify(recursive) // returns '{"recursive":"[Circular]"}'
maybeStringify(recursive, {safe: false}) // returns the object as-is since it can't stringify