Get Full Access

Markdown list

/**
 * Formats an array of strings into a Markdown unordered list.
 *
 * @param {string[]} lines - The array of strings to format as a Markdown list.
 * @param {object} [options] - Formatting options.
 * @param {number} [options.indentation] - The number of spaces for indentation.
 * @param {string} [options.sign] - The character for the bullet.
 * @returns {string} A newline-separated Markdown unordered list.
 */
function formatAsMarkdownList(lines, options) {
  const listFormatter = new MarkdownListFormatter(options);

  return lines.map(listFormatter.formatItem).join("\n");
}

class MarkdownListFormatter {
  constructor(options = {}) {
    this.indent = " ".repeat(options.indentation ?? 0);
    this.sign = options.sign ?? "*";
  }

  formatItem(item) {
    return `${this.indent}${this.sign} ${item}`;
  }
}