All files / src/sections list-section.ts

100% Statements 44/44
100% Branches 9/9
100% Functions 4/4
100% Lines 44/44

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 451x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 44x 44x 44x 44x 44x 44x 44x 44x 33x 33x 33x 44x 44x 44x 44x 35x 35x 35x 35x 1x 1x 35x 22x 22x 35x 12x 12x 35x 35x 35x 35x 44x  
import { DocumentSection, DocumentSectionOptions } from './section.js';
import { heading } from '../elements/heading.js';
import { orderedList, unorderedList } from '../elements/list.js';
 
export type ListType = 'None' | 'Ordered' | 'Unordered';
 
export interface ListOptions<ListItem> extends DocumentSectionOptions {
  type: ListType;
  items?: Array<ListItem>;
}
 
export abstract class ListSection<ListItem> extends DocumentSection {
  protected items: Array<ListItem>;
 
  constructor(private options: ListOptions<ListItem>) {
    super(options);
    this.items = options.items ?? [];
  }
 
  public add(item: ListItem) {
    this.items.push(item);
    return this;
  }
 
  protected abstract itemMapper(item: ListItem): string;
 
  protected synthesizeContent(): Array<string> {
    const items = this.items.map((item) => this.itemMapper(item));
    let listItems: string;
    switch (this.options.type) {
      case 'Ordered':
        listItems = orderedList(items);
        break;
      case 'Unordered':
        listItems = unorderedList(items);
        break;
      case 'None':
        listItems = items.join('\n\n');
        break;
    }
 
    return [heading(this.headingLevel, this.title), '', listItems];
  }
}