forked from eemeli/dot-properties
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.js
More file actions
185 lines (175 loc) · 4.86 KB
/
parse.js
File metadata and controls
185 lines (175 loc) · 4.86 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
const atComment = (src, offset) => {
const ch = src[offset]
return ch === '#' || ch === '!'
}
const atLineEnd = (src, offset) => {
const ch = src[offset]
return !ch || ch === '\r' || ch === '\n'
}
const endOfIndent = (src, offset) => {
let ch = src[offset]
while (ch === '\t' || ch === '\f' || ch === ' ') {
offset += 1
ch = src[offset]
}
return offset
}
const endOfComment = (src, offset) => {
let ch = src[offset]
while (ch && ch !== '\r' && ch !== '\n') {
offset += 1
ch = src[offset]
}
return offset
}
const endOfKey = (src, offset) => {
let ch = src[offset]
while (ch && ch !== '\r' && ch !== '\n' && ch !== '\t' && ch !== '\f' && ch !== ' ' && ch !== ':' && ch !== '=') {
if (ch === '\\') {
if (src[offset + 1] === '\n') {
offset = endOfIndent(src, offset + 2)
} else {
offset += 2
}
} else {
offset += 1
}
ch = src[offset]
}
return offset
}
const endOfSeparator = (src, offset) => {
let ch = src[offset]
let hasEqSign = false
loop: while (ch === '\t' || ch === '\f' || ch === ' ' || ch === '=' || ch === ':' || ch === '\\') {
switch (ch) {
case '\\':
if (src[offset + 1] !== '\n') break loop
offset = endOfIndent(src, offset + 2)
break
case '=':
case ':':
if (hasEqSign) break loop
hasEqSign = true
// fallthrough
default:
offset += 1
}
ch = src[offset]
}
return offset
}
const endOfValue = (src, offset) => {
let ch = src[offset]
while (ch && ch !== '\r' && ch !== '\n') {
offset += ch === '\\' ? 2 : 1
ch = src[offset]
}
return offset
}
const unescape = (str) => str.replace(/\\(u[0-9a-fA-F]{4}|\r?\n[ \t\f]*|.)?/g, (match, code) => {
switch (code && code[0]) {
case 'f': return '\f'
case 'n': return '\n'
case 'r': return '\r'
case 't': return '\t'
case 'u':
const c = parseInt(code.substr(1), 16)
return isNaN(c) ? code : String.fromCharCode(c)
case '\r':
case '\n':
case undefined:
return ''
default:
return code
}
})
/**
* Splits the input string into an array of logical lines
*
* Key-value pairs are [key, value] arrays with string values. Escape sequences
* in keys and values are parsed. Empty lines are included as empty strings, and
* comments as strings that start with '#' or '! characters. Leading whitespace
* is not included.
*
* @see https://docs.oracle.com/javase/9/docs/api/java/util/Properties.html#load(java.io.Reader)
*
* @param {string} src
* @returns Array<string | string[]]>
*/
function parseLines (src) {
const lines = []
for (i = 0; i < src.length; ++i) {
if (src[i] === '\n' && src[i - 1] === '\r') i += 1
if (!src[i]) break
const keyStart = endOfIndent(src, i)
if (atLineEnd(src, keyStart)) {
lines.push('')
i = keyStart
continue
}
if (atComment(src, keyStart)) {
const commentEnd = endOfComment(src, keyStart)
lines.push(src.slice(keyStart, commentEnd))
i = commentEnd
continue
}
const keyEnd = endOfKey(src, keyStart)
const key = unescape(src.slice(keyStart, keyEnd))
const valueStart = endOfSeparator(src, keyEnd)
if (atLineEnd(src, valueStart)) {
lines.push([key, ''])
i = valueStart
continue
}
const valueEnd = endOfValue(src, valueStart)
const value = unescape(src.slice(valueStart, valueEnd))
lines.push([key, value])
i = valueEnd
}
return lines
}
/**
* Parses an input string read from a .properties file into a JavaScript Object
*
* If the second `path` parameter is true, dots '.' in keys will result in a
* multi-level object (use a string value to customise). If a parent level is
* directly assigned a value while it also has a child with an assigned value,
* the parent value will be assigned to its empty string '' key. Repeated keys
* will take the last assigned value. Key order is not guaranteed, but is likely
* to match the order of the input lines.
*
* @param {string} src
* @param {boolean | string} [path=false]
*/
function parse (src, path) {
const pathSep = typeof path === 'string' ? path : '.'
return parseLines(src).reduce((res, line) => {
if (Array.isArray(line)) {
const [key, value] = line
if (path) {
const keyPath = key.split(pathSep)
let parent = res
while (keyPath.length >= 2) {
const p = keyPath.shift()
if (!parent[p]) {
parent[p] = {}
} else if (typeof parent[p] !== 'object') {
parent[p] = { '': parent[p] }
}
parent = parent[p]
}
const leaf = keyPath[0]
if (typeof parent[leaf] === 'object') {
parent[leaf][''] = value
} else {
parent[leaf] = value
}
} else {
res[key] = value
}
}
return res
}, {})
}
module.exports = { parse, parseLines }