graphology-gexf
Advanced tools
Comparing version 0.9.0 to 0.10.0
import Graph, {Attributes, GraphConstructor} from 'graphology-types'; | ||
import {GexfWriterOptions} from '../common/types'; | ||
import {GexfParserOptions, GexfWriterOptions} from '../common/types'; | ||
@@ -10,3 +10,4 @@ export function parse< | ||
Graph: GraphConstructor<NodeAttributes, EdgeAttributes, GraphAttributes>, | ||
source: string | Document | ||
source: string | Document, | ||
options?: GexfParserOptions | ||
): Graph<NodeAttributes, EdgeAttributes, GraphAttributes>; | ||
@@ -13,0 +14,0 @@ |
@@ -8,5 +8,5 @@ /* eslint no-self-compare: 0 */ | ||
*/ | ||
var isGraphConstructor = require('graphology-utils/is-graph-constructor'), | ||
mergeEdge = require('graphology-utils/add-edge').mergeEdge, | ||
helpers = require('../common/helpers.js'); | ||
var isGraphConstructor = require('graphology-utils/is-graph-constructor'); | ||
var mergeEdge = require('graphology-utils/add-edge').mergeEdge; | ||
var helpers = require('../common/helpers.js'); | ||
@@ -209,4 +209,5 @@ var cast = helpers.cast; | ||
* | ||
* @param {function} Graph - A graphology constructor. | ||
* @param {string|Document} source - The source to parse. | ||
* @param {function} Graph - A graphology constructor. | ||
* @param {string|Document} source - The source to parse. | ||
* @param {object} options - Parsing options. | ||
*/ | ||
@@ -216,3 +217,8 @@ | ||
// TODO: option to disable the model mapping heuristic | ||
return function parse(Graph, source) { | ||
return function parse(Graph, source, options) { | ||
options = options || {}; | ||
var addMissingNodes = options.addMissingNodes === true; | ||
var mergeResult; | ||
var xmlDoc = source; | ||
@@ -328,3 +334,16 @@ | ||
mergeEdge(graph, type !== 'directed', id || null, s, t, attributes); | ||
mergeResult = mergeEdge( | ||
graph, | ||
type !== 'directed', | ||
id || null, | ||
s, | ||
t, | ||
attributes | ||
); | ||
if (!addMissingNodes && (mergeResult[2] || mergeResult[3])) { | ||
throw new Error( | ||
'graphology-gexf/parser: one of your gexf file edges points to an inexisting node. Set the parser `addMissingNodes` option to `true` if you do not care.' | ||
); | ||
} | ||
} | ||
@@ -331,0 +350,0 @@ |
import {Attributes} from 'graphology-types'; | ||
export type GexfParserOptions = { | ||
addMissingNodes?: boolean; | ||
}; | ||
type RGBAColor = { | ||
@@ -4,0 +8,0 @@ r: number; |
import Graph, {Attributes, GraphConstructor} from 'graphology-types'; | ||
import {GexfWriterOptions} from '../common/types'; | ||
import {GexfParserOptions, GexfWriterOptions} from '../common/types'; | ||
@@ -10,3 +10,4 @@ export function parse< | ||
Graph: GraphConstructor<NodeAttributes, EdgeAttributes, GraphAttributes>, | ||
source: string | Document | ||
source: string | Document, | ||
options?: GexfParserOptions | ||
): Graph<NodeAttributes, EdgeAttributes, GraphAttributes>; | ||
@@ -13,0 +14,0 @@ |
{ | ||
"name": "graphology-gexf", | ||
"version": "0.9.0", | ||
"version": "0.10.0", | ||
"description": "GEXF parser & writer for graphology.", | ||
@@ -41,3 +41,3 @@ "main": "index.js", | ||
"dependencies": { | ||
"graphology-utils": "^2.1.0", | ||
"graphology-utils": "^2.4.1", | ||
"xml-writer": "^1.7.0", | ||
@@ -44,0 +44,0 @@ "@xmldom/xmldom": "^0.7.5" |
@@ -20,2 +20,3 @@ [![Build Status](https://github.com/graphology/graphology-gexf/workflows/Tests/badge.svg)](https://github.com/graphology/graphology-gexf/actions) | ||
* [Writer](#writer) | ||
* [Notes](#notes) | ||
@@ -39,2 +40,5 @@ ### Parser | ||
var graph = gexf.parse(Graph, xmlDocument); | ||
// Passing options | ||
var graph = gexf.parse(Graph, string, {addMissingNodes: true}); | ||
``` | ||
@@ -44,4 +48,6 @@ | ||
* **constructor** *GraphClass*: graphology constructor to use. | ||
* **source** *string|Document*: source data to parse. | ||
- **constructor** *GraphClass*: graphology constructor to use. | ||
- **source** *string\|Document*: source data to parse. | ||
- **options** *?object*: parsing options: | ||
- **addMissingNodes** *?boolean* [`false`]: whether to add missing nodes referenced in the file's edges. | ||
@@ -106,1 +112,5 @@ ### Writer | ||
- **pretty** *?boolean* [`true`]: pretty-print output? | ||
### Notes | ||
Currently, `mutual` edges are parsed as undirected ones rather than two directed ones because it could produce a key conflict. An option to deal differently with this may be added in the future if it becomes a problem. |
32240
857
113
Updatedgraphology-utils@^2.4.1