stringify.js 354 B

123456789101112131415
  1. const stringifiedRegexp = /^'|".*'|"$/;
  2. /**
  3. * If already stringified - return original content
  4. * @param {Object|Array} content
  5. * @return {string}
  6. */
  7. function stringify(content) {
  8. if (typeof content === 'string' && stringifiedRegexp.test(content)) {
  9. return content;
  10. }
  11. return JSON.stringify(content, null, 2);
  12. }
  13. module.exports = stringify;