All files / utils jsonSafeStringify.js

100% Statements 4/4
100% Branches 1/1
100% Functions 1/1
100% Lines 4/4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18                  12x 12x   1x       1x  
/**
 * We'll try to convert a JSON into string or return '' empy string but never throw
 *
 * @category utils
 *
 * @param {String} [data={}] Input that we'll try to convert to string
 * @return {String} Either a successful result of `JSON.stringify()` or empty string ''
 */
function jsonSafeStringify(data = {}) {
  try {
    return JSON.stringify(data);
  } catch (error) {
    return '';
  }
}
 
module.exports = jsonSafeStringify;