import { select } from 'd3-selection'

export function appendIfMissing(tagAndClasses, initAttributes) {

  return function append(d, i) {
    const target = select(this)
      , selection = target.select(tagAndClasses)

    let elements
      , classes
      , tag
      , element

    if (!selection.empty()) return selection.node()

    elements = tagAndClasses.split('.')
    tag = _.first(elements)
    classes = _.rest(elements)

    element = target.append(tag)

    if (initAttributes) element.attr(initAttributes)
    if (classes.length) element.classed(classes.join(' '), true)

    return element.node()
  }

}

@@ -30,16 +30,57 @@
 lection'
+%0Aimport %7B first, rest %7D from 'underscore'
 %0A%0Aexport
@@ -419,18 +419,16 @@
   tag = 
-_.
 first(el
@@ -453,10 +453,8 @@
 s = 
-_.
 rest

fix incorrect underscore global.