All files / src/draw/parts pad.ts

100% Statements 53/53
100% Branches 21/21
100% Functions 9/9
100% Lines 53/53

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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 1371x 1x 1x                                                     1x 6x 6x 6x             1x 6x       6x 6x 6x 6x             1x 2x             1x 2x             1x 1x 1x 1x             1x 1x 1x 1x             1x 1x 1x 1x 1x 1x             1x 1x 1x 1x 1x 1x             1x 5x 5x 5x 5x   5x 1x 4x 1x 1x 1x 1x 3x 1x 2x           1x  
import {filterDefined, monoRecord} from '#Record'
import {directions, type Directed} from '../direction.js'
import {normalizeStruts, type Struts} from '../struts.js'
 
/**
 * An object with a possibly zero padding at every direction.
 * @category drawing
 */
export type DirectedPad = Directed<number>
 
/**
 * An object with a possibly zero padding at every direction and a strut for
 * each such padding: a pair of _vertical_ struts for the _top_ and _bottom_,
 * and a pair of _horizontal_ struts for the _left_ and _right_.
 * @category drawing
 */
export interface Padded {
  /** How wide/tall should padding be in every direction? */
  pad: DirectedPad
  /** With what do we fill available pad space in every direction? */
  padStruts: Struts
}
 
/**
 * Upgrade a partial {@link DirectedPad} into a total one, filling in empty
 * slots with zeros.
 * @category drawing
 * @function
 */
export const normalizePad = (padded?: Partial<DirectedPad>): DirectedPad => ({
  ...monoRecord(0)(...directions),
  ...filterDefined(padded ?? {}),
})
 
/**
 * Upgrade a partial {@link Padded} into a total one, filling in with defaults.
 * @category drawing
 * @function
 */
export const normalizePadded = (
  padded?: Partial<{
    pad?: Partial<DirectedPad>
    padStruts?: Partial<Struts>
  }>,
): Padded => ({
  padStruts: normalizeStruts(padded?.padStruts),
  pad: normalizePad(padded?.pad),
})
 
/**
 * Compute pad width for the given {@link Padded}.
 * @category drawing
 * @function
 */
export const computePadWidth = ({pad: {left, right}}: Padded): number =>
  left + right
 
/**
 * Compute pad height for the given {@link Padded}.
 * @category drawing
 * @function
 */
export const computePadHeight = ({pad: {top, bottom}}: Padded): number =>
  top + bottom
 
/**
 * Add the width of the given padded to the given width.
 * @category drawing
 * @function
 */
export const addPadWidth =
  ({pad: {left, right}}: Padded) =>
  (width: number): number =>
    left + width + right
 
/**
 * Add the height of the given padded to the given height.
 * @category drawing
 * @function
 */
export const addPadHeight =
  ({pad: {top, bottom}}: Padded) =>
  (height: number): number =>
    top + height + bottom
 
/**
 * Compute padding width/hight for the given {@link Padded}.
 * @category drawing
 * @function
 */
export const computePadSize = (
  padded: Padded,
): [width: number, height: number] => [
  computePadWidth(padded),
  computePadHeight(padded),
]
 
/**
 * Add the given width and height to the given {@link Padded}.
 * @category drawing
 * @function
 */
export const addPadSize =
  (padded: Padded) =>
  (width: number, height: number): [width: number, height: number] => [
    width + computePadWidth(padded),
    height + computePadHeight(padded),
  ]
 
/**
 * Build a padding entry for every direction from a partial list of values.
 * @category drawing
 * @function
 */
export const padding = (
  top: number,
  right?: number,
  bottom?: number,
  left?: number,
): DirectedPad =>
  right === undefined
    ? monoRecord(top)(...directions)
    : bottom === undefined
      ? {
          ...monoRecord(top)('top', 'bottom'),
          ...monoRecord(right)('left', 'right'),
        }
      : left === undefined
        ? {top, bottom, ...monoRecord(right)('left', 'right')}
        : {top, right, bottom, left}
 
/**
 * A {@link DirectedPad} of zero padding in all directions.
 * @category drawing
 */
export const noPadding: DirectedPad = monoRecord(0)(...directions)