-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtypeChecker.js
More file actions
90 lines (82 loc) · 2.4 KB
/
typeChecker.js
File metadata and controls
90 lines (82 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/**
* PRIMITIVE TYPES
* IF undefined or a number needs to be incremented use let,
* ALWAYS use const to avoid reassignment based mutations,
*/
const vString = "some text";
const vNumber = 10;
const vBoolean = false;
const vSymbol = Symbol('foo');
const vBigInt = BigInt(9007199254740991);
const vNull = null;
let vUndefined;
/**
* NON-PRIMITIVE "OBJECTS"
* Anything not mentioned above.
* Assume it will mutate unless you lock it down.
*/
const vObjectObj = {
property: "Value"
}
const vArrayObject = [
1,
2.1,
"string",
]
function functionObject(params) {}
async function functionAsync(params) {}
// Syntactical "salt" for a function,
// Does NOT use object-oriented inheritance, still prototype-based inheritance
class ClassObj {}
const logType = (VAR) => {
const LOG = console.log;
// 1. check if the variable is a primitive type and not an object ?
( typeof VAR==="string"||typeof VAR==="number"||typeof VAR==="boolean"||typeof VAR==="symbol"||typeof VAR==="bigint"
&& typeof VAR !=="object" && VAR !==null && VAR !==undefined
) ?
LOG("\n PRIMITIVE: ", typeof VAR, "\n Value: ",VAR)
:
// 2. ELSE, Check what kind of object it is
// A. check if variable is an array ?
( Array.isArray(VAR) ) ?
// if it is an array, log it with table
LOG(`type: Array Object,
data:
`)& console.table(VAR)
:
// B. check if variable is object object and the state of the object
(VAR !==null && typeof VAR === "object") ?
LOG(`type: ${VAR}
frozen: ${Object.isFrozen(VAR)}
sealed: ${Object.isSealed(VAR)}
data:
${JSON.stringify(VAR)}
`)
:
// C. check if variable is function
(typeof VAR === "function") ?
LOG(`type: ${VAR},
frozen: ${Object.isFrozen(VAR)},
sealed: ${Object.isSealed(VAR)}
`)
:
// D. Null BUG Check
// due to the typeof null bug we, we check the value instead of using the operator
(VAR===null) ?
LOG("\n must be null: ", "'",typeof VAR,"'")
:
// E. Log as undefined
LOG("\n must be undefined... \n");
}
// Log Primitives
logType( vString );
logType( vNumber );
logType( vBoolean );
logType( vSymbol );
logType( vBigInt );
logType( vNull );
logType( vUndefined, true);
// Log Objects
logType( vObjectObj );
logType( vArrayObject );
logType( functionObject );