Skip to content

Commit

Permalink
Give dice type
Browse files Browse the repository at this point in the history
  • Loading branch information
alxjrvs committed Sep 20, 2024
1 parent c9c7d9d commit 08be320
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/D.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
import { isCustomSides } from '~guards'
import { DicePoolType } from '~types'

type Type<T> = T extends string[] ? DicePoolType.custom : DicePoolType.standard
type Faces<T> = T extends string[] ? T : number[]
type Result<T> = T extends number[] ? number : string
type Result<F> = F extends number[] ? number : string

class D<Sides extends string[] | number> {
sides: number
faces: Faces<Sides>
type: Type<Sides>

constructor(sides: Sides) {
if (isCustomSides(sides)) {
this.sides = sides.length
this.type = DicePoolType.custom as Type<Sides>
this.faces = sides as Faces<Sides>
return
}
this.sides = sides
this.type = DicePoolType.standard as Type<Sides>
this.faces = Array.from(
{ length: Number(sides) },
(_, index) => index + 1
Expand Down

0 comments on commit 08be320

Please sign in to comment.