All files / core/systems/maidenhead system.ts

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

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                14x 14x   14x         2058x     14x 8018x     8018x     1001x 1001x                
import { Option, pipe } from 'effect'
 
import { cellBox } from '../cell'
import { makeParser } from '../parser'
import { singleReading } from '../reading'
import type { Formatter, Parser } from '../types'
import { fromMaidenhead, isMaidenhead, toMaidenhead } from './maidenhead'
 
const PAIRS = 4
const MIN_LOCATOR_LENGTH = 4
 
export const formatter: Formatter = {
  id: 'Maidenhead',
  label: 'Maidenhead',
  maybeGridId: Option.none(),
  format: point =>
    singleReading(toMaidenhead(point, PAIRS), `${PAIRS * 2} characters`),
}
 
export const parser: Parser = makeParser('Maidenhead', text =>
  pipe(
    text.replace(/\s+/g, ''),
    Option.liftPredicate(
      locator => locator.length >= MIN_LOCATOR_LENGTH && isMaidenhead(locator),
    ),
    Option.map(locator => {
      const cell = fromMaidenhead(locator)
      return {
        kind: 'Maidenhead',
        point: cell.center,
        maybeBounds: Option.some(cellBox(cell)),
      }
    }),
  ),
)