@rdfine/hydra
Version:
> ## @rdfine/hydra > ### Generated JavaScript types for Hydra Core Vocabulary
34 lines (33 loc) • 1.4 kB
JavaScript
import { hydra } from '../lib/namespace.js';
import { xsd } from '@tpluscode/rdf-ns-builders';
export function VariableRepresentationExMixin(Resource) {
class VariableRepresentationClass extends Resource {
mapValue(term) {
if (this.equals(hydra.BasicRepresentation)) {
return term.value;
}
let explicitRepresentation;
switch (term.termType) {
case 'NamedNode':
explicitRepresentation = term.value;
break;
case 'Literal':
if (!term.datatype || term.datatype.equals(xsd.string)) {
explicitRepresentation = `"${term.value}"`;
}
else if (term.language) {
explicitRepresentation = `"${term.value}"@${term.language}`;
}
else {
explicitRepresentation = `"${term.value}"^^${term.datatype.value}`;
}
break;
default:
throw new Error(`Cannot use term ${term.termType} for template variable expansion`);
}
return decodeURIComponent(explicitRepresentation);
}
}
return VariableRepresentationClass;
}
VariableRepresentationExMixin.appliesTo = hydra.VariableRepresentation;