cleargraph
Advanced tools
Comparing version 5.2.0 to 5.3.0
@@ -7,2 +7,9 @@ # Change Log | ||
## [5.3.0] - 2020-03-23 | ||
### New | ||
* Added g.toJson() that returns the graph as a JSON object. User can define on N and E a specific toJson() method. If not defined, the N and E objects will be used as they are. | ||
* Added optional parameters to g.parse(): parseNode() and parseEdge() which allows users to define their own parse functions to the generic Node and Edge | ||
## [5.2.0] - 2020-03-12 | ||
@@ -9,0 +16,0 @@ |
@@ -18,3 +18,3 @@ import { NodeId } from './node'; | ||
edge: any; | ||
}): { | ||
}, parseEdge: (data: any) => any): { | ||
sourceId: string; | ||
@@ -31,1 +31,3 @@ targetId: string; | ||
} | ||
export declare function genericParseEdge(edge: any): any; | ||
export declare function genericEdgeToJson(edge: any): any; |
@@ -22,3 +22,3 @@ "use strict"; | ||
} | ||
static fromObject(obj) { | ||
static fromObject(obj, parseEdge) { | ||
if (!obj.hasOwnProperty('sourceId')) { | ||
@@ -30,3 +30,3 @@ throw Error('missing source id'); | ||
} | ||
return { sourceId: obj.sourceId, targetId: obj.targetId, edge: obj.edge }; | ||
return { sourceId: obj.sourceId, targetId: obj.targetId, edge: parseEdge(obj.edge) }; | ||
} | ||
@@ -48,2 +48,13 @@ static edgeId(sourceId, targetId) { | ||
exports.GraphEdge = GraphEdge; | ||
function genericParseEdge(edge) { | ||
if ((Object.keys(edge).length === 0 && edge.constructor === Object) || typeof (edge) !== 'object') { | ||
return edge; | ||
} | ||
return JSON.parse(edge); | ||
} | ||
exports.genericParseEdge = genericParseEdge; | ||
function genericEdgeToJson(edge) { | ||
return edge; | ||
} | ||
exports.genericEdgeToJson = genericEdgeToJson; | ||
//# sourceMappingURL=edge.js.map |
@@ -116,8 +116,9 @@ import { GraphNode, NodeId } from './index'; | ||
}): string[][]; | ||
toJson(graph?: Graph<N, E>): any; | ||
stringify(graph?: Graph<N, E>): string; | ||
static parse(json: string): Graph<unknown, unknown>; | ||
_toJson(graph: Graph<any, any>): string; | ||
static _fromJson(json: string): Graph<unknown, unknown>; | ||
static parse(json: string, parseNode?: (data: any) => any, parseEdge?: (data: any) => any): Graph<unknown, unknown>; | ||
_toJson(graph: Graph<any, any>, returnType: 'object' | 'string'): any; | ||
static _fromJson(json: string, parseNode: (data: any) => any, parseEdge: (data: any) => any): Graph<unknown, unknown>; | ||
bfs(): void; | ||
dfs(): void; | ||
} |
@@ -11,2 +11,4 @@ "use strict"; | ||
const algorithms_1 = require("./algorithms"); | ||
const node_1 = require("./node"); | ||
const edge_1 = require("./edge"); | ||
class Graph { | ||
@@ -469,9 +471,12 @@ constructor(nodes = [], edges = []) { | ||
} | ||
toJson(graph) { | ||
return graph ? this._toJson(graph, 'object') : this._toJson(this, 'object'); | ||
} | ||
stringify(graph) { | ||
return graph ? this._toJson(graph) : this._toJson(this); | ||
return graph ? this._toJson(graph, 'string') : this._toJson(this, 'string'); | ||
} | ||
static parse(json) { | ||
return this._fromJson(json); | ||
static parse(json, parseNode = node_1.genericParseNode, parseEdge = edge_1.genericParseEdge) { | ||
return this._fromJson(json, parseNode, parseEdge); | ||
} | ||
_toJson(graph) { | ||
_toJson(graph, returnType) { | ||
let nodeArray = []; | ||
@@ -481,5 +486,17 @@ for (let [nodeId, nodeData] of graph.nodes.entries()) { | ||
if (!!graphNode) { | ||
let convertedNode; | ||
if (returnType === 'object') { | ||
if (!!graphNode.attr['toJson'] && typeof graphNode.attr['toJson'] === 'function') { | ||
convertedNode = graphNode.attr.toJson(); | ||
} | ||
else { | ||
convertedNode = node_1.genericNodeToJson(graphNode.attr); | ||
} | ||
} | ||
else { | ||
convertedNode = graphNode.stringify(); | ||
} | ||
nodeArray.push({ | ||
id: nodeId, | ||
node: graphNode.stringify() | ||
node: convertedNode | ||
}); | ||
@@ -492,6 +509,18 @@ } | ||
if (!!graphEdge) { | ||
let convertedEdge; | ||
if (returnType === 'object') { | ||
if (!!graphEdge.attr['toJson'] && typeof graphEdge.attr['toJson'] === 'function') { | ||
convertedEdge = graphEdge.attr.toJson(); | ||
} | ||
else { | ||
convertedEdge = node_1.genericNodeToJson(graphEdge.attr); | ||
} | ||
} | ||
else { | ||
convertedEdge = graphEdge.stringify(); | ||
} | ||
edgeArray.push({ | ||
sourceId: graphEdge.sourceId, | ||
targetId: graphEdge.targetId, | ||
edge: graphEdge.stringify() | ||
edge: convertedEdge | ||
}); | ||
@@ -504,5 +533,5 @@ } | ||
}; | ||
return JSON.stringify(json); | ||
return returnType === 'object' ? json : JSON.stringify(json); | ||
} | ||
static _fromJson(json) { | ||
static _fromJson(json, parseNode, parseEdge) { | ||
const obj = JSON.parse(json); | ||
@@ -514,7 +543,7 @@ let graph = new Graph(); | ||
obj.nodes.forEach(nodeObj => { | ||
const res = index_1.GraphNode.fromObject(nodeObj); | ||
const res = index_1.GraphNode.fromObject(nodeObj, parseNode); | ||
graph.setNode(res.id, res.node); | ||
}); | ||
obj.edges.forEach(edgeObj => { | ||
const res = index_2.GraphEdge.fromObject(edgeObj); | ||
const res = index_2.GraphEdge.fromObject(edgeObj, parseEdge); | ||
graph.setEdge(res.sourceId, res.targetId, res.edge); | ||
@@ -521,0 +550,0 @@ }); |
@@ -192,2 +192,7 @@ "use strict"; | ||
}); | ||
it('should convert graph to json object', () => { | ||
const res = g.toJson(); | ||
chai_1.expect(res.nodes.length).to.equal(7); | ||
chai_1.expect(res.edges.length).to.equal(7); | ||
}); | ||
it('should build graph from JSON', () => { | ||
@@ -194,0 +199,0 @@ const json = { |
@@ -22,3 +22,3 @@ import { EdgeId } from './edge'; | ||
node: any; | ||
}): { | ||
}, parseNode: (data: any) => any): { | ||
id: string; | ||
@@ -28,1 +28,3 @@ node: any; | ||
} | ||
export declare function genericParseNode(node: any): any; | ||
export declare function genericNodeToJson(node: any): any; |
@@ -59,10 +59,21 @@ "use strict"; | ||
} | ||
static fromObject(obj) { | ||
static fromObject(obj, parseNode) { | ||
if (!obj.hasOwnProperty('id')) { | ||
throw Error('missing object id'); | ||
} | ||
return { id: obj.id, node: obj.node }; | ||
return { id: obj.id, node: parseNode(obj.node) }; | ||
} | ||
} | ||
exports.GraphNode = GraphNode; | ||
function genericParseNode(node) { | ||
if ((Object.keys(node).length === 0 && node.constructor === Object) || typeof (node) !== 'object') { | ||
return node; | ||
} | ||
return JSON.parse(node); | ||
} | ||
exports.genericParseNode = genericParseNode; | ||
function genericNodeToJson(node) { | ||
return node; | ||
} | ||
exports.genericNodeToJson = genericNodeToJson; | ||
//# sourceMappingURL=node.js.map |
{ | ||
"name": "cleargraph", | ||
"version": "5.2.0", | ||
"version": "5.3.0", | ||
"description": "A library for modeling and traversing graphs", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
100123
1215