Graph generation and handling utils, built with pure functions


Keywords
pure, graph, utils, functional
License
MIT
Install
npm install pure-graph-utils@1.4.17

Documentation

pure-graph-utils

npm Build Status Github file size

Graph generation and handling utils, built with pure functions.

Table of contents

Quick foreword

The motivation behind this project came from a strong interest in functional programming and a desire to learn more about — and practice — TDD, Jest, CI, ES6, Ramda, etc. As a relative beginner to JavaScript, building a small library such as this one felt like a good starting step. Issues, PRs, suggestions and/or opinions are all more than welcome!

Installation

There are no dependencies. To install it using npm, just run:

npm install pure-graph-utils

Usage

Below are all available functions, along with very brief explanations and simple examples. To get a better grasp of each function's behavior and edge cases, please check the tests file.

createNode(id, value)

Returns an object with an 'id' and a 'value' properties.

createNode('nodeId', 'nodeValue')

// Output
{
  id: 'nodeId',
  value: 'nodeValue'
}

changeNodeId(newId, node)

Returns a new node with newNode.id === newId.

changeNodeId(
  'newId',
  createNode('nodeId', 'nodeValue')
)

// Output
{
  id: 'newId',
  value: 'nodeValue'
}

addNodeProperty(propKey, propValue, node)

Returns a new node with newNode[propKey] === propValue.

addNodeProperty(
  'extraProperty',
  'extraPropertyValue',
  createNode('nodeId', 'nodeValue')
)

// Output
{
  extraProperty: 'extraPropertyValue',
  id: 'nodeId',
  value: 'nodeValue'
}

editNodeProperty(propKey, propNewValue, node)

Returns a new node with newNode[propKey] === propNewValue.

editNodeProperty(
  'value',
  'newValue',
  createNode('nodeId', 'nodeValue')
)

// Output
{
  id: 'nodeId',
  value: 'newValue'
}

removeNodeProperty(propKey, node)

Returns a new node without propKey.

removeNodeProperty(
  'value',
  createNode('nodeId', 'nodeValue')
)

// Output
{
  id: 'nodeId',
}

createGraph(node)

Returns an object with node.

createGraph(createNode('nodeId', 'nodeValue'))

// Output
{
  nodeId: {
    connections: [],
    id: 'nodeId',
    value: 'nodeValue'
  }
}

changeGraphNodeId(nodeId, nodeNewId, graph)

Returns a new graph with graph[nodeId] adapted to newGraph[nodeNewId].

changeGraphNodeId(
  'nodeId',
  'nodeNewId',
  createGraph(createNode('nodeId', 'nodeValue'))
)

// Output
{
  nodeNewId: {
    connections: [],
    id: 'nodeNewId',
    value: 'nodeValue'
  }
}

addNode(node, graph)

Returns a new graph with node.

addNode(
  createNode('id2', 'value2'),
  createGraph(createNode('id1', 'value1'))
)

// Output
{
  id1: {
    connections: [],
    id: 'id1',
    value: 'value1'
  },
  id2: {
    connections: [],
    id: 'id2',
    value: 'value2'
  }
}

addGraphNodeProperty(nodeId, propKey, propValue, graph)

Returns a new graph with newGraph[nodeId][propKey] === propValue.

addGraphNodeProperty(
  'nodeId',
  'extraProperty',
  'extraPropertyValue',
  createGraph(createNode('nodeId', 'nodeValue'))
)

// Output
{
  nodeId: {
    connections: [],
    extraProperty: 'extraPropertyValue',
    id: 'nodeId',
    value: 'nodeValue'
  }
}

editGraphNodeProperty(nodeId, propKey, propNewValue, graph)

Returns a new graph with newGraph[nodeId][propKey] === propNewValue.

editGraphNodeProperty(
  'nodeId',
  'value',
  'newValue',
  createGraph(createNode('nodeId', 'nodeValue'))
)

// Output
{
  nodeId: {
    connections: [],
    id: 'nodeId',
    value: 'newValue'
  }
}

removeGraphNodeProperty(nodeId, propKey, graph)

Returns a new graph without graph[nodeId]'s propKey.

removeGraphNodeProperty(
  'nodeId',
  'value',
  createGraph(createNode('nodeId', 'nodeValue'))
)

// Output
{
  nodeId: {
    connections: [],
    id: 'nodeId'
  }
}

addConnection(nodeAId, nodeBId, graph)

Returns a new graph with nodeA and nodeB connected.

addConnection(
  'id1',
  'id2',
  addNode(
    createNode('id2', 'value2'),
    createGraph(createNode('id1', 'value1'))
  )
)


// Output
{
  id1: {
    connections: ['id2'],
    id: 'id1',
    value: 'value1'
  },
  id2: {
    connections: ['id1'],
    id: 'id2',
    value: 'value2'
  }
}

removeConnection(nodeAId, nodeBId, graph)

Returns a new graph with nodeA and nodeB disconnected.

removeConnection(
  'id1',
  'id2',
  addConnection(
    'id1',
    'id2',
    addNode(
      createNode('id2', 'value2'),
      createGraph(createNode('id1', 'value1'))
    )
  )
)


// Output
{
  id1: {
    connections: [],
    id: 'id1',
    value: 'value1'
  },
  id2: {
    connections: [],
    id: 'id2',
    value: 'value2'
  }
}

removeAllConnections(nodeId, graph)

Returns a new graph with graph[nodeId] disconnected from other nodes.

removeAllConnections(
  'id1',
  addConnection(
    'id2',
    'id3',
    addConnection(
      'id1',
      'id3',
      addConnection(
        'id1',
        'id2',
        addNode(
          createNode('id3', 'value3'),
          addNode(
            createNode('id2', 'value2'),
            createGraph(createNode('id1', 'value1'))
          )
        )
      )
    )
  )
)


// Output
{
  id1: {
    connections: [],
    id: 'id1',
    value: 'value1'
  },
  id2: {
    connections: ['id3'],
    id: 'id2',
    value: 'value2'
  },
  id3: {
    connections: ['id2'],
    id: 'id3',
    value: 'value3'
  }
}

removeNode(nodeId, graph)

Returns a new graph without graph[nodeId].

removeNode(
  'id1',
  addNode(
    createNode('id2', 'value2'),
    createGraph(createNode('id1', 'value1'))
  )
)

// Output
{
  id2: {
    connections: [],
    id: 'id2',
    value: 'value2'
  }
}

Development

Testing (Jest)

To run all tests once, use:

npm run test

To run a watcher that reruns tests related to changed files, use:

npm run test:watch

Building (Babel)

npm run build

Next steps

  • More functions?