lib/rdfToJsonld.js
function rdfToJsonld (triples, config) {
  var useRdfType = config.useRdfType;
  var useNativeTypes = config.useNativeTypes;

  var graphs = getGraphs(triples);
  var defaultGraph = {};
  var graphMap = {
    '@default': defaultGraph
  };

  Object.keys(graphs)
    .forEach(function (name) {
      var triples = graphs[name];
      if (name === '') {
        name = '@default';
      } else if (!defaultGraph[name]) {
        defaultGraph[name] = {
          '@id': name
        };
      }
      graphMap[name] = graphMap[name] || {};
      var nodeMap = graphMap[name];
      triples.forEach(function (triple) {
        if (!nodeMap[triple.subject]) {
          nodeMap[triple.subject] = {
            '@id': triple.subject
          };
        }
        var node = nodeMap[triple.subject];
        var object = triple.object;
        if (object.id) {
          if (!nodeMap[object.id]) {
            nodeMap[object.id] = {
              '@id': object.id
            };
          }
        }
        if (triple.predicate === RDF__TYPE && !useRdfType && object.id) {
          if (!node['@type']) {
            node['@type'] = [ object.id ];
          }
          return 'continue';
        }

        var value = rdfObjectToJsonldObject(object, useNativeTypes);
        if (!node[triple.predicate]) {
          node[triple.predicate] = [];
        }
        var alreadyExists = node[triple.predicate].some(function (existingValue) {
          var areEquivalent = equivalentObjects(value, existingValue);
          return areEquivalent;
        });
        if (!alreadyExists) {
          node[triple.predicate].push(value);
        }
        if (object.id) {
          if (!node.usages) {
            node.usages = [];
          }
          node.usages.push({
            node: node,
            property: triple.predicate,
            value: value
          });
        }
      });
    });

  Object.keys(graphMap)
    .forEach(function (name) {
      var graph = graphMap[name];
      var nil = graph[RDF__NIL];
      if (!nil) {
        return 'continue';
      }
      nil.usages
        .forEach(function (usage) {
          var node = usage.node;
          var property = usage.property;
          var head = usage.value;
          var list = [];
          var listNodes = [];

          while (property === RDF__REST && isWellFormedListNode(node)) {
            list.push(node[RDF__FIRST][0]);
            listNodes.push(node['@id']);
            var nodeUsage = node.usages[0];
            node = nodeUsage.node;
            property = nodeUsage.property;
            head = nodeUsage.value;
            if (!/^_:/.test(node['@id'])) {
              break;
            }
          }

          if (property === RDF__FIRST) {
            if (node['@id'] === RDF__NIL) {
              return 'continue';
            }
            var headId = head['@id'];
            head = graph[headId];
            head = head[RDF__REST][0];
            list.pop();
            listNodes.pop();
          }

          delete head['@id'];
          list.reverse();
          head['@list'] = list;
          listNodes
            .forEach(function (nodeId) {
              delete graph[nodeId];
            });
        });
    });

  var result = [];
  Object.keys(defaultGraph)
    .sort()
    .forEach(function (subject) {
      var node = defaultGraph[subject];
      if (graphMap[subject]) {
        node['@graph'] = [];
        Object.keys(graphMap[subject])
          .sort()
          .forEach(function (s) {
            var n = graphMap[s];
            delete n.usages;
            if (Object.keys(n).length === 1 && n['@id']) {
              return 'continue';
            }
            node['@graph'].push(n);
          });
      }

      delete node.usages;
      if (Object.keys(node).length === 1 && node['@id']) {
        return 'continue';
      }
      result.push(node);
    });

  return result;
}
function rdfToJsonld (triples, config) {
  var useRdfType = config.useRdfType;
  var useNativeTypes = config.useNativeTypes;

  var graphs = getGraphs(triples);
  var defaultGraph = {};
  var graphMap = {
    '@default': defaultGraph
  };

  Object.keys(graphs)
    .forEach(function (name) {
      var triples = graphs[name];
      if (name === '') {
        name = '@default';
      } else if (!defaultGraph[name]) {
        defaultGraph[name] = {
          '@id': name
        };
      }
      graphMap[name] = graphMap[name] || {};
      var nodeMap = graphMap[name];
      triples.forEach(function (triple) {
        if (!nodeMap[triple.subject]) {
          nodeMap[triple.subject] = {
            '@id': triple.subject
          };
        }
        var node = nodeMap[triple.subject];
        var object = triple.object;
        if (object.id) {
          if (!nodeMap[object.id]) {
            nodeMap[object.id] = {
              '@id': object.id
            };
          }
        }
        if (triple.predicate === RDF__TYPE && !useRdfType && object.id) {
          if (!node['@type']) {
            node['@type'] = [ object.id ];
          }
          return 'continue';
        }

        var value = rdfObjectToJsonldObject(object, useNativeTypes);
        if (!node[triple.predicate]) {
          node[triple.predicate] = [];
        }
        var alreadyExists = node[triple.predicate].some(function (existingValue) {
          var areEquivalent = equivalentObjects(value, existingValue);
          return areEquivalent;
        });
        if (!alreadyExists) {
          node[triple.predicate].push(value);
        }
        if (object.id) {
          if (!node.usages) {
            node.usages = [];
          }
          node.usages.push({
            node: node,
            property: triple.predicate,
            value: value
          });
        }
      });
    });

  Object.keys(graphMap)
    .forEach(function (name) {
      var graph = graphMap[name];
      var nil = graph[RDF__NIL];
      if (!nil) {
        return 'continue';
      }
      nil.usages
        .forEach(function (usage) {
          var node = usage.node;
          var property = usage.property;
          var head = usage.value;
          var list = [];
          var listNodes = [];

          while (property === RDF__REST && isWellFormedListNode(node)) {
            list.push(node[RDF__FIRST][0]);
            listNodes.push(node['@id']);
            var nodeUsage = node.usages[0];
            node = nodeUsage.node;
            property = nodeUsage.property;
            head = nodeUsage.value;
            if (!/^_:/.test(node['@id'])) {
              break;
            }
          }

          if (property === RDF__FIRST) {
            if (node['@id'] === RDF__NIL) {
              return 'continue';
            }
            var headId = head['@id'];
            head = graph[headId];
            head = head[RDF__REST][0];
            list.pop();
            listNodes.pop();
          }

          delete head['@id'];
          list.reverse();
          head['@list'] = list;
          listNodes
            .forEach(function (nodeId) {
              delete graph[nodeId];
            });
        });
    });

  var result = [];
  Object.keys(defaultGraph)
    .sort()
    .forEach(function (subject) {
      var node = defaultGraph[subject];
      if (graphMap[subject]) {
        node['@graph'] = [];
        Object.keys(graphMap[subject])
          .sort()
          .forEach(function (s) {
            var n = graphMap[s];
            delete n.usages;
            if (Object.keys(n).length === 1 && n['@id']) {
              return 'continue';
            }
            node['@graph'].push(n);
          });
      }

      delete node.usages;
      if (Object.keys(node).length === 1 && node['@id']) {
        return 'continue';
      }
      result.push(node);
    });

  return result;
}
http://www.w3.org/TR/json-ld-api/#serialize-rdf-as-json-ld-algorithm