index.js
function (value, type) {
    type = type || 'smart';

    switch (type) {
      case 'boolean':
      case 'bool':
        if (typeof value !== 'string') {
          value = !!value;
        } else {
          value = ['null', 'undefined', '0', 'false'].indexOf(value) === -1;
        }

        break;

      case 'string':
      case 'text':
        value = this.cast(value, 'boolean') ? value + '' : null
        break;

      case 'date':
      case 'datetime':
        value = new Date(value);
        break;

      case 'int':
      case 'integer':
      case 'number':
        value = ~~value;
        break;

      case 'float':
        value = parseFloat(value);
        break;

      case 'smart':
        value = this.cast(value, this.detect(value));
        break;

      default:
        throw new Error('Expected valid casting type.');
    }

    return value;
  }
function (value, type) {
    type = type || 'smart';

    switch (type) {
      case 'boolean':
      case 'bool':
        if (typeof value !== 'string') {
          value = !!value;
        } else {
          value = ['null', 'undefined', '0', 'false'].indexOf(value) === -1;
        }

        break;

      case 'string':
      case 'text':
        value = this.cast(value, 'boolean') ? value + '' : null
        break;

      case 'date':
      case 'datetime':
        value = new Date(value);
        break;

      case 'int':
      case 'integer':
      case 'number':
        value = ~~value;
        break;

      case 'float':
        value = parseFloat(value);
        break;

      case 'smart':
        value = this.cast(value, this.detect(value));
        break;

      default:
        throw new Error('Expected valid casting type.');
    }

    return value;
  }
function ( value , type ) { type = type || 'smart' ; switch ( type ) { case 'boolean' : case 'bool' : if ( typeof value !== 'string' ) { value = ! ! value ; } else { value = [ 'null' , 'undefined' , '0' , 'false' ] . indexOf ( value ) === - 1 ; } break ; case 'string' : case 'text' : value = this . cast ( value , 'boolean' ) ? value + '' : null break ; case 'date' : case 'datetime' : value = new Date ( value ) ; break ; case 'int' : case 'integer' : case 'number' : value = ~ ~ value ; break ; case 'float' : value = parseFloat ( value ) ; break ; case 'smart' : value = this . cast ( value , this . detect ( value ) ) ; break ; default : throw new Error ( 'Expected valid casting type.' ) ; } return value ; }
Cast `value` to given `type`.

@param {*}      value
@param {String} [type]

@returns {*}
Cast value to given type .