simple-is.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // simple-is.js
  2. // MIT licensed, see LICENSE file
  3. // Copyright (c) 2013 Olov Lassus <olov.lassus@gmail.com>
  4. var is = (function() {
  5. "use strict";
  6. var hasOwnProperty = Object.prototype.hasOwnProperty;
  7. var toString = Object.prototype.toString;
  8. var _undefined = void 0;
  9. return {
  10. nan: function(v) {
  11. return v !== v;
  12. },
  13. boolean: function(v) {
  14. return typeof v === "boolean";
  15. },
  16. number: function(v) {
  17. return typeof v === "number";
  18. },
  19. string: function(v) {
  20. return typeof v === "string";
  21. },
  22. fn: function(v) {
  23. return typeof v === "function";
  24. },
  25. object: function(v) {
  26. return v !== null && typeof v === "object";
  27. },
  28. primitive: function(v) {
  29. var t = typeof v;
  30. return v === null || v === _undefined ||
  31. t === "boolean" || t === "number" || t === "string";
  32. },
  33. array: Array.isArray || function(v) {
  34. return toString.call(v) === "[object Array]";
  35. },
  36. finitenumber: function(v) {
  37. return typeof v === "number" && isFinite(v);
  38. },
  39. someof: function(v, values) {
  40. return values.indexOf(v) >= 0;
  41. },
  42. noneof: function(v, values) {
  43. return values.indexOf(v) === -1;
  44. },
  45. own: function(obj, prop) {
  46. return hasOwnProperty.call(obj, prop);
  47. },
  48. };
  49. })();
  50. if (typeof module !== "undefined" && typeof module.exports !== "undefined") {
  51. module.exports = is;
  52. }