function generalpattern(selection, cb) { var enter = selection.enter(); if (cb.before) cb.before(selection); if (cb.enter) enter = cb.enter(enter); if (cb.exit) cb.exit(selection.exit()); if (cb.update) cb.update(selection.merge(enter)); } // USAGE svg.selectAll('path').data([…]) .call(generalupdate, { enter: function (g) { return g.append('path') }, exit: function (g) { g .transition() .remove() }, update: function (g) { g .transition() .attr('d', function (d) {}) }, // before: function(g) {…}, } });