-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathPropertiesFileFormat.js
95 lines (88 loc) · 2.16 KB
/
PropertiesFileFormat.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
const flatstr = typeof chrome === "object" || typeof v8 === "object" ? (s, iConcatOps) => {
if (iConcatOps > 2 && 40 * iConcatOps > s.length) {
Number(s);
}
return s;
} : s => s;
const rLines = /(?:\r\n|\r|\n|^)[ \t\f]*/;
const rEscapesOrSeparator = /(\\u[0-9a-fA-F]{0,4})|(\\.)|(\\$)|([ \t\f]*[ \t\f:=][ \t\f]*)/g;
const rEscapes = /(\\u[0-9a-fA-F]{0,4})|(\\.)|(\\$)/g;
const mEscapes = {
"\\f": "\f",
"\\n": "\n",
"\\r": "\r",
"\\t": "\t",
};
/**
* Parses a .properties format
* @param {string} sText the contents a of a .properties file
* @returns a object with key/value pairs parsed from the .properties file format
* @public
*/
const parseProperties = sText => {
const properties = {},
aLines = sText.split(rLines);
let sLine,
rMatcher,
sKey,
sValue,
i,
m,
iLastIndex,
iConcatOps;
const append = s => {
if (sValue) {
sValue = `${sValue}${s}`;
iConcatOps++;
} else {
sValue = s;
iConcatOps = 0;
}
};
for (i = 0; i < aLines.length; i++) {
sLine = aLines[i];
const skipLine = sLine === "" || sLine.charAt(0) === "#" || sLine.charAt(0) === "!";
if (!skipLine) {
rMatcher = rEscapesOrSeparator;
iLastIndex = 0;
rMatcher.lastIndex = iLastIndex;
sKey = null;
sValue = "";
m = rMatcher.exec(sLine);
while (m !== null) {
if (iLastIndex < m.index) {
append(sLine.slice(iLastIndex, m.index));
}
iLastIndex = rMatcher.lastIndex;
if (m[1]) {
if (m[1].length !== 6) {
throw new Error(`Incomplete Unicode Escape '${m[1]}'`);
}
append(String.fromCharCode(parseInt(m[1].slice(2), 16)));
} else if (m[2]) {
append(mEscapes[m[2]] || m[2].slice(1));
} else if (m[3]) {
sLine = aLines[++i];
iLastIndex = 0;
rMatcher.lastIndex = iLastIndex;
} else if (m[4]) {
sKey = sValue;
sValue = "";
rMatcher = rEscapes;
rMatcher.lastIndex = iLastIndex;
}
m = rMatcher.exec(sLine);
}
if (iLastIndex < sLine.length) {
append(sLine.slice(iLastIndex));
}
if (sKey == null) {
sKey = sValue;
sValue = "";
}
properties[sKey] = flatstr(sValue, sValue ? iConcatOps : 0);
}
}
return properties;
};
export default parseProperties;