Remove helper functions in favor of using a smarter next() function in while loops.
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
{
|
||||
"type": "module",
|
||||
"dependencies": {
|
||||
"@terrace/core": "0.0.1"
|
||||
"@terrace/core": "workspace:*"
|
||||
},
|
||||
"scripts": {
|
||||
"example": "node ./parsers/v5/example.js"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,24 +70,55 @@ const lines = [
|
||||
|
||||
|
||||
async function main() {
|
||||
const { toLineArray } = useDocument(createStringReader(lines))
|
||||
console.dir(await toLineArray(), { depth: null })
|
||||
const { head, tail, next, match, level, line } = useDocument(createStringReader(lines))
|
||||
|
||||
const { head, tail, each, match, toArray, toObject } = useDocument(createStringReader(lines))
|
||||
const structure = {}
|
||||
|
||||
const structure = await toObject({
|
||||
'name': true,
|
||||
'version': () => tail().trim(),
|
||||
'license': () => tail().trim(),
|
||||
'exports': () => toObject({
|
||||
'#any': () => toObject({ import: true, require: true })
|
||||
}),
|
||||
'scripts': () => toObject({ '#any': true }),
|
||||
'devDependencies': () => toObject(),
|
||||
'authors': () => toArray({
|
||||
'author': () => toObject({ name: true, email: true, '#text': true })
|
||||
}),
|
||||
})
|
||||
async function kvObject(handle) {
|
||||
const obj = {}
|
||||
const l = level()
|
||||
while (await next(l)) {
|
||||
if (!head()) continue
|
||||
obj[head()] = handle ? await handle(level()) : tail().trim()
|
||||
}
|
||||
return obj
|
||||
}
|
||||
|
||||
while (await next()) {
|
||||
if (match('name')) structure.name = tail().trim()
|
||||
if (match('version')) structure.version = tail().trim()
|
||||
if (match('exports')) structure.exports = await kvObject(async l => {
|
||||
const obj = {}
|
||||
while (await next(l)) {
|
||||
if (match('import')) obj.import = tail().trim()
|
||||
if (match('require')) obj.require = tail().trim()
|
||||
}
|
||||
return obj
|
||||
})
|
||||
|
||||
if (match('scripts')) structure.scripts = await kvObject()
|
||||
if (match('devDependencies')) structure.devDependencies = await kvObject()
|
||||
|
||||
if (match('author')) {
|
||||
if (!structure.authors) structure.authors = []
|
||||
const author = {}
|
||||
structure.authors.push(author)
|
||||
|
||||
const l = level()
|
||||
while (await next(l)) {
|
||||
if (!head()) continue
|
||||
if (match('name')) author.name = tail().trim()
|
||||
else if (match('email')) author.email = tail().trim()
|
||||
else {
|
||||
if (!author['#text']) author['#text'] = [line()]
|
||||
// Loop through all remaining lines to avoid re-matching name or email above.
|
||||
while(await next(l)) {
|
||||
author['#text'].push(line())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
console.dir(structure, { depth: null })
|
||||
}
|
||||
Reference in New Issue
Block a user