diff --git a/src/lib/ini.ts b/src/lib/ini.ts new file mode 100644 index 0000000..4a8a490 --- /dev/null +++ b/src/lib/ini.ts @@ -0,0 +1,36 @@ +const sectionExpr = /^\[(.*)\]/ +const lineBreak = /\r?\n/g + +const isSectionLine = (line: string) => sectionExpr.test(line) +const formatSection = (text: string) => + text.split(lineBreak) + .map(t => t.trim()) + .filter(t => t && t[0] !== ';') + .map(t => t.split('=', 2)) + .filter(pair => pair.length === 2) + .reduce((map, [key, value]) => map.set(key.trim(), value.trim()), new Map()) + +const iniParser = (text = '') => { + const section = new Map() + if (text.length === 0) return + const lines = text.split(lineBreak) + let content: string[] = [] + let sectionName = '' + for (const line of lines) { + if (isSectionLine(line)) { + if (sectionName !== '') { + section.set(sectionName, content.join('\n')) + } + content = [] + const match = line.match(sectionExpr) + sectionName = match && match[1] + } else { + content.push(line) + } + } + + if (sectionName !== '') { + section.set(sectionName, content.join('\n')) + } + return section +} diff --git a/tsconfig.json b/tsconfig.json index 5d24d92..d872cb0 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -9,6 +9,7 @@ "jsx": "react", "lib": ["es5", "es6", "dom"], "experimentalDecorators": true, + "downlevelIteration": true, "baseUrl": ".", "paths": { "@assets": ["src/assets"],