Advertisement
stuppid_bot

JS Type Detection

Sep 10th, 2014
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function typeName(o) {
  2.   return Object.prototype.toString.call(o).slice(8, -1);
  3. }
  4.  
  5. function is(t, o) {
  6.   return t == typeName(o);
  7. }
  8.  
  9. function isNumber(o) {
  10.   return is('Number', o);
  11. }
  12.  
  13. var isFinite = Number.isFinite || function (o) {
  14.   return isNumber(o) && o != Infinity && o != -Infinity;
  15. };
  16.  
  17. var isInteger = Number.isInteger || function (o) {
  18.   return isFinite(o) && o % 1 == 0;
  19. };
  20.  
  21. var isInt = isInteger;
  22.  
  23. function isFloat(o) {
  24.   return isFinite(o) && o % 1 != 0;
  25. }
  26.  
  27. function isString(o) {
  28.   return is('String', o);
  29. }
  30.  
  31. function isBoolean(o) {
  32.   return is('Boolean', o);
  33. }
  34.  
  35. function isObject(o) {
  36.   return o != null && typeof o == 'object';
  37. }
  38.  
  39. function isPlainObject(o) {
  40.   return o != null && o.constructor == Object;
  41. };
  42.  
  43. function isArray(o) {
  44.   return Array.isArray(o);
  45. }
  46.  
  47. function isFunction(o) {
  48.   return typeof o == 'function';
  49. }
  50.  
  51. function isRegExp(o) {
  52.   return is('RegExp', o);
  53. }
  54.  
  55. function isDate(o) {
  56.   return is('Date', o);
  57. }
  58.  
  59. function isElement(o) {
  60.   return o instanceof HTMLElement;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement