UNPKG

lincd

Version:

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

61 lines 2.42 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Order = void 0; const rdfs_1 = require("../ontologies/rdfs"); class Order { static propertiesByDepth(properties) { return properties.sort((c1, c2) => { return c1.has(rdfs_1.rdfs.subPropertyOf, c2) ? -1 : 1; }); } /** * Counts the number of connections each node makes to antoher * and then returns a new set of the same nodes sorted by that numb connections with the most connections first * @param nodes * @param property * @param shortestPathFirst * @returns {NodeSet<NamedNode>} */ static byCrossPaths(nodes, property, shortestPathFirst = false) { var counts = this.getCrossPaths(nodes, property); if (shortestPathFirst) { return nodes.sort((r1, r2) => { //use strings as backup when counts are equal to make sure sorting is always the same if (counts.get(r1) == counts.get(r2)) { return r1.toString() >= r2.toString() ? 1 : -1; } return counts.get(r1) > counts.get(r2) ? 1 : -1; //if bigger, then 1, thus lower }); } else { //by default return the longest path first return nodes.sort((r1, r2) => { //use strings as backup when counts are equal to make sure sorting is always the same if (counts.get(r1) == counts.get(r2)) { return r1.toString() >= r2.toString() ? -1 : 1; } return counts.get(r1) >= counts.get(r2) ? -1 : 1; //if bigger, then -1, thus higher }); } } static getCrossPaths(nodes, property) { var counts = new Map(); nodes.forEach((node) => { var crossRelations = 0; nodes.forEach((resource2) => { //'cross' => against others => not against self if (node === resource2) return; if (node.has(property, resource2)) { //counts.set(node.uri,[counts.get(node.uri)[0]++,node]); //counts[node.uri]++; crossRelations++; } }); counts.set(node, crossRelations); }); return counts; } } exports.Order = Order; //# sourceMappingURL=Order.js.map