graphology-gexf
Advanced tools
Comparing version 0.10.3 to 0.11.0
@@ -17,2 +17,6 @@ /** | ||
switch (type) { | ||
case 'string': | ||
// NOTE: this is quite common so having this here | ||
// can speed things up a bit. | ||
return value; | ||
case 'boolean': | ||
@@ -19,0 +23,0 @@ value = value === 'true'; |
@@ -33,6 +33,6 @@ /* eslint no-self-compare: 0 */ | ||
function toRGBString(element) { | ||
var a = element.getAttribute('a'), | ||
r = element.getAttribute('r'), | ||
g = element.getAttribute('g'), | ||
b = element.getAttribute('b'); | ||
var a = element.getAttribute('a'); | ||
var r = element.getAttribute('r'); | ||
var g = element.getAttribute('g'); | ||
var b = element.getAttribute('b'); | ||
@@ -69,5 +69,5 @@ return a | ||
function collectMeta(elements) { | ||
var meta = {}, | ||
element, | ||
value; | ||
var meta = {}; | ||
var element; | ||
var value; | ||
@@ -94,7 +94,7 @@ for (var i = 0, l = elements.length; i < l; i++) { | ||
function extractModel(elements) { | ||
var model = {}, | ||
defaults = {}, | ||
element, | ||
defaultElement, | ||
id; | ||
var model = {}; | ||
var defaults = {}; | ||
var element; | ||
var defaultElement; | ||
var id; | ||
@@ -127,11 +127,17 @@ for (var i = 0, l = elements.length; i < l; i++) { | ||
* | ||
* @param {object} model - Data model to use. | ||
* @param {object} defaults - Default values. | ||
* @param {Node} element - Target DOM element. | ||
* @return {object} - The collected attributes. | ||
* @param {object} model - Data model to use. | ||
* @param {object} defaults - Default values. | ||
* @param {Node} element - Target DOM element. | ||
* @param {boolean} allowUndeclaredAttributes - Whether to allow undeclared attributes. | ||
* @return {object} - The collected attributes. | ||
*/ | ||
function collectAttributes(model, defaults, element) { | ||
var data = {}, | ||
label = element.getAttribute('label'), | ||
weight = element.getAttribute('weight'); | ||
function collectAttributes( | ||
model, | ||
defaults, | ||
element, | ||
allowUndeclaredAttributes | ||
) { | ||
var data = {}; | ||
var label = element.getAttribute('label'); | ||
var weight = element.getAttribute('weight'); | ||
@@ -142,5 +148,9 @@ if (label) data.label = label; | ||
var valueElements = element.getElementsByTagName('attvalue'), | ||
valueElement, | ||
id; | ||
var valueElements = element.getElementsByTagName('attvalue'); | ||
var valueElement; | ||
var attr; | ||
var title; | ||
var value; | ||
var type; | ||
var id; | ||
@@ -150,7 +160,20 @@ for (var i = 0, l = valueElements.length; i < l; i++) { | ||
id = valueElement.getAttribute('id') || valueElement.getAttribute('for'); | ||
value = valueElement.getAttribute('value'); | ||
attr = model[id]; | ||
data[model[id].title] = cast( | ||
model[id].type, | ||
valueElement.getAttribute('value') | ||
); | ||
if (!attr) { | ||
if (allowUndeclaredAttributes) { | ||
title = id; | ||
type = 'string'; | ||
} else { | ||
throw new Error( | ||
'graphology-gexf/parser: Found undeclared attribute "' + id + '"' | ||
); | ||
} | ||
} else { | ||
title = attr.title; | ||
type = attr.type; | ||
} | ||
data[title] = cast(type, value); | ||
} | ||
@@ -227,2 +250,3 @@ | ||
var addMissingNodes = options.addMissingNodes === true; | ||
var allowUndeclaredAttributes = options.allowUndeclaredAttributes === true; | ||
var mergeResult; | ||
@@ -247,10 +271,10 @@ | ||
// Finding useful elements | ||
var GRAPH_ELEMENT = xmlDoc.getElementsByTagName('graph')[0], | ||
META_ELEMENT = xmlDoc.getElementsByTagName('meta')[0], | ||
META_ELEMENTS = (META_ELEMENT && META_ELEMENT.childNodes) || [], | ||
NODE_ELEMENTS = xmlDoc.getElementsByTagName('node'), | ||
EDGE_ELEMENTS = xmlDoc.getElementsByTagName('edge'), | ||
MODEL_ELEMENTS = xmlDoc.getElementsByTagName('attributes'), | ||
NODE_MODEL_ELEMENTS = [], | ||
EDGE_MODEL_ELEMENTS = []; | ||
var GRAPH_ELEMENT = xmlDoc.getElementsByTagName('graph')[0]; | ||
var META_ELEMENT = xmlDoc.getElementsByTagName('meta')[0]; | ||
var META_ELEMENTS = (META_ELEMENT && META_ELEMENT.childNodes) || []; | ||
var NODE_ELEMENTS = xmlDoc.getElementsByTagName('node'); | ||
var EDGE_ELEMENTS = xmlDoc.getElementsByTagName('edge'); | ||
var MODEL_ELEMENTS = xmlDoc.getElementsByTagName('attributes'); | ||
var NODE_MODEL_ELEMENTS = []; | ||
var EDGE_MODEL_ELEMENTS = []; | ||
@@ -275,9 +299,9 @@ for (i = 0, l = MODEL_ELEMENTS.length; i < l; i++) { | ||
var NODE_MODEL = result[0], | ||
NODE_DEFAULT_ATTRIBUTES = result[1]; | ||
var NODE_MODEL = result[0]; | ||
var NODE_DEFAULT_ATTRIBUTES = result[1]; | ||
result = extractModel(EDGE_MODEL_ELEMENTS); | ||
var EDGE_MODEL = result[0], | ||
EDGE_DEFAULT_ATTRIBUTES = result[1]; | ||
var EDGE_MODEL = result[0]; | ||
var EDGE_DEFAULT_ATTRIBUTES = result[1]; | ||
@@ -295,5 +319,5 @@ // Polling the first edge to guess the type of the edges | ||
// Collecting meta | ||
var meta = collectMeta(META_ELEMENTS), | ||
lastModifiedDate = | ||
META_ELEMENT && META_ELEMENT.getAttribute('lastmodifieddate'); | ||
var meta = collectMeta(META_ELEMENTS); | ||
var lastModifiedDate = | ||
META_ELEMENT && META_ELEMENT.getAttribute('lastmodifieddate'); | ||
@@ -311,3 +335,8 @@ graph.replaceAttributes(meta); | ||
element.getAttribute('id'), | ||
collectAttributes(NODE_MODEL, NODE_DEFAULT_ATTRIBUTES, element) | ||
collectAttributes( | ||
NODE_MODEL, | ||
NODE_DEFAULT_ATTRIBUTES, | ||
element, | ||
allowUndeclaredAttributes | ||
) | ||
); | ||
@@ -327,3 +356,4 @@ } | ||
EDGE_DEFAULT_ATTRIBUTES, | ||
element | ||
element, | ||
allowUndeclaredAttributes | ||
); | ||
@@ -330,0 +360,0 @@ |
@@ -5,2 +5,3 @@ import {Attributes} from 'graphology-types'; | ||
addMissingNodes?: boolean; | ||
allowUndeclaredAttributes?: boolean; | ||
}; | ||
@@ -7,0 +8,0 @@ |
{ | ||
"name": "graphology-gexf", | ||
"version": "0.10.3", | ||
"version": "0.11.0", | ||
"description": "GEXF parser & writer for graphology.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -7,3 +7,3 @@ [![Build Status](https://github.com/graphology/graphology-gexf/workflows/Tests/badge.svg)](https://github.com/graphology/graphology-gexf/actions) | ||
For more information about the GEXF file format, you can head [there](https://gephi.org/gexf/format/). | ||
For more information about the GEXF file format, you can head [there](https://gexf.net). | ||
@@ -50,2 +50,3 @@ ## Installation | ||
- **addMissingNodes** _?boolean_ [`false`]: whether to add missing nodes referenced in the file's edges. | ||
- **allowUndeclaredAttributes** _?boolean_ [`false`]: whether to allow undeclared attributes for both nodes & edges, which will be considered as strings. | ||
@@ -52,0 +53,0 @@ ### Writer |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
33978
906
113