This repository was archived by the owner on Aug 30, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsson.js
More file actions
95 lines (62 loc) · 2.48 KB
/
Copy pathsson.js
File metadata and controls
95 lines (62 loc) · 2.48 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
const trySetObjects = (rawObjectData) => {
if(!Array.isArray(rawObjectData)) return "expected an array of strings";
let readingObject = false, readingDefault = false;
let lineCount = 0;
let currentObject;
let objects = {}, defaultValues = {};
for (const str of rawObjectData) {
++lineCount;
if (str.startsWith("#") || str.length === 0) continue;
if (str.startsWith(".")) {
if (!readingObject)
return `${str} at line ${lineCount} is supposed to be a property,
however it is cut off from its parent object. You probably misplaced a
; just before that line.`;
let keyValuePair = str.split("=");
if (keyValuePair.length < 2)
return `expected a value after ${str} at line ${lineCount}; property
cannot be empty.`;
if (keyValuePair.length > 2)
keyValuePair[1] = keyValuePair.slice(1).join("=");
// remove the dot at the beginning of the attribute
keyValuePair[0] = keyValuePair[0].slice(1);
// removes the ; from the value if it's at the end of it
if (keyValuePair[1].endsWith(";"))
keyValuePair[1] = keyValuePair[1].slice(0, keyValuePair[1].length - 1);
keyValuePair[0] = keyValuePair[0].trim();
keyValuePair[1] = keyValuePair[1].trim();
if (readingDefault) {
if (!defaultValues.hasOwnProperty(currentObject)) defaultValues[currentObject] = {};
defaultValues[currentObject][keyValuePair[0]] = keyValuePair[1];
}
else {
if (!objects.hasOwnProperty(currentObject)) objects[currentObject] = {};
objects[currentObject][keyValuePair[0]] = keyValuePair[1];
}
}
else if (str !== ";") {
if (readingObject) return `expected a ; before line ${lineCount}`;
if (str.startsWith("default")) {
readingObject = true;
readingDefault = true;
// removes the default part of the string
currentObject = str.slice(7).trim();
}
else {
const cleanStr = str.trim();
currentObject = `${cleanStr}_${lineCount}`;
readingObject = true;
if (defaultValues.hasOwnProperty(cleanStr)) {
objects[currentObject] = {};
for (const property in defaultValues[cleanStr])
objects[currentObject][property] = defaultValues[cleanStr][property];
}
}
}
if (str.endsWith(";")) {
readingObject = false;
readingDefault = false;
}
}
return objects;
};