code: 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))
    );
  }
docs: 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.