jsfunx 1.0.1 → 1.0.2
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/jsfunx.cjs +476 -31
- package/jsfunx.mjs +443 -22
- package/package.json +1 -1
package/jsfunx.cjs
CHANGED
|
@@ -32,7 +32,11 @@ const and = "and";
|
|
|
32
32
|
|
|
33
33
|
/** @type {string} */
|
|
34
34
|
|
|
35
|
-
const
|
|
35
|
+
const bi = "bigint";
|
|
36
|
+
|
|
37
|
+
/** @type {BigIntConstructor} */
|
|
38
|
+
|
|
39
|
+
const BI = BigInt;
|
|
36
40
|
|
|
37
41
|
/** @type {string} */
|
|
38
42
|
|
|
@@ -40,55 +44,95 @@ const bigint = "bigint";
|
|
|
40
44
|
|
|
41
45
|
/** @type {string} */
|
|
42
46
|
|
|
43
|
-
const
|
|
47
|
+
const bool = "boolean";
|
|
48
|
+
|
|
49
|
+
/** @type {BooleanConstructor} */
|
|
50
|
+
|
|
51
|
+
const Bool = Boolean;
|
|
44
52
|
|
|
45
53
|
/** @type {string} */
|
|
46
54
|
|
|
47
|
-
const
|
|
55
|
+
const boolean = "boolean";
|
|
48
56
|
|
|
49
57
|
/** @type {string} */
|
|
50
58
|
|
|
51
59
|
const fn = "function";
|
|
52
60
|
|
|
61
|
+
/** @type {FunctionConstructor} */
|
|
62
|
+
|
|
63
|
+
const Fn = Function;
|
|
64
|
+
|
|
53
65
|
/** @type {string} */
|
|
54
66
|
|
|
55
|
-
const
|
|
67
|
+
const fun = "function";
|
|
68
|
+
|
|
69
|
+
/** @type {FunctionConstructor} */
|
|
70
|
+
|
|
71
|
+
const Fun = Function;
|
|
72
|
+
|
|
73
|
+
/** @type {string} */
|
|
74
|
+
|
|
75
|
+
const func = "function";
|
|
76
|
+
|
|
77
|
+
/** @type {FunctionConstructor} */
|
|
78
|
+
|
|
79
|
+
const Func = Function;
|
|
80
|
+
|
|
81
|
+
/** @type {null} */
|
|
82
|
+
|
|
83
|
+
const nil = null;
|
|
56
84
|
|
|
57
85
|
/** @type {string} */
|
|
58
86
|
|
|
59
87
|
const num = "number";
|
|
60
88
|
|
|
89
|
+
/** @type {NumberConstructor} */
|
|
90
|
+
|
|
91
|
+
const Num = Number;
|
|
92
|
+
|
|
61
93
|
/** @type {string} */
|
|
62
94
|
|
|
63
|
-
const
|
|
95
|
+
const number = "number";
|
|
64
96
|
|
|
65
97
|
/** @type {string} */
|
|
66
98
|
|
|
67
99
|
const obj = "object";
|
|
68
100
|
|
|
101
|
+
/** @type {ObjectConstructor} */
|
|
102
|
+
|
|
103
|
+
const Obj = Object;
|
|
104
|
+
|
|
69
105
|
/** @type {string} */
|
|
70
106
|
|
|
71
|
-
const
|
|
107
|
+
const object = "object";
|
|
72
108
|
|
|
73
109
|
/** @type {string} */
|
|
74
110
|
|
|
75
|
-
const
|
|
111
|
+
const or = "or";
|
|
76
112
|
|
|
77
113
|
/** @type {string} */
|
|
78
114
|
|
|
79
115
|
const str = "string";
|
|
80
116
|
|
|
117
|
+
/** @type {StringConstructor} */
|
|
118
|
+
|
|
119
|
+
const Str = String;
|
|
120
|
+
|
|
81
121
|
/** @type {string} */
|
|
82
122
|
|
|
83
|
-
const
|
|
123
|
+
const string = "string";
|
|
84
124
|
|
|
85
125
|
/** @type {string} */
|
|
86
126
|
|
|
87
|
-
const
|
|
127
|
+
const sym = "symbol";
|
|
128
|
+
|
|
129
|
+
/** @type {SymbolConstructor} */
|
|
130
|
+
|
|
131
|
+
const Sym = Symbol;
|
|
88
132
|
|
|
89
133
|
/** @type {string} */
|
|
90
134
|
|
|
91
|
-
const
|
|
135
|
+
const symbol = "symbol";
|
|
92
136
|
|
|
93
137
|
/** @type {string} */
|
|
94
138
|
|
|
@@ -110,19 +154,21 @@ const undef = "undefined";
|
|
|
110
154
|
*/
|
|
111
155
|
|
|
112
156
|
function checkType(data, dataType, logic, match, strict) {
|
|
113
|
-
/** @type {Set<string>} */
|
|
157
|
+
/** @type {Set<string | null | undefined>} */
|
|
114
158
|
|
|
115
159
|
let setOfTypes = new Set([
|
|
116
160
|
and,
|
|
161
|
+
bi,
|
|
162
|
+
bool,
|
|
163
|
+
fn,
|
|
164
|
+
null,
|
|
165
|
+
num,
|
|
166
|
+
obj,
|
|
117
167
|
or,
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
number,
|
|
121
|
-
object,
|
|
122
|
-
string,
|
|
123
|
-
symbol,
|
|
124
|
-
boolean,
|
|
168
|
+
str,
|
|
169
|
+
sym,
|
|
125
170
|
undef,
|
|
171
|
+
undefined,
|
|
126
172
|
]);
|
|
127
173
|
|
|
128
174
|
if (!(dataType instanceof Array)) {
|
|
@@ -131,6 +177,14 @@ function checkType(data, dataType, logic, match, strict) {
|
|
|
131
177
|
}
|
|
132
178
|
|
|
133
179
|
if (!(data instanceof Array)) {
|
|
180
|
+
if (data === null) {
|
|
181
|
+
return dataType === null;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
if (dataType === undefined) {
|
|
185
|
+
return data === undefined;
|
|
186
|
+
}
|
|
187
|
+
|
|
134
188
|
return typeof data === dataType;
|
|
135
189
|
}
|
|
136
190
|
|
|
@@ -142,6 +196,20 @@ function checkType(data, dataType, logic, match, strict) {
|
|
|
142
196
|
for (let dt of data) {
|
|
143
197
|
// 1 step directly in all cases
|
|
144
198
|
|
|
199
|
+
if (dt === null) {
|
|
200
|
+
if (dataType !== null) {
|
|
201
|
+
return false;
|
|
202
|
+
}
|
|
203
|
+
continue;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
if (dataType === undefined) {
|
|
207
|
+
if (dt !== undefined) {
|
|
208
|
+
return false;
|
|
209
|
+
}
|
|
210
|
+
continue;
|
|
211
|
+
}
|
|
212
|
+
|
|
145
213
|
if (!(typeof dt === dataType)) {
|
|
146
214
|
return false;
|
|
147
215
|
}
|
|
@@ -161,6 +229,20 @@ function checkType(data, dataType, logic, match, strict) {
|
|
|
161
229
|
for (let dt of data) {
|
|
162
230
|
// 1 step directly in all cases
|
|
163
231
|
|
|
232
|
+
if (dt === null) {
|
|
233
|
+
if (dataType === null) {
|
|
234
|
+
return true;
|
|
235
|
+
}
|
|
236
|
+
continue;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
if (dataType === undefined) {
|
|
240
|
+
if (dt === undefined) {
|
|
241
|
+
return true;
|
|
242
|
+
}
|
|
243
|
+
continue;
|
|
244
|
+
}
|
|
245
|
+
|
|
164
246
|
if (typeof dt === dataType) {
|
|
165
247
|
return true;
|
|
166
248
|
}
|
|
@@ -187,6 +269,20 @@ function checkType(data, dataType, logic, match, strict) {
|
|
|
187
269
|
throw new TypeError(`type ${dtype} undefined !`);
|
|
188
270
|
}
|
|
189
271
|
|
|
272
|
+
if (data === null) {
|
|
273
|
+
if (dtype === null) {
|
|
274
|
+
return true;
|
|
275
|
+
}
|
|
276
|
+
continue;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
if (dtype === undefined) {
|
|
280
|
+
if (data === undefined) {
|
|
281
|
+
return true;
|
|
282
|
+
}
|
|
283
|
+
continue;
|
|
284
|
+
}
|
|
285
|
+
|
|
190
286
|
if (typeof data === dtype) {
|
|
191
287
|
return true;
|
|
192
288
|
}
|
|
@@ -214,6 +310,20 @@ function checkType(data, dataType, logic, match, strict) {
|
|
|
214
310
|
throw new TypeError(`type ${dtype} undefined !`);
|
|
215
311
|
}
|
|
216
312
|
|
|
313
|
+
if (dt === null) {
|
|
314
|
+
if (dtype === null) {
|
|
315
|
+
continue outerLoop;
|
|
316
|
+
}
|
|
317
|
+
continue;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
if (dtype === undefined) {
|
|
321
|
+
if (dt === undefined) {
|
|
322
|
+
continue outerLoop;
|
|
323
|
+
}
|
|
324
|
+
continue;
|
|
325
|
+
}
|
|
326
|
+
|
|
217
327
|
if (typeof dt === dtype) {
|
|
218
328
|
continue outerLoop;
|
|
219
329
|
}
|
|
@@ -255,6 +365,20 @@ function checkType(data, dataType, logic, match, strict) {
|
|
|
255
365
|
// // // throw new TypeError(`type ${dtype} undefined !`);
|
|
256
366
|
// // // }
|
|
257
367
|
|
|
368
|
+
// // // if (data[0] === null) {
|
|
369
|
+
// // // if (dtype === null) {
|
|
370
|
+
// // // return true;
|
|
371
|
+
// // // }
|
|
372
|
+
// // // continue;
|
|
373
|
+
// // // }
|
|
374
|
+
|
|
375
|
+
// // // if (dtype === undefined) {
|
|
376
|
+
// // // if (data[0] === undefined) {
|
|
377
|
+
// // // return true;
|
|
378
|
+
// // // }
|
|
379
|
+
// // // continue;
|
|
380
|
+
// // // }
|
|
381
|
+
|
|
258
382
|
// // // if (typeof data[0] === dtype) {
|
|
259
383
|
// // // return true;
|
|
260
384
|
// // // }
|
|
@@ -291,6 +415,20 @@ function checkType(data, dataType, logic, match, strict) {
|
|
|
291
415
|
// // // throw new TypeError("type undefined !");
|
|
292
416
|
// // // }
|
|
293
417
|
|
|
418
|
+
// // // if (dt === null) {
|
|
419
|
+
// // // if (dataType[0] !== null) {
|
|
420
|
+
// // // return false;
|
|
421
|
+
// // // }
|
|
422
|
+
// // // continue;
|
|
423
|
+
// // // }
|
|
424
|
+
|
|
425
|
+
// // // if (dataType[0] === undefined) {
|
|
426
|
+
// // // if (dt !== undefined) {
|
|
427
|
+
// // // return false;
|
|
428
|
+
// // // }
|
|
429
|
+
// // // continue;
|
|
430
|
+
// // // }
|
|
431
|
+
|
|
294
432
|
// // // if (!(typeof dt === dataType[0])) {
|
|
295
433
|
// // // return false;
|
|
296
434
|
// // // }
|
|
@@ -353,6 +491,20 @@ function checkType(data, dataType, logic, match, strict) {
|
|
|
353
491
|
throw new TypeError(`type ${dtype} undefined !`);
|
|
354
492
|
}
|
|
355
493
|
|
|
494
|
+
if (dt === null) {
|
|
495
|
+
if (dtype === null) {
|
|
496
|
+
return true;
|
|
497
|
+
}
|
|
498
|
+
continue;
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
if (dtype === undefined) {
|
|
502
|
+
if (dt === undefined) {
|
|
503
|
+
return true;
|
|
504
|
+
}
|
|
505
|
+
continue;
|
|
506
|
+
}
|
|
507
|
+
|
|
356
508
|
if (typeof dt === dtype) {
|
|
357
509
|
return true;
|
|
358
510
|
}
|
|
@@ -390,6 +542,20 @@ function checkType(data, dataType, logic, match, strict) {
|
|
|
390
542
|
// // // throw new TypeError(`type ${dtype} undefined !`);
|
|
391
543
|
// // // }
|
|
392
544
|
|
|
545
|
+
// // // if (data[0] === null) {
|
|
546
|
+
// // // if (dtype === null) {
|
|
547
|
+
// // // return true;
|
|
548
|
+
// // // }
|
|
549
|
+
// // // continue;
|
|
550
|
+
// // // }
|
|
551
|
+
|
|
552
|
+
// // // if (dtype === undefined) {
|
|
553
|
+
// // // if (data[0] === undefined) {
|
|
554
|
+
// // // return true;
|
|
555
|
+
// // // }
|
|
556
|
+
// // // continue;
|
|
557
|
+
// // // }
|
|
558
|
+
|
|
393
559
|
// // // if (typeof data[0] === dtype) {
|
|
394
560
|
// // // return true;
|
|
395
561
|
// // // }
|
|
@@ -428,6 +594,20 @@ function checkType(data, dataType, logic, match, strict) {
|
|
|
428
594
|
// // // throw new TypeError("type undefined !");
|
|
429
595
|
// // // }
|
|
430
596
|
|
|
597
|
+
// // // if (dt === null) {
|
|
598
|
+
// // // if (dataType[0] === null) {
|
|
599
|
+
// // // return true;
|
|
600
|
+
// // // }
|
|
601
|
+
// // // continue;
|
|
602
|
+
// // // }
|
|
603
|
+
|
|
604
|
+
// // // if (dataType[0] === undefined) {
|
|
605
|
+
// // // if (dt === undefined) {
|
|
606
|
+
// // // return true;
|
|
607
|
+
// // // }
|
|
608
|
+
// // // continue;
|
|
609
|
+
// // // }
|
|
610
|
+
|
|
431
611
|
// // // if (typeof dt === dataType[0]) {
|
|
432
612
|
// // // return true;
|
|
433
613
|
// // // }
|
|
@@ -547,6 +727,20 @@ function checkType(data, dataType, logic, match, strict) {
|
|
|
547
727
|
throw new TypeError(`type ${dataType[i]} undefined !`);
|
|
548
728
|
}
|
|
549
729
|
|
|
730
|
+
if (data[i] === null) {
|
|
731
|
+
if (dataType[i] !== null) {
|
|
732
|
+
return false;
|
|
733
|
+
}
|
|
734
|
+
continue;
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
if (dataType[i] === undefined) {
|
|
738
|
+
if (data[i] !== undefined) {
|
|
739
|
+
return false;
|
|
740
|
+
}
|
|
741
|
+
continue;
|
|
742
|
+
}
|
|
743
|
+
|
|
550
744
|
if (!(typeof data[i] === dataType[i])) {
|
|
551
745
|
return false;
|
|
552
746
|
}
|
|
@@ -572,6 +766,20 @@ function checkType(data, dataType, logic, match, strict) {
|
|
|
572
766
|
throw new TypeError(`type ${dataType[i]} undefined !`);
|
|
573
767
|
}
|
|
574
768
|
|
|
769
|
+
if (data[i] === null) {
|
|
770
|
+
if (dataType[i] === null) {
|
|
771
|
+
return true;
|
|
772
|
+
}
|
|
773
|
+
continue;
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
if (dataType[i] === undefined) {
|
|
777
|
+
if (data[i] === undefined) {
|
|
778
|
+
return true;
|
|
779
|
+
}
|
|
780
|
+
continue;
|
|
781
|
+
}
|
|
782
|
+
|
|
575
783
|
if (typeof data[i] === dataType[i]) {
|
|
576
784
|
return true;
|
|
577
785
|
}
|
|
@@ -600,6 +808,20 @@ function checkType(data, dataType, logic, match, strict) {
|
|
|
600
808
|
throw new TypeError(`type ${dataType[i]} undefined !`);
|
|
601
809
|
}
|
|
602
810
|
|
|
811
|
+
if (data[i] === null) {
|
|
812
|
+
if (dataType[i] !== null) {
|
|
813
|
+
return false;
|
|
814
|
+
}
|
|
815
|
+
continue;
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
if (dataType[i] === undefined) {
|
|
819
|
+
if (data[i] !== undefined) {
|
|
820
|
+
return false;
|
|
821
|
+
}
|
|
822
|
+
continue;
|
|
823
|
+
}
|
|
824
|
+
|
|
603
825
|
if (!(typeof data[i] === dataType[i])) {
|
|
604
826
|
return false;
|
|
605
827
|
}
|
|
@@ -625,6 +847,20 @@ function checkType(data, dataType, logic, match, strict) {
|
|
|
625
847
|
throw new TypeError(`type ${dataType[i]} undefined !`);
|
|
626
848
|
}
|
|
627
849
|
|
|
850
|
+
if (data[i] === null) {
|
|
851
|
+
if (dataType[i] === null) {
|
|
852
|
+
return true;
|
|
853
|
+
}
|
|
854
|
+
continue;
|
|
855
|
+
}
|
|
856
|
+
|
|
857
|
+
if (dataType[i] === undefined) {
|
|
858
|
+
if (data[i] === undefined) {
|
|
859
|
+
return true;
|
|
860
|
+
}
|
|
861
|
+
continue;
|
|
862
|
+
}
|
|
863
|
+
|
|
628
864
|
if (typeof data[i] === dataType[i]) {
|
|
629
865
|
return true;
|
|
630
866
|
}
|
|
@@ -662,7 +898,7 @@ function checkType(data, dataType, logic, match, strict) {
|
|
|
662
898
|
* @returns {boolean} `true` if the provided object(s) match the given type(s) according to the chosen logic, otherwise `false`.
|
|
663
899
|
*/
|
|
664
900
|
|
|
665
|
-
function
|
|
901
|
+
function isinstancesof(objs, type, logic, match, strict) {
|
|
666
902
|
if (!(type instanceof Array)) {
|
|
667
903
|
// uncomment (return objs instanceof type)
|
|
668
904
|
// to enable the 1/4 of (one to one)
|
|
@@ -1216,6 +1452,21 @@ function isinstances(objs, type, logic, match, strict) {
|
|
|
1216
1452
|
throw new TypeError("logic type undefined !");
|
|
1217
1453
|
}
|
|
1218
1454
|
|
|
1455
|
+
/**
|
|
1456
|
+
* Logs any number of values to the console.
|
|
1457
|
+
*
|
|
1458
|
+
* @function
|
|
1459
|
+
* @param {...any} data - The values to be logged.
|
|
1460
|
+
* @returns {void}
|
|
1461
|
+
*/
|
|
1462
|
+
|
|
1463
|
+
function echo(data) {
|
|
1464
|
+
if (arguments.length > 1) {
|
|
1465
|
+
throw new TypeError('unexpected token ","');
|
|
1466
|
+
}
|
|
1467
|
+
process.stdout.write(data);
|
|
1468
|
+
}
|
|
1469
|
+
|
|
1219
1470
|
/**
|
|
1220
1471
|
* Checks whether one or more objects are instances of a given class (or classes),
|
|
1221
1472
|
* similar to using the `instanceof` operator — but extended to handle arrays
|
|
@@ -1260,7 +1511,7 @@ function instancesof(
|
|
|
1260
1511
|
|
|
1261
1512
|
if (len === 3) {
|
|
1262
1513
|
if (checkType(arguments[2], boolean) || arguments[2] instanceof Boolean) {
|
|
1263
|
-
return
|
|
1514
|
+
return isinstancesof(objs, type, and, logic_or_match, strict);
|
|
1264
1515
|
}
|
|
1265
1516
|
}
|
|
1266
1517
|
|
|
@@ -1273,11 +1524,56 @@ function instancesof(
|
|
|
1273
1524
|
}
|
|
1274
1525
|
|
|
1275
1526
|
if (checkType(arguments[2], boolean) || arguments[2] instanceof Boolean) {
|
|
1276
|
-
return
|
|
1527
|
+
return isinstancesof(objs, type, and, logic_or_match, match_or_strict);
|
|
1277
1528
|
}
|
|
1278
1529
|
}
|
|
1279
1530
|
|
|
1280
|
-
return
|
|
1531
|
+
return isinstancesof(objs, type, logic_or_match, match_or_strict, strict);
|
|
1532
|
+
}
|
|
1533
|
+
|
|
1534
|
+
/**
|
|
1535
|
+
* Checks whether an object is an instance of a given class or constructor.
|
|
1536
|
+
*
|
|
1537
|
+
* @function
|
|
1538
|
+
* @param {any} obj - The object to check.
|
|
1539
|
+
* @param {Function} cls - The class or constructor to check against.
|
|
1540
|
+
* @returns {boolean} True if `obj` is an instance of `cls`, otherwise false.
|
|
1541
|
+
* @throws {TypeError} If `cls` is not a valid constructor.
|
|
1542
|
+
*/
|
|
1543
|
+
|
|
1544
|
+
function isinstance(obj, cls) {
|
|
1545
|
+
try {
|
|
1546
|
+
return obj instanceof cls;
|
|
1547
|
+
} catch (error) {
|
|
1548
|
+
throw new TypeError("Right-hand side of 'isinstance' is not callable");
|
|
1549
|
+
}
|
|
1550
|
+
}
|
|
1551
|
+
|
|
1552
|
+
/**
|
|
1553
|
+
* Checks whether one or more objects are instances of a given class (or classes),
|
|
1554
|
+
* similar to using the `instanceof` operator — but extended to handle arrays
|
|
1555
|
+
* and multiple constructors with logical control.
|
|
1556
|
+
*
|
|
1557
|
+
* Works like a plural form of `instanceof`, allowing you to check several objects
|
|
1558
|
+
* against one or more classes, using `"and"` or `"or"` logic.
|
|
1559
|
+
*
|
|
1560
|
+
* @template T - The instance type returned by the constructor(s).
|
|
1561
|
+
* @param {*|Array<*>} objs - The object or array of objects to test.
|
|
1562
|
+
* @param {(new (...args: any[]) => T) | Array<new (...args: any[]) => T>} type - The constructor or list of constructors to test against.
|
|
1563
|
+
* @param {"and" | "or" | boolean} logic_or_match - Logic mode: `"and"` requires all matches, `"or"` allows any match.
|
|
1564
|
+
* @param {boolean} match_or_strict - Whether to compare classes names strictly or loosely.
|
|
1565
|
+
* @param {boolean} strict - If `true`, enables strict comparison behavior.
|
|
1566
|
+
* @returns {boolean} `true` if the provided object(s) match the given type(s) according to the chosen logic, otherwise `false`.
|
|
1567
|
+
*/
|
|
1568
|
+
|
|
1569
|
+
function isinstances(
|
|
1570
|
+
objs,
|
|
1571
|
+
type,
|
|
1572
|
+
logic_or_match = and,
|
|
1573
|
+
match_or_strict = true,
|
|
1574
|
+
strict = true
|
|
1575
|
+
) {
|
|
1576
|
+
return instancesof(objs, type, logic_or_match, match_or_strict, strict);
|
|
1281
1577
|
}
|
|
1282
1578
|
|
|
1283
1579
|
/**
|
|
@@ -1342,6 +1638,22 @@ function isType(
|
|
|
1342
1638
|
return checkType(data, dataType, logic_or_match, match_or_strict, strict);
|
|
1343
1639
|
}
|
|
1344
1640
|
|
|
1641
|
+
/**
|
|
1642
|
+
* Returns the length of an iterable or object that has a `length` property.
|
|
1643
|
+
*
|
|
1644
|
+
* @function
|
|
1645
|
+
* @param {any} iter - The value to measure. Must have a `length` property.
|
|
1646
|
+
* @returns {number} The length of the given value.
|
|
1647
|
+
* @throws {TypeError} If the value does not have a `length` property.
|
|
1648
|
+
*/
|
|
1649
|
+
|
|
1650
|
+
function len(iter) {
|
|
1651
|
+
if (iter.length === undefined) {
|
|
1652
|
+
throw new TypeError(`type '${typeof iter}' has no len()`);
|
|
1653
|
+
}
|
|
1654
|
+
return iter.length;
|
|
1655
|
+
}
|
|
1656
|
+
|
|
1345
1657
|
/**
|
|
1346
1658
|
* Checks whether all provided values have the same length.
|
|
1347
1659
|
* Each value must have a numeric `length` property (e.g., arrays, strings, typed arrays, etc.).
|
|
@@ -1397,6 +1709,35 @@ function lengths(...objs) {
|
|
|
1397
1709
|
return [lens, equal];
|
|
1398
1710
|
}
|
|
1399
1711
|
|
|
1712
|
+
/**
|
|
1713
|
+
* Checks whether all provided values have the same length.
|
|
1714
|
+
* Each value must have a numeric `length` property (e.g., arrays, strings, typed arrays, etc.).
|
|
1715
|
+
*
|
|
1716
|
+
* If all lengths are equal, returns a tuple containing that length and `true`.
|
|
1717
|
+
* Otherwise, returns a tuple containing an array of all detected lengths and `false`.
|
|
1718
|
+
*
|
|
1719
|
+
* @param {...{length: number}} objs - The values to compare by their length property.
|
|
1720
|
+
* @returns {[number | number[], boolean]} A tuple:
|
|
1721
|
+
* - `[length, true]` if all values have the same length.
|
|
1722
|
+
* - `[lengths, false]` if the lengths differ.
|
|
1723
|
+
*/
|
|
1724
|
+
|
|
1725
|
+
function lens(...objs) {
|
|
1726
|
+
return lengths(...objs);
|
|
1727
|
+
}
|
|
1728
|
+
|
|
1729
|
+
/**
|
|
1730
|
+
* Logs any number of values to the console.
|
|
1731
|
+
*
|
|
1732
|
+
* @function
|
|
1733
|
+
* @param {...any} data - The values to be logged.
|
|
1734
|
+
* @returns {void}
|
|
1735
|
+
*/
|
|
1736
|
+
|
|
1737
|
+
function log(...data) {
|
|
1738
|
+
console.log(...data);
|
|
1739
|
+
}
|
|
1740
|
+
|
|
1400
1741
|
/**
|
|
1401
1742
|
* Evaluates the truthiness of a given value.
|
|
1402
1743
|
* Works like the logical NOT operator (`!`), returning `true` if the value is falsy and `false` if it is truthy.
|
|
@@ -1417,6 +1758,86 @@ function not(obj) {
|
|
|
1417
1758
|
return obj?.length === 0 || !obj; // handling the object case
|
|
1418
1759
|
}
|
|
1419
1760
|
|
|
1761
|
+
/**
|
|
1762
|
+
* Logs any number of values to the console.
|
|
1763
|
+
*
|
|
1764
|
+
* @function
|
|
1765
|
+
* @param {...any} data - The values to be logged.
|
|
1766
|
+
* @returns {void}
|
|
1767
|
+
*/
|
|
1768
|
+
|
|
1769
|
+
function print(...data) {
|
|
1770
|
+
console.log(...data);
|
|
1771
|
+
}
|
|
1772
|
+
|
|
1773
|
+
/**
|
|
1774
|
+
* Logs any number of values to the console.
|
|
1775
|
+
*
|
|
1776
|
+
* @function
|
|
1777
|
+
* @param {...any} data - The values to be logged.
|
|
1778
|
+
* @returns {void}
|
|
1779
|
+
*/
|
|
1780
|
+
|
|
1781
|
+
function printf(data) {
|
|
1782
|
+
if (arguments.length > 1) {
|
|
1783
|
+
throw new TypeError('unexpected token ","');
|
|
1784
|
+
}
|
|
1785
|
+
process.stdout.write(...data);
|
|
1786
|
+
}
|
|
1787
|
+
|
|
1788
|
+
/**
|
|
1789
|
+
* Logs any number of values to the console.
|
|
1790
|
+
*
|
|
1791
|
+
* @function
|
|
1792
|
+
* @param {...any} data - The values to be logged.
|
|
1793
|
+
* @returns {void}
|
|
1794
|
+
*/
|
|
1795
|
+
|
|
1796
|
+
function println(...data) {
|
|
1797
|
+
console.log(...data);
|
|
1798
|
+
}
|
|
1799
|
+
|
|
1800
|
+
/**
|
|
1801
|
+
* Logs any number of values to the console.
|
|
1802
|
+
*
|
|
1803
|
+
* @function
|
|
1804
|
+
* @param {...any} data - The values to be logged.
|
|
1805
|
+
* @returns {void}
|
|
1806
|
+
*/
|
|
1807
|
+
|
|
1808
|
+
function puts(...data) {
|
|
1809
|
+
console.log(...data);
|
|
1810
|
+
}
|
|
1811
|
+
|
|
1812
|
+
/**
|
|
1813
|
+
* Returns the JavaScript type of a value using the `typeof` operator.
|
|
1814
|
+
*
|
|
1815
|
+
* @function
|
|
1816
|
+
* @param {any} data - The value whose type will be determined.
|
|
1817
|
+
* @returns {string} The type of the value (e.g., "string", "number", "object", "undefined").
|
|
1818
|
+
*/
|
|
1819
|
+
|
|
1820
|
+
function type(data) {
|
|
1821
|
+
return typeof data;
|
|
1822
|
+
}
|
|
1823
|
+
|
|
1824
|
+
/**
|
|
1825
|
+
* Checks whether all provided values are of the same type.
|
|
1826
|
+
* Similar to the `typeof` operator but supports multiple inputs.
|
|
1827
|
+
*
|
|
1828
|
+
* If all values share the same type, returns a tuple containing that type as a string and `true`.
|
|
1829
|
+
* Otherwise, returns a tuple containing an array of the detected types and `false`.
|
|
1830
|
+
*
|
|
1831
|
+
* @param {...*} objs - The values to check.
|
|
1832
|
+
* @returns {[string | string[], boolean]} A tuple:
|
|
1833
|
+
* - `[type, true]` if all values are of the same type.
|
|
1834
|
+
* - `[types, false]` if values have mixed types.
|
|
1835
|
+
*/
|
|
1836
|
+
|
|
1837
|
+
function types(...objs) {
|
|
1838
|
+
return typesof(...objs);
|
|
1839
|
+
}
|
|
1840
|
+
|
|
1420
1841
|
/**
|
|
1421
1842
|
* Checks whether all provided values are of the same type.
|
|
1422
1843
|
* Similar to the `typeof` operator but supports multiple inputs.
|
|
@@ -1714,26 +2135,50 @@ if (require.main === module) {
|
|
|
1714
2135
|
|
|
1715
2136
|
module.exports = {
|
|
1716
2137
|
and,
|
|
1717
|
-
|
|
2138
|
+
bi,
|
|
2139
|
+
BI,
|
|
1718
2140
|
bigint,
|
|
1719
|
-
|
|
1720
|
-
|
|
2141
|
+
bool,
|
|
2142
|
+
Bool,
|
|
2143
|
+
boolean,
|
|
1721
2144
|
fn,
|
|
1722
|
-
|
|
2145
|
+
Fn,
|
|
2146
|
+
fun,
|
|
2147
|
+
Fun,
|
|
2148
|
+
func,
|
|
2149
|
+
Func,
|
|
2150
|
+
nil,
|
|
1723
2151
|
num,
|
|
1724
|
-
|
|
2152
|
+
Num,
|
|
2153
|
+
number,
|
|
1725
2154
|
obj,
|
|
1726
|
-
|
|
1727
|
-
|
|
2155
|
+
Obj,
|
|
2156
|
+
object,
|
|
2157
|
+
or,
|
|
1728
2158
|
str,
|
|
2159
|
+
Str,
|
|
2160
|
+
string,
|
|
2161
|
+
sym,
|
|
2162
|
+
Sym,
|
|
1729
2163
|
symbol,
|
|
1730
|
-
boolean,
|
|
1731
|
-
bool,
|
|
1732
2164
|
undef,
|
|
2165
|
+
|
|
2166
|
+
echo,
|
|
1733
2167
|
instancesof,
|
|
2168
|
+
isinstance,
|
|
2169
|
+
isinstances,
|
|
1734
2170
|
isType,
|
|
2171
|
+
len,
|
|
1735
2172
|
lengths,
|
|
2173
|
+
lens,
|
|
2174
|
+
log,
|
|
1736
2175
|
not,
|
|
2176
|
+
print,
|
|
2177
|
+
printf,
|
|
2178
|
+
println,
|
|
2179
|
+
puts,
|
|
2180
|
+
type,
|
|
2181
|
+
types,
|
|
1737
2182
|
typesof,
|
|
1738
2183
|
$,
|
|
1739
2184
|
};
|
package/jsfunx.mjs
CHANGED
|
@@ -38,7 +38,11 @@ export const and = "and";
|
|
|
38
38
|
|
|
39
39
|
/** @type {string} */
|
|
40
40
|
|
|
41
|
-
export const
|
|
41
|
+
export const bi = "bigint";
|
|
42
|
+
|
|
43
|
+
/** @type {BigIntConstructor} */
|
|
44
|
+
|
|
45
|
+
export const BI = BigInt;
|
|
42
46
|
|
|
43
47
|
/** @type {string} */
|
|
44
48
|
|
|
@@ -46,55 +50,95 @@ export const bigint = "bigint";
|
|
|
46
50
|
|
|
47
51
|
/** @type {string} */
|
|
48
52
|
|
|
49
|
-
export const
|
|
53
|
+
export const bool = "boolean";
|
|
54
|
+
|
|
55
|
+
/** @type {BooleanConstructor} */
|
|
56
|
+
|
|
57
|
+
export const Bool = Boolean;
|
|
50
58
|
|
|
51
59
|
/** @type {string} */
|
|
52
60
|
|
|
53
|
-
export const
|
|
61
|
+
export const boolean = "boolean";
|
|
54
62
|
|
|
55
63
|
/** @type {string} */
|
|
56
64
|
|
|
57
65
|
export const fn = "function";
|
|
58
66
|
|
|
67
|
+
/** @type {FunctionConstructor} */
|
|
68
|
+
|
|
69
|
+
export const Fn = Function;
|
|
70
|
+
|
|
59
71
|
/** @type {string} */
|
|
60
72
|
|
|
61
|
-
export const
|
|
73
|
+
export const fun = "function";
|
|
74
|
+
|
|
75
|
+
/** @type {FunctionConstructor} */
|
|
76
|
+
|
|
77
|
+
export const Fun = Function;
|
|
78
|
+
|
|
79
|
+
/** @type {string} */
|
|
80
|
+
|
|
81
|
+
export const func = "function";
|
|
82
|
+
|
|
83
|
+
/** @type {FunctionConstructor} */
|
|
84
|
+
|
|
85
|
+
export const Func = Function;
|
|
86
|
+
|
|
87
|
+
/** @type {null} */
|
|
88
|
+
|
|
89
|
+
export const nil = null;
|
|
62
90
|
|
|
63
91
|
/** @type {string} */
|
|
64
92
|
|
|
65
93
|
export const num = "number";
|
|
66
94
|
|
|
95
|
+
/** @type {NumberConstructor} */
|
|
96
|
+
|
|
97
|
+
export const Num = Number;
|
|
98
|
+
|
|
67
99
|
/** @type {string} */
|
|
68
100
|
|
|
69
|
-
export const
|
|
101
|
+
export const number = "number";
|
|
70
102
|
|
|
71
103
|
/** @type {string} */
|
|
72
104
|
|
|
73
105
|
export const obj = "object";
|
|
74
106
|
|
|
107
|
+
/** @type {ObjectConstructor} */
|
|
108
|
+
|
|
109
|
+
export const Obj = Object;
|
|
110
|
+
|
|
75
111
|
/** @type {string} */
|
|
76
112
|
|
|
77
|
-
export const
|
|
113
|
+
export const object = "object";
|
|
78
114
|
|
|
79
115
|
/** @type {string} */
|
|
80
116
|
|
|
81
|
-
export const
|
|
117
|
+
export const or = "or";
|
|
82
118
|
|
|
83
119
|
/** @type {string} */
|
|
84
120
|
|
|
85
121
|
export const str = "string";
|
|
86
122
|
|
|
123
|
+
/** @type {StringConstructor} */
|
|
124
|
+
|
|
125
|
+
export const Str = String;
|
|
126
|
+
|
|
87
127
|
/** @type {string} */
|
|
88
128
|
|
|
89
|
-
export const
|
|
129
|
+
export const string = "string";
|
|
90
130
|
|
|
91
131
|
/** @type {string} */
|
|
92
132
|
|
|
93
|
-
export const
|
|
133
|
+
export const sym = "symbol";
|
|
134
|
+
|
|
135
|
+
/** @type {SymbolConstructor} */
|
|
136
|
+
|
|
137
|
+
export const Sym = Symbol;
|
|
94
138
|
|
|
95
139
|
/** @type {string} */
|
|
96
140
|
|
|
97
|
-
export const
|
|
141
|
+
export const symbol = "symbol";
|
|
98
142
|
|
|
99
143
|
/** @type {string} */
|
|
100
144
|
|
|
@@ -116,19 +160,21 @@ export const undef = "undefined";
|
|
|
116
160
|
*/
|
|
117
161
|
|
|
118
162
|
function checkType(data, dataType, logic, match, strict) {
|
|
119
|
-
/** @type {Set<string>} */
|
|
163
|
+
/** @type {Set<string | null | undefined>} */
|
|
120
164
|
|
|
121
165
|
let setOfTypes = new Set([
|
|
122
166
|
and,
|
|
167
|
+
bi,
|
|
168
|
+
bool,
|
|
169
|
+
fn,
|
|
170
|
+
null,
|
|
171
|
+
num,
|
|
172
|
+
obj,
|
|
123
173
|
or,
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
number,
|
|
127
|
-
object,
|
|
128
|
-
string,
|
|
129
|
-
symbol,
|
|
130
|
-
boolean,
|
|
174
|
+
str,
|
|
175
|
+
sym,
|
|
131
176
|
undef,
|
|
177
|
+
undefined,
|
|
132
178
|
]);
|
|
133
179
|
|
|
134
180
|
if (!(dataType instanceof Array)) {
|
|
@@ -137,6 +183,14 @@ function checkType(data, dataType, logic, match, strict) {
|
|
|
137
183
|
}
|
|
138
184
|
|
|
139
185
|
if (!(data instanceof Array)) {
|
|
186
|
+
if (data === null) {
|
|
187
|
+
return dataType === null;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
if (dataType === undefined) {
|
|
191
|
+
return data === undefined;
|
|
192
|
+
}
|
|
193
|
+
|
|
140
194
|
return typeof data === dataType;
|
|
141
195
|
}
|
|
142
196
|
|
|
@@ -148,6 +202,20 @@ function checkType(data, dataType, logic, match, strict) {
|
|
|
148
202
|
for (let dt of data) {
|
|
149
203
|
// 1 step directly in all cases
|
|
150
204
|
|
|
205
|
+
if (dt === null) {
|
|
206
|
+
if (dataType !== null) {
|
|
207
|
+
return false;
|
|
208
|
+
}
|
|
209
|
+
continue;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
if (dataType === undefined) {
|
|
213
|
+
if (dt !== undefined) {
|
|
214
|
+
return false;
|
|
215
|
+
}
|
|
216
|
+
continue;
|
|
217
|
+
}
|
|
218
|
+
|
|
151
219
|
if (!(typeof dt === dataType)) {
|
|
152
220
|
return false;
|
|
153
221
|
}
|
|
@@ -167,6 +235,20 @@ function checkType(data, dataType, logic, match, strict) {
|
|
|
167
235
|
for (let dt of data) {
|
|
168
236
|
// 1 step directly in all cases
|
|
169
237
|
|
|
238
|
+
if (dt === null) {
|
|
239
|
+
if (dataType === null) {
|
|
240
|
+
return true;
|
|
241
|
+
}
|
|
242
|
+
continue;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
if (dataType === undefined) {
|
|
246
|
+
if (dt === undefined) {
|
|
247
|
+
return true;
|
|
248
|
+
}
|
|
249
|
+
continue;
|
|
250
|
+
}
|
|
251
|
+
|
|
170
252
|
if (typeof dt === dataType) {
|
|
171
253
|
return true;
|
|
172
254
|
}
|
|
@@ -193,6 +275,20 @@ function checkType(data, dataType, logic, match, strict) {
|
|
|
193
275
|
throw new TypeError(`type ${dtype} undefined !`);
|
|
194
276
|
}
|
|
195
277
|
|
|
278
|
+
if (data === null) {
|
|
279
|
+
if (dtype === null) {
|
|
280
|
+
return true;
|
|
281
|
+
}
|
|
282
|
+
continue;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
if (dtype === undefined) {
|
|
286
|
+
if (data === undefined) {
|
|
287
|
+
return true;
|
|
288
|
+
}
|
|
289
|
+
continue;
|
|
290
|
+
}
|
|
291
|
+
|
|
196
292
|
if (typeof data === dtype) {
|
|
197
293
|
return true;
|
|
198
294
|
}
|
|
@@ -220,6 +316,20 @@ function checkType(data, dataType, logic, match, strict) {
|
|
|
220
316
|
throw new TypeError(`type ${dtype} undefined !`);
|
|
221
317
|
}
|
|
222
318
|
|
|
319
|
+
if (dt === null) {
|
|
320
|
+
if (dtype === null) {
|
|
321
|
+
continue outerLoop;
|
|
322
|
+
}
|
|
323
|
+
continue;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
if (dtype === undefined) {
|
|
327
|
+
if (dt === undefined) {
|
|
328
|
+
continue outerLoop;
|
|
329
|
+
}
|
|
330
|
+
continue;
|
|
331
|
+
}
|
|
332
|
+
|
|
223
333
|
if (typeof dt === dtype) {
|
|
224
334
|
continue outerLoop;
|
|
225
335
|
}
|
|
@@ -261,6 +371,20 @@ function checkType(data, dataType, logic, match, strict) {
|
|
|
261
371
|
// // // throw new TypeError(`type ${dtype} undefined !`);
|
|
262
372
|
// // // }
|
|
263
373
|
|
|
374
|
+
// // // if (data[0] === null) {
|
|
375
|
+
// // // if (dtype === null) {
|
|
376
|
+
// // // return true;
|
|
377
|
+
// // // }
|
|
378
|
+
// // // continue;
|
|
379
|
+
// // // }
|
|
380
|
+
|
|
381
|
+
// // // if (dtype === undefined) {
|
|
382
|
+
// // // if (data[0] === undefined) {
|
|
383
|
+
// // // return true;
|
|
384
|
+
// // // }
|
|
385
|
+
// // // continue;
|
|
386
|
+
// // // }
|
|
387
|
+
|
|
264
388
|
// // // if (typeof data[0] === dtype) {
|
|
265
389
|
// // // return true;
|
|
266
390
|
// // // }
|
|
@@ -297,6 +421,20 @@ function checkType(data, dataType, logic, match, strict) {
|
|
|
297
421
|
// // // throw new TypeError("type undefined !");
|
|
298
422
|
// // // }
|
|
299
423
|
|
|
424
|
+
// // // if (dt === null) {
|
|
425
|
+
// // // if (dataType[0] !== null) {
|
|
426
|
+
// // // return false;
|
|
427
|
+
// // // }
|
|
428
|
+
// // // continue;
|
|
429
|
+
// // // }
|
|
430
|
+
|
|
431
|
+
// // // if (dataType[0] === undefined) {
|
|
432
|
+
// // // if (dt !== undefined) {
|
|
433
|
+
// // // return false;
|
|
434
|
+
// // // }
|
|
435
|
+
// // // continue;
|
|
436
|
+
// // // }
|
|
437
|
+
|
|
300
438
|
// // // if (!(typeof dt === dataType[0])) {
|
|
301
439
|
// // // return false;
|
|
302
440
|
// // // }
|
|
@@ -359,6 +497,20 @@ function checkType(data, dataType, logic, match, strict) {
|
|
|
359
497
|
throw new TypeError(`type ${dtype} undefined !`);
|
|
360
498
|
}
|
|
361
499
|
|
|
500
|
+
if (dt === null) {
|
|
501
|
+
if (dtype === null) {
|
|
502
|
+
return true;
|
|
503
|
+
}
|
|
504
|
+
continue;
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
if (dtype === undefined) {
|
|
508
|
+
if (dt === undefined) {
|
|
509
|
+
return true;
|
|
510
|
+
}
|
|
511
|
+
continue;
|
|
512
|
+
}
|
|
513
|
+
|
|
362
514
|
if (typeof dt === dtype) {
|
|
363
515
|
return true;
|
|
364
516
|
}
|
|
@@ -396,6 +548,20 @@ function checkType(data, dataType, logic, match, strict) {
|
|
|
396
548
|
// // // throw new TypeError(`type ${dtype} undefined !`);
|
|
397
549
|
// // // }
|
|
398
550
|
|
|
551
|
+
// // // if (data[0] === null) {
|
|
552
|
+
// // // if (dtype === null) {
|
|
553
|
+
// // // return true;
|
|
554
|
+
// // // }
|
|
555
|
+
// // // continue;
|
|
556
|
+
// // // }
|
|
557
|
+
|
|
558
|
+
// // // if (dtype === undefined) {
|
|
559
|
+
// // // if (data[0] === undefined) {
|
|
560
|
+
// // // return true;
|
|
561
|
+
// // // }
|
|
562
|
+
// // // continue;
|
|
563
|
+
// // // }
|
|
564
|
+
|
|
399
565
|
// // // if (typeof data[0] === dtype) {
|
|
400
566
|
// // // return true;
|
|
401
567
|
// // // }
|
|
@@ -434,6 +600,20 @@ function checkType(data, dataType, logic, match, strict) {
|
|
|
434
600
|
// // // throw new TypeError("type undefined !");
|
|
435
601
|
// // // }
|
|
436
602
|
|
|
603
|
+
// // // if (dt === null) {
|
|
604
|
+
// // // if (dataType[0] === null) {
|
|
605
|
+
// // // return true;
|
|
606
|
+
// // // }
|
|
607
|
+
// // // continue;
|
|
608
|
+
// // // }
|
|
609
|
+
|
|
610
|
+
// // // if (dataType[0] === undefined) {
|
|
611
|
+
// // // if (dt === undefined) {
|
|
612
|
+
// // // return true;
|
|
613
|
+
// // // }
|
|
614
|
+
// // // continue;
|
|
615
|
+
// // // }
|
|
616
|
+
|
|
437
617
|
// // // if (typeof dt === dataType[0]) {
|
|
438
618
|
// // // return true;
|
|
439
619
|
// // // }
|
|
@@ -553,6 +733,20 @@ function checkType(data, dataType, logic, match, strict) {
|
|
|
553
733
|
throw new TypeError(`type ${dataType[i]} undefined !`);
|
|
554
734
|
}
|
|
555
735
|
|
|
736
|
+
if (data[i] === null) {
|
|
737
|
+
if (dataType[i] !== null) {
|
|
738
|
+
return false;
|
|
739
|
+
}
|
|
740
|
+
continue;
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
if (dataType[i] === undefined) {
|
|
744
|
+
if (data[i] !== undefined) {
|
|
745
|
+
return false;
|
|
746
|
+
}
|
|
747
|
+
continue;
|
|
748
|
+
}
|
|
749
|
+
|
|
556
750
|
if (!(typeof data[i] === dataType[i])) {
|
|
557
751
|
return false;
|
|
558
752
|
}
|
|
@@ -578,6 +772,20 @@ function checkType(data, dataType, logic, match, strict) {
|
|
|
578
772
|
throw new TypeError(`type ${dataType[i]} undefined !`);
|
|
579
773
|
}
|
|
580
774
|
|
|
775
|
+
if (data[i] === null) {
|
|
776
|
+
if (dataType[i] === null) {
|
|
777
|
+
return true;
|
|
778
|
+
}
|
|
779
|
+
continue;
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
if (dataType[i] === undefined) {
|
|
783
|
+
if (data[i] === undefined) {
|
|
784
|
+
return true;
|
|
785
|
+
}
|
|
786
|
+
continue;
|
|
787
|
+
}
|
|
788
|
+
|
|
581
789
|
if (typeof data[i] === dataType[i]) {
|
|
582
790
|
return true;
|
|
583
791
|
}
|
|
@@ -606,6 +814,20 @@ function checkType(data, dataType, logic, match, strict) {
|
|
|
606
814
|
throw new TypeError(`type ${dataType[i]} undefined !`);
|
|
607
815
|
}
|
|
608
816
|
|
|
817
|
+
if (data[i] === null) {
|
|
818
|
+
if (dataType[i] !== null) {
|
|
819
|
+
return false;
|
|
820
|
+
}
|
|
821
|
+
continue;
|
|
822
|
+
}
|
|
823
|
+
|
|
824
|
+
if (dataType[i] === undefined) {
|
|
825
|
+
if (data[i] !== undefined) {
|
|
826
|
+
return false;
|
|
827
|
+
}
|
|
828
|
+
continue;
|
|
829
|
+
}
|
|
830
|
+
|
|
609
831
|
if (!(typeof data[i] === dataType[i])) {
|
|
610
832
|
return false;
|
|
611
833
|
}
|
|
@@ -631,6 +853,20 @@ function checkType(data, dataType, logic, match, strict) {
|
|
|
631
853
|
throw new TypeError(`type ${dataType[i]} undefined !`);
|
|
632
854
|
}
|
|
633
855
|
|
|
856
|
+
if (data[i] === null) {
|
|
857
|
+
if (dataType[i] === null) {
|
|
858
|
+
return true;
|
|
859
|
+
}
|
|
860
|
+
continue;
|
|
861
|
+
}
|
|
862
|
+
|
|
863
|
+
if (dataType[i] === undefined) {
|
|
864
|
+
if (data[i] === undefined) {
|
|
865
|
+
return true;
|
|
866
|
+
}
|
|
867
|
+
continue;
|
|
868
|
+
}
|
|
869
|
+
|
|
634
870
|
if (typeof data[i] === dataType[i]) {
|
|
635
871
|
return true;
|
|
636
872
|
}
|
|
@@ -668,7 +904,7 @@ function checkType(data, dataType, logic, match, strict) {
|
|
|
668
904
|
* @returns {boolean} `true` if the provided object(s) match the given type(s) according to the chosen logic, otherwise `false`.
|
|
669
905
|
*/
|
|
670
906
|
|
|
671
|
-
function
|
|
907
|
+
function isinstancesof(objs, type, logic, match, strict) {
|
|
672
908
|
if (!(type instanceof Array)) {
|
|
673
909
|
// uncomment (return objs instanceof type)
|
|
674
910
|
// to enable the 1/4 of (one to one)
|
|
@@ -1222,6 +1458,21 @@ function isinstances(objs, type, logic, match, strict) {
|
|
|
1222
1458
|
throw new TypeError("logic type undefined !");
|
|
1223
1459
|
}
|
|
1224
1460
|
|
|
1461
|
+
/**
|
|
1462
|
+
* Logs any number of values to the console.
|
|
1463
|
+
*
|
|
1464
|
+
* @function
|
|
1465
|
+
* @param {...any} data - The values to be logged.
|
|
1466
|
+
* @returns {void}
|
|
1467
|
+
*/
|
|
1468
|
+
|
|
1469
|
+
export function echo(data) {
|
|
1470
|
+
if (arguments.length > 1) {
|
|
1471
|
+
throw new TypeError('unexpected token ","');
|
|
1472
|
+
}
|
|
1473
|
+
process.stdout.write(data);
|
|
1474
|
+
}
|
|
1475
|
+
|
|
1225
1476
|
/**
|
|
1226
1477
|
* Checks whether one or more objects are instances of a given class (or classes),
|
|
1227
1478
|
* similar to using the `instanceof` operator — but extended to handle arrays
|
|
@@ -1266,7 +1517,7 @@ export function instancesof(
|
|
|
1266
1517
|
|
|
1267
1518
|
if (len === 3) {
|
|
1268
1519
|
if (checkType(arguments[2], boolean) || arguments[2] instanceof Boolean) {
|
|
1269
|
-
return
|
|
1520
|
+
return isinstancesof(objs, type, and, logic_or_match, strict);
|
|
1270
1521
|
}
|
|
1271
1522
|
}
|
|
1272
1523
|
|
|
@@ -1279,11 +1530,56 @@ export function instancesof(
|
|
|
1279
1530
|
}
|
|
1280
1531
|
|
|
1281
1532
|
if (checkType(arguments[2], boolean) || arguments[2] instanceof Boolean) {
|
|
1282
|
-
return
|
|
1533
|
+
return isinstancesof(objs, type, and, logic_or_match, match_or_strict);
|
|
1283
1534
|
}
|
|
1284
1535
|
}
|
|
1285
1536
|
|
|
1286
|
-
return
|
|
1537
|
+
return isinstancesof(objs, type, logic_or_match, match_or_strict, strict);
|
|
1538
|
+
}
|
|
1539
|
+
|
|
1540
|
+
/**
|
|
1541
|
+
* Checks whether an object is an instance of a given class or constructor.
|
|
1542
|
+
*
|
|
1543
|
+
* @function
|
|
1544
|
+
* @param {any} obj - The object to check.
|
|
1545
|
+
* @param {Function} cls - The class or constructor to check against.
|
|
1546
|
+
* @returns {boolean} True if `obj` is an instance of `cls`, otherwise false.
|
|
1547
|
+
* @throws {TypeError} If `cls` is not a valid constructor.
|
|
1548
|
+
*/
|
|
1549
|
+
|
|
1550
|
+
export function isinstance(obj, cls) {
|
|
1551
|
+
try {
|
|
1552
|
+
return obj instanceof cls;
|
|
1553
|
+
} catch (error) {
|
|
1554
|
+
throw new TypeError("Right-hand side of 'isinstance' is not callable");
|
|
1555
|
+
}
|
|
1556
|
+
}
|
|
1557
|
+
|
|
1558
|
+
/**
|
|
1559
|
+
* Checks whether one or more objects are instances of a given class (or classes),
|
|
1560
|
+
* similar to using the `instanceof` operator — but extended to handle arrays
|
|
1561
|
+
* and multiple constructors with logical control.
|
|
1562
|
+
*
|
|
1563
|
+
* Works like a plural form of `instanceof`, allowing you to check several objects
|
|
1564
|
+
* against one or more classes, using `"and"` or `"or"` logic.
|
|
1565
|
+
*
|
|
1566
|
+
* @template T - The instance type returned by the constructor(s).
|
|
1567
|
+
* @param {*|Array<*>} objs - The object or array of objects to test.
|
|
1568
|
+
* @param {(new (...args: any[]) => T) | Array<new (...args: any[]) => T>} type - The constructor or list of constructors to test against.
|
|
1569
|
+
* @param {"and" | "or" | boolean} logic_or_match - Logic mode: `"and"` requires all matches, `"or"` allows any match.
|
|
1570
|
+
* @param {boolean} match_or_strict - Whether to compare classes names strictly or loosely.
|
|
1571
|
+
* @param {boolean} strict - If `true`, enables strict comparison behavior.
|
|
1572
|
+
* @returns {boolean} `true` if the provided object(s) match the given type(s) according to the chosen logic, otherwise `false`.
|
|
1573
|
+
*/
|
|
1574
|
+
|
|
1575
|
+
export function isinstances(
|
|
1576
|
+
objs,
|
|
1577
|
+
type,
|
|
1578
|
+
logic_or_match = and,
|
|
1579
|
+
match_or_strict = true,
|
|
1580
|
+
strict = true
|
|
1581
|
+
) {
|
|
1582
|
+
return instancesof(objs, type, logic_or_match, match_or_strict, strict);
|
|
1287
1583
|
}
|
|
1288
1584
|
|
|
1289
1585
|
/**
|
|
@@ -1348,6 +1644,22 @@ export function isType(
|
|
|
1348
1644
|
return checkType(data, dataType, logic_or_match, match_or_strict, strict);
|
|
1349
1645
|
}
|
|
1350
1646
|
|
|
1647
|
+
/**
|
|
1648
|
+
* Returns the length of an iterable or object that has a `length` property.
|
|
1649
|
+
*
|
|
1650
|
+
* @function
|
|
1651
|
+
* @param {any} iter - The value to measure. Must have a `length` property.
|
|
1652
|
+
* @returns {number} The length of the given value.
|
|
1653
|
+
* @throws {TypeError} If the value does not have a `length` property.
|
|
1654
|
+
*/
|
|
1655
|
+
|
|
1656
|
+
export function len(iter) {
|
|
1657
|
+
if (iter.length === undefined) {
|
|
1658
|
+
throw new TypeError(`type '${typeof iter}' has no len()`);
|
|
1659
|
+
}
|
|
1660
|
+
return iter.length;
|
|
1661
|
+
}
|
|
1662
|
+
|
|
1351
1663
|
/**
|
|
1352
1664
|
* Checks whether all provided values have the same length.
|
|
1353
1665
|
* Each value must have a numeric `length` property (e.g., arrays, strings, typed arrays, etc.).
|
|
@@ -1403,6 +1715,35 @@ export function lengths(...objs) {
|
|
|
1403
1715
|
return [lens, equal];
|
|
1404
1716
|
}
|
|
1405
1717
|
|
|
1718
|
+
/**
|
|
1719
|
+
* Checks whether all provided values have the same length.
|
|
1720
|
+
* Each value must have a numeric `length` property (e.g., arrays, strings, typed arrays, etc.).
|
|
1721
|
+
*
|
|
1722
|
+
* If all lengths are equal, returns a tuple containing that length and `true`.
|
|
1723
|
+
* Otherwise, returns a tuple containing an array of all detected lengths and `false`.
|
|
1724
|
+
*
|
|
1725
|
+
* @param {...{length: number}} objs - The values to compare by their length property.
|
|
1726
|
+
* @returns {[number | number[], boolean]} A tuple:
|
|
1727
|
+
* - `[length, true]` if all values have the same length.
|
|
1728
|
+
* - `[lengths, false]` if the lengths differ.
|
|
1729
|
+
*/
|
|
1730
|
+
|
|
1731
|
+
export function lens(...objs) {
|
|
1732
|
+
return lengths(...objs);
|
|
1733
|
+
}
|
|
1734
|
+
|
|
1735
|
+
/**
|
|
1736
|
+
* Logs any number of values to the console.
|
|
1737
|
+
*
|
|
1738
|
+
* @function
|
|
1739
|
+
* @param {...any} data - The values to be logged.
|
|
1740
|
+
* @returns {void}
|
|
1741
|
+
*/
|
|
1742
|
+
|
|
1743
|
+
export function log(...data) {
|
|
1744
|
+
console.log(...data);
|
|
1745
|
+
}
|
|
1746
|
+
|
|
1406
1747
|
/**
|
|
1407
1748
|
* Evaluates the truthiness of a given value.
|
|
1408
1749
|
* Works like the logical NOT operator (`!`), returning `true` if the value is falsy and `false` if it is truthy.
|
|
@@ -1423,6 +1764,86 @@ export function not(obj) {
|
|
|
1423
1764
|
return obj?.length === 0 || !obj; // handling the object case
|
|
1424
1765
|
}
|
|
1425
1766
|
|
|
1767
|
+
/**
|
|
1768
|
+
* Logs any number of values to the console.
|
|
1769
|
+
*
|
|
1770
|
+
* @function
|
|
1771
|
+
* @param {...any} data - The values to be logged.
|
|
1772
|
+
* @returns {void}
|
|
1773
|
+
*/
|
|
1774
|
+
|
|
1775
|
+
export function print(...data) {
|
|
1776
|
+
console.log(...data);
|
|
1777
|
+
}
|
|
1778
|
+
|
|
1779
|
+
/**
|
|
1780
|
+
* Logs any number of values to the console.
|
|
1781
|
+
*
|
|
1782
|
+
* @function
|
|
1783
|
+
* @param {...any} data - The values to be logged.
|
|
1784
|
+
* @returns {void}
|
|
1785
|
+
*/
|
|
1786
|
+
|
|
1787
|
+
export function printf(data) {
|
|
1788
|
+
if (arguments.length > 1) {
|
|
1789
|
+
throw new TypeError('unexpected token ","');
|
|
1790
|
+
}
|
|
1791
|
+
process.stdout.write(...data);
|
|
1792
|
+
}
|
|
1793
|
+
|
|
1794
|
+
/**
|
|
1795
|
+
* Logs any number of values to the console.
|
|
1796
|
+
*
|
|
1797
|
+
* @function
|
|
1798
|
+
* @param {...any} data - The values to be logged.
|
|
1799
|
+
* @returns {void}
|
|
1800
|
+
*/
|
|
1801
|
+
|
|
1802
|
+
export function println(...data) {
|
|
1803
|
+
console.log(...data);
|
|
1804
|
+
}
|
|
1805
|
+
|
|
1806
|
+
/**
|
|
1807
|
+
* Logs any number of values to the console.
|
|
1808
|
+
*
|
|
1809
|
+
* @function
|
|
1810
|
+
* @param {...any} data - The values to be logged.
|
|
1811
|
+
* @returns {void}
|
|
1812
|
+
*/
|
|
1813
|
+
|
|
1814
|
+
export function puts(...data) {
|
|
1815
|
+
console.log(...data);
|
|
1816
|
+
}
|
|
1817
|
+
|
|
1818
|
+
/**
|
|
1819
|
+
* Returns the JavaScript type of a value using the `typeof` operator.
|
|
1820
|
+
*
|
|
1821
|
+
* @function
|
|
1822
|
+
* @param {any} data - The value whose type will be determined.
|
|
1823
|
+
* @returns {string} The type of the value (e.g., "string", "number", "object", "undefined").
|
|
1824
|
+
*/
|
|
1825
|
+
|
|
1826
|
+
export function type(data) {
|
|
1827
|
+
return typeof data;
|
|
1828
|
+
}
|
|
1829
|
+
|
|
1830
|
+
/**
|
|
1831
|
+
* Checks whether all provided values are of the same type.
|
|
1832
|
+
* Similar to the `typeof` operator but supports multiple inputs.
|
|
1833
|
+
*
|
|
1834
|
+
* If all values share the same type, returns a tuple containing that type as a string and `true`.
|
|
1835
|
+
* Otherwise, returns a tuple containing an array of the detected types and `false`.
|
|
1836
|
+
*
|
|
1837
|
+
* @param {...*} objs - The values to check.
|
|
1838
|
+
* @returns {[string | string[], boolean]} A tuple:
|
|
1839
|
+
* - `[type, true]` if all values are of the same type.
|
|
1840
|
+
* - `[types, false]` if values have mixed types.
|
|
1841
|
+
*/
|
|
1842
|
+
|
|
1843
|
+
export function types(...objs) {
|
|
1844
|
+
return typesof(...objs);
|
|
1845
|
+
}
|
|
1846
|
+
|
|
1426
1847
|
/**
|
|
1427
1848
|
* Checks whether all provided values are of the same type.
|
|
1428
1849
|
* Similar to the `typeof` operator but supports multiple inputs.
|