Skip to content
This repository has been archived by the owner on Dec 30, 2022. It is now read-only.
/ pure-graph-utils Public archive

🍡 Graph generation and handling utils, built with pure functions.

License

Notifications You must be signed in to change notification settings

alcar/pure-graph-utils

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pure-graph-utils

npm npm bundle size

Graph generation and handling utils, built with pure functions.

Disclaimer

This project is no longer being maintained.

Table of contents

Installation

npm install pure-graph-utils

API

To get a better grasp of each function's behavior, 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

Single run

npm run test

Watch mode

npm run test:watch

Building

npm run build

About

🍡 Graph generation and handling utils, built with pure functions.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published