forked from reisxd/revanced-builder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
opteric.js
81 lines (65 loc) · 1.89 KB
/
opteric.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
/**
* @typedef {object} FlagsAndOptions
* @prop {string[]} flags The parsed flags.
* @prop {Record<string, string>} options The parsed options.
* @prop {string} content The content of the text without the flags and options.
*/
/**
* Parses flags and options from text.
*
* @param {string} text The text to parse.
* @returns {FlagsAndOptions} The parsed flags and options.
*/
function parse (text) {
if (typeof text !== 'string') {
throw new TypeError("Argument 'text' must be of type string.");
}
const flags = [];
const options = {};
const len = text.length + 1;
let content;
for (
let i = 0,
parsing = '',
isParsing = false,
optContent = '',
isParsingContent = false;
i < len;
i++
) {
const char = text[i];
const charPrev = text[i - 1];
const charNext = text[i + 1];
if (isParsing) {
if (char === ' ' || char === undefined) {
isParsing = false;
if (charNext !== ' ' && charNext !== '-' && charNext !== undefined) {
isParsingContent = true;
continue;
}
flags.push(parsing);
parsing = '';
if (content[0] === '-') content = '';
} else parsing += char;
} else if (isParsingContent) {
if ((char === ' ' && charNext === '-') || char === undefined) {
isParsingContent = false;
options[parsing] = optContent;
parsing = '';
optContent = '';
} else optContent += char;
} else if ((charPrev === ' ' || charPrev === undefined) && char === '-') {
const truncateLen = i - 1;
i++;
isParsing = true;
if (charNext === '-') i++;
if (content === undefined) {
content = charPrev === undefined ? text : text.slice(0, truncateLen);
}
parsing += text[i];
}
}
if (content === undefined) content = text;
return { flags, options, content };
}
module.exports = parse;