generated from MJGTwo/advent-of-code-nodets-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.ts
42 lines (37 loc) · 1.06 KB
/
util.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
import path from 'path'
import { read, write, position } from 'promise-path'
import { fileURLToPath } from 'url'
import { LOG_NAME } from './constants.ts'
const __filename = fileURLToPath(import.meta.url)
const fromHere = position(path.dirname(__filename))
export const reportGenerator = (filename: string) => async (
...messages: string[]
) =>
console.log(
`[${LOG_NAME} / ${filename
.split(path.sep)
.pop()
?.split('.ts')
.shift()}]`,
...messages
)
export const replaceInFile = async (
filename: string,
search: string | RegExp,
replace: string
): Promise<any> => {
const haystack: string = await read(filename, 'utf8')
const ashes: string = haystack.replace(search, replace)
return write(filename, ashes, 'utf8')
}
export const arrToNumberArr = (arr: Array<string>): Array<number> => {
return arr.map(Number)
}
export const sortNumbers = (arr: Array<number>): Array<number> => {
return arr.sort((a, b) => a - b)
}
export const log = (statement: any, debug: boolean): void => {
if (debug) {
console.log(statement)
}
}