function escapeCarriageReturn(txt) {
  if(!txt) return '';
  txt = txt.replace(/\r+\n/gm, '\n'); // \r followed by \n --> newline
  while (txt.search(/\r[^$]/g) > -1) {
      var base = txt.match(/^(.*)\r+/m)[1];
      var insert = txt.match(/\r+(.*)$/m)[1];
      insert = insert + base.slice(insert.length, base.length);
      txt = txt.replace(/\r+.*$/m, '\r').replace(/^.*\r/m, insert);
  }
  return txt;
}

module.exports = escapeCarriageReturn;

function escapeCarriageReturn(txt) {
  if (!txt) return '';
  if (!/\r/.test(txt)) return txt;
  txt = txt.replace(/\r+\n/gm, '\n'); // \r followed by \n --> newline
  while (/\r[^$]/.test(txt)) {
    var base = /^(.*)\r+/m.exec(txt)[1];
    var insert = /\r+(.*)$/m.exec(txt)[1];
    insert = insert + base.slice(insert.length, base.length);
    txt = txt.replace(/\r+.*$/m, '\r').replace(/^.*\r/m, insert);
  }
  return txt;
}

module.exports = escapeCarriageReturn;

Improve performance for normal text
Improve performance for normal text
