docs: function toposort (nodes) {
    // Test if a node has got any incoming edges
    function hasIncomingEdge(list, node) {
      for (var i = 0, l = list.length; i < l; ++i) {
        if (list[i].links.find(function(e,i,a){
          return node._id == e;
        })) return true;
      }
      return false;
    };
  
    // Kahn's Algorithm
    var L = [],
        S = nodes.filter(function(node) {
          return !hasIncomingEdge(nodes, node);
        }),
        n = null;
  
    while(S.length) {
      // Remove a node n from S
      n = S.pop();
      // Add n to tail of L
      L.push(n);
  
      var i = n.links.length;
      while (i--) {
        // Getting the node associated to the current stored id in links
        var m = nodes.find(function(e){
          return n.links[i] === e._id;
        });
        if (!m) throw new Error('Could not find node from link: '+n.links[i]);
  
        // Remove edge e from the graph
        n.links.pop();
  
        if (!hasIncomingEdge(nodes, m)) {
          S.push(m);
        }
      }
    }
  
    // If any of them still have links, there is cycle somewhere
    var nodesWithEdges = nodes.filter(function(node) {
      return node.links.length !== 0;
    });
  
    // modified to return both the cyclic nodes and the sort
    return {
      sort : nodesWithEdges.length == 0 ? L : null,
      nodesWithEdges : nodesWithEdges
    };
  }