Context util now supports typeNames

main
Jackson Morgan 2 years ago
parent 0c2f981356
commit 9b3632e177
  1. 143
      packages/jsonld-dataset-proxy/src/ContextUtil.ts

@ -1,5 +1,8 @@
import type { ContextDefinition, ExpandedTermDefinition } from "jsonld"; import type { ContextDefinition, ExpandedTermDefinition } from "jsonld";
import type { LdoJsonldContext } from "./LdoJsonldContext"; import type {
LdoJsonldContext,
LdoJsonldContextExpandedTermDefinition,
} from "./LdoJsonldContext";
// Create JSONLD Shorthands // Create JSONLD Shorthands
const shorthandToIriMap: Record<string, string> = { const shorthandToIriMap: Record<string, string> = {
@ -13,38 +16,70 @@ const shorthandToIriMap: Record<string, string> = {
export class ContextUtil { export class ContextUtil {
public readonly context: ContextDefinition | LdoJsonldContext; public readonly context: ContextDefinition | LdoJsonldContext;
private iriToKeyMap: Record<string, string>; private iriToKeyMap: Record<string, string>;
private typeNameToIriToKeyMap: Record<string, Record<string, string>>;
constructor(context: ContextDefinition | LdoJsonldContext) { constructor(context: ContextDefinition | LdoJsonldContext) {
this.context = context; this.context = context;
this.iriToKeyMap = {}; this.iriToKeyMap = {};
// Create the iriToKeyMap
this.iriToKeyMap = this.createIriToKeyMap(context);
this.typeNameToIriToKeyMap = {};
Object.entries(context).forEach(([contextKey, contextValue]) => {
if (
typeof contextValue === "object" &&
contextValue !== null &&
!!contextValue["@id"] &&
(contextValue as ExpandedTermDefinition)["@context"]
) {
this.typeNameToIriToKeyMap[contextKey] = this.createIriToKeyMap(
contextValue["@context"],
);
}
});
}
private createIriToKeyMap(
context: ContextDefinition,
): Record<string, string> {
const iriToKeyMap = {};
Object.entries(context).forEach(([contextKey, contextValue]) => { Object.entries(context).forEach(([contextKey, contextValue]) => {
if (typeof contextValue === "string") { if (typeof contextValue === "string") {
this.iriToKeyMap[this.keyIdToIri(contextValue)] = contextKey; iriToKeyMap[this.keyIdToIri(contextValue)] = contextKey;
} else if ( } else if (
typeof contextValue === "object" && typeof contextValue === "object" &&
// eslint-disable-next-line @typescript-eslint/no-explicit-any contextValue !== null &&
(contextValue as any)["@id"] !!contextValue["@id"]
) { ) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any const iri = this.keyIdToIri(contextValue["@id"]);
this.iriToKeyMap[this.keyIdToIri((contextValue as any)["@id"])] = iriToKeyMap[iri] = contextKey;
contextKey;
} }
}); });
return iriToKeyMap;
} }
public keyToIri(key: string): string { /**
if (!this.context[key]) { * Helper method that gets the relevant context to use if a typename is
return key; * provided
} else if (typeof this.context[key] === "string") { */
return this.keyIdToIri(this.context[key] as string); private getRelevantContext(
// eslint-disable-next-line @typescript-eslint/no-explicit-any key: string,
} else if (this.context[key] && (this.context[key] as any)["@id"]) { typeName?: string,
// eslint-disable-next-line @typescript-eslint/no-explicit-any ): ContextDefinition | LdoJsonldContext {
return this.keyIdToIri((this.context[key] as any)["@id"]); if (
typeName &&
typeof this.context[typeName] === "object" &&
this.context[typeName]?.["@context"] &&
this.context[typeName]?.["@context"][key]
) {
return this.context[typeName]?.["@context"];
} }
return key; return this.context;
} }
/**
* Helper function that applies shorthands to keys
*/
private keyIdToIri(keyId: string) { private keyIdToIri(keyId: string) {
if (shorthandToIriMap[keyId]) { if (shorthandToIriMap[keyId]) {
return shorthandToIriMap[keyId]; return shorthandToIriMap[keyId];
@ -53,38 +88,78 @@ export class ContextUtil {
} }
} }
public iriToKey(iri: string): string { /**
if (this.iriToKeyMap[iri]) { * Converts a given JsonLd key into an RDF IRI
return this.iriToKeyMap[iri]; */
public keyToIri(key: string, typeName: string): string {
const relevantContext = this.getRelevantContext(key, typeName);
if (!relevantContext[key]) {
return key;
} else if (typeof relevantContext[key] === "string") {
return this.keyIdToIri(relevantContext[key] as string);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} else if (relevantContext[key] && (relevantContext[key] as any)["@id"]) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return this.keyIdToIri((relevantContext[key] as any)["@id"]);
}
return key;
}
/**
* Converts a given RDF IRI into the JsonLd key
*/
public iriToKey(iri: string, typeName: string): string {
const relevantMap =
this.typeNameToIriToKeyMap[typeName] || this.iriToKeyMap;
if (relevantMap[iri]) {
return relevantMap[iri];
} }
return iri; return iri;
} }
public getType(key: string): string { /**
* Returns the IRI of a datatype of a specific object
*/
public getDataType(key: string, typeName: string): string {
const relevantContext = this.getRelevantContext(key, typeName);
if ( if (
typeof this.context[key] === "object" && typeof relevantContext[key] === "object" &&
(this.context[key] as ExpandedTermDefinition)["@type"] (relevantContext[key] as ExpandedTermDefinition)["@type"]
) { ) {
return (this.context[key] as ExpandedTermDefinition)["@type"] as string; return (relevantContext[key] as ExpandedTermDefinition)[
"@type"
] as string;
} }
return "http://www.w3.org/2001/XMLSchema#string"; return "http://www.w3.org/2001/XMLSchema#string";
} }
public isArray(key: string): boolean { /**
* Returns true if the object is a collection
*/
public isArray(key: string, typeName: string): boolean {
const relevantContext = this.getRelevantContext(key, typeName);
return !!( return !!(
this.context[key] && relevantContext[key] &&
typeof this.context[key] === "object" && typeof relevantContext[key] === "object" &&
(this.context[key] as ExpandedTermDefinition)["@container"] && (relevantContext[key] as ExpandedTermDefinition)["@container"] &&
(this.context[key] as ExpandedTermDefinition)["@container"] === "@set" ((relevantContext[key] as ExpandedTermDefinition)["@container"] ===
"@set" ||
(relevantContext[key] as LdoJsonldContextExpandedTermDefinition)[
"@isCollection"
])
); );
} }
public isLangString(key: string): boolean { /**
* Returns true if the object is a language string
*/
public isLangString(key: string, typeName: string): boolean {
const relevantContext = this.getRelevantContext(key, typeName);
return !!( return !!(
this.context[key] && relevantContext[key] &&
typeof this.context[key] === "object" && typeof relevantContext[key] === "object" &&
(this.context[key] as ExpandedTermDefinition)["@type"] && (relevantContext[key] as ExpandedTermDefinition)["@type"] &&
(this.context[key] as ExpandedTermDefinition)["@type"] === (relevantContext[key] as ExpandedTermDefinition)["@type"] ===
"http://www.w3.org/1999/02/22-rdf-syntax-ns#langString" "http://www.w3.org/1999/02/22-rdf-syntax-ns#langString"
); );
} }

Loading…
Cancel
Save