-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
45 lines (36 loc) · 1.12 KB
/
index.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
module.exports = function (content, map, meta) {
const value = convertPhpArrayToJson(content);
return `module.exports = ${value}`;
};
function convertPhpArrayToJson(phpArray) {
const lines = phpArray.split("\n").filter(line => {
return line.indexOf("=>") > 0;
});
const obj = {};
lines.forEach(line => {
let [key, text] = line.split("=>");
key = trimQuotes(key.trim());
text = trimQuotes(text.trim());
obj[key] = text;
});
const value = JSON.stringify(obj)
.replace(/\u2028/g, "\\u2028")
.replace(/\u2029/g, "\\u2029");
return value;
}
function trimQuotes(text) {
text = text.replace(/(.+),$/, "$1").replace("\\'", "'");
if (text.startsWith(`'`)) {
return text.replace(/^'(.+)'$/, "$1");
}
if (text.startsWith(`"`)) {
return text.replace(/^"(.+)"$/, "$1");
}
if (text.match(/^[0-9]+$/)) {
return parseInt(text, 10);
}
if (text.toLowerCase() === "true" || text.toLowerCase() === "false") {
return text.toLowerCase() === "true";
}
throw Error("Unsupported quotes to trim.");
}