-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
stringify.js
154 lines (144 loc) · 3.8 KB
/
stringify.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
const { Pair, Comment, EmptyLine } = require('./ast')
const escapeNonPrintable = (str, latin1) => {
const re =
latin1 !== false ? /[^\t\n\f\r -~\xa1-\xff]/g : /[\0-\b\v\x0e-\x1f]/g
return String(str).replace(re, ch => {
const esc = ch.charCodeAt(0).toString(16)
return '\\u' + ('0000' + esc).slice(-4)
})
}
const escape = str =>
String(str)
.replace(/\\/g, '\\\\')
.replace(/\f/g, '\\f')
.replace(/\n/g, '\\n')
.replace(/\r/g, '\\r')
.replace(/\t/g, '\\t')
const pairWithSeparator = (key, value, sep) =>
escape(key).replace(/[ =:]/g, '\\$&') +
sep +
escape(value).replace(/^ /, '\\ ')
const commentWithPrefix = (str, prefix) =>
str.replace(/^\s*([#!][ \t\f]*)?/g, prefix)
const getFold = ({ indent, latin1, lineWidth, newline }) => line => {
if (!lineWidth || lineWidth < 0) return line
line = escapeNonPrintable(line, latin1)
let start = 0
let split = undefined
for (let i = 0, ch = line[0]; ch; ch = line[(i += 1)]) {
let end = i - start >= lineWidth ? split || i : undefined
if (!end) {
switch (ch) {
case '\r':
if (line[i + 1] === '\n') i += 1
// fallthrough
case '\n':
end = i + 1
break
case '\\':
i += 1
switch (line[i]) {
case 'r':
if (line[i + 1] === '\\' && line[i + 2] === 'n') i += 2
// fallthrough
case 'n':
end = i + 1
break
case ' ':
case 'f':
case 't':
split = i + 1
break
}
break
case '\f':
case '\t':
case ' ':
case '.':
split = i + 1
break
}
}
if (end) {
let lineEnd = end
let ch = line[lineEnd - 1]
while (ch === '\n' || ch === '\r') {
lineEnd -= 1
ch = line[lineEnd - 1]
}
const next = line[end]
const atWhitespace = next === '\t' || next === '\f' || next === ' '
line =
line.slice(0, lineEnd) +
newline +
indent +
(atWhitespace ? '\\' : '') +
line.slice(end)
start = lineEnd + newline.length
split = undefined
i = start + indent.length - 1
}
}
return line
}
const toLines = (obj, pathSep, defaultKey, prefix = '') => {
return Object.keys(obj).reduce((lines, key) => {
const value = obj[key]
if (value && typeof value === 'object') {
return lines.concat(
toLines(value, pathSep, defaultKey, prefix + key + pathSep)
)
} else {
const k =
key === defaultKey ? prefix.slice(0, -pathSep.length) : prefix + key
lines.push([k, value])
return lines
}
}, [])
}
function stringify(
input,
{
commentPrefix = '# ',
defaultKey = '',
indent = ' ',
keySep = ' = ',
latin1 = true,
lineWidth = 80,
newline = '\n',
pathSep = '.'
} = {}
) {
if (!input) return ''
if (!Array.isArray(input)) input = toLines(input, pathSep, defaultKey)
const foldLine = getFold({
indent,
latin1,
lineWidth,
newline: '\\' + newline
})
const foldComment = getFold({
indent: commentPrefix,
latin1,
lineWidth,
newline
})
return input
.map(line => {
switch (true) {
case !line:
case line instanceof EmptyLine:
return ''
case Array.isArray(line):
return foldLine(pairWithSeparator(line[0], line[1], keySep))
case line instanceof Pair:
return foldLine(pairWithSeparator(line.key, line.value, keySep))
case line instanceof Comment:
return foldComment(commentWithPrefix(line.comment, commentPrefix))
default:
return foldComment(commentWithPrefix(String(line), commentPrefix))
}
})
.join(newline)
}
module.exports = { stringify }