-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
163 lines (148 loc) · 3.77 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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
export const defaultDictionary = {}
export const defaultConfig = {
dictionary: defaultDictionary,
extractIndent: extractIndent,
removeSingleLineComments: removeSingleLineComments,
getOrder: getOrder,
extractIndicator: extractIndicator,
extractName: extractName,
extractMetaData: extractMetaData,
extractComments: extractComments
}
export function toJSON(content, config) {
const {
dictionary,
extractIndent,
removeSingleLineComments,
getOrder,
extractIndicator,
extractName,
extractMetaData,
extractComments
} = { ...defaultConfig, ...config };
const items = [];
const lines = content.trim().split("\n");
for (let i = 0; i < lines.length; i++) {
const indent = extractIndent(lines[i]);
const line = removeSingleLineComments(lines[i]);
const newItem = {
lineNumber: i,
order: getOrder(i),
indent: indent,
indicator: extractIndicator(line),
name: extractName(line),
metaData: extractMetaData(line, dictionary),
comment: extractComments(lines[i], "//"),
subItems: [],
};
findParentArray(items, "indent", indent, "subItems").push(
newItem
);
}
return items[0];
}
export function toOML(obj, config) {
let str='';
const items = [obj];
let indent = null;
let indicator = null;
parse(items, indent, indicator);
function parse(items, indent, indicator) {
items.forEach((item) => {
str +=
" ".repeat(item.indent) +
item.indicator + " " +
item.name + ", " +
parseMetaData(item.metaData) +
(item.comment!="" ? " //" + item.comment : '')
+ "\n";
console.log("str", str);
if (item.subItems.length > 0) {
parse(item.subItems, indent, indicator);
}
});
return str;
}
return str;
}
export function findParentArray(array, key, value, arrayKey) {
for (let i = array.length - 1; i >= 0; i--) {
const obj = array[i];
if (obj[key] === value) {
return array;
} else if (obj[arrayKey]) {
const result = findParentArray(obj[arrayKey], key, value, arrayKey);
if (result) {
return result;
}
}
}
return array;
}
export function parseMetaData(obj, dictionary) {
console.log(obj);
let str = '';
for (let key in obj) {
str += ' ' + key + ' ';
if (Array.isArray(obj[key])) {
str += obj[key].join(' ') + ',';
} else {
str += obj[key] + ',v';
}
}
return str; //.slice(0, -1);
}
export function getOrder(i) {
return i;
}
export function extractComments(str, subStr) {
const index = str.indexOf(subStr);
if (index === -1) {
return "";
} else {
return str.substring(index + subStr.length);
}
}
export function extractName(str) {
return str.trim().split(",")[0].split(/\s+/).slice(1).join(" ");
}
export function extractIndicator(str) {
return str.trim().split(/\s+/)[0];
}
export function extractIndent(str) {
return str.search(/\S/);
}
export function extractMetaData(str, dictionary) {
const obj = {};
const pairs = str.split(",").slice(1);
pairs.forEach((pair) => {
const [key, ...values] = pair.trim().split(/\s+/);
if (obj[key]) {
obj[key].push(...values);
} else {
obj[key] = values;
}
});
return obj;
}
export function getLine(str, lineNumber) {
const lines = str.split("\n");
if (lineNumber < 1 || lineNumber > lines.length) {
return "";
} else {
return lines[lineNumber - 1];
}
}
export function getItem(str, indent, number) {
const re = new RegExp(`^ {${indent}}\\S`);
const lines = str.split("\n");
const matches = lines.filter((line) => re.test(line));
if (number < 1 || number > matches.length) {
return "";
} else {
return matches[number - 1].trim();
}
}
export function removeSingleLineComments(str) {
return str.replace(/\/\/.*$/gm, '');
}