Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
public predicateToTerm(context: IJsonLdContextNormalized, key: string): RDF.Term {
const expanded: string = ContextParser.expandTerm(key, context, true);
// Immediately return if the predicate was disabled in the context
if (!expanded) {
return null;
}
// Check if the predicate is a blank node
if (expanded[0] === '_' && expanded[1] === ':') {
if (this.parsingContext.produceGeneralizedRdf) {
return this.dataFactory.blankNode(expanded.substr(2));
} else {
return null;
}
}
// Check if the predicate is a valid IRI
public resourceToTerm(context: IJsonLdContextNormalized, key: string): RDF.Term {
if (key.startsWith('_:')) {
return this.dataFactory.blankNode(key.substr(2));
}
const iri = ContextParser.expandTerm(key, context, false);
if (!Util.isValidIri(iri)) {
if (iri && this.parsingContext.errorOnInvalidProperties) {
this.parsingContext.emitError(new Error(`Invalid resource IRI: ${iri}`));
} else {
return null;
}
}
return this.dataFactory.namedNode(iri);
}
public createVocabOrBaseTerm(context: IJsonLdContextNormalized, key: string): RDF.Term {
if (key.startsWith('_:')) {
return this.dataFactory.blankNode(key.substr(2));
}
let expanded = ContextParser.expandTerm(key, context, true);
if (expanded === key) {
expanded = ContextParser.expandTerm(key, context, false);
}
if (!Util.isValidIri(expanded)) {
if (expanded && this.parsingContext.errorOnInvalidProperties) {
this.parsingContext.emitError(new Error(`Invalid term IRI: ${expanded}`));
} else {
return null;
}
}
return this.dataFactory.namedNode(expanded);
}
public createVocabOrBaseTerm(context: IJsonLdContextNormalized, key: string): RDF.Term {
if (key.startsWith('_:')) {
return this.dataFactory.blankNode(key.substr(2));
}
let expanded = ContextParser.expandTerm(key, context, true);
if (expanded === key) {
expanded = ContextParser.expandTerm(key, context, false);
}
if (!Util.isValidIri(expanded)) {
if (expanded && this.parsingContext.errorOnInvalidProperties) {
this.parsingContext.emitError(new Error(`Invalid term IRI: ${expanded}`));
} else {
return null;
}
}
return this.dataFactory.namedNode(expanded);
}
public static termToValue(term: RDF.Term, context: IJsonLdContextNormalized, options: ITermToValueOptions = {
compactIds: false,
useNativeTypes: false,
}): any {
switch (term.termType) {
case 'NamedNode':
const compacted = ContextParser.compactIri(term.value, context, options.vocab);
return options.compactIds ? compacted : { '@id': compacted };
case 'DefaultGraph':
return options.compactIds ? term.value : { '@id': term.value };
case 'BlankNode':
const id = `_:${term.value}`;
return options.compactIds ? id : { '@id': id };
case 'Literal':
const stringType = term.datatype.value === Util.XSD_STRING;
const rawValue = {
'@value': !stringType && options.useNativeTypes
? Util.stringToNativeType(term.value, term.datatype.value) : term.value,
};
if (term.language) {
return { ...rawValue, '@language': term.language };
} else if (!stringType && typeof rawValue['@value'] === 'string') {
return { ...rawValue, '@type': term.datatype.value };
public static isValidIri(iri: string): boolean {
return ContextParser.isValidIri(iri);
}