base Ox.typeOf on Object.prototype.toString, add Ox.checkType, Ox.isInt and Ox.isPrimitive

This commit is contained in:
rolux 2012-05-19 12:15:04 +04:00
parent 6e4a940ddd
commit 49fc8dc8ed

View file

@ -1,5 +1,17 @@
'use strict'; 'use strict';
/*@
Ox.checkType <f> Throws a TypeError if a value is not of a given type
(val, type) -> <u> undefined
val <*> Any value
type <s> Type
@*/
Ox.checkType = function(val, type) {
if (!Ox.in(Ox.makeArray(type), Ox.typeOf(val))) {
throw new TypeError();
}
};
/*@ /*@
Ox.isArguments <f> Tests if a value is an arguments "array" Ox.isArguments <f> Tests if a value is an arguments "array"
(value) -> <b> True if the value is an arguments "array" (value) -> <b> True if the value is an arguments "array"
@ -8,15 +20,8 @@ Ox.isArguments <f> Tests if a value is an arguments "array"
true true
@*/ @*/
Ox.isArguments = function(val) { Ox.isArguments = function(val) {
return !!(val && val.toString() == '[object Arguments]'); return Ox.typeOf(val) == 'arguments';
} };
if (!(function() {
return Ox.isArguments(arguments);
}())) {
Ox.isArguments = function(val) {
return !!(val && Object.hasOwnProperty.call(val, 'callee'));
};
}
/*@ /*@
Ox.isArray <f> Tests if a value is an array Ox.isArray <f> Tests if a value is an array
@ -30,8 +35,8 @@ Ox.isArray <f> Tests if a value is an array
false false
@*/ @*/
Ox.isArray = function(val) { Ox.isArray = function(val) {
return val instanceof Array; return Ox.typeOf(val) == 'array';
} };
/*@ /*@
Ox.isBoolean <f> Tests if a value is boolean Ox.isBoolean <f> Tests if a value is boolean
@ -41,7 +46,7 @@ Ox.isBoolean <f> Tests if a value is boolean
true true
@*/ @*/
Ox.isBoolean = function(val) { Ox.isBoolean = function(val) {
return typeof val == 'boolean'; return Ox.typeOf(val) == 'boolean';
}; };
/*@ /*@
@ -52,18 +57,18 @@ Ox.isDate <f> Tests if a value is a date
true true
@*/ @*/
Ox.isDate = function(val) { Ox.isDate = function(val) {
return val instanceof Date; return Ox.typeOf(val) == 'date';
}; };
/*@ /*@
Ox.isElement <f> Tests if a value is a DOM element Ox.isElement <f> Tests if a value is a DOM element
(value) -> <b> True if the value is a DOM element (value) -> <b> True if the value is a DOM element
value <*> Any value value <*> Any value
> Ox.isElement(document.createElement()) > Ox.isElement(document.createElement('a'))
true true
@*/ @*/
Ox.isElement = function(val) { Ox.isElement = function(val) {
return !!(val && val.nodeType == 1); return Ox.endsWith(Ox.typeOf(val), 'element');
}; };
/*@ /*@
@ -152,12 +157,12 @@ Ox.isFunction <f> Tests if a value is a function
false false
@*/ @*/
Ox.isFunction = function(val) { Ox.isFunction = function(val) {
return typeof val == 'function' && !Ox.isRegExp(val); return Ox.typeOf(val) == 'function';
}; };
/*@ /*@
Ox.isInfinity <f> Tests if a value is infinite Ox.isInfinity <f> Tests if a value is positive or negative Infinity
(value) -> <b> True if the value is infinite (value) -> <b> True if the value is positive or negative Infinity
value <*> Any value value <*> Any value
> Ox.isInfinity(Infinity) > Ox.isInfinity(Infinity)
true true
@ -167,17 +172,29 @@ Ox.isInfinity <f> Tests if a value is infinite
false false
@*/ @*/
Ox.isInfinity = function(val) { Ox.isInfinity = function(val) {
return typeof val == 'number' && !isFinite(val) && !Ox.isNaN(val); return Ox.typeOf(val) == 'number' && !isFinite(val) && !Ox.isNaN(val);
}; };
/*@ /*@
Ox.isNaN <f> Tests if a value is "Not a Number" Ox.isInt <f> Tests if a value is an integer
(value) -> <b> True if the value is "Not a Number" (value) -> <b> True if the value is an integer
value <*> Any value
> Ox.isInt(0)
true
> Ox.isInt(0.5)
false
@*/
Ox.isInt = function(val) {
return val === Math.floor(val);
};
/*@
Ox.isNaN <f> Tests if a value is NaN
(value) -> <b> True if the value is NaN
value <*> Any value value <*> Any value
> Ox.isNaN(NaN) > Ox.isNaN(NaN)
true true
@*/ @*/
Ox.isNaN = function(val) { Ox.isNaN = function(val) {
return val !== val; return val !== val;
} }
@ -190,7 +207,7 @@ Ox.isNull <f> Tests if a value is <code>null</code>
true true
@*/ @*/
Ox.isNull = function(val) { Ox.isNull = function(val) {
return val === null; return Ox.typeOf(val) == 'null';
}; };
/*@ /*@
@ -206,9 +223,8 @@ Ox.isNumber <f> Tests if a value is a number
> Ox.isNumber(NaN) > Ox.isNumber(NaN)
false false
@*/ @*/
Ox.isNumber = function(val) { Ox.isNumber = function(val) {
return typeof val == 'number' && isFinite(val); return Ox.typeOf(val) == 'number';
}; };
/*@ /*@
@ -226,11 +242,28 @@ Ox.isObject <f> Tests if a value is a an object
> Ox.isObject(/ /) > Ox.isObject(/ /)
false false
@*/ @*/
Ox.isObject = function(val) { Ox.isObject = function(val) {
return typeof val == 'object' && !Ox.isArguments(val) return Ox.typeOf(val) == 'object';
&& !Ox.isArray(val) && !Ox.isDate(val) };
&& !Ox.isNull(val) && !Ox.isRegExp(val);
/*@
Ox.isPrimitive <f> Tests if a value is a primitive (boolean, number or string)
(value) -> <b> True if the value is a primitive
value <*> Any value
> Ox.isPrimitive(false)
true
> Ox.isPrimitive(0)
true
> Ox.isPrimitive('')
true
> Ox.isPrimitive([])
false
> Ox.isPrimitive({})
false
@*/
Ox.isPrimitive = function(val) {
var type = Ox.typeOf(val);
return type == 'boolean' || type == 'number' || type == 'string';
}; };
/*@ /*@
@ -240,9 +273,8 @@ Ox.isRegExp <f> Tests if a value is a regular expression
> Ox.isRegExp(/ /) > Ox.isRegExp(/ /)
true true
@*/ @*/
Ox.isRegExp = function(val) { Ox.isRegExp = function(val) {
return val instanceof RegExp; return Ox.typeOf(val) == 'regexp';
}; };
/*@ /*@
@ -252,9 +284,8 @@ Ox.isString <f> Tests if a value is a string
> Ox.isString('') > Ox.isString('')
true true
@*/ @*/
Ox.isString = function(val) { Ox.isString = function(val) {
return typeof val == 'string'; return Ox.typeOf(val) == 'string';
}; };
/*@ /*@
@ -264,53 +295,45 @@ Ox.isUndefined <f> Tests if a value is undefined
> Ox.isUndefined() > Ox.isUndefined()
true true
@*/ @*/
Ox.isUndefined = function(val) { Ox.isUndefined = function(val) {
return val === void 0; return Ox.typeOf(val) == 'undefined';
}; };
/*@ /*@
Ox.typeOf <f> Returns the type of a value Ox.typeOf <f> Returns the type of a value
(value) -> <s> type (value) -> <s> Type
value <*> Any value value <*> Any value
# Examples
> (function() { return Ox.typeOf(arguments); }()) > (function() { return Ox.typeOf(arguments); }())
"arguments" 'arguments'
> Ox.typeOf([]) > Ox.typeOf([])
"array" 'array'
> Ox.typeOf(false) > Ox.typeOf(false)
"boolean" 'boolean'
> Ox.typeOf(new Date()) > Ox.typeOf(new Date())
"date" 'date'
> Ox.typeOf(document.createElement())
"element"
> Ox.typeOf(function() {}) > Ox.typeOf(function() {})
"function" 'function'
> Ox.typeOf(Infinity) > Ox.typeOf(document.createElement('a'))
"infinity" "htmlanchorelement"
> Ox.typeOf(NaN) > Ox.typeOf(document.getElementsByTagName('a'))
"nan" 'nodelist'
> Ox.typeOf(null) > Ox.typeOf(null)
"null" 'null'
> Ox.typeOf(0) > Ox.typeOf(0)
"number" 'number'
> Ox.typeOf(Infinity)
'number'
> Ox.typeOf(NaN)
'number'
> Ox.typeOf({}) > Ox.typeOf({})
"object" 'object'
> Ox.typeOf(/ /) > Ox.typeOf(/ /)
"regexp" 'regexp'
> Ox.typeOf('') > Ox.typeOf('')
"string" 'string'
> Ox.typeOf() > Ox.typeOf()
"undefined" 'undefined'
@*/ @*/
Ox.typeOf = function(val) { Ox.typeOf = function(val) {
var ret; return Object.prototype.toString.call(val).slice(8, -1).toLowerCase();
Ox.forEach(Ox.TYPES, function(type) { };
if (Ox['is' + type](val)) {
ret = type.toLowerCase();
return false;
}
});
return ret;
};