diff options
| author | caleb.mabry.15@cnu.edu <caleb.mabry.15@cnu.edu> | 2022-03-07 12:43:33 -0500 |
|---|---|---|
| committer | caleb.mabry.15@cnu.edu <caleb.mabry.15@cnu.edu> | 2022-03-07 12:43:33 -0500 |
| commit | 4bd674c196c0e06b460dffee440c5d2a48061bcc (patch) | |
| tree | 2338cf231c8535280c283d8d08d91f4fa4018912 /webAO/iniParse.js | |
| parent | 01a7239f8b18710ce1871c502fd8d10a782efcaf (diff) | |
| parent | 9c5fd198c11a0e2b976c6a2802eff9c4fef836f6 (diff) | |
Merge branch 'master' of https://github.com/AttorneyOnline/webAO into refactor-constants
Diffstat (limited to 'webAO/iniParse.js')
| -rw-r--r-- | webAO/iniParse.js | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/webAO/iniParse.js b/webAO/iniParse.js new file mode 100644 index 0000000..fb04e67 --- /dev/null +++ b/webAO/iniParse.js @@ -0,0 +1,42 @@ +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; |
