refactor IDV parser to use map why not
This commit is contained in:
parent
6c6c9386be
commit
af07866ac0
2 changed files with 11 additions and 8 deletions
18
lib/idv.ts
18
lib/idv.ts
|
@ -7,7 +7,7 @@ const LEADING_WHITESPACE = /^([ \t]+)/;
|
||||||
const ENTRY = /^(.+?):\s*(.*)/;
|
const ENTRY = /^(.+?):\s*(.*)/;
|
||||||
|
|
||||||
export class Idv {
|
export class Idv {
|
||||||
collections: Record<string, undefined | [string, string[]][]> = {};
|
collections: Map<string, undefined | [string, string[]][]> = new Map();
|
||||||
|
|
||||||
public static parse(input: string): Idv {
|
public static parse(input: string): Idv {
|
||||||
const idv = new Idv();
|
const idv = new Idv();
|
||||||
|
@ -53,14 +53,16 @@ export class Idv {
|
||||||
if (matches) {
|
if (matches) {
|
||||||
const [, collection, distinguisher] = matches;
|
const [, collection, distinguisher] = matches;
|
||||||
|
|
||||||
if (this.collections[collection] == undefined) {
|
if (!this.collections.has(collection)) {
|
||||||
this.collections[collection] = [];
|
this.collections.set(collection, []);
|
||||||
}
|
}
|
||||||
|
|
||||||
currentDocument = [];
|
currentDocument = [];
|
||||||
currentIndent = undefined;
|
currentIndent = undefined;
|
||||||
// TODO: implement backslash escaping in the distinguisher
|
// TODO: implement backslash escaping in the distinguisher
|
||||||
this.collections[collection].push([distinguisher, currentDocument]);
|
this.collections
|
||||||
|
.get(collection)!
|
||||||
|
.push([distinguisher, currentDocument]);
|
||||||
} else {
|
} else {
|
||||||
throw new Error("Failed to parse a property");
|
throw new Error("Failed to parse a property");
|
||||||
}
|
}
|
||||||
|
@ -71,12 +73,12 @@ export class Idv {
|
||||||
}
|
}
|
||||||
|
|
||||||
public getProperty<T>(name: string, parse: UnionParser<T>): T | undefined {
|
public getProperty<T>(name: string, parse: UnionParser<T>): T | undefined {
|
||||||
const firstEntry = this.collections[name]?.[0];
|
const firstEntry = this.collections.get(name)?.[0];
|
||||||
return firstEntry && parse(firstEntry[0], firstEntry[1]);
|
return firstEntry && parse(firstEntry[0], firstEntry[1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public getList<T>(name: string, parse: UnionParser<T>): T[] {
|
public getList<T>(name: string, parse: UnionParser<T>): T[] {
|
||||||
return (this.collections[name] ?? []).map(([distinguisher, document]) =>
|
return (this.collections.get(name) ?? []).map(([distinguisher, document]) =>
|
||||||
parse(distinguisher, document)
|
parse(distinguisher, document)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -86,7 +88,7 @@ export class Idv {
|
||||||
parseDocument: DocumentParser<T>
|
parseDocument: DocumentParser<T>
|
||||||
): Record<string, T> {
|
): Record<string, T> {
|
||||||
return Object.fromEntries(
|
return Object.fromEntries(
|
||||||
(this.collections[name] ?? []).map(([distinguisher, document]) => [
|
(this.collections.get(name) ?? []).map(([distinguisher, document]) => [
|
||||||
distinguisher,
|
distinguisher,
|
||||||
parseDocument(document),
|
parseDocument(document),
|
||||||
])
|
])
|
||||||
|
@ -99,7 +101,7 @@ export class Idv {
|
||||||
): Record<string, T> {
|
): Record<string, T> {
|
||||||
const idvMap: Map<string, Idv> = new Map();
|
const idvMap: Map<string, Idv> = new Map();
|
||||||
|
|
||||||
(this.collections[name] ?? []).forEach(([distinguisher, document]) => {
|
(this.collections.get(name) ?? []).forEach(([distinguisher, document]) => {
|
||||||
let idv = idvMap.get(distinguisher);
|
let idv = idvMap.get(distinguisher);
|
||||||
if (idv == undefined) {
|
if (idv == undefined) {
|
||||||
idvMap.set(distinguisher, (idv = new Idv()));
|
idvMap.set(distinguisher, (idv = new Idv()));
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
"isolatedModules": true,
|
"isolatedModules": true,
|
||||||
"lib": ["ES2019", "DOM"],
|
"lib": ["ES2019", "DOM"],
|
||||||
"strict": true,
|
"strict": true,
|
||||||
|
"target": "ES2020",
|
||||||
"noEmit": true
|
"noEmit": true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue