stringify.js 765 B

123456789101112131415161718192021222324
  1. /**
  2. * Module dependencies
  3. */
  4. var _ = require('@sailshq/lodash');
  5. var rebuildSanitized = require('./helpers/sanitize');
  6. /**
  7. * A variation on JSON.stringify that also takes care of a few additional
  8. * edge-cases like:
  9. * • stringifying functions, dates, regexps, and errors, as well
  10. * • taking care of circular references
  11. * • normalizing -Infinity, Infinity, and NaN (to 0)
  12. * • stripping undefined (and potentially null) keys and values. If `allowNull` is set, `null` values will not be stripped from the encoded string.
  13. *
  14. * @param {===} value
  15. * @param {Boolean} allowNull
  16. * @return {String}
  17. */
  18. module.exports = function stringify (value, allowNull) {
  19. // TODO: optimize
  20. return JSON.stringify(rebuildSanitized(value,allowNull));
  21. };