@ix-xs/node-comfort 1.0.57 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/Checker.js ADDED
@@ -0,0 +1,188 @@
1
+ const { types } = require("node:util");
2
+
3
+ /**
4
+ * Tiny runtime type-checking helper.
5
+ *
6
+ * All methods are simple boolean guards that work on any input value.
7
+ */
8
+ module.exports = {
9
+ /**
10
+ * Checks if a value is a real Array.
11
+ *
12
+ * @example
13
+ * nodeComfort.Checker.isArray([1, 2, 3]); // true
14
+ * nodeComfort.Checker.isArray("not array"); // false
15
+ *
16
+ * @param {*} value - Value to check.
17
+ * @returns {boolean} True if the value is an Array.
18
+ */
19
+ isArray(value) {
20
+ return Array.isArray(value);
21
+ },
22
+
23
+ /**
24
+ * Checks if a value is a number (excluding NaN).
25
+ *
26
+ * @example
27
+ * nodeComfort.Checker.isNumber(42); // true
28
+ * nodeComfort.Checker.isNumber(NaN); // false
29
+ * nodeComfort.Checker.isNumber("42"); // false
30
+ *
31
+ * @param {*} value - Value to check.
32
+ * @returns {boolean} True if the value is a finite number or Infinity/-Infinity.
33
+ */
34
+ isNumber(value) {
35
+ return typeof value === "number" && !Number.isNaN(value);
36
+ },
37
+
38
+ /**
39
+ * Checks if a value is a function.
40
+ *
41
+ * @example
42
+ * nodeComfort.Checker.isFunction(() => {}); // true
43
+ * nodeComfort.Checker.isFunction(class {}); // true
44
+ * nodeComfort.Checker.isFunction("fn"); // false
45
+ *
46
+ * @param {*} value - Value to check.
47
+ * @returns {boolean} True if the value is a function.
48
+ */
49
+ isFunction(value) {
50
+ return typeof value === "function";
51
+ },
52
+
53
+ /**
54
+ * Checks if a value is a non-null object.
55
+ *
56
+ * Note: Arrays, Maps, Sets, Dates, etc. are also considered objects here.
57
+ *
58
+ * @example
59
+ * nodeComfort.Checker.isObject({}); // true
60
+ * nodeComfort.Checker.isObject([]); // true
61
+ * nodeComfort.Checker.isObject(null); // false
62
+ * nodeComfort.Checker.isObject("str"); // false
63
+ *
64
+ * @param {*} value - Value to check.
65
+ * @returns {boolean} True if the value is a non-null object.
66
+ */
67
+ isObject(value) {
68
+ return typeof value === "object" && value !== null;
69
+ },
70
+
71
+ /**
72
+ * Checks if a value is a boolean.
73
+ *
74
+ * @example
75
+ * nodeComfort.Checker.isBoolean(true); // true
76
+ * nodeComfort.Checker.isBoolean(false); // true
77
+ * nodeComfort.Checker.isBoolean(0); // false
78
+ *
79
+ * @param {*} value - Value to check.
80
+ * @returns {boolean} True if the value is a boolean.
81
+ */
82
+ isBoolean(value) {
83
+ return typeof value === "boolean";
84
+ },
85
+
86
+ /**
87
+ * Checks if a value is a string.
88
+ *
89
+ * @example
90
+ * nodeComfort.Checker.isString("hello"); // true
91
+ * nodeComfort.Checker.isString(123); // false
92
+ *
93
+ * @param {*} value - Value to check.
94
+ * @returns {boolean} True if the value is a string.
95
+ */
96
+ isString(value) {
97
+ return typeof value === "string";
98
+ },
99
+
100
+ /**
101
+ * Checks if a value is exactly undefined.
102
+ *
103
+ * @example
104
+ * nodeComfort.Checker.isUndefined(undefined); // true
105
+ * nodeComfort.Checker.isUndefined(null); // false
106
+ *
107
+ * @param {*} value - Value to check.
108
+ * @returns {boolean} True if the value is undefined.
109
+ */
110
+ isUndefined(value) {
111
+ return typeof value === "undefined";
112
+ },
113
+
114
+ /**
115
+ * Checks if a value is exactly null.
116
+ *
117
+ * @example
118
+ * nodeComfort.Checker.isNull(null); // true
119
+ * nodeComfort.Checker.isNull(undefined); // false
120
+ *
121
+ * @param {*} value - Value to check.
122
+ * @returns {boolean} True if the value is null.
123
+ */
124
+ isNull(value) {
125
+ return value === null;
126
+ },
127
+
128
+ /**
129
+ * Checks if a value is a built-in Date instance.
130
+ *
131
+ * Uses `util.types.isDate` under the hood to handle cross-realm Dates.
132
+ *
133
+ * @example
134
+ * nodeComfort.Checker.isDate(new Date()); // true
135
+ * nodeComfort.Checker.isDate("2024-01-01"); // false
136
+ *
137
+ * @param {*} value - Value to check.
138
+ * @returns {boolean} True if the value is a Date instance.
139
+ */
140
+ isDate(value) {
141
+ return types.isDate(value);
142
+ },
143
+
144
+ /**
145
+ * Checks if a value is a built-in Map instance.
146
+ *
147
+ * @example
148
+ * nodeComfort.Checker.isMap(new Map()); // true
149
+ * nodeComfort.Checker.isMap({}); // false
150
+ *
151
+ * @param {*} value - Value to check.
152
+ * @returns {boolean} True if the value is a Map.
153
+ */
154
+ isMap(value) {
155
+ return types.isMap(value);
156
+ },
157
+
158
+ /**
159
+ * Checks if a value is a built-in Set instance.
160
+ *
161
+ * @example
162
+ * nodeComfort.Checker.isSet(new Set()); // true
163
+ * nodeComfort.Checker.isSet([]); // false
164
+ *
165
+ * @param {*} value - Value to check.
166
+ * @returns {boolean} True if the value is a Set.
167
+ */
168
+ isSet(value) {
169
+ return types.isSet(value);
170
+ },
171
+
172
+ /**
173
+ * Checks if a value is a native Error instance.
174
+ *
175
+ * Uses `util.types.isNativeError` to correctly handle errors from other realms.
176
+ *
177
+ * @example
178
+ * nodeComfort.Checker.isError(new Error("x")); // true
179
+ * nodeComfort.Checker.isError(new TypeError("x")); // true
180
+ * nodeComfort.Checker.isError({ message: "x" }); // false
181
+ *
182
+ * @param {*} value - Value to check.
183
+ * @returns {boolean} True if the value is a native Error.
184
+ */
185
+ isError(value) {
186
+ return types.isNativeError(value);
187
+ },
188
+ };