From 8991e7106329fe9a66279b75208eb295b91a7055 Mon Sep 17 00:00:00 2001 From: Tpt Date: Wed, 24 Aug 2022 21:13:10 +0200 Subject: [PATCH] Small code simplification --- lib/JsonLdParser.ts | 112 ++++++++++++++++++++------------------------ 1 file changed, 50 insertions(+), 62 deletions(-) diff --git a/lib/JsonLdParser.ts b/lib/JsonLdParser.ts index 56c06d3..5e3bb91 100644 --- a/lib/JsonLdParser.ts +++ b/lib/JsonLdParser.ts @@ -62,7 +62,7 @@ export class JsonLdParser extends Transform implements RDF.Sink; // 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[]; @@ -410,80 +410,72 @@ export class JsonLdParser extends Transform implements RDF.Sink[undefined, ...this.jsonKeyStack]; + protected onValue(key: string | number | undefined, value: any, keyStack: (string | number | undefined)[]) { + const depth = keyStack.length; + const keys = [...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} A promise resolving if all jobs are finished. @@ -537,17 +529,13 @@ export class JsonLdParser extends Transform implements RDF.Sink