forked from eemeli/dot-properties
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringify.js
More file actions
140 lines (133 loc) · 4.49 KB
/
stringify.js
File metadata and controls
140 lines (133 loc) · 4.49 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
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 escapeKey = (str) => escape(str).replace(/[ =:]/g, '\\$&')
const escapeValue = (str) => escape(str).replace(/^ /, '\\ ')
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
}
}, [])
}
/**
* Stringifies a hierarchical object or an array of lines to .properties format
*
* If the input is a hierarchical object, keys will consist of the path parts
* joined by '.' characters. With array input, string values represent blank or
* comment lines and string arrays are [key, value] pairs. The characters \, \n
* and \r will be appropriately escaped. If the `latin1` option is not set to
* false, all non-Latin-1 characters will also be `\u` escaped.
*
* Output styling is controlled by the second options parameter; by default a
* spaced '=' separates the key from the value, '\n' is the newline separator,
* lines are folded at 80 characters, with subsequent lines indented by four
* spaces, and comment lines are prefixed with a '#'. '' as a key value is
* considered the default, and set as the value of a key corresponding to its
* parent object's path.
*
* @param {Object | Array<string | string[]>} input
* @param {Object} [options={}]
* @param {string} [options.commentPrefix='# ']
* @param {string} [options.defaultKey='']
* @param {string} [options.indent=' ']
* @param {string} [options.keySep=' = ']
* @param {boolean} [options.latin1=true]
* @param {number} [options.lineWidth=80]
* @param {string} [options.newline='\n']
* @param {string} [options.pathSep='.']
*/
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 => Array.isArray(line) ? (
foldLine(escapeKey(line[0]) + keySep + escapeValue(line[1]))
) : (
line === '' ? line : foldComment(String(line || '').replace(/^\s*([#!][ \t\f]*)?/g, commentPrefix))
))
.join(newline)
}
module.exports = { stringify }