All files / src/draw/part row.ts

100% Statements 40/40
100% Branches 15/15
100% Functions 3/3
100% Lines 40/40

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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 921x 1x           1x 1x                                     1x 1x 949x 1789x 573x                   1x   1x 1x 1x 1x   1x 1x 1263x 1263x 22x 22x   1x 1x 1262x 1262x 440x 490x               1x   1x 1x 1x 420x 1x 1x               1x   1x 1x 1x 420x 1x 1x  
import {flow, type EndoOf} from '#Function'
import {
  forHorizontalAlignments,
  forVerticalAlignments,
  type HorizontalAlignment,
  type VerticalAlignment,
} from '../align.js'
import {normalizeStruts, type Struts} from '../struts.js'
import {Row, type Part} from './types.js'
 
export interface BuildRow {
  (cells: readonly Part[], struts?: Partial<Struts>): Part
}
 
export interface BuildPart {
  (part: Part, struts?: Partial<Struts>): EndoOf<Part>
}
 
export type BuildAlignedRow = Record<
  VerticalAlignment,
  Record<HorizontalAlignment, BuildRow>
>
export type BuildAlignedPart = Record<
  VerticalAlignment,
  Record<HorizontalAlignment, BuildPart>
>
 
const _row =
  (vAlign: VerticalAlignment = 'middle') =>
  (hAlign: HorizontalAlignment = 'center'): BuildRow =>
  (cells, struts) =>
    Row({hAlign, vAlign, ...normalizeStruts(struts)})(cells as Part[])
 
/**
 * Combine parts horizontally.
 *
 * The 1st cell will be drawn at the left of the row, the final at the bottom.
 * @returns A new part that is composed of the given parts.
 * @category drawing
 * @function
 */
export const row: {
  (vAlign?: VerticalAlignment): (hAlign?: HorizontalAlignment) => BuildRow
} & BuildAlignedRow = Object.assign(
  _row,
  forVerticalAlignments(flow(_row, forHorizontalAlignments)),
)
 
const _before =
  (vAlign: VerticalAlignment = 'middle') =>
  (hAlign: HorizontalAlignment = 'center'): BuildPart =>
  (after, struts) =>
  before =>
    row(vAlign)(hAlign)([before, after], struts)
 
const _after =
  (vAlign: VerticalAlignment = 'middle') =>
  (hAlign: HorizontalAlignment = 'center'): BuildPart =>
  (before, struts) =>
  after =>
    row(vAlign)(hAlign)([before, after], struts)
 
/**
 * Add the prefix part to the left of the suffix part. See {@link after} for a
 * flipped version.
 * @category drawing
 * @function
 */
export const before: {
  (vAlign?: VerticalAlignment): (hAlign?: HorizontalAlignment) => BuildPart
} & BuildAlignedPart = Object.assign(
  _before,
  forVerticalAlignments(vAlign =>
    forHorizontalAlignments(hAlign => _before(vAlign)(hAlign)),
  ),
)
 
/**
 * Add the prefix part to the left of the suffix part. A flipped version of
 * {@link before}.
 * @category drawing
 * @function
 */
export const after: {
  (vAlign?: VerticalAlignment): (hAlign?: HorizontalAlignment) => BuildPart
} & BuildAlignedPart = Object.assign(
  _after,
  forVerticalAlignments(vAlign =>
    forHorizontalAlignments(hAlign => _after(vAlign)(hAlign)),
  ),
)