UNPKG

lincd

Version:

LINCD is a JavaScript library for building user interfaces with linked data (also known as 'structured data', or RDF)

130 lines 6.52 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Find = void 0; /* * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ const NodeSet_1 = require("../collections/NodeSet"); const models_1 = require("../models"); const rdf_1 = require("../ontologies/rdf"); const rdfs_1 = require("../ontologies/rdfs"); const QuadSet_1 = require("../collections/QuadSet"); const URI_1 = require("./URI"); class Find { static byPropertyValues(valuesToProperties, targetType, includeLocalResources = true, exactMatch = true, sanitized = true) { // var results =new NodeSet<NamedNode>(); let result = new QuadSet_1.QuadSet(); let subjects; valuesToProperties.forEach((searchValue, properties) => { let iterationResult = this.byPropertyValue(searchValue, properties, targetType, includeLocalResources, exactMatch, sanitized, subjects); let iterationSubjects = iterationResult.getSubjects(); //after added this results for this searchValue.. //if this was the first iteration if (!subjects) { //then we will use the subjects of this loop to check against next iterations subjects = iterationSubjects; result = iterationResult; } else { //else we need to check if all the subjects from the first iteration ALSO occurred in this iteration subjects.forEach((subject) => { if (!iterationSubjects.has(subject)) { //if not, we retract results from that subject result = result.filter((quad) => { return quad.subject !== subject; }); } }); } }); return result; } static valueMatches(propertyValueResource, value, sanitized, exactMatch) { //if local nodes are allowed not allowed and the object is a local node, dont continue if (propertyValueResource instanceof models_1.NamedNode) return false; //get the value let propertyValue = propertyValueResource.value; if (sanitized) propertyValue = URI_1.URI.sanitize(propertyValue); //not exact match? then we only test if the value starts with the identifier we're searching for if (!exactMatch) propertyValue = propertyValue.substr(0, value.length); // if the value matches and the target type matches if (propertyValue === value) { return true; } return false; } /** * Returns a set of quads where the search value is found and the predicate matches the given property/properties/property-type * @param searchValue * @param properties a single property, a set of properties, a property type or '*' to indicate ANY property * @param targetType only include quads whos subject is of this type * @param includeLocalResources if false, temporary / local nodes will be excluded from results * @param exactMatch if true, only returns exact matches, if false, returns values that START WITH the given searchValue * @param sanitized indicates whether the searchValue has been sanitized * @param subjects if given, will only return quads who's subject occurs in this set */ static byPropertyValue(searchValue, properties = '*', targetType, includeLocalResources = true, exactMatch, sanitized, subjects) { var result = new QuadSet_1.QuadSet(); let propertySet; if (properties instanceof models_1.NamedNode) { //if a propertyOrPropertyType TYPE was given (like ObjectProperty or IdProperty) then we look for ALL properties that are instances of this type if (properties.has(rdf_1.rdf.type, rdfs_1.rdfs.Class) && properties.has(rdfs_1.rdfs.subClassOf, rdf_1.rdf.Property)) { propertySet = properties.getAllInverse(rdf_1.rdf.type); } else { propertySet = new NodeSet_1.NodeSet([properties]); } } else if (properties == '*') { //by default use all properties propertySet = rdf_1.rdf.Property.getAllInverse(rdf_1.rdf.type); } else if (properties instanceof NodeSet_1.NodeSet) { propertySet = properties; } else { throw Error("Invalid property given. Please provide a property, a property type, a set of properties or '*' to search for this value for any property"); } //go through all properties propertySet.forEach((searchProp) => { let potentialQuads; //if we already have a set of subjects to test (from a previous result for example, see byPropertyValues) if (subjects) { //then we only have to look through the results of these subjects potentialQuads = subjects.getQuads(searchProp); } else { //if not, then we look through ALL LOCALLY KNOWN USAGES of this property //TODO: this has been deprecated, probably in new LINCD this is not necessary anymore throw new Error("Deprecated. Using find is discouraged."); // potentialQuads = searchProp.getAsPredicateQuads(); } if (!potentialQuads) return; //go through the quads for this property potentialQuads.forEach((quad) => { //option to exclude local nodes if (!includeLocalResources && quad.subject.isTemporaryNode) return; //option to only include subjects of a certain type if (targetType && !quad.subject.has(rdf_1.rdf.type, targetType)) return; if (searchValue instanceof models_1.NamedNode && quad.object === searchValue) { result.add(quad); } //if we find a value that matches the search string else if (this.valueMatches(quad.object, searchValue, sanitized, exactMatch)) { result.add(quad); } }); }); return result; } } exports.Find = Find; //# sourceMappingURL=Find.js.map