yano3nora
10/14/2017 - 10:13 AM

[js: is()] Verifying to data type method from JavaScript Garden. #js

[js: is()] Verifying to data type method from JavaScript Garden. #js

/**
 * Variable data type is ?
 * @param  string - Type name (String, Number, Boolean, Date, Error, Array, Function, RegExp, Object).
 * @param  any    - Target variable.
 * @return bool
 * @link   http://bonsaiden.github.io/JavaScript-Garden/ja/#types.typeof
 */
function is(type, obj) {
  var clas = Object.prototype.toString.call(obj).slice(8, -1);
  return obj !== undefined && obj !== null && clas === type;
}

is('String', 'hoge'); // true
is('String', new String('hoge')); // true