I need some feedback for this set of value-test functions (are they doing what they say they do). Also ways to improve some of them, suggestions to add more test functions, etc.
// Object.test.isnumeric('0x12') -> true, etc.
!(( function ( field, define ) {
this[field] = define();
} ).call(
Object,
"test",
function () {
var _inner = {
corstr:function ( o ) { return Object.prototype.toString.call( o ); },
corePrimitiveTypes : ['[object Undefined]', '[object Null]', '[object Number]', '[object String]', '[object Boolean]'],
emptyValues : [void 0, null, false, 0, ""],
reg:{
SCALAR : /^boolean|number|string$/,
URL : /^(?:[a-z]+:\/\/)?(?:\/)?(?:\.\.\/)*[a-z][\w\-.]*(?:\:\d+)?[\/\w#!:.?+=&%@!\-]+$/i,
EMAIL : /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/,
VALUE_TYPE : /\b(\w+)\]$/
}
};
return {
// #isbool
isbool: function (o) {
return _inner.corstr( o ) === '[object Boolean]';
},
// #isnum
isnum: function (o) {
return isFinite( o ) && _inner.corstr(o) === "[object Number]";
},
// #isnumeric
isnumeric: function (o) {
try {
return isFinite( o = o["valueOf"]() )
&& ( eval( o ) === parseFloat( Number( o ) ) );
} catch(e) {}
return false;
},
// #isunum
isunum: function (n) {
return this.isnum( n ) && ( n >= 0 );
},
// #isint
isint: function ( n ) {
return n === +n && isFinite( n ) && !( n%1 );
},
// #isuint
isuint: function ( n ) {
return this.isint( n ) && ( n >= 0 );
},
// #isfloat
isfloat: function ( n ) {
return +n === n && ( !isFinite(n) || !!( n % 1 ) );
},
// #isprimitive
isprimitive: function ( o ) {
return _inner.corePrimitiveTypes.indexOf( _inner.corstr( o ) ) != -1;
},
// #isstr
isstr: function ( o ) {
return _inner.corstr( o ) === "[object String]";
},
// #isfn
isfn: function (o) {
return typeof o === "function";
},
// #iswin
iswin: function (o) {
return o && o.top && ( o === o.window );
},
// #isarray
isarray: function ( o ) {
return this.isfn( Array.isArray )
? Array.isArray( o )
: _inner.corstr( o ) === '[object Array]';
},
// #isarraylike
isarraylike: function (o) {
if ( !o ) return false;
if ( this.iswin( o ) ) return false;
var len = o.length,
T = this.type( o );
if ( o.nodeType === 1 && len ) return true;
return T === "array" ||
T !== "string" &&
T !== "function" &&
(
len === 0
|| typeof len === "number"
&& len > 0
&& (len - 1) in Object( o )
);
},
// #isplainobj
isplainobj: function (o) {
return _inner.corstr( o ) === "[object Object]";
},
// #isemptyobj
isemptyobj: function ( o ) {
try {
for ( var l in o )
return false;
return true;
} catch( e ) {
return false;
}
},
// #isdata , -> true for: [ {}+ ] structures
isdata: function (o) {
return this.isarray( o )
&& o.every(
function ( v ) {
return this.isplainobj( v );
},
this
);
},
// #isobj
isobj: function (o) {
return o === Object(o);
},
// #isscalar, -> true for: true/false, string, number
isscalar: function ( o ) {
return _inner.reg.SCALAR.test( this.type( o ) );
},
// #isempty, -> true for: undefined, null, 0, "", false, NaN, {}, []
isempty: function ( o ) {
if ( arguments.length === 0 )
return void 0;
var key, i, len, T;
for (
i = 0,
len = _inner.emptyValues.length;
i < len;
i++
) {
if (
o === _inner.emptyValues[i]
|| o !== o
) return true;
}
if ( this.isarray( o ) ) return o.length == 0;
if ( this.isobj( o ) ) return this.isemptyobj( o );
return false;
},
// #isvalid, -> false, for: undefined, null, NaN
isvalid: function ( o ) {
return o !== void 0
&& o !== null
&& ( o === o );
},
// #isdomobj
isdomobj: function (el) {
return this.isobj(el)
&& ( el instanceof Node )
&& (
( el.ownerDocument || el )
.documentElement
.nodeName
.toUpperCase() === "HTML"
);
},
// #isurl
isurl: function ( str ) {
return _inner.reg.URL.test( String( str ) );
},
// #isemail
isemail: function (str) {
return _inner.reg.EMAIL.test( String( str ) );
},
// #isxmlnode
isxmlnode: function ( elm ) {
var docElement =
( elm ? elm.ownerDocument || elm : 0 ).documentElement;
return docElement
? documentElement.nodeName !== "HTML"
: false;
},
// #ishtm
ishtm: function ( input, _testel ) {
return (
_testel = doc.createElement("div"),
_testel.innerHTML = String( input ),
_testel.getElementsByTagName("*").length > 0
);
},
// #type
type : function ( o ) {
if ( o === void 0 )
return "undefined";
var out;
(
( out = this.iswin( o )
&& 'window'
|| _inner.corstr( o )
.match( _inner.reg.VALUE_TYPE )[1]
.toLowerCase()
) === 'number'
)
&& (
isFinite( o ) || ( out = String( o ) )
);
return out;
}
};
//
}
));