/**
 * Remove initial and final spaces and tabs at the line breaks in `value`.
 * Does not trim initial and final spaces and tabs of the value itself.
 *
 * @param {string} value
 *   Value to trim.
 * @returns {string}
 *   Trimmed value.
 */
export function trimLines(value) {
  return String(value).replace(/[ \t]*(\r?\n|\r)[ \t]*/g, '$1')
}

/**
 * Remove initial and final spaces and tabs at the line breaks in `value`.
 * Does not trim initial and final spaces and tabs of the value itself.
 *
 * @param {string} value
 *   Value to trim.
 * @returns {string}
 *   Trimmed value.
 */
export function trimLines(value) {
  const source = String(value)
  const search = /\r?\n|\r/g
  let match = search.exec(source)
  let last = 0
  /** @type {Array<string>} */
  const lines = []

  while (match) {
    lines.push(
      trimLine(source.slice(last, match.index), last === 0, false),
      match[0]
    )

    last = match.index + match[0].length
    match = search.exec(source)
  }

  lines.push(trimLine(source.slice(last), last === 0, true))

  return lines.join('')
}

/**
 * @param {string} value
 * @param {boolean} start
 * @param {boolean} end
 * @returns {string}
 */
function trimLine(value, start, end) {
  if (!start) {
    value = value.replace(/^[ \t]+/, '')
  }

  if (!end) {
    value = value.replace(/[ \t]+$/, '')
  }

  return value
}

Fix to much improve performance
Fix to much improve performance

Related-to: syntax-tree/mdast-util-to-hast#62.

Co-authored-by: Nathaniel Hunter <77fd1ce3b5fe4bf20a364c9bd443082c90a7e35f@gmail.com>
