UNPKG

@barchart/common-js

Version:
159 lines (153 loc) 4.3 kB
var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var Vertex_exports = {}; __export(Vertex_exports, { default: () => Vertex }); module.exports = __toCommonJS(Vertex_exports); var assert = __toESM(require("./../../lang/assert.js")); var import_Edge = __toESM(require("./Edge.js")); class Vertex { #data; #edges; /** * @param {*=} data */ constructor(data) { this.#data = data || null; this.#edges = []; } /** * Ad hoc data associated with the vertex (in other words the "value" * of the vertex). * * @public * @returns {*} */ get data() { return this.#data; } /** * Returns all edges from this vertex to other vertices. * * @public * @returns {Edge[]} */ getEdges() { return this.#edges; } /** * Adds an edge. * * @public * @param {Vertex} other * @param {*=} data * @returns {Edge} */ addEdge(other, data) { assert.argumentIsRequired(other, "other", Vertex, "Vertex"); if (other === this) { throw new Error("Graph vertex cannot connect to itself."); } if (this.hasEdge(other)) { throw new Error(`Graph already has edge between [ ${this.data.toString()} ] and [ ${other.data.toString()} ]`); } const edge = new import_Edge.default(this, other, data); this.#edges.push(edge); return edge; } /** * Locates an edge. * * @public * @param {Vertex} other * @returns {Edge|null} */ getEdge(other) { return this.#edges.find((e) => e.to === other) || null; } /** * Indicates if this vertex has an edge. * * @public * @param {Vertex} other * @returns {boolean} */ hasEdge(other) { return this.getEdge(other) !== null; } /** * Finds all possible paths from this vertex (node) to another vertex (node). * * @public * @param {Vertex} other * @param {Edge[]=} walk * @returns {Edge[][]} */ getPaths(other, walk) { if (walk && this === other) { return [walk]; } if (walk && walk.some((edge) => edge.from === this)) { return []; } let paths = []; this.#edges.forEach((edge) => { let current; if (walk) { current = walk.slice(0); } else { current = []; } current.push(edge); paths = paths.concat(edge.to.getPaths(other, current)); }); return paths; } /** * Returns a string representation. * * @public * @returns {string} */ toString() { return `[Vertex (data=${this.data.toString()})]`; } } { const cjsExports = module.exports; const cjsDefaultExport = cjsExports && cjsExports.__esModule ? cjsExports.default : cjsExports; if (cjsDefaultExport && (typeof cjsDefaultExport === 'function' || typeof cjsDefaultExport === 'object')) { Object.keys(cjsExports).forEach((key) => { if (key !== 'default' && key !== '__esModule') { cjsDefaultExport[key] = cjsExports[key]; } }); } module.exports = cjsDefaultExport; }