-
Notifications
You must be signed in to change notification settings - Fork 1
/
parsers.js
128 lines (126 loc) · 2.95 KB
/
parsers.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
const fs = require("fs");
function parseArray(value) {
if (!value) {
return [];
}
if (Array.isArray(value)) {
return value;
}
if (typeof (value) === "string") {
return value.split("\n").map((line) => line.trim()).filter((line) => line);
}
throw new Error("Unsupported array format");
}
module.exports = {
boolean: (value) => {
if (!value || value === "false") {
return false;
}
return true;
},
text: (value) => {
if (value) {
return value.split("\n");
}
return undefined;
},
number: (value) => {
if (!value) {
return undefined;
}
const parsed = parseInt(value, 10);
if (Number.isNaN(parsed)) {
throw new Error(`Value ${value} is not a valid number`);
}
return parsed;
},
autocomplete: (value, getVal) => {
if (!value) {
return undefined;
}
if (typeof (value) === "object") {
return (getVal ? value.value : value.id) || value;
}
return value;
},
autocompleteOrArray: (value) => {
if (!value) {
return [];
}
if (Array.isArray(value)) {
return value;
}
if (typeof (value) === "object") {
return [value.id || value];
}
return [value];
},
tags: (value, letKeyOnly) => {
if (!value) {
return undefined;
}
if (typeof (value) === "string") {
const obj = {};
value.split("\n").forEach((line) => {
const [key, ...val] = line.trim().split("=");
if (!key || (!letKeyOnly && !val)) {
throw new Error("bad labels/tags format");
}
if (val.length) {
obj[key] = val.join("=");
} else {
obj[key] = "";
}
});
return obj;
}
throw new Error(`Value ${value} is not a valid tags/labels input.`);
},
jsonString: (value) => {
if (!value) {
return undefined;
}
if (typeof (value) === "object") {
return value;
}
if (typeof (value) === "string") {
try {
return JSON.parse(value);
} catch (e) {
throw new Error(`Invalid JSON! ${e.message}`);
}
}
throw new Error(`Value ${value} is not an object`);
},
objectOrFromPath: (value) => {
if (!value) {
return undefined;
}
if (typeof (value) === "string") {
if (!fs.existsSync(value)) {
throw new Error(`Couldn't find file '${value}'.`);
}
const fileContent = fs.readFileSync(value, "utf8");
try {
const obj = JSON.parse(fileContent);
return obj;
} catch {
throw new Error(`The file '${value}' doesn't contain a valid JSON.`);
}
}
if (typeof (value) === "object") {
return value;
}
throw new Error(`value ${value} is not a valid object or a file path`);
},
string: (value) => {
if (!value) {
return undefined;
}
if (typeof (value) === "string") {
return value.trim();
}
throw new Error(`Value ${value} is not a valid string`);
},
array: parseArray,
};