1 |
|
2 | export type Data = Record<string, string>
|
3 |
|
4 |
|
5 | export type Input = Record<string, any>
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | export function parse(src: string): Data {
|
11 | const result: Data = {}
|
12 | const lines = src.toString().split('\n')
|
13 | for (const line of lines) {
|
14 | const match = line.match(/^([^=:#]+?)[=:](.*)/)
|
15 | if (match) {
|
16 | const key = match[1].trim()
|
17 | const value = match[2].trim()
|
18 | result[key] = value
|
19 | }
|
20 | }
|
21 | return result
|
22 | }
|
23 |
|
24 |
|
25 | export function stringify(obj: Input): string {
|
26 | let result = ''
|
27 | for (const [key, value] of Object.entries(obj)) {
|
28 | if (key) {
|
29 | const line = `${key}=${String(value)}`
|
30 | result += line + '\n'
|
31 | }
|
32 | }
|
33 | return result
|
34 | }
|