var typeOf = require('component-type')

module.exports = castFromString

function castFromString (string, to) {
  if (castTo[to]) return castTo[to](string)
  throw new Error('Invalid cast type.')
}

var castTo = {
  array: function (string) {
    if (typeOf(string) === 'string') {
      if (string === '') return []
      return string.split(',')
    }
    throw new Error('Should be a string of values separated by commas.')
  },

  boolean: function (string) {
    if (string === 'true') return true
    if (string === 'false') return false
    throw new Error('Should be a boolean written as string.')
  },

  number: function (string) {
    var number = parseInt(string, 10)
    if (typeOf(number) === 'number') return number
    throw new Error('Should be a number written as string.')
  },

  string: function (string) {
    if (typeOf(string) === 'string') return string
    throw new Error('Should be string.')
  },

  object: function (string) {
    try {
      return JSON.parse(string)
    } catch (err) {
      throw new Error('Should be object.')
    }
  }
}

@@ -965,22 +965,27 @@
 %7B%0A      
-return
+var value =
  JSON.pa
@@ -996,16 +996,137 @@
 string)%0A
+      if (typeOf(value) === 'object') return value%0A      throw new Error('Should be a JSON object written as a string.')%0A
     %7D ca
@@ -1170,22 +1170,49 @@
 ould be 
-object
+a JSON object written as a string
 .')%0A    

check if JSON value is object, dont collide with arrays