/
index.mts
48 lines (43 loc) · 1.28 KB
/
index.mts
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
import { type Color, colorix } from 'colorix'
import splashes, { je, be } from './splashes.mjs'
export default splashes
export { je, be }
/**
* Pick a random element from an array.
* @example
* ```ts
* const arr = [1, 2, 3, 4, 5]
* const x = pickRandom(arr) // 1 | 2 | 3 | 4 | 5
* ```
* @utility
*/
export const pickRandom = <T extends any[] | readonly any[]>(arr: T): T[number] =>
arr[Math.floor(Math.random() * arr.length)]
/**
* Log a random minecraft splash message to the console.
* If the console supports ANSI color, the message will be italicized and gray.
*
* @example
* Alternatively, color/modifier arguments may be passed through to colorix.
* ```ts
* logRandomSplash('italic', 'bold', 'yellow')
* ```
* Or you can pass `false` to completely disable color.
* ```ts
* logRandomSplash(false)
* ```
* @utility
*/
export function logRandomSplash(...colors: [false]): void
export function logRandomSplash(...colors: Color[]): void
export function logRandomSplash(...colors: Color[] | [false]) {
if (colors[0] === false) {
console.log(pickRandom(splashes))
return
}
const color = colors.length
? colorix(...(colors as Exclude<typeof colors, [false]>))
: colorix('italic', 'gray')
const splash = pickRandom(splashes)
console.log(color(splash))
}