import m from 'mithril';

var isDom = function isDom (x) {
    return x.nodeType > 0;
    };

var notDom = function notDom (x) {
    return !isDom(x);
    };

export default function n (...input) {
    // Wrap any DOM arguments in an array:
    // Avoids Mithril parsing DOM as vdom attrs and makes our logic simpler.
    var view = m(...input.map(x => isDom(x) ? [x] : x));

    var cfg = view.attrs.config;
    var kids = view.children;

    var dom = [];
    var vdom = [];

    // Divide children between dom & vdom
    if (Array.isArray(kids)) for (let x of kids) {
        (isDom(x) ? dom : vdom).push(x);
        }

    if (dom.length) {
        view.attrs.config = function config (el, init, context) {
            if (!init) {
                dom.map(dom, (node, index) => {
                    var next = kids.slice(kids.indexOf(node)).find(notDom);
                    var location = el.childNodes[vdom.indexOf(next)] || null;

                    (function insertNode (node) {
                        if (node instanceof Array) {
                            node = node;
                            }
                        else if (node.nodeType === node.DOCUMENT_FRAGMENT_NODE) {
                            node = Array.from(node.childNodes);
                            }
                        else {
                            return el.insertBefore(node, location);
                            }

                        dom[index] = node;

                        for (let subNode of node){
                            insertNode(subNode);
                            }
                        })(node);

                    });
                }

            if (cfg) return cfg(el, init, context);
            };

        // Make sure only the virtual elements are parsed by m.render.
        view.children = vdom;
        }

    return view;
    }

@@ -43,25 +43,31 @@
 tion isDom (
-x
+element
 ) %7B%0A    retu
@@ -69,17 +69,23 @@
  return 
-x
+element
 .nodeTyp
@@ -194,21 +194,20 @@
 n n (...
-input
+args
 ) %7B%0A    
@@ -346,19 +346,20 @@
 (...
-input
+args
 .map(
-x
+arg
  =%3E 
@@ -368,20 +368,26 @@
 Dom(
-x
+arg
 ) ? %5B
-x
+arg
 %5D : 
-x
+arg
 ));%0A
@@ -396,17 +396,20 @@
    var c
-f
+onfi
 g = view
@@ -572,17 +572,19 @@
 or (let 
-x
+kid
  of kids
@@ -602,17 +602,19 @@
  (isDom(
-x
+kid
 ) ? dom 
@@ -626,17 +626,19 @@
 m).push(
-x
+kid
 );%0A     
@@ -706,24 +706,41 @@
 ion 
-config (el, init
+injectNodes (element, initialized
 , co
@@ -769,35 +769,24 @@
 f (!init
-) %7B%0A               
+ialized)
  dom.map
@@ -810,20 +810,16 @@
 x) =%3E %7B%0A
-    
         
@@ -894,28 +894,24 @@
             
-    
 var location
@@ -915,16 +915,21 @@
 ion = el
+ement
 .childNo
@@ -962,20 +962,16 @@
  null;%0A%0A
-    
         
@@ -990,19 +990,19 @@
 ction in
-ser
+jec
 tNode (n
@@ -1004,28 +1004,24 @@
 de (node) %7B%0A
-    
             
@@ -1077,28 +1077,24 @@
             
-    
 node = node;
@@ -1114,34 +1114,26 @@
             
- 
-   %7D%0A    
+%7D%0A
             
@@ -1218,28 +1218,24 @@
             
-    
 node = Array
@@ -1278,34 +1278,26 @@
             
-  
-  %7D%0A    
+%7D%0A
             
@@ -1335,20 +1335,16 @@
         
-    
 return e
@@ -1344,16 +1344,21 @@
 eturn el
+ement
 .insertB
@@ -1400,35 +1400,27 @@
             
-    %7D%0A%0A    
+%7D%0A%0A
             
@@ -1467,20 +1467,16 @@
         
-   
- 
 for (let
@@ -1522,17 +1522,13 @@
     
-    inser
+injec
 tNod
@@ -1539,20 +1539,16 @@
 bNode);%0A
-    
         
@@ -1585,20 +1585,16 @@
         
-    
 %7D)(node)
@@ -1595,19 +1595,16 @@
 (node);%0A
-%0A  
         
@@ -1615,73 +1615,91 @@
     
-  
 %7D);%0A
-                %7D%0A%0A            if (cfg) return cfg(el, init
+%0A            if (config instanceof Function) return config(element, initialized
 , co
@@ -1852,13 +1852,14 @@
  view;%0A    %7D
+;
 %0A

Refactor for more expressive variable names