-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinduk.js
63 lines (47 loc) · 1.21 KB
/
induk.js
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
54
55
56
57
58
59
60
61
62
63
import { readFile } from 'node:fs/promises'
function mengandungSamaDengan(line) {
return line.includes('=')
}
function throwNonString(input) {
const tipeInput = typeof input
if (tipeInput != 'string') {
throw new TypeError(`Inputmu bukan string, tapi ${tipeInput}`)
}
return
}
function jadikanObyek(input) {
// hapus spasi dan tab di awal, di akhir, dan dimanapun kecuali karakter baris baru
// pemisahan berdasarkan baris
const bahan = input
.trim()
.replace(/ +?/g, '')
.split('\n') // step 1
if (!bahan.every(mengandungSamaDengan)) {
throw new TypeError('String tidak berformat env. Coba lihat lagi.')
}
// pemisahan berdasarkan karakter =
const bahan2 = bahan.map((item) => {
return item.split('=')
})
return Object.fromEntries(bahan2)
}
class EnvDariString {
constructor(input) {
throwNonString(input)
this.input = input
}
keObyek() {
return jadikanObyek(this.input)
}
}
class EnvDariFile {
constructor(input) {
throwNonString(input)
this.input = input
}
async keObyek() {
const isiFile = (await readFile(this.input)).toString()
return jadikanObyek(isiFile)
}
}
export { EnvDariString, EnvDariFile }