Compare commits

..

No commits in common. "baab2e75d9e618a9a7ab49d76e094a341782733d" and "300a5383145c866634c1aaaceb20269f4991d9b7" have entirely different histories.

2 changed files with 13 additions and 55 deletions

View File

@ -1,6 +1,4 @@
import { type CursorPos } from 'node:readline'
import { type Either, fold, type Left, left, right } from '../util/basic/either'
import { PaintCursorError } from './errors'
// Cursor stuffs
interface CursorPosX {
@ -13,35 +11,14 @@ interface CursorPosY {
readonly value: number
}
type CursorPosVal = (str: 'X' | 'Y') => (x: number) => CursorPosX | CursorPosY
const cursorPosVal: CursorPosVal = str => x => ({_tag: str, value: x})
const cursorPosX = (x: number): CursorPosX => ({_tag: 'X', value: x})
type IsCursorPos = (x: 'X' | 'Y') => (y: CursorPosX | CursorPosY) => boolean
const cursorPosY = (x: number): CursorPosY => ({_tag: 'Y', value: x})
type IsCursorPos =
(x: 'X' | 'Y') => (y: CursorPosX | CursorPosY) => boolean
const isCursorPos: IsCursorPos = x => y => y._tag === x
const cursorPos = (x: number) => (y: number): CursorPos => ({rows: x, cols: y})
// Cursor Position
type SafeCursorPos =
(x: CursorPosX | CursorPosY) =>
(y: CursorPosX | CursorPosY) => Either<CursorPos, Error>
const safeCursorPos: SafeCursorPos = (x) => y => {
return isCursorPos('X')(x) && isCursorPos('Y')(y)
? right(cursorPos(x.value)(y.value))
: isCursorPos('Y')(x) && isCursorPos('X')(y)
? right(cursorPos(y.value)(x.value))
: left(
new PaintCursorError(
`Both arguments provided were of the same axis, or otherwise invalid.`,
{x, y},
),
)
}
console.log(
safeCursorPos(cursorPosVal('X')(5))(cursorPosVal('X')(7)),
)
// Tagged types
interface CanvasOrigin {
readonly _tag: 'Origin'
@ -53,23 +30,15 @@ interface CanvasEnd {
readonly value: CursorPos
}
type CanvasOriginConstructor = (x: SafeCursorPos) => Either<CanvasOrigin, E>
const canvasOrigin = (x: CursorPos): CanvasOrigin => ({
_tag: 'Origin',
value: x,
})
const canvasEnd = (x: CursorPos): CanvasEnd => ({
_tag: 'End',
value: x,
})
// Canvas type using tagged positions
type Canvas = {
origin: CanvasOrigin,
end: CanvasEnd,
}
// Cursor Position
// const cursorPos: CursorPos = ()
// Constructor helpers
const origin = (pos: CursorPos): CanvasOrigin => ({
_tag: 'Origin',
@ -82,7 +51,8 @@ const end = (pos: CursorPos): CanvasEnd => ({
})
// Curried canvas creator with type safety
const canvas = (origin: CanvasOrigin) => (end: CanvasEnd): Canvas => ({
const canvas =
(origin: CanvasOrigin) => (end: CanvasEnd): Canvas => ({
origin,
end,
})

View File

@ -1,12 +0,0 @@
export class PaintCursorError extends Error {
cursorX
cursorY
constructor(message: string, {x, y}: {x: unknown, y: unknown}) {
super(message)
this.name = this.constructor.name
this.cursorX = !x ? 'No data' : x
this.cursorY = !y ? 'No data' : y
}
}