|
1 | 1 |
|
2 | | -const basicTypes = [ |
3 | | - "undefined", |
4 | | - "null", |
5 | | - "number", |
6 | | - "bigint", |
7 | | - "boolean", |
8 | | - "string", |
9 | | - "symbol", |
10 | | - "array", |
11 | | - "date", |
12 | | - "regexp", |
13 | | - "function", |
14 | | -]; |
15 | | - |
16 | 2 | class Type extends String { |
17 | 3 | /** |
18 | 4 | * @constructor |
19 | 5 | * @param {string} typeName - The type's name, regardless of whether it's an object or a primitive. |
20 | | - * @param {boolean} [isPrimitive] - Is this a primitive value? |
| 6 | + * @param {string} [objectType] - If this is an object, the object's name. Falsy for a primitive. |
21 | 7 | */ |
22 | | - constructor(typeName, isPrimitive){ |
23 | | - typeName = String(typename); |
| 8 | + constructor(typeName, objectType){ |
| 9 | + if(!(typeof typeName === "string" || typeName instanceof String)) |
| 10 | + throw new TypeError("'typeName' must be a string"); |
| 11 | + typeName = String(typeName); |
| 12 | + if(objectType){ |
| 13 | + if(!(typeof objectType === "string" || objectType instanceof String)) |
| 14 | + throw new TypeError("'objectType' must be a string"); |
| 15 | + objectType = String(objectType); |
| 16 | + } |
| 17 | + |
24 | 18 | super(typeName); |
25 | | - this.type = this[ isPrimitive ? "primitive" : "object" ] = typeName; |
| 19 | + |
| 20 | + if(objectType){ |
| 21 | + this.type = this.object = typeName; |
| 22 | + this.objectType = objectType; |
| 23 | + } |
| 24 | + else{ |
| 25 | + this.type = this.primitive = typeName; |
| 26 | + } |
26 | 27 | } |
27 | 28 | } |
28 | 29 |
|
| 30 | +function getToStringTag(obj){ |
| 31 | + return Object.prototype.toString.call(obj).slice(8, -1); |
| 32 | +} |
| 33 | + |
29 | 34 | function is(value){ |
30 | 35 | if(value === void 0) |
31 | | - return new Type("undefined", true); |
| 36 | + return new Type("undefined"); |
32 | 37 | if(value === null) |
33 | | - return new Type("null", true); |
| 38 | + return new Type("null"); |
34 | 39 | const type = typeof value; |
35 | 40 | if(type === "function") |
36 | | - return new Type("function", false); |
| 41 | + return new Type("function", "Function"); |
37 | 42 | if(type === "object"){ |
38 | | - const deepType = Object.prototype.toString.call(o).slice(8, -1).toLowerCase(); |
39 | | - if(deepType.match(new RegExp(`^${basicTypes.join("|")}$`, "ui"))) |
40 | | - return new Type(deepType, false); |
41 | | - return new Type(type, false); |
| 43 | + const toStringTag = getToStringTag(value); |
| 44 | + for(const objectType of ["Boolean","Number","String"]){ |
| 45 | + if(value instanceof globalThis[objectType]) |
| 46 | + return new Type(objectType.toLowerCase(), toStringTag); |
| 47 | + } |
| 48 | + return new Type(type, toStringTag); |
42 | 49 | } |
43 | | - return new Type(type, true); |
| 50 | + return new Type(type); |
44 | 51 | } |
45 | 52 |
|
46 | | -function fn(type){ |
47 | | - return (v)=>(is(v).type === type); |
48 | | -} |
49 | 53 |
|
50 | | -is.primitive = (v)=>(is(v).primitive !== void 0); |
51 | | -is.object = (v)=>(is(v).object !== void 0); |
| 54 | +is.object = (v)=>(v instanceof Object); |
| 55 | +is.primitive = (v)=>!is.object(v); |
52 | 56 |
|
53 | | -for(const type of basicTypes){ |
54 | | - is[type] = fn(type); |
| 57 | +const typeofTypes = [ |
| 58 | + /*** primitives ***/ |
| 59 | + "undefined", |
| 60 | + "null", |
| 61 | + "bigint", |
| 62 | + "symbol", |
| 63 | + |
| 64 | + /*** primitives & objects ***/ |
| 65 | + "boolean", |
| 66 | + "number", |
| 67 | + "string", |
| 68 | + |
| 69 | + /*** objects ***/ |
| 70 | + //"object", |
| 71 | + "function", |
| 72 | +]; |
| 73 | +for(const type of typeofTypes){ |
| 74 | + is[type] = (v)=>(is(v).type === type); |
55 | 75 | } |
56 | 76 |
|
57 | 77 | is.number.real = (v)=>(is.number(v) && Number.isFinite(1*v)); |
58 | 78 | is.number.infinite = (v)=>(is.number(v) && !Number.isFinite(1*v) && !Number.isNaN(1*v)); |
59 | 79 | is.number.NaN = (v)=>(is.number(v) && Number.isNaN(1*v)); //Note that JavaScript doesn't correctly treat all undefined forms as NaN (e.g., 1/0 and 0**0). |
60 | 80 |
|
| 81 | +const otherCommonTypes = [ |
| 82 | + "Error", |
| 83 | + "Date", |
| 84 | + "RegExp", |
| 85 | + "Array", |
| 86 | + "Map", |
| 87 | + "Set", |
| 88 | + "WeakMap", |
| 89 | + "WeakSet", |
| 90 | + "Promise", |
| 91 | +]; |
| 92 | +for(const objectType of otherCommonTypes){ |
| 93 | + is[objectType.toLowerCase()] = (v)=>(v instanceof globalThis[objectType]); |
| 94 | +} |
| 95 | + |
61 | 96 |
|
62 | 97 |
|
63 | 98 | export { is as default }; |
0 commit comments