src/document/doc-printer.js/trim
function trim(out) {
  if (out.length === 0) {
    return 0;
  }

  let trimCount = 0;

  // Trim whitespace at the end of line
  while (
    out.length > 0 &&
    typeof getLast(out) === "string" &&
    /^[\t ]*$/.test(getLast(out))
  ) {
    trimCount += out.pop().length;
  }

  if (out.length > 0 && typeof getLast(out) === "string") {
    const trimmed = getLast(out).replace(/[\t ]*$/, "");
    trimCount += getLast(out).length - trimmed.length;
    out[out.length - 1] = trimmed;
  }

  return trimCount;
}