@tpluscode/rdfine
Version:
RDF/JS idiomatic, native, effective
44 lines (43 loc) • 1.54 kB
JavaScript
import * as compare from '../../compare.js';
import { getPointer } from '../../resource.js';
import { propertyDecorator } from './decoratorFactory.js';
function resourcePropertyDecorator(options = {}) {
return propertyDecorator({
...options,
fromTerm(obj) {
if (options.implicitTypes) {
obj.addOut(this.env.ns.rdf.type, options.implicitTypes);
}
return this._create(obj, options.as, { parent: this });
},
toTerm(value) {
const valueNode = getPointer(this.pointer, value.id);
if (value.types && Array.isArray(value.types)) {
valueNode.addOut(this.env.ns.rdf.type, value.types);
}
if (options.implicitTypes) {
valueNode.addOut(this.env.ns.rdf.type, options.implicitTypes);
}
this._create(valueNode, options.as, {
initializer: value,
});
return valueNode.term;
},
valueTypeName: 'RdfResource instance',
assertSetValue: (value) => {
let term = null;
if ('termType' in value) {
term = value;
}
else if ('term' in value) {
term = value.term;
}
if (term) {
return term.termType === 'NamedNode' || term.termType === 'BlankNode';
}
return true;
},
compare: compare.resources(true),
});
}
export default resourcePropertyDecorator;