UNPKG

json2csv

Version:
19 lines (14 loc) 468 B
function stringFormatter(opts = {}) { const quote = typeof opts.quote === 'string' ? opts.quote : '"'; const escapedQuote = typeof opts.escapedQuote === 'string' ? opts.escapedQuote : `${quote}${quote}`; if (!quote) { return (value) => value; } return (value) => { if(value.includes(quote)) { value = value.replace(new RegExp(quote, 'g'), escapedQuote); } return `${quote}${value}${quote}`; } } module.exports = stringFormatter;