function escapeCarriageReturnSafe(txt) {
  if (!txt) return "";
  if (!/\r/.test(txt)) return txt;
  if (!/\n/.test(txt)) return escapeSingleLineSafe(txt);
  txt = txt.replace(/\r+\n/gm, "\n"); // \r followed by \n --> newline
  var idx = txt.lastIndexOf("\n");

  return (
    escapeCarriageReturn(txt.slice(0, idx)) +
    "\n" +
    escapeSingleLineSafe(txt.slice(idx + 1))
  );
}
/**
 * Safely escape carrigage returns like a terminal.
 * This allows to escape carrigage returns while allowing future output to be appended
 * without loosing information.
 * Use this as a intermediate escape step if your stream hasn't completed yet.
 * @param {string} txt - String to escape.
 * @return {string}    - Escaped string.
 */
Safely escape carrigage returns like a terminal.
This allows to escape carrigage returns while allowing future output to be appended
without loosing information.
Use this as a intermediate escape step if your stream hasn't completed yet.
@param {string} txt - String to escape.
@return {string}    - Escaped string.
Safely escape carrigage returns like a terminal . This allows to escape carrigage returns while allowing future output to be appended without loosing information . Use this as a intermediate escape step if your stream hasn ' t completed yet . @param { string } txt - String to escape . @return { string } - Escaped string .
function escapeCarriageReturnSafe(txt) {
  if (!txt) return "";
  if (!/\r/.test(txt)) return txt;
  if (!/\n/.test(txt)) return escapeSingleLineSafe(txt);
  txt = txt.replace(/\r+\n/gm, "\n"); 
  var idx = txt.lastIndexOf("\n");
  return (
    escapeCarriageReturn(txt.slice(0, idx)) +
    "\n" +
    escapeSingleLineSafe(txt.slice(idx + 1))
  );
}
function escapeCarriageReturnSafe ( txt ) { if ( ! txt ) return "" ; if ( ! / \r / . test ( txt ) ) return txt ; if ( ! / \n / . test ( txt ) ) return escapeSingleLineSafe ( txt ) ; txt = txt . replace ( / \r+\n / gm , "\n" ) ; var idx = txt . lastIndexOf ( "\n" ) ; return ( escapeCarriageReturn ( txt . slice ( 0 , idx ) ) + "\n" + escapeSingleLineSafe ( txt . slice ( idx + 1 ) ) ) ; }
Safely escape carrigage returns like a terminal.
Safely escape carrigage returns like a terminal .
// \r followed by \n --> newline