-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
52 lines (50 loc) · 1.52 KB
/
index.ts
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
/**
* Roll any number. When passing no arguments it returns a number between 0 - 100; including 0 and 100
* @param {number} [minOrMax] When passing only 1 argument it returns a number between 0 and the argument; including 0 and the argument
* @param {number} [max] When passing 2 arguments, they are considered "min" and "max"; it returns a number between min - max; including min and max
* @returns {number}
*/
export function roll(minOrMax?: number, max?: number): number {
let _min = 0
let _max = 100
if (minOrMax === void 0) {
// keep 0 - 100
} else if (max === void 0) {
_min = 0
_max = minOrMax
} else {
_min = minOrMax
_max = max
}
return Math.floor(Math.random() * (_max - _min + 1)) + _min
}
/**
* Returns 'heads' or 'tails' at random
* @returns {'heads' | 'tails'}
*/
export function flip(): 'heads' | 'tails' {
return roll(1) === 0 ? 'heads' : 'tails'
}
/**
* Create a dice with any number of sides, dice(6).roll() to roll it!
* You can also try rolling a specific side to return a boolean like so: dice(6).rollAssert(6).
*
* @export
* @param {?number} [sides=6] The number of sides
* @returns {{ sides: number, roll: () => number, rollAssert: (target: number) => boolean }}
*/
export function Dice(sides = 6): {
sides: number
roll: () => number
rollAssert: (target: number) => boolean
} {
return {
sides,
roll(): number {
return roll(1, this.sides)
},
rollAssert(targetNumber: number): boolean {
return roll(1, this.sides) === targetNumber
},
}
}