-
Notifications
You must be signed in to change notification settings - Fork 2
/
helpers.ts
53 lines (50 loc) · 1.42 KB
/
helpers.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
53
/**
* Pick a random boolean, number, or element from an array
*/
export function random(): boolean
export function random(min: number, max: number): number
export function random<T extends unknown>(arr: T[]): T
export function random(
minOrArr?: number | unknown[],
max?: number,
) {
if (Array.isArray(minOrArr)) {
return minOrArr[Math.floor(Math.random() * minOrArr.length)]
} else if (typeof minOrArr === 'number' && typeof max === 'number') {
return Math.floor(Math.random() * (max - minOrArr)) + minOrArr
} else {
return Math.random() < 0.5
}
}
export function chunkArray<T extends unknown>(arr: T[], chunkSize: number) {
const chunks: T[][] = []
for (let i = 0; i < arr.length; i += chunkSize) {
chunks.push(arr.slice(i, i + chunkSize))
}
return chunks
}
// export async function getJSONFile(path: string | URL) {
// try {
// const text = await Deno.readTextFile(path)
// const json = JSON.parse(text)
// return json
// } catch (error) {
// if (!('name' in error && error.name === 'NotFound')) throw error
// return undefined
// }
// }
// export type jsonData = {
// title: string
// length: string
// url: string
// id: string
// }
// export async function fileExists(path: string | URL) {
// try {
// await Deno.stat(path)
// return true
// } catch (error) {
// if (!(error instanceof Deno.errors.NotFound)) throw error
// return false
// }
// }