'use strict'

var types = require('./types')
var OBJECT = types.OBJECT
var STRING = types.STRING
var BOOLEAN = types.BOOLEAN
var NUMBER = types.NUMBER

module.exports = function valid(obj) {
  var type = typeof obj
  switch (type) {
    case STRING:
    case BOOLEAN:
      return true
    case NUMBER:
      return isFinite(obj)
  }

  if (obj === null)
    return true
  else if (type !== OBJECT)
    return false

  var c
  var it

  if (global.Set && obj instanceof Set) {
    c = true
    var values = obj.values()
    while (c === true) {
      it = values.next()
      if (it.done === true) c = false
      else if (!valid(it.value)) return false
    }
    return true
  }

  if (global.Map && obj instanceof Map) {
    c = true
    var entries = obj.entries()
    while (c === true) {
      it = entries.next()
      if (it.done === true) {
        c = false
        continue
      }
      if (typeof it.value[0] !== 'string') return false
      else if (!valid(it.value[1])) return false
    }
    return true
  }

  var i
  var l

  if (Array.isArray(obj)) {
    for (i = 0, l = obj.length; i < l; i++)
      if (!valid(obj[i])) return false
    return true
  }

  var keys = Object.keys(obj)
  for (i = 0, l = keys.length; i < l; i++) {
    var k = keys[i]
    if (!valid(obj[k])) return false
  }
  return true
}

@@ -420,18 +420,18 @@
 var 
-c%0A  var
+i, l,
  it
+em
 %0A%0A  
@@ -466,37 +466,24 @@
 ceof Set) %7B%0A
-    c = true%0A
     var valu
@@ -508,103 +508,52 @@
     
-while (c === true) %7B%0A      it = values.next()%0A      if (it.done === true) c = false
+for (i = 0, l = obj.size; i %3C l; i++)
 %0A      
-else 
 if (
@@ -555,26 +555,37 @@
  if (!valid(
-it
+values.next()
 .value)) ret
@@ -586,38 +586,32 @@
 )) return false%0A
-    %7D%0A
     return true%0A
@@ -661,21 +661,8 @@
 ) %7B%0A
-    c = true%0A
     
@@ -697,25 +697,44 @@
     
-while (c === true
+for (i = 0, l = obj.size; i %3C l; i++
 ) %7B%0A
@@ -741,16 +741,18 @@
       it
+em
  = entri
@@ -764,82 +764,15 @@
 xt()
-%0A      if (it.done === true) %7B%0A        c = false%0A        contin
+.val
 ue%0A
-      %7D%0A
     
@@ -786,22 +786,18 @@
 ypeof it
-.valu
 e
+m
 %5B0%5D !== 
@@ -843,22 +843,18 @@
 valid(it
-.valu
 e
+m
 %5B1%5D)) re
@@ -895,25 +895,8 @@
  %7D%0A%0A
-  var i%0A  var l%0A%0A
   if
@@ -1099,30 +1099,8 @@
 i++)
- %7B%0A    var k = keys%5Bi%5D
 %0A   
@@ -1116,16 +1116,22 @@
 id(obj%5Bk
+eys%5Bi%5D
 %5D)) retu
@@ -1139,20 +1139,16 @@
 n false%0A
-  %7D%0A
   return

rework valid