All files / src/draw/glyph borders.ts

100% Statements 94/94
100% Branches 8/8
100% Functions 4/4
100% Lines 94/94

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 137 138 139 140 141 142 143 144 145 146 1471x   1x 1x                       1x       1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                     1x       1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x         1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                           1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x             1x   2898x 2898x 2898x 2898x 2898x 2898x  
import {dual, type EndoOf} from '#Function'
import type {Direction} from '../direction.js'
import {elbowSet, replaceElbow, type ElbowSetName} from './elbows.js'
import {lineSet, replaceLine, type LineSetName} from './lines.js'
import type {BorderSet, CornerDirection} from './types.js'
 
/**
 * Replace the border line at a direction in the given set.
 * @param set The border set to change.
 * @param direction Line direction to change.
 * @param glyph New glyph.
 * @returns Updated border set.
 * @category drawing
 * @function
 */
export const replaceBorderLine: {
  (set: BorderSet, direction: Direction, glyph: string): BorderSet
  (direction: Direction, glyph: string): EndoOf<BorderSet>
  named: (direction: Direction, from: LineSetName) => EndoOf<BorderSet>
} = Object.assign(
  dual(
    3,
    (
      {lines, ...set}: BorderSet,
      direction: Direction,
      glyph: string,
    ): BorderSet => ({...set, lines: replaceLine(lines, direction, glyph)}),
  ),
  {
    named:
      (direction: Direction, from: LineSetName): EndoOf<BorderSet> =>
      ({lines, ...set}) => ({
        ...set,
        lines: replaceLine(lines, direction, borderSet(from).lines[direction]),
      }),
  },
)
 
/**
 * Replace the border elbow at a direction in the given set.
 * @param set The border set to change.
 * @param direction Corner direction to change.
 * @param glyph New glyph.
 * @returns Updated border set.
 * @category drawing
 * @function
 */
export const replaceBorderElbow: {
  (set: BorderSet, direction: CornerDirection, glyph: string): BorderSet
  (direction: CornerDirection, glyph: string): EndoOf<BorderSet>
  named: (direction: CornerDirection, from: ElbowSetName) => EndoOf<BorderSet>
} = Object.assign(
  dual(
    3,
    (
      {elbows, ...set}: BorderSet,
      direction: CornerDirection,
      glyph: string,
    ): BorderSet => ({...set, elbows: replaceElbow(elbows, direction, glyph)}),
  ),
  {
    named:
      (direction: CornerDirection, from: ElbowSetName): EndoOf<BorderSet> =>
      ({elbows, ...set}) => ({
        ...set,
        elbows: replaceElbow(elbows, direction, elbowSet(from)[direction]),
      }),
  },
)
/**
 * Names of all border sets.
 * @category drawing
 */
export const borderSetNames = [
  'ascii',
  'beveled',
  'dashed',
  'dashedWide',
  'dotted',
  'double',
  'halfSolid',
  'halfSolidFar',
  'halfSolidNear',
  'hDouble',
  'hThick',
  'near',
  'solid',
  'space',
  'thick',
  'thickDashed',
  'thickDashedWide',
  'thickDotted',
  'thin',
  'vDouble',
  'vThick',
] as const
 
/**
 * The type of a border set name.
 * @category drawing
 */
export type BorderSetName = (typeof borderSetNames)[number]
 
/**
 * A record of all border sets.
 * @category drawing
 */
export type BorderSets = Record<BorderSetName, BorderSet>
 
const borderSets: BorderSets = {
  ascii: fromSets('ascii', 'ascii'),
  thin: fromSets('thin', 'thin'),
  dashed: fromSets('dashed', 'thin'),
  dashedWide: fromSets('dashedWide', 'thin'),
  dotted: fromSets('dotted', 'thin'),
  thick: fromSets('thick', 'thick'),
  hThick: fromSets('hThick', 'hThick'),
  vThick: fromSets('vThick', 'vThick'),
  thickDashed: fromSets('thickDashed', 'thick'),
  thickDashedWide: fromSets('dashedWide', 'thick'),
  thickDotted: fromSets('thickDotted', 'thick'),
  double: fromSets('double', 'double'),
  hDouble: fromSets('hDouble', 'hDouble'),
  vDouble: fromSets('vDouble', 'vDouble'),
  solid: fromSets('solid', 'solid'),
  space: fromSets('space', 'space'),
  near: fromSets('near', 'space'),
  halfSolid: fromSets('halfSolid', 'halfSolid'),
  halfSolidNear: fromSets('halfSolidNear', 'halfSolidNear'),
  halfSolidFar: fromSets('halfSolidFar', 'halfSolidFar'),
  beveled: fromSets('near', 'diagonal'),
}
 
/**
 * Get a line set by name.
 * @category drawing
 * @function
 */
export const borderSet = (name: BorderSetName): BorderSet => borderSets[name]
 
function fromSets(lines: LineSetName, elbows: ElbowSetName): BorderSet {
  return {
    lines: lineSet(lines),
    elbows: elbowSet(elbows),
  }
}