New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

js-network-vis

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

js-network-vis - npm Package Compare versions

Comparing version 0.0.1 to 0.0.2

36

examples/index.js

@@ -13,2 +13,38 @@ var data = require('../fixtures/topology_small.json')

function get_node(id, nodes) {
return nodes.filter(function (n) {
return n.name === id
})[0]
}
// handle request events
function request (ev, nodes, edges) {
var node = get_node(ev.node, nodes)
if (!node) return
node.requests.push({
id: ev.data_ID
, loc: ev.node
})
}
// handle request_hop events
function request_hop (ev, nodes, edges) {
var src = get_node(ev.from_node, nodes)
if (!src) return
src.requests = src.requests.filter(function (r) {
return r.id !== ev.data_ID
})
var dst = get_node(ev.to_node, nodes)
if (!dst) return
dst.requests.push({
id: ev.data_ID
, loc: ev.to_node
})
}
network.event('request', request)
network.event('request_hop', request_hop)
var id = Math.floor(Math.random()*1000)

@@ -15,0 +51,0 @@ var evs = [

6

index.js

@@ -45,3 +45,7 @@ var d3 = require('d3')

}
network.event = function (name, fn) {
network.graph.event(name, fn)
}
network.canvas = d3.select(opts.element).append('svg')

@@ -48,0 +52,0 @@ .attr('width', opts.width)

2

package.json
{
"name": "js-network-vis",
"version": "0.0.1",
"version": "0.0.2",
"description": "visualization tool for network simulations",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -6,2 +6,4 @@ # js-network-vis

```npm install js-network-vis```
```js

@@ -22,2 +24,14 @@ var data = require('./graph_data.json')

/* register handlers to determine how graph responds to events */
network.event('request', function (event, nodes, edges) {
var node = nodes.filter(function (n) {
return n.name == event.node
})[0]
if (!node) return
node.requests.push({
id: ev.id
, location: ev.node
})
})
/* updates network with event */

@@ -27,2 +41,3 @@ network.update({

, node: 837
, id: Math.floor(Math.random() * 1000)
})

@@ -75,2 +90,3 @@

update: function(ev) // updates graph according to an event object
, event: function(name, handler) // adds event handler to network
, opts: { ... } // reference to opts object passed to draw

@@ -83,22 +99,9 @@ , canvas: d3 svg selection // reference to graph canvas

## network.event(name, handler)
adds handler function to network. when network.update is called with an event with the named typed, the handler is fired. handler should have the signature (event, nodes, edges) where nodes, edges are the data objects (*not* the d3 selection objects).
## network.update(event)
updates network according to one of the following events:
calls the handler registered against the type specified by event.type. if none is found, nothing happens.
### request
creates a request on the specified node
```js
{
type: 'request'
, node: node.name
}
```
### request\_hop
moves a request from the from\_node to the to\_node
```js
{
type: 'request_hop'
, from_node: node1.name
, to_node: node2.name
}
```
# license
MIT

@@ -14,5 +14,47 @@ const test = require('tape')

test('topology#event', t => {
var network = topology(data.nodes, data.edges)
network.event('request', function (ev, nodes, edges) {
return ev.data_ID
})
var id = Math.floor(Math.random() * 1000)
t.equals(network.update({
type: 'request'
, node: data.nodes[0].name
, data_ID: id
}), id, 'event registered and handled')
t.end()
})
function h_request (ev, nodes, edges) {
var node = nodes.filter(n => n.name === ev.node)[0]
if (!node) return
node.requests.push({
id: ev.data_ID
, loc: ev.node
})
}
function h_request_hop (ev, nodes, edges) {
var src = nodes.filter(n => n.name === ev.from_node)[0]
if (!src) return
src.requests = src.requests.filter(function (r) {
return r.id !== ev.data_ID
})
var dst = nodes.filter(n => n.name === ev.to_node)[0]
if (!dst) return
dst.requests.push({
id: ev.data_ID
, loc: ev.to_node
})
}
test('topology#update -> request', (t) => {
var network = topology(data.nodes, data.edges)
network.event('request', h_request)
network.event('request_hop', h_request_hop)
var id = Math.floor(Math.random()*10)

@@ -42,2 +84,4 @@ network.update({

var network = topology(data.nodes, data.edges)
network.event('request', h_request)
network.event('request_hop', h_request_hop)
var id = Math.floor(Math.random()*10)

@@ -71,47 +115,1 @@ network.update({

})
test('faulty requests', t => {
var network = topology(data.nodes, data.edges)
var id = '100000'
t.notOk(network.nodes.filter(n => n.name === id)[0],
'node ' + id + ' doesn\'t exist')
try {
network.update({
type: 'request'
, node: id
})
} catch (e) {
t.fail('request event on non-existent node has no effect')
}
try {
network.update({
type: 'request_hop'
, to_node: id2
, from_node: id
})
} catch (e) {
t.fail('request_hop event on non-existent node has no effect')
}
id = '1'
var id2 = '100002'
t.ok(network.nodes.filter(n => n.name === id)[0],
'node ' + id + ' exists')
t.notOk(network.nodes.filter(n => n.name === id2)[0],
'node ' + id2 + ' doesn\'t exist')
try {
network.update({
type: 'request_hop'
, to_node: id2
, from_node: id
})
} catch (e) {
t.fail('request_hop event on non-existent node has no effect')
}
t.end()
})

@@ -5,2 +5,3 @@ module.exports = topology

var t = {}
var handlers = {}

@@ -12,5 +13,17 @@ t.edges = edges

})
t.update = function update(ev) {
var h = handlers[ev.type]
if (!h) return
return h(ev, t.nodes, t.edges)
}
t.event = function (name, fn) {
if (!name || !fn || typeof fn !== 'function') {
throw new Error('must register event-type with function')
}
handlers[name] = fn
}
/*
switch (ev.type) {

@@ -44,10 +57,4 @@ case 'request':

}
*/
return t
}
function get_node(id, nodes) {
return nodes.filter(function (node) {
return node.name === id
})[0]
}

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc