From 49fc8dc8ed8b7429cebce417b9831d97a8631a16 Mon Sep 17 00:00:00 2001 From: rolux Date: Sat, 19 May 2012 12:15:04 +0400 Subject: [PATCH] base Ox.typeOf on Object.prototype.toString, add Ox.checkType, Ox.isInt and Ox.isPrimitive --- source/Ox/js/Type.js | 151 +++++++++++++++++++++++++------------------ 1 file changed, 87 insertions(+), 64 deletions(-) diff --git a/source/Ox/js/Type.js b/source/Ox/js/Type.js index fb710a71..2bec9120 100644 --- a/source/Ox/js/Type.js +++ b/source/Ox/js/Type.js @@ -1,5 +1,17 @@ 'use strict'; +/*@ +Ox.checkType Throws a TypeError if a value is not of a given type + (val, type) -> undefined + val <*> Any value + type Type +@*/ +Ox.checkType = function(val, type) { + if (!Ox.in(Ox.makeArray(type), Ox.typeOf(val))) { + throw new TypeError(); + } +}; + /*@ Ox.isArguments Tests if a value is an arguments "array" (value) -> True if the value is an arguments "array" @@ -8,15 +20,8 @@ Ox.isArguments 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 Tests if a value is an array @@ -30,8 +35,8 @@ Ox.isArray Tests if a value is an array false @*/ Ox.isArray = function(val) { - return val instanceof Array; -} + return Ox.typeOf(val) == 'array'; +}; /*@ Ox.isBoolean Tests if a value is boolean @@ -41,7 +46,7 @@ Ox.isBoolean 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 Tests if a value is a date true @*/ Ox.isDate = function(val) { - return val instanceof Date; + return Ox.typeOf(val) == 'date'; }; /*@ Ox.isElement Tests if a value is a DOM element (value) -> 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 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 Tests if a value is infinite - (value) -> True if the value is infinite +Ox.isInfinity Tests if a value is positive or negative Infinity + (value) -> True if the value is positive or negative Infinity value <*> Any value > Ox.isInfinity(Infinity) true @@ -167,17 +172,29 @@ Ox.isInfinity 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 Tests if a value is "Not a Number" - (value) -> True if the value is "Not a Number" +Ox.isInt Tests if a value is an integer + (value) -> 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 Tests if a value is NaN + (value) -> 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 Tests if a value is null true @*/ Ox.isNull = function(val) { - return val === null; + return Ox.typeOf(val) == 'null'; }; /*@ @@ -206,9 +223,8 @@ Ox.isNumber 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 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 Tests if a value is a primitive (boolean, number or string) + (value) -> 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 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 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 Tests if a value is undefined > Ox.isUndefined() true @*/ - Ox.isUndefined = function(val) { - return val === void 0; + return Ox.typeOf(val) == 'undefined'; }; /*@ Ox.typeOf Returns the type of a value - (value) -> type + (value) -> 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; -}; \ No newline at end of file + return Object.prototype.toString.call(val).slice(8, -1).toLowerCase(); +};