diff options
| author | stonedDiscord <Tukz@gmx.de> | 2025-06-26 18:25:52 +0200 |
|---|---|---|
| committer | stonedDiscord <Tukz@gmx.de> | 2025-06-26 18:25:52 +0200 |
| commit | 576eacb298a6e4816a16f48e837785924ee2045f (patch) | |
| tree | 592e2e0447bfc2d7bc40351c041cdc241b3eaf83 | |
| parent | 365a515d4916857518bbffb52af9ac919f0a88d1 (diff) | |
iniparse to typescript
| -rw-r--r-- | webAO/iniParse.js | 43 | ||||
| -rw-r--r-- | webAO/iniParse.ts | 58 |
2 files changed, 58 insertions, 43 deletions
diff --git a/webAO/iniParse.js b/webAO/iniParse.js deleted file mode 100644 index 62eeab0..0000000 --- a/webAO/iniParse.js +++ /dev/null @@ -1,43 +0,0 @@ -const regexPatterns = { - section: /^\s*\[\s*([^\]]*)\s*\]\s*$/, - param: /^\s*([\w.\-_]+)\s*=\s*(.*?)\s*$/, - comment: /^\s*;.*$/, -}; - -const valueHandler = (matchKey, matchValue) => - matchKey === "showname" ? matchValue : matchValue.toLowerCase(); - -const lineFilter = (value) => { - const isEmpty = value.length === 0; - const isComment = regexPatterns.comment.test(value); - if (isComment || isEmpty) { - return false; - } - return true; -}; - -const iniParse = (data) => { - const parsedIni = {}; - const lines = data.split(/\r\n|\r|\n/); - const filteredLines = lines.filter(lineFilter); - - let currentSection; - filteredLines.forEach((line) => { - const isParameter = regexPatterns.param.test(line); - const isSection = regexPatterns.section.test(line); - if (isParameter && currentSection) { - const match = line.match(regexPatterns.param); - const matchKey = match[1].toLowerCase(); - const matchValue = match[2]; - parsedIni[currentSection][matchKey] = valueHandler(matchKey, matchValue); - } else if (isSection) { - const match = line.match(regexPatterns.section); - const matchKey = match[1].toLowerCase(); - parsedIni[matchKey] = {}; - currentSection = matchKey; - } - }); - return parsedIni; -}; - -export default iniParse; diff --git a/webAO/iniParse.ts b/webAO/iniParse.ts new file mode 100644 index 0000000..ca84c35 --- /dev/null +++ b/webAO/iniParse.ts @@ -0,0 +1,58 @@ + +interface ParsedIni { + [section: string]: { [key: string]: string }; +} + +const regexPatterns = { + section: /^\s*\[\s*([^\]]*)\s*\]\s*$/, + param: /^\s*([\w.\-_]+)\s*=\s*(.*?)\s*$/, + comment: /^\s*;.*$/, +}; + +const valueHandler = (matchKey: string, matchValue: string): string => { + return matchKey === "showname" ? matchValue : matchValue.toLowerCase(); +}; + +const lineFilter = (value: string): boolean => { + const isEmpty: boolean = value.length === 0; + const isComment: boolean = regexPatterns.comment.test(value); + + if (isComment || isEmpty) { + return false; + } + return true; +}; + +const iniParse = (data: string): ParsedIni => { + const parsedIni: ParsedIni = {}; + const lines: string[] = data.split(/\r\n|\r|\n/); + const filteredLines: string[] = lines.filter(lineFilter); + + let currentSection: string | undefined; + + filteredLines.forEach((line) => { + const isParameter: boolean = regexPatterns.param.test(line); + const isSection: boolean = regexPatterns.section.test(line); + if (isParameter && currentSection) { + const match: RegExpMatchArray | null = line.match(regexPatterns.param); + + if (match) { + const matchKey: string = match[1].toLowerCase(); + const matchValue: string = match[2]; + parsedIni[currentSection][matchKey] = valueHandler(matchKey, matchValue); + } + } else if (isSection) { + const match: RegExpMatchArray | null = line.match(regexPatterns.section); + + if (match) { + const matchKey: string = match[1].toLowerCase(); + parsedIni[matchKey] = {}; + currentSection = matchKey; + } + } + }); + + return parsedIni; +}; + +export default iniParse; |
