Add: sample ini parser

This commit is contained in:
Dreamacro 2018-09-26 15:05:41 +08:00
parent 6b77ac5e9c
commit 002bfec657
2 changed files with 37 additions and 0 deletions

36
src/lib/ini.ts Normal file
View File

@ -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<string, string>())
const iniParser = (text = '') => {
const section = new Map<string, string>()
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
}

View File

@ -9,6 +9,7 @@
"jsx": "react",
"lib": ["es5", "es6", "dom"],
"experimentalDecorators": true,
"downlevelIteration": true,
"baseUrl": ".",
"paths": {
"@assets": ["src/assets"],