Giter Club home page Giter Club logo

node-connection-paths's Introduction

node-connection-paths

Description

Determine if two things are connected (e.g. network nodes) and generate a list of possible paths. For example, if A connects to B and C, B connects to C and D, and C connects to D and A, then A can connect to D via an infinite number of paths, assuming a thing is allowed to reconnect to previous things in the path.

API

Connections(opt)

Instantiate Connections

  • object opt - An options object
    • string | array{array{string}} source - An array of arrays of two strings or URL or file system path to a CSV file that maps sources to destinations
    • boolean hasHeader - (Optional) Whether or not source has a header. If true, the first row will be removed. Defaults to false.

Example

var Connections = require('node-connection-paths'),
    connections = Connections(
        {
            hasHeader: true,
            source: [['A','B'], ['A','C'], ['B','C'], ['B','D'], ['C','D'], ['C','A']]
        }
    );

connections.getDestinations(source, cb)

Get the destinations of a source

  • string opt - A source
  • function(null | object err, array destinations) cb - A function to be executed after the destinations are collected

Example

connections.getDestinations('A', function(err, destinations) {
    if (err){ throw err; }
    console.log(destinations); //[ 'B', 'C' ]
});

connections.getPaths(opt, cb)

Generate possible paths between a source and destination

  • object opt - An options object
    • string source - A source
    • string destination - A destination
    • boolean reverse - Whether or not a node is allowed to connect to the previous node. Defaults to false.
    • boolean revisit - Whether or not a node is allowed to connect to any previous nodes. Defaults to false.
    • string format - (Optional) If "string", paths will be an array of strings. If "array", paths will be an array of arrays of strings. Defaults to "array".
    • number max - (Optional) The maximum number of paths to generate. Defaults to 100.
    • object depth
      • number queue - (Optional) Concurrency limit for recursive operations. Defaults to 500.
      • number recursion - (Optional) Maximum path recursion. Defaults to 5.
  • function(null | object err, array{string} | array{array{string}} paths) cb - A function to be executed after the paths are generated

Example

connections.getPaths(
    {
        source: 'A',
        destination: 'D',
        reverse: true,
        revisit: true,
        format: 'string',
        max: 2,
        depth: {
            queue: 10,
            recursion: 3
        }
    }, function(err, paths) {
        if (err) { throw err; }
        console.log(paths); //[ 'A,B,D', 'A,C,D' ]
    }
);

connections.areConnected(opt, cb)

Determine if a source and destination are connected. Note that it is much faster to use a directed graph. There are several directed graph libraries available on NPM, including graph.js and directed-graph.

  • object opt - An options object
    • string source - A source
    • string destination - A destination
    • object depth
      • number queue - (Optional) Concurrency limit for recursive operations. Defaults to 100.
      • number recursion - (Optional) Maximum path recursion. Defaults to 5.
  • function(null | object err, boolean areConnected) cb - A function to be executed after the check is completed

Example

connections.areConnected(
    {
        source: 'A',
        destination: 'D',
        depth: {
            queue: 100,
            recursion: 10
        }
    }, function(err, areConnected) {
        if (err) { throw err; }
        console.log(areConnected); //true
    }
);

Events

connections.events.on('ready', function(null | object err))

If the connection source / destination CSV data is loaded via URL or file system path, the API methods cannot be called until this event is emitted

Example

var path = require('path'),
    connections = Connections({source: path.join(__dirname, 'test.csv')});

connections.events.on('ready', function(err) {
    if (err) { throw err; }

    connections.areConnected(
        {
            source: 'A',
            destination: 'D',
            depth: {
                queue: 100,
                recursion: 10
            }
        }, function(err, areConnected) {
            if (err) { throw err; }
            console.log(areConnected); //true
        }
    );
});

Installation

Npm

npm install connection-paths --save

node-connection-paths's People

Contributors

mjhasbach avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.