import {Selection} from "./index.js";

export default function(selection) {
  if (!(selection instanceof Selection)) throw new Error("invalid merge");

  for (var groups0 = this._groups, groups1 = selection._groups, m0 = groups0.length, m1 = groups1.length, m = Math.min(m0, m1), merges = new Array(m0), j = 0; j < m; ++j) {
    for (var group0 = groups0[j], group1 = groups1[j], n = group0.length, merge = merges[j] = new Array(n), node, i = 0; i < n; ++i) {
      if (node = group0[i] || group1[i]) {
        merge[i] = node;
      }
    }
  }

  for (; j < m0; ++j) {
    merges[j] = groups0[j];
  }

  return new Selection(merges, this._parents);
}

@@ -56,25 +56,23 @@
 unction(
-selection
+context
 ) %7B%0A  if
@@ -106,16 +106,38 @@
 lection)
+ && !context.selection
 ) throw 
@@ -163,16 +163,85 @@
 merge%22);
+%0A  var selection = context.selection ? context.selection() : context;
 %0A%0A  for 

Implement selection.merge(transition) Closes #257