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';
/*@
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"
(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
@*/
Ox.isArguments = function(val) {
return !!(val && val.toString() == '[object Arguments]');
}
if (!(function() {
return Ox.isArguments(arguments);
}())) {
Ox.isArguments = function(val) {
return !!(val && Object.hasOwnProperty.call(val, 'callee'));
};
}
return Ox.typeOf(val) == 'arguments';
};
/*@
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
@*/
Ox.isArray = function(val) {
return val instanceof Array;
}
return Ox.typeOf(val) == 'array';
};
/*@
Ox.isBoolean <f> Tests if a value is boolean
@ -41,7 +46,7 @@ Ox.isBoolean <f> Tests if a value is boolean
true
@*/
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
@*/
Ox.isDate = function(val) {
return val instanceof Date;
return Ox.typeOf(val) == 'date';
};
/*@
Ox.isElement <f> Tests if a value is a DOM element
(value) -> <b> True if the value is a DOM element
value <*> Any value
> Ox.isElement(document.createElement())
> Ox.isElement(document.createElement('a'))
true
@*/
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
@*/
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
(value) -> <b> True if the value is infinite
Ox.isInfinity <f> Tests if a value is positive or negative Infinity
(value) -> <b> True if the value is positive or negative Infinity
value <*> Any value
> Ox.isInfinity(Infinity)
true
@ -167,17 +172,29 @@ Ox.isInfinity <f> Tests if a value is infinite
false
@*/
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"
(value) -> <b> True if the value is "Not a Number"
Ox.isInt <f> Tests if a value is an integer
(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
> Ox.isNaN(NaN)
true
@*/
Ox.isNaN = function(val) {
return val !== val;
}
@ -190,7 +207,7 @@ Ox.isNull <f> Tests if a value is <code>null</code>
true
@*/
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)
false
@*/
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(/ /)
false
@*/
Ox.isObject = function(val) {
return typeof val == 'object' && !Ox.isArguments(val)
&& !Ox.isArray(val) && !Ox.isDate(val)
&& !Ox.isNull(val) && !Ox.isRegExp(val);
return Ox.typeOf(val) == 'object';
};
/*@
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(/ /)
true
@*/
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('')
true
@*/
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()
true
@*/
Ox.isUndefined = function(val) {
return val === void 0;
return Ox.typeOf(val) == 'undefined';
};
/*@
Ox.typeOf <f> Returns the type of a value
(value) -> <s> type
(value) -> <s> Type
value <*> Any value
# Examples
> (function() { return Ox.typeOf(arguments); }())
"arguments"
'arguments'
> Ox.typeOf([])
"array"
'array'
> Ox.typeOf(false)
"boolean"
'boolean'
> Ox.typeOf(new Date())
"date"
> Ox.typeOf(document.createElement())
"element"
'date'
> Ox.typeOf(function() {})
"function"
> Ox.typeOf(Infinity)
"infinity"
> Ox.typeOf(NaN)
"nan"
'function'
> Ox.typeOf(document.createElement('a'))
"htmlanchorelement"
> Ox.typeOf(document.getElementsByTagName('a'))
'nodelist'
> Ox.typeOf(null)
"null"
'null'
> Ox.typeOf(0)
"number"
'number'
> Ox.typeOf(Infinity)
'number'
> Ox.typeOf(NaN)
'number'
> Ox.typeOf({})
"object"
'object'
> Ox.typeOf(/ /)
"regexp"
'regexp'
> Ox.typeOf('')
"string"
'string'
> Ox.typeOf()
"undefined"
'undefined'
@*/
Ox.typeOf = function(val) {
var ret;
Ox.forEach(Ox.TYPES, function(type) {
if (Ox['is' + type](val)) {
ret = type.toLowerCase();
return false;
}
});
return ret;
return Object.prototype.toString.call(val).slice(8, -1).toLowerCase();
};