1 /**
  2  * @namespace Simple methods to assert that various objects exist or are of a
  3  * given type.
  4  */
  5 Jelo.Valid = function() {
  6     
  7     // the easy way
  8     Array.prototype.isArray = true;
  9     Function.prototype.isFunction = true;
 10     Number.prototype.isNumber = true;
 11     String.prototype.isString = true;
 12     
 13     /** @scope Jelo.Valid */
 14     return {
 15         
 16         /**
 17          * Checks whether an object exists (not necessarily truthy).
 18          * 
 19          * @param {Mixed} item The item to investiate.
 20          * @return {Boolean} False only for null and undefined values
 21          */
 22         is         : function(i) {
 23             return (typeof i === "undefined") || (i === null);
 24         },
 25         
 26         /**
 27          * Checks whether an object is an array or array subclass.
 28          * 
 29          * @param {Mixed} item The item to investiate.
 30          * @return {Boolean} True if item is an array or array subclass.
 31          */
 32         isArray    : function(a) {
 33             return a && (a.constructor == Array);
 34         },
 35         
 36         /**
 37          * Checks whether an object is "Array-like" and can probably 
 38          * be iterated over, looped through, etc.
 39          * 
 40          * @param {Mixed} item The item to investiate.
 41          * @return {Boolean} True if item is iterable.
 42          */
 43         isIterable : function(a) {
 44             return (typeof a == 'object' && typeof a.length == 'number');
 45         },
 46         
 47         /**
 48          * Checks whether an object is an HTML/DOM element.
 49          * 
 50          * @param {Mixed} item The item to investiate.
 51          * @return {Boolean} True if item is an HTMLElement-like object.
 52          */
 53         isElement  : function(e) {
 54             return (typeof e == "object") && (e !== null) &&
 55                 (typeof e.tagName == "string") &&
 56                 (typeof e.className == "string");
 57         },
 58         
 59         /**
 60          * Checks whether an object is a Function.
 61          * 
 62          * @param {Mixed} item The item to investiate.
 63          * @return {Boolean} True if item is a Function.
 64          */
 65         isFunction : function(f) {
 66             return !!f.isFunction || (f instanceof Function) ||
 67                 (typeof f == "function");
 68         },
 69         
 70         /**
 71          * Check whether an object is a Number.
 72          * 
 73          * @param {Mixed} item The item to investiate.
 74          * @return {Boolean} True if item is a Number.
 75          */
 76         isNumber   : function(n) {
 77             return n && (!!n.isNumber || !isNaN(n));
 78         },
 79         
 80         /**
 81          * Checks whether an e-mail address is formatted correctly.
 82          * 
 83          * @param {Mixed} item The item to investiate.
 84          * @return {Boolean} True if item is formatted like a valid e-mail
 85          * address. The actual account may or may not exist. Future versions of
 86          * Jelo will include a check for actual e-mail accounts.
 87          */
 88         isEmail    : function(e) {
 89             var regex = /^[a-z0-9_\-]+(\.[_a-z0-9\-]+)*@([_a-z0-9\-]+\.)+([a-z]{2}|aero|arpa|biz|com|coop|edu|gov|info|int|jobs|mil|museum|name|nato|net|org|pro|travel)$/;
 90             return regex.test(e);
 91         }
 92     };
 93 }();
 94