UNPKG

@dasch-swiss/dsp-js

Version:

JavaScript library that handles API requests to Knora

70 lines 3.01 kB
import { Cardinality } from "../../../models/v2/ontologies/class-definition"; /** * Utility methods to facilitate the handling of a property's cardinality. * * @category Model V2 */ export var CardinalityUtil; (function (CardinalityUtil) { /** * Returns the cardinality for a property from a `ResourceClassDefinition`. * * @param propertyIri Iri of the property. * @param entityInfo class information. */ var getCardinalityForProp = function (propertyIri, entityInfo) { var cardinalityForProp = entityInfo.propertiesList.filter(function (hasProp) { return hasProp.propertyIndex === propertyIri; }); if (cardinalityForProp.length !== 1) { throw new Error("No cardinality found for " + propertyIri); } return cardinalityForProp[0].cardinality; }; CardinalityUtil.cardinalities = new Map([ [Cardinality._0_1, "0-1"], [Cardinality._0_n, "0-n"], [Cardinality._1, "1"], [Cardinality._1_n, "1-n"] ]); /** * Determines if a value can be created for a given property. * * @param propertyIri Iri of the property. * @param numberOfInstances number of existing values of the property in question. * @param entityInfo class information. */ CardinalityUtil.createValueForPropertyAllowed = function (propertyIri, numberOfInstances, entityInfo) { var cardinality = getCardinalityForProp(propertyIri, entityInfo); if (numberOfInstances > 0) { // at least one existing value for property // 1-n and 0-n allow for the creation of additional values return (cardinality === Cardinality._1_n || cardinality === Cardinality._0_n); } else { // no existing value for property // 0-1 and 0-n allow for the creation of a value return (cardinality === Cardinality._0_1 || cardinality === Cardinality._0_n); } }; /** * Determines if a value can be deleted for a given property. * * @param propertyIri Iri of the property. * @param numberOfInstances number of existing values of the property in question. * @param entityInfo class information. */ CardinalityUtil.deleteValueForPropertyAllowed = function (propertyIri, numberOfInstances, entityInfo) { var cardinality = getCardinalityForProp(propertyIri, entityInfo); if (numberOfInstances > 1) { // more than one existing value // 1-n allows for the deletion of a value // if there is at least one remaining value return (cardinality === Cardinality._1_n || cardinality === Cardinality._0_n); } else { // single value // 0-1 and 0-n allow for deletion of a single value return (cardinality === Cardinality._0_1 || cardinality === Cardinality._0_n); } }; })(CardinalityUtil || (CardinalityUtil = {})); //# sourceMappingURL=cardinality-util.js.map