Skip to content

Commit

Permalink
Small code simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
Tpt committed Aug 25, 2022
1 parent 732a605 commit 8991e71
Showing 1 changed file with 50 additions and 62 deletions.
112 changes: 50 additions & 62 deletions lib/JsonLdParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export class JsonLdParser extends Transform implements RDF.Sink<EventEmitter, RD
// A promise representing the last job
private lastOnValueJob: Promise<void>;
// The keys inside of the JSON tree
private readonly jsonKeyStack: (string | number)[];
private readonly jsonKeyStack: (string | number | undefined)[];
// The value inside of the JSON tree
private readonly jsonValueStack: any[];

Expand Down Expand Up @@ -410,80 +410,72 @@ export class JsonLdParser extends Transform implements RDF.Sink<EventEmitter, RD
* This should only be called once.
*/
protected onJsonEvent(event: JsonEvent) {
let key: any;
let value: any;
switch (event.type) {
case 'open-object':
this.insertInStack(event.key, {}, true);
return;
this.jsonKeyStack.push(event.key);
this.insertContainerInValueStack(event.key, {});
break;
case 'open-array':
this.insertInStack(event.key, [], true);
return;
this.jsonKeyStack.push(event.key);
this.insertContainerInValueStack(event.key, []);
break;
case 'value':
this.insertInStack(event.key, event.value, false);
key = event.key;
value = event.value;
if (!this.jsonKeyStack.includes('@context')) { // Don't parse inner nodes inside @context
this.onValue(event.key, event.value, this.jsonKeyStack);
}
if (typeof event.key === 'string') {
this.jsonValueStack[this.jsonValueStack.length - 1][event.key] = event.value;
} else if (typeof event.key === 'number') {
this.jsonValueStack[this.jsonValueStack.length - 1].push(event.value);
}
break;
case 'close-object':
case 'close-array':
key = this.jsonKeyStack[this.jsonKeyStack.length - 1];
value = this.jsonValueStack[this.jsonValueStack.length - 1];
const key = this.jsonKeyStack.pop();
const value = this.jsonValueStack.pop();
if (!this.jsonKeyStack.includes('@context')) { // Don't parse inner nodes inside @context
this.onValue(key, value, this.jsonKeyStack);
}
}
}

const depth = this.jsonKeyStack.length;
const keys = <string[]><any[]>[undefined, ...this.jsonKeyStack];
protected onValue(key: string | number | undefined, value: any, keyStack: (string | number | undefined)[]) {
const depth = keyStack.length;
const keys = <string[]><any[]>[...keyStack, key];

if (!this.isParsingContextInner()) { // Don't parse inner nodes inside @context
const valueJobCb = () => this.newOnValueJob(keys, value, depth, true);
if (!this.parsingContext.streamingProfile
const valueJobCb = () => this.newOnValueJob(keys, value, depth, true);
if (!this.parsingContext.streamingProfile
&& !this.parsingContext.contextTree.getContext(keys.slice(0, -1))) {
// If an out-of-order context is allowed,
// we have to buffer everything.
// We store jobs for @context's and @type's separately,
// because at the end, we have to process them first.
// We also handle @type because these *could* introduce a type-scoped context.
if (key === '@context') {
let jobs = this.contextJobs[depth];
if (!jobs) {
jobs = this.contextJobs[depth] = [];
}
jobs.push(valueJobCb);
} else if (key === '@type'
|| typeof key === 'number' && this.jsonKeyStack[this.jsonKeyStack.length - 2] === '@type') { // Also capture @type with array values
// Remove @type from keys, because we want it to apply to parent later on
this.typeJobs.push({ job: valueJobCb, keys: keys.slice(0, keys.length - 1) });
} else {
this.contextAwaitingJobs.push({ job: valueJobCb, keys });
// If an out-of-order context is allowed,
// we have to buffer everything.
// We store jobs for @context's and @type's separately,
// because at the end, we have to process them first.
// We also handle @type because these *could* introduce a type-scoped context.
if (key === '@context') {
let jobs = this.contextJobs[depth];
if (!jobs) {
jobs = this.contextJobs[depth] = [];
}
jobs.push(valueJobCb);
} else if (key === '@type'
|| typeof key === 'number' && keys[keys.length - 2] === '@type') { // Also capture @type with array values
// Remove @type from keys, because we want it to apply to parent later on
this.typeJobs.push({ job: valueJobCb, keys: keys.slice(0, keys.length - 1) });
} else {
// Make sure that our value jobs are chained synchronously
this.lastOnValueJob = this.lastOnValueJob.then(valueJobCb);
}

// Execute all buffered jobs on deeper levels
if (!this.parsingContext.streamingProfile && depth === 0) {
this.lastOnValueJob = this.lastOnValueJob
.then(() => this.executeBufferedJobs());
this.contextAwaitingJobs.push({ job: valueJobCb, keys });
}
} else {
// Make sure that our value jobs are chained synchronously
this.lastOnValueJob = this.lastOnValueJob.then(valueJobCb);
}

switch (event.type) {
case 'close-object':
case 'close-array':
this.jsonValueStack.pop();
case "value":
this.jsonKeyStack.pop();
// Execute all buffered jobs on deeper levels
if (!this.parsingContext.streamingProfile && depth === 0) {
this.lastOnValueJob = this.lastOnValueJob
.then(() => this.executeBufferedJobs());
}
}

/**
* Check if the parser is currently parsing an element that is part of an @context entry.
* @return {boolean} A boolean.
*/
protected isParsingContextInner() {
return this.jsonKeyStack.slice(0, -1).includes('@context');
}

/**
* Execute all buffered jobs.
* @return {Promise<void>} A promise resolving if all jobs are finished.
Expand Down Expand Up @@ -537,17 +529,13 @@ export class JsonLdParser extends Transform implements RDF.Sink<EventEmitter, RD
}
}

private insertInStack(key: string | number | undefined, value: any, push: boolean): void {
private insertContainerInValueStack(key: string | number | undefined, value: any): void {
if (typeof key === 'string') {
this.jsonKeyStack.push(key);
this.jsonValueStack[this.jsonValueStack.length - 1][key] = value;
} else if (typeof key === 'number') {
this.jsonKeyStack.push(key);
this.jsonValueStack[this.jsonValueStack.length - 1].push(value);
}
if (push) {
this.jsonValueStack.push(value);
}
this.jsonValueStack.push(value);
}
}

Expand Down

0 comments on commit 8991e71

Please sign in to comment.