UnlyEd/utils

View on GitHub
src/convertLineBreaks.js

Summary

Maintainability
A
0 mins
Test Coverage
/**
 * Replace line breaks in a string by a HTML line break (<br>)
 *
 * @param str
 * @param replacer
 * @returns {string}
 */
export const convertLineBreaks = (str, replacer = '<br>') => {
  if (typeof str !== 'string') {
    throw Error(`convertLineBreaks waiting for string but receive a ${typeof str}`);
  }

  return str.replace(/\n/g, replacer);
};