@tpluscode/rdfine
Version:
RDF/JS idiomatic, native, effective
26 lines (25 loc) • 983 B
JavaScript
import literalPropertyDecorator from '../property/literal.js';
import resourcePropertyDecorator from '../property/resource.js';
import * as compare from '../../compare.js';
import { fromInitializer } from '../../resource.js';
import { propertyDecorator } from './decoratorFactory.js';
function isInitializer(obj) {
return typeof obj === 'object' && obj instanceof Object && 'id' in obj;
}
export function property(options = {}) {
return propertyDecorator({
...options,
fromTerm: (obj) => obj.term,
toTerm(value) {
if (isInitializer(value)) {
return fromInitializer(this, value).id;
}
return this.env.rdfine().convert.toLiteral(value) || this.env.literal(value.toString());
},
valueTypeName: 'RDF/JS term object',
assertSetValue: () => true,
compare: compare.terms,
});
}
property.literal = literalPropertyDecorator;
property.resource = resourcePropertyDecorator;