|
| 1 | +/** |
| 2 | + * A more robust 'typeof'. |
| 3 | + * https://gist.github.com/wizard04wsu/8055356 |
| 4 | + * |
| 5 | + * |
| 6 | + * For each of the type testing methods, the only parameter is the item to be tested. These methods do not perform coercion. |
| 7 | + * |
| 8 | + * is(o) - Returns the item's type. |
| 9 | + * - NaN is considered its own type (instead of a number), since it essentially represents something that cannot be converted to a number. |
| 10 | + * - For objects, the type of the object is given instead of just 'object' (e.g., 'Array'). |
| 11 | + * |
| 12 | + * is.defined(o) |
| 13 | + * is.undefined(o) |
| 14 | + * is.null(o) |
| 15 | + * is.nan(o) - NaN or a Number object with value NaN |
| 16 | + * |
| 17 | + * is.array(o) |
| 18 | + * is.bigint(o) |
| 19 | + * is.boolean(o) |
| 20 | + * is.date(o) |
| 21 | + * is.function(o) |
| 22 | + * is.number(o) - excludes NaN |
| 23 | + * is.number.infinite(o) |
| 24 | + * is.number.nan(o) - same as is.nan(o) |
| 25 | + * is.number.real(o) |
| 26 | + * is.regexp(o) |
| 27 | + * is.string(o) |
| 28 | + * is.symbol(o) |
| 29 | + * |
| 30 | + * is.object(o) |
| 31 | + * is.primitive(o) |
| 32 | + * |
| 33 | + * |
| 34 | + * is.noConflict() - Restores 'is' to what it was before this script replaced it. |
| 35 | + */ |
| 36 | + |
| 37 | +(function (){ |
| 38 | + |
| 39 | + "use strict"; |
| 40 | + |
| 41 | + function is(o){ |
| 42 | + if(o === void 0) return "undefined"; |
| 43 | + let t = typeof o; |
| 44 | + if(t === "object"){ |
| 45 | + if(o === null) return "null"; |
| 46 | + let name = Object.prototype.toString.call(o).slice(8, -1); |
| 47 | + if(name === "Number" && 1*o !== 1*o) return "NaN"; |
| 48 | + return name; //object type |
| 49 | + } |
| 50 | + if(o !== o) return "NaN"; |
| 51 | + return t; //primitive type or "function" |
| 52 | + } |
| 53 | + |
| 54 | + function fn(type, eitherCase){ |
| 55 | + if(eitherCase) return function (o){ return is(o).toLowerCase() === type; }; |
| 56 | + return function (o){ return is(o) === type; }; |
| 57 | + } |
| 58 | + |
| 59 | + is.undefined = function (o){ return o === void 0; }; |
| 60 | + is.defined = function (o){ return o !== void 0; }; |
| 61 | + is.null = function (o){ return o === null; }; |
| 62 | + |
| 63 | + is.nan = fn("NaN"); //NaN or a Number object with value NaN |
| 64 | + //Note that JavaScript doesn't correctly treat all undefined forms as NaN (e.g., 1/0 and 0**0). |
| 65 | + |
| 66 | + is.array = fn("Array"); |
| 67 | + is.bigint = fn("bigint"); |
| 68 | + is.boolean = fn("boolean", true); |
| 69 | + is.date = fn("Date"); |
| 70 | + is.function = fn("function"); |
| 71 | + is.number = fn("number", true); //excludes NaN |
| 72 | + is.number.nan = is.nan; |
| 73 | + is.regexp = fn("RegExp"); |
| 74 | + is.string = fn("string", true); |
| 75 | + is.symbol = fn("symbol"); |
| 76 | + |
| 77 | + let infinity = 1/0; //Technically, 1/0 is an undefined form, but JavaScript treats it as Infinity. |
| 78 | + is.number.infinite = function (o){ return is.number(o) && Math.abs(o) === infinity; }; |
| 79 | + is.number.real = function (o){ return is.number(o) && Math.abs(o) !== infinity; }; |
| 80 | + |
| 81 | + is.object = function (o){ let t = typeof o; return (t === "object" && o !== null) || t === "function"; }; |
| 82 | + is.primitive = function (o){ return !is.object(o); }; |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | + let context = this, |
| 87 | + oldIs = context.is; |
| 88 | + Object.defineProperty(is, "noConflict", { value:function (){ context.is = oldIs; context = oldIs = null; delete is.noConflict; return is; }, writable:true, enumerable:false, configurable:true }); |
| 89 | + |
| 90 | + |
| 91 | + this.is = is; |
| 92 | + |
| 93 | +}).call(window); |
| 94 | + |
| 95 | + |
| 96 | + |
| 97 | +(function test(){ |
| 98 | + |
| 99 | + let i = 1, |
| 100 | + a = function (a,b){ console.assert(a === b, (i++)+". "+a+" !== "+b); }; |
| 101 | + |
| 102 | + a(is(), "undefined"); |
| 103 | + a(is(null), "null"); |
| 104 | + a(is({}), "Object"); |
| 105 | + a(is([]), "Array"); |
| 106 | + a(is(arguments), "Arguments"); |
| 107 | + if(window.BigInt) a(is(BigInt(5)), "bigint"); |
| 108 | + else i++; |
| 109 | + a(is(false), "boolean"); |
| 110 | + a(is(new Boolean()), "Boolean"); |
| 111 | + a(is(new Date()), "Date"); |
| 112 | + a(is(function(){}), "function"); |
| 113 | + a(is(new Function()), "function"); |
| 114 | + a(is(/a/), "RegExp"); |
| 115 | + a(is(new RegExp("a")), "RegExp"); |
| 116 | + a(is(""), "string"); |
| 117 | + a(is(new String("")), "String"); |
| 118 | + if(window.Symbol) a(is(Symbol()), "symbol"); |
| 119 | + else i++; |
| 120 | + a(is(5), "number"); |
| 121 | + a(is(new Number(5)), "Number"); |
| 122 | + a(is(NaN), "NaN"); |
| 123 | + a(is(new Number(NaN)), "NaN"); |
| 124 | + a(is(1/0), "number"); |
| 125 | + a(is(new Number(1/0)), "Number"); |
| 126 | + |
| 127 | + a(is.defined(), false); |
| 128 | + a(is.defined(5), true); |
| 129 | + a(is.undefined(), true); |
| 130 | + a(is.undefined(5), false); |
| 131 | + a(is.null(null), true); |
| 132 | + a(is.null(5), false); |
| 133 | + a(is.array([]), true); |
| 134 | + a(is.array(5), false); |
| 135 | + a(is.array(arguments), false); |
| 136 | + if(window.BigInt) a(is.bigint(BigInt(5)), true); |
| 137 | + else i++; |
| 138 | + a(is.bigint(5), false); |
| 139 | + a(is.boolean(false), true); |
| 140 | + a(is.boolean(), false); |
| 141 | + a(is.boolean(5), false); |
| 142 | + a(is.date(new Date()), true); |
| 143 | + a(is.date(5), false); |
| 144 | + a(is.date(1*(new Date())), false); |
| 145 | + a(is.function(function(){}), true); |
| 146 | + a(is.function(new Function()), true); |
| 147 | + a(is.function(5), false); |
| 148 | + a(is.regexp(/a/), true); |
| 149 | + a(is.regexp(new RegExp("a")), true); |
| 150 | + a(is.regexp(5), false); |
| 151 | + a(is.string(""), true); |
| 152 | + a(is.string(new String()), true); |
| 153 | + a(is.string(5), false); |
| 154 | + if(window.Symbol) a(is.symbol(Symbol()), true); |
| 155 | + else i++; |
| 156 | + a(is.symbol(5), false); |
| 157 | + a(is.nan(NaN), true); |
| 158 | + a(is.nan(new Number(NaN)), true); |
| 159 | + a(is.nan(5), false); |
| 160 | + a(is.nan(new Number(5)), false); |
| 161 | + a(is.number(5), true); |
| 162 | + a(is.number(new Number()), true); |
| 163 | + a(is.number(NaN), false); |
| 164 | + a(is.number(new Number(NaN)), false); |
| 165 | + a(is.number(1/0), true); |
| 166 | + a(is.number(new Number(1/0)), true); |
| 167 | + a(is.number(""), false); |
| 168 | + a(is.number(new Date()), false); |
| 169 | + a(is.number(1*(new Date())), true); |
| 170 | + a(is.number.real(5), true); |
| 171 | + a(is.number.real(1/0), false); |
| 172 | + a(is.number.real(NaN), false); |
| 173 | + a(is.number.real("5"), false); |
| 174 | + a(is.number.infinite(1/0), true); |
| 175 | + a(is.number.infinite(5), false); |
| 176 | + a(is.number.infinite(NaN), false); |
| 177 | + a(is.number.infinite("Infinity"), false); |
| 178 | + |
| 179 | + a(is.object({}), true); |
| 180 | + a(is.object(5), false); |
| 181 | + a(is.object(new Number()), true); |
| 182 | + a(is.object(function(){}), true); |
| 183 | + a(is.primitive(), true); |
| 184 | + a(is.primitive(5), true); |
| 185 | + a(is.primitive(new Number()), false); |
| 186 | + a(is.primitive(NaN), true); |
| 187 | + a(is.primitive(new Number(NaN)), false); |
| 188 | + |
| 189 | +})(); |
0 commit comments