# all files / clique/adapter/ NodeLinkList.js

51.72% Statements 45/87
56.36% Branches 31/55
44.44% Functions 8/18
13.33% Lines 6/45
10 statements, 2 functions, 16 branches Ignored

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    841×

    1×
    1×

    1×
    1×

     import _ from 'underscore';

    import Adapter from './Adapter';
    import { deepCopy } from '../../candela/util';

    export default class NodeLinkList extends Adapter {
      constructor (nodes, links) {
        super();

        this.nodes = nodes;
        this.links = links;

        let table = [];
        _.each(this.nodes, (node, i) => {
          // Compute a suitable temporary namespace.
          let tmpNs = 'datax';
          while (node.hasOwnProperty(tmpNs)) {
            tmpNs += 'x';
          }

          // Create the namespace on the node.
          let ns = node[tmpNs] = {};

          // Move all top-level properties into the namespace.
          _.each(node, (v, k) => {
            if (k !== tmpNs) {
              ns[k] = v;
              delete node[k];
            }
          });

          // Rename the temp namespace to "data".
          if (tmpNs !== 'data') {
            node.data = ns;
            delete node[tmpNs];
          }

          node.key = _.uniqueId('node_');

          table.push(node);
        });

        _.each(this.links, link => {
          link.key = _.uniqueId('link_');

          link.source = table[link.source];
          link.target = table[link.target];
        });
      }

      findNodesRaw (_spec, offset, limit) {
        const spec = deepCopy(_spec);
        let searchspace = this.nodes;

        if (spec.key) {
          searchspace = _.filter(searchspace, node => node.key === spec.key);
          delete spec.key;
        }

        let result = _.filter(searchspace, node => _.isMatch(node.data, spec));
        return result.slice(offset, _.isNull(limit) ? undefined : (offset + limit));
      }

      findLinksRaw (_spec, source, target, directed, offset, limit) {
        const spec = deepCopy(_spec);
        let searchspace = this.links;

        if (spec.key) {
          searchspace = _.filter(searchspace, link => link.key === spec.key);
          delete spec.key;
        }

        return _.filter(searchspace, link => {
          const sourceMatch = _.isNull(source) || (link.source.key === source);
          const targetMatch = _.isNull(target) || (link.target.key === target);
          const dataMatch = _.isMatch(link.data, spec);
          const directedMatch = _.isNull(directed) ? true : (directed ? !link.undirected : link.undirected);

          return sourceMatch && targetMatch && dataMatch && directedMatch;
        }).slice(offset, _.isNull(limit) ? undefined : (offset + limit));
      }
    }