jsfunx 1.2.0 → 1.2.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 +180 -42
- package/jsfunx.mjs +177 -42
- package/package.json +1 -1
package/jsfunx.cjs
CHANGED
|
@@ -32,6 +32,10 @@ Author: Khiat Mohammed Abderrezzak <khiat.dev@gmail.com>
|
|
|
32
32
|
|
|
33
33
|
const and = "and";
|
|
34
34
|
|
|
35
|
+
/** @type {ArrayConstructor} */
|
|
36
|
+
|
|
37
|
+
const Arr = Array;
|
|
38
|
+
|
|
35
39
|
/** @type {string} */
|
|
36
40
|
|
|
37
41
|
const bi = "bigint";
|
|
@@ -144,6 +148,132 @@ const symbol = "symbol";
|
|
|
144
148
|
|
|
145
149
|
const undef = "undefined";
|
|
146
150
|
|
|
151
|
+
/**
|
|
152
|
+
* Runtime-guarded, readonly constructor functions map (Object).
|
|
153
|
+
* Accessing an unknown key throws at runtime.
|
|
154
|
+
*
|
|
155
|
+
* @type {Readonly<{
|
|
156
|
+
* Arr: Array;
|
|
157
|
+
* Array: Array;
|
|
158
|
+
* BI: BigInt;
|
|
159
|
+
* BigInt: BigInt;
|
|
160
|
+
* Bool: Boolean;
|
|
161
|
+
* Boolean: Boolean;
|
|
162
|
+
* Fn: Function;
|
|
163
|
+
* Fun: Function;
|
|
164
|
+
* Func: Function;
|
|
165
|
+
* Function: Function;
|
|
166
|
+
* Num: Number;
|
|
167
|
+
* Number: Number;
|
|
168
|
+
* Obj: Object;
|
|
169
|
+
* Object: Object;
|
|
170
|
+
* Str: String;
|
|
171
|
+
* String: String;
|
|
172
|
+
* Sym: Symbol;
|
|
173
|
+
* Symbol: Symbol;
|
|
174
|
+
* }>}
|
|
175
|
+
*/
|
|
176
|
+
|
|
177
|
+
const C = new Proxy(
|
|
178
|
+
Object.freeze({
|
|
179
|
+
Arr: Array,
|
|
180
|
+
Array: Array,
|
|
181
|
+
BI: BigInt,
|
|
182
|
+
BigInt: BigInt,
|
|
183
|
+
Bool: Boolean,
|
|
184
|
+
Boolean: Boolean,
|
|
185
|
+
Fn: Function,
|
|
186
|
+
Fun: Function,
|
|
187
|
+
Func: Function,
|
|
188
|
+
Function: Function,
|
|
189
|
+
Num: Number,
|
|
190
|
+
Number: Number,
|
|
191
|
+
Obj: Object,
|
|
192
|
+
Object: Object,
|
|
193
|
+
Str: String,
|
|
194
|
+
String: String,
|
|
195
|
+
Sym: Symbol,
|
|
196
|
+
Symbol: Symbol,
|
|
197
|
+
}),
|
|
198
|
+
|
|
199
|
+
{
|
|
200
|
+
get(target, prop, receiver) {
|
|
201
|
+
if (typeof prop !== "string")
|
|
202
|
+
throw new TypeError("operation not allowed");
|
|
203
|
+
|
|
204
|
+
if (prop in target) return Reflect.get(target, prop, receiver);
|
|
205
|
+
|
|
206
|
+
throw new TypeError(`constructor function '${prop}' undefined !`);
|
|
207
|
+
},
|
|
208
|
+
}
|
|
209
|
+
);
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Runtime-guarded, readonly type map (Object).
|
|
213
|
+
* Accessing an unknown key throws at runtime.
|
|
214
|
+
*
|
|
215
|
+
* @type {Readonly<{
|
|
216
|
+
* bi: "bigint";
|
|
217
|
+
* bigint: "bigint";
|
|
218
|
+
* bool: "boolean";
|
|
219
|
+
* boolean: "boolean";
|
|
220
|
+
* fn: "function";
|
|
221
|
+
* fun: "function";
|
|
222
|
+
* func: "function";
|
|
223
|
+
* function: "function";
|
|
224
|
+
* nil: null;
|
|
225
|
+
* none: null;
|
|
226
|
+
* null: null;
|
|
227
|
+
* num: "number";
|
|
228
|
+
* number: "number";
|
|
229
|
+
* obj: "object";
|
|
230
|
+
* object: "object";
|
|
231
|
+
* str: "string";
|
|
232
|
+
* string: "string";
|
|
233
|
+
* sym: "symbol";
|
|
234
|
+
* symbol: "symbol";
|
|
235
|
+
* undef: "undefined";
|
|
236
|
+
* undefined: "undefined";
|
|
237
|
+
* }>}
|
|
238
|
+
*/
|
|
239
|
+
|
|
240
|
+
const T = new Proxy(
|
|
241
|
+
Object.freeze({
|
|
242
|
+
bi: "bigint",
|
|
243
|
+
bigint: "bigint",
|
|
244
|
+
bool: "boolean",
|
|
245
|
+
boolean: "boolean",
|
|
246
|
+
fn: "function",
|
|
247
|
+
fun: "function",
|
|
248
|
+
func: "function",
|
|
249
|
+
function: "function",
|
|
250
|
+
nil: null,
|
|
251
|
+
none: null,
|
|
252
|
+
null: null,
|
|
253
|
+
num: "number",
|
|
254
|
+
number: "number",
|
|
255
|
+
obj: "object",
|
|
256
|
+
object: "object",
|
|
257
|
+
str: "string",
|
|
258
|
+
string: "string",
|
|
259
|
+
sym: "symbol",
|
|
260
|
+
symbol: "symbol",
|
|
261
|
+
undef: "undefined",
|
|
262
|
+
undefined: "undefined",
|
|
263
|
+
}),
|
|
264
|
+
|
|
265
|
+
{
|
|
266
|
+
get(target, prop, receiver) {
|
|
267
|
+
if (typeof prop !== "string")
|
|
268
|
+
throw new TypeError("operation not allowed");
|
|
269
|
+
|
|
270
|
+
if (prop in target) return Reflect.get(target, prop, receiver);
|
|
271
|
+
|
|
272
|
+
throw new TypeError(`type '${prop}' undefined !`);
|
|
273
|
+
},
|
|
274
|
+
}
|
|
275
|
+
);
|
|
276
|
+
|
|
147
277
|
/**
|
|
148
278
|
* Compares a value or a list of values against a given type (or list of types),
|
|
149
279
|
* similar to using `typeof x === y`, but in a more flexible and readable way.
|
|
@@ -162,7 +292,7 @@ const undef = "undefined";
|
|
|
162
292
|
function checkType(data, dataType, logic, match, strict) {
|
|
163
293
|
/** @type {Set<string | null | undefined>} */
|
|
164
294
|
|
|
165
|
-
|
|
295
|
+
const setOfTypes = new Set([
|
|
166
296
|
and,
|
|
167
297
|
bi,
|
|
168
298
|
bool,
|
|
@@ -178,7 +308,8 @@ function checkType(data, dataType, logic, match, strict) {
|
|
|
178
308
|
]);
|
|
179
309
|
|
|
180
310
|
if (!(dataType instanceof Array)) {
|
|
181
|
-
if (!setOfTypes.has(dataType))
|
|
311
|
+
if (!setOfTypes.has(dataType))
|
|
312
|
+
throw new TypeError(`type '${String(dataType)}' undefined !`);
|
|
182
313
|
|
|
183
314
|
if (!(data instanceof Array)) {
|
|
184
315
|
if (data === null) return dataType === null;
|
|
@@ -252,7 +383,7 @@ function checkType(data, dataType, logic, match, strict) {
|
|
|
252
383
|
// 1 step directly in all cases
|
|
253
384
|
|
|
254
385
|
if (!setOfTypes.has(dtype))
|
|
255
|
-
throw new TypeError(`type ${dtype} undefined !`);
|
|
386
|
+
throw new TypeError(`type '${String(dtype)}' undefined !`);
|
|
256
387
|
|
|
257
388
|
if (data === null) {
|
|
258
389
|
if (dtype === null) return true;
|
|
@@ -284,7 +415,7 @@ function checkType(data, dataType, logic, match, strict) {
|
|
|
284
415
|
outerLoop: for (let dt of data) {
|
|
285
416
|
for (let dtype of dataType) {
|
|
286
417
|
if (!setOfTypes.has(dtype))
|
|
287
|
-
throw new TypeError(`type ${dtype} undefined !`);
|
|
418
|
+
throw new TypeError(`type '${String(dtype)}' undefined !`);
|
|
288
419
|
|
|
289
420
|
if (dt === null) {
|
|
290
421
|
if (dtype === null) continue outerLoop;
|
|
@@ -330,7 +461,7 @@ function checkType(data, dataType, logic, match, strict) {
|
|
|
330
461
|
// // // the same as this
|
|
331
462
|
|
|
332
463
|
// // // if (!setOfTypes.has(dtype))
|
|
333
|
-
// // // throw new TypeError(`type ${dtype} undefined !`);
|
|
464
|
+
// // // throw new TypeError(`type '${String(dtype)}' undefined !`);
|
|
334
465
|
|
|
335
466
|
// // // if (data[0] === null) {
|
|
336
467
|
// // // if (dtype === null) return true;
|
|
@@ -371,7 +502,7 @@ function checkType(data, dataType, logic, match, strict) {
|
|
|
371
502
|
// // // the same as this
|
|
372
503
|
|
|
373
504
|
// // // if (!setOfTypes.has(dataType[0]))
|
|
374
|
-
// // // throw new TypeError(
|
|
505
|
+
// // // throw new TypeError(`type '${String(dataType[0])}' undefined !`);
|
|
375
506
|
|
|
376
507
|
// // // if (dt === null) {
|
|
377
508
|
// // // if (dataType[0] !== null) return false;
|
|
@@ -438,7 +569,7 @@ function checkType(data, dataType, logic, match, strict) {
|
|
|
438
569
|
for (let dt of data) {
|
|
439
570
|
for (let dtype of dataType) {
|
|
440
571
|
if (!setOfTypes.has(dtype))
|
|
441
|
-
throw new TypeError(`type ${dtype} undefined !`);
|
|
572
|
+
throw new TypeError(`type '${String(dtype)}' undefined !`);
|
|
442
573
|
|
|
443
574
|
if (dt === null) {
|
|
444
575
|
if (dtype === null) return true;
|
|
@@ -480,7 +611,7 @@ function checkType(data, dataType, logic, match, strict) {
|
|
|
480
611
|
// // // the same as this
|
|
481
612
|
|
|
482
613
|
// // // if (!setOfTypes.has(dtype))
|
|
483
|
-
// // // throw new TypeError(`type ${dtype} undefined !`);
|
|
614
|
+
// // // throw new TypeError(`type '${String(dtype)}' undefined !`);
|
|
484
615
|
|
|
485
616
|
// // // if (data[0] === null) {
|
|
486
617
|
// // // if (dtype === null) return true;
|
|
@@ -523,7 +654,7 @@ function checkType(data, dataType, logic, match, strict) {
|
|
|
523
654
|
// // // the same as this
|
|
524
655
|
|
|
525
656
|
// // // if (!setOfTypes.has(dataType[0]))
|
|
526
|
-
// // // throw new TypeError(
|
|
657
|
+
// // // throw new TypeError(`type '${dataType[0]}' undefined !`);
|
|
527
658
|
|
|
528
659
|
// // // if (dt === null) {
|
|
529
660
|
// // // if (dataType[0] === null) return true;
|
|
@@ -642,7 +773,7 @@ function checkType(data, dataType, logic, match, strict) {
|
|
|
642
773
|
// 1 step directly in all cases
|
|
643
774
|
|
|
644
775
|
if (!setOfTypes.has(dataType[i]))
|
|
645
|
-
throw new TypeError(`type ${dataType[i]} undefined !`);
|
|
776
|
+
throw new TypeError(`type '${String(dataType[i])}' undefined !`);
|
|
646
777
|
|
|
647
778
|
if (data[i] === null) {
|
|
648
779
|
if (dataType[i] !== null) return false;
|
|
@@ -675,7 +806,7 @@ function checkType(data, dataType, logic, match, strict) {
|
|
|
675
806
|
// 1 step directly in all cases
|
|
676
807
|
|
|
677
808
|
if (!setOfTypes.has(dataType[i]))
|
|
678
|
-
throw new TypeError(`type ${dataType[i]} undefined !`);
|
|
809
|
+
throw new TypeError(`type '${String(dataType[i])}' undefined !`);
|
|
679
810
|
|
|
680
811
|
if (data[i] === null) {
|
|
681
812
|
if (dataType[i] === null) return true;
|
|
@@ -710,7 +841,7 @@ function checkType(data, dataType, logic, match, strict) {
|
|
|
710
841
|
// 1 step directly in all cases
|
|
711
842
|
|
|
712
843
|
if (!setOfTypes.has(dataType[i]))
|
|
713
|
-
throw new TypeError(`type ${dataType[i]} undefined !`);
|
|
844
|
+
throw new TypeError(`type '${String(dataType[i])}' undefined !`);
|
|
714
845
|
|
|
715
846
|
if (data[i] === null) {
|
|
716
847
|
if (dataType[i] !== null) return false;
|
|
@@ -742,7 +873,7 @@ function checkType(data, dataType, logic, match, strict) {
|
|
|
742
873
|
// 1 step directly in all cases
|
|
743
874
|
|
|
744
875
|
if (!setOfTypes.has(dataType[i]))
|
|
745
|
-
throw new TypeError(`type ${dataType[i]} undefined !`);
|
|
876
|
+
throw new TypeError(`type '${String(dataType[i])}' undefined !`);
|
|
746
877
|
|
|
747
878
|
if (data[i] === null) {
|
|
748
879
|
if (dataType[i] === null) return true;
|
|
@@ -798,7 +929,7 @@ function isinstancesof(objs, type, logic, match, strict) {
|
|
|
798
929
|
// return objs instanceof type;
|
|
799
930
|
|
|
800
931
|
throw new TypeError(
|
|
801
|
-
`instancesof expected at least : 2 objects as first argument 'objs' or 2
|
|
932
|
+
`instancesof expected at least : 2 objects as first argument 'objs' or 2 constructor functions as second argument 'type', got 1`
|
|
802
933
|
);
|
|
803
934
|
|
|
804
935
|
// comment this condition to enable the 1/4 of (one to one)
|
|
@@ -816,7 +947,7 @@ function isinstancesof(objs, type, logic, match, strict) {
|
|
|
816
947
|
if (!(obj instanceof type)) return false;
|
|
817
948
|
} catch (error) {
|
|
818
949
|
throw new TypeError(
|
|
819
|
-
"second argument of 'instancesof' is not a
|
|
950
|
+
"second argument of 'instancesof' is not a constructor function or array of constructor functions"
|
|
820
951
|
);
|
|
821
952
|
}
|
|
822
953
|
|
|
@@ -837,7 +968,7 @@ function isinstancesof(objs, type, logic, match, strict) {
|
|
|
837
968
|
if (obj instanceof type) return true;
|
|
838
969
|
} catch (error) {
|
|
839
970
|
throw new TypeError(
|
|
840
|
-
"second argument of 'instancesof' is not a
|
|
971
|
+
"second argument of 'instancesof' is not a constructor function or array of constructor functions"
|
|
841
972
|
);
|
|
842
973
|
}
|
|
843
974
|
|
|
@@ -858,7 +989,7 @@ function isinstancesof(objs, type, logic, match, strict) {
|
|
|
858
989
|
|
|
859
990
|
if (type.length < 2)
|
|
860
991
|
throw new TypeError(
|
|
861
|
-
`instancesof expected at least 2
|
|
992
|
+
`instancesof expected at least 2 constructor functions as second argument 'type', got 1`
|
|
862
993
|
);
|
|
863
994
|
|
|
864
995
|
for (let dt of type) {
|
|
@@ -868,7 +999,7 @@ function isinstancesof(objs, type, logic, match, strict) {
|
|
|
868
999
|
if (objs instanceof dt) return true;
|
|
869
1000
|
} catch (error) {
|
|
870
1001
|
throw new TypeError(
|
|
871
|
-
"second argument of 'instancesof' is not a
|
|
1002
|
+
"second argument of 'instancesof' is not a constructor function or array of constructor functions"
|
|
872
1003
|
);
|
|
873
1004
|
}
|
|
874
1005
|
|
|
@@ -885,7 +1016,7 @@ function isinstancesof(objs, type, logic, match, strict) {
|
|
|
885
1016
|
|
|
886
1017
|
if (objs.length < 2 && type.length < 2)
|
|
887
1018
|
throw new TypeError(
|
|
888
|
-
`instancesof expected at least : 2 objects as first argument 'objs' or 2
|
|
1019
|
+
`instancesof expected at least : 2 objects as first argument 'objs' or 2 constructor functions as second argument 'type', got 1`
|
|
889
1020
|
);
|
|
890
1021
|
|
|
891
1022
|
if (not(match)) {
|
|
@@ -898,7 +1029,7 @@ function isinstancesof(objs, type, logic, match, strict) {
|
|
|
898
1029
|
if (obj instanceof dt) continue outerLoop;
|
|
899
1030
|
} catch (error) {
|
|
900
1031
|
throw new TypeError(
|
|
901
|
-
"second argument of 'instancesof' is not a
|
|
1032
|
+
"second argument of 'instancesof' is not a constructor function or array of constructor functions"
|
|
902
1033
|
);
|
|
903
1034
|
}
|
|
904
1035
|
}
|
|
@@ -1013,7 +1144,7 @@ function isinstancesof(objs, type, logic, match, strict) {
|
|
|
1013
1144
|
if (obj instanceof dt) return true;
|
|
1014
1145
|
} catch (error) {
|
|
1015
1146
|
throw new TypeError(
|
|
1016
|
-
"second argument of 'instancesof' is not a
|
|
1147
|
+
"second argument of 'instancesof' is not a constructor function or array of constructor functions"
|
|
1017
1148
|
);
|
|
1018
1149
|
}
|
|
1019
1150
|
}
|
|
@@ -1157,7 +1288,7 @@ function isinstancesof(objs, type, logic, match, strict) {
|
|
|
1157
1288
|
if (objs.length !== type.length) {
|
|
1158
1289
|
if (strict)
|
|
1159
1290
|
throw new TypeError(
|
|
1160
|
-
"length of
|
|
1291
|
+
"length of constructor functions array must match the number of objects"
|
|
1161
1292
|
);
|
|
1162
1293
|
|
|
1163
1294
|
/** @type {number} */
|
|
@@ -1178,7 +1309,7 @@ function isinstancesof(objs, type, logic, match, strict) {
|
|
|
1178
1309
|
if (!(objs[i] instanceof type[i])) return false;
|
|
1179
1310
|
} catch (error) {
|
|
1180
1311
|
throw new TypeError(
|
|
1181
|
-
"second argument of 'instancesof' is not a
|
|
1312
|
+
"second argument of 'instancesof' is not a constructor function or array of constructor functions"
|
|
1182
1313
|
);
|
|
1183
1314
|
}
|
|
1184
1315
|
|
|
@@ -1202,7 +1333,7 @@ function isinstancesof(objs, type, logic, match, strict) {
|
|
|
1202
1333
|
if (objs[i] instanceof type[i]) return true;
|
|
1203
1334
|
} catch (error) {
|
|
1204
1335
|
throw new TypeError(
|
|
1205
|
-
"second argument of 'instancesof' is not a
|
|
1336
|
+
"second argument of 'instancesof' is not a constructor function or array of constructor functions"
|
|
1206
1337
|
);
|
|
1207
1338
|
}
|
|
1208
1339
|
|
|
@@ -1228,7 +1359,7 @@ function isinstancesof(objs, type, logic, match, strict) {
|
|
|
1228
1359
|
if (!(objs[i] instanceof type[i])) return false;
|
|
1229
1360
|
} catch (error) {
|
|
1230
1361
|
throw new TypeError(
|
|
1231
|
-
"second argument of 'instancesof' is not a
|
|
1362
|
+
"second argument of 'instancesof' is not a constructor function or array of constructor functions"
|
|
1232
1363
|
);
|
|
1233
1364
|
}
|
|
1234
1365
|
|
|
@@ -1251,7 +1382,7 @@ function isinstancesof(objs, type, logic, match, strict) {
|
|
|
1251
1382
|
if (objs[i] instanceof type[i]) return true;
|
|
1252
1383
|
} catch (error) {
|
|
1253
1384
|
throw new TypeError(
|
|
1254
|
-
"second argument of 'instancesof' is not a
|
|
1385
|
+
"second argument of 'instancesof' is not a constructor function or array of constructor functions"
|
|
1255
1386
|
);
|
|
1256
1387
|
}
|
|
1257
1388
|
|
|
@@ -1307,7 +1438,7 @@ function instancesof(
|
|
|
1307
1438
|
) {
|
|
1308
1439
|
/** @type {number} */
|
|
1309
1440
|
|
|
1310
|
-
|
|
1441
|
+
const len = arguments.length;
|
|
1311
1442
|
|
|
1312
1443
|
// ================================================================
|
|
1313
1444
|
|
|
@@ -1409,7 +1540,7 @@ function isType(
|
|
|
1409
1540
|
) {
|
|
1410
1541
|
/** @type {number} */
|
|
1411
1542
|
|
|
1412
|
-
|
|
1543
|
+
const len = arguments.length;
|
|
1413
1544
|
|
|
1414
1545
|
// ================================================================
|
|
1415
1546
|
|
|
@@ -1481,7 +1612,7 @@ function lengths(...objs) {
|
|
|
1481
1612
|
|
|
1482
1613
|
/** @type {Array<number>} */
|
|
1483
1614
|
|
|
1484
|
-
|
|
1615
|
+
const lens = [objs[0].length];
|
|
1485
1616
|
|
|
1486
1617
|
/** @type {boolean} */
|
|
1487
1618
|
|
|
@@ -1546,14 +1677,18 @@ function log(...data) {
|
|
|
1546
1677
|
function not(obj) {
|
|
1547
1678
|
/** @type {number} */
|
|
1548
1679
|
|
|
1549
|
-
|
|
1680
|
+
const len = arguments.length;
|
|
1550
1681
|
|
|
1551
1682
|
if (len !== 1)
|
|
1552
1683
|
throw new TypeError(`not() takes exactly one argument (${len} given)`);
|
|
1553
1684
|
|
|
1554
|
-
|
|
1685
|
+
/** @type {*} */
|
|
1686
|
+
|
|
1687
|
+
const val = obj?.valueOf();
|
|
1688
|
+
|
|
1689
|
+
if (!(val instanceof Object) || val instanceof Function) return !val;
|
|
1555
1690
|
|
|
1556
|
-
return
|
|
1691
|
+
return val.length === 0 || !val; // handling the true object case
|
|
1557
1692
|
}
|
|
1558
1693
|
|
|
1559
1694
|
/**
|
|
@@ -1579,7 +1714,7 @@ function print(...data) {
|
|
|
1579
1714
|
function printf(data) {
|
|
1580
1715
|
if (arguments.length > 1) throw new TypeError('unexpected token ","');
|
|
1581
1716
|
|
|
1582
|
-
process.stdout.write(`${data}`);
|
|
1717
|
+
process.stdout.write(`${String(data)}`);
|
|
1583
1718
|
}
|
|
1584
1719
|
|
|
1585
1720
|
/**
|
|
@@ -1741,7 +1876,7 @@ function typesof(...objs) {
|
|
|
1741
1876
|
|
|
1742
1877
|
/** @type {Array<string>} */
|
|
1743
1878
|
|
|
1744
|
-
|
|
1879
|
+
const types = [typeof objs[0]];
|
|
1745
1880
|
|
|
1746
1881
|
/** @type {boolean} */
|
|
1747
1882
|
|
|
@@ -1823,15 +1958,15 @@ function main() {
|
|
|
1823
1958
|
|
|
1824
1959
|
/** @type {Number} */
|
|
1825
1960
|
|
|
1826
|
-
|
|
1961
|
+
const a = new Number(3);
|
|
1827
1962
|
|
|
1828
1963
|
/** @type {Number} */
|
|
1829
1964
|
|
|
1830
|
-
|
|
1965
|
+
const b = new Number(7);
|
|
1831
1966
|
|
|
1832
1967
|
/** @type {String} */
|
|
1833
1968
|
|
|
1834
|
-
|
|
1969
|
+
const c = new String("test");
|
|
1835
1970
|
|
|
1836
1971
|
// one to one (disabled)
|
|
1837
1972
|
|
|
@@ -1897,15 +2032,15 @@ function main() {
|
|
|
1897
2032
|
|
|
1898
2033
|
/** @type {number} */
|
|
1899
2034
|
|
|
1900
|
-
|
|
2035
|
+
const x = 3; // or Number(3)
|
|
1901
2036
|
|
|
1902
2037
|
/** @type {number} */
|
|
1903
2038
|
|
|
1904
|
-
|
|
2039
|
+
const y = 7; // or Number(7)
|
|
1905
2040
|
|
|
1906
2041
|
/** @type {number} */
|
|
1907
2042
|
|
|
1908
|
-
|
|
2043
|
+
const z = "test"; // or String("test")
|
|
1909
2044
|
|
|
1910
2045
|
// one to one
|
|
1911
2046
|
|
|
@@ -1977,7 +2112,7 @@ function main() {
|
|
|
1977
2112
|
// two args +
|
|
1978
2113
|
|
|
1979
2114
|
console.log(lengths([1, 2, 3], [1, 2, 3])); // [ 3, true ]
|
|
1980
|
-
console.log(lengths([1, 2, 3], "
|
|
2115
|
+
console.log(lengths([1, 2, 3], "test")); // [ [3, 4], false ]
|
|
1981
2116
|
|
|
1982
2117
|
console.log("--------------!--------------");
|
|
1983
2118
|
|
|
@@ -2029,7 +2164,7 @@ function main() {
|
|
|
2029
2164
|
// two args +
|
|
2030
2165
|
|
|
2031
2166
|
console.log(typesof(1, 2)); // [ number, true ]
|
|
2032
|
-
console.log(typesof(1, "
|
|
2167
|
+
console.log(typesof(1, "test")); // [ [number, string], false ]
|
|
2033
2168
|
|
|
2034
2169
|
console.log("------------------------------");
|
|
2035
2170
|
|
|
@@ -2048,12 +2183,14 @@ if (require.main === module) {
|
|
|
2048
2183
|
|
|
2049
2184
|
module.exports = {
|
|
2050
2185
|
and,
|
|
2186
|
+
Arr,
|
|
2051
2187
|
bi,
|
|
2052
2188
|
BI,
|
|
2053
2189
|
bigint,
|
|
2054
2190
|
bool,
|
|
2055
2191
|
Bool,
|
|
2056
2192
|
boolean,
|
|
2193
|
+
C,
|
|
2057
2194
|
fn,
|
|
2058
2195
|
Fn,
|
|
2059
2196
|
fun,
|
|
@@ -2075,6 +2212,7 @@ module.exports = {
|
|
|
2075
2212
|
sym,
|
|
2076
2213
|
Sym,
|
|
2077
2214
|
symbol,
|
|
2215
|
+
T,
|
|
2078
2216
|
undef,
|
|
2079
2217
|
|
|
2080
2218
|
echo,
|
package/jsfunx.mjs
CHANGED
|
@@ -36,6 +36,10 @@ import { fileURLToPath } from "url";
|
|
|
36
36
|
|
|
37
37
|
export const and = "and";
|
|
38
38
|
|
|
39
|
+
/** @type {ArrayConstructor} */
|
|
40
|
+
|
|
41
|
+
export const Arr = Array;
|
|
42
|
+
|
|
39
43
|
/** @type {string} */
|
|
40
44
|
|
|
41
45
|
export const bi = "bigint";
|
|
@@ -148,6 +152,132 @@ export const symbol = "symbol";
|
|
|
148
152
|
|
|
149
153
|
export const undef = "undefined";
|
|
150
154
|
|
|
155
|
+
/**
|
|
156
|
+
* Runtime-guarded, readonly constructor functions map (Object).
|
|
157
|
+
* Accessing an unknown key throws at runtime.
|
|
158
|
+
*
|
|
159
|
+
* @type {Readonly<{
|
|
160
|
+
* Arr: Array;
|
|
161
|
+
* Array: Array;
|
|
162
|
+
* BI: BigInt;
|
|
163
|
+
* BigInt: BigInt;
|
|
164
|
+
* Bool: Boolean;
|
|
165
|
+
* Boolean: Boolean;
|
|
166
|
+
* Fn: Function;
|
|
167
|
+
* Fun: Function;
|
|
168
|
+
* Func: Function;
|
|
169
|
+
* Function: Function;
|
|
170
|
+
* Num: Number;
|
|
171
|
+
* Number: Number;
|
|
172
|
+
* Obj: Object;
|
|
173
|
+
* Object: Object;
|
|
174
|
+
* Str: String;
|
|
175
|
+
* String: String;
|
|
176
|
+
* Sym: Symbol;
|
|
177
|
+
* Symbol: Symbol;
|
|
178
|
+
* }>}
|
|
179
|
+
*/
|
|
180
|
+
|
|
181
|
+
export const C = new Proxy(
|
|
182
|
+
Object.freeze({
|
|
183
|
+
Arr: Array,
|
|
184
|
+
Array: Array,
|
|
185
|
+
BI: BigInt,
|
|
186
|
+
BigInt: BigInt,
|
|
187
|
+
Bool: Boolean,
|
|
188
|
+
Boolean: Boolean,
|
|
189
|
+
Fn: Function,
|
|
190
|
+
Fun: Function,
|
|
191
|
+
Func: Function,
|
|
192
|
+
Function: Function,
|
|
193
|
+
Num: Number,
|
|
194
|
+
Number: Number,
|
|
195
|
+
Obj: Object,
|
|
196
|
+
Object: Object,
|
|
197
|
+
Str: String,
|
|
198
|
+
String: String,
|
|
199
|
+
Sym: Symbol,
|
|
200
|
+
Symbol: Symbol,
|
|
201
|
+
}),
|
|
202
|
+
|
|
203
|
+
{
|
|
204
|
+
get(target, prop, receiver) {
|
|
205
|
+
if (typeof prop !== "string")
|
|
206
|
+
throw new TypeError("operation not allowed");
|
|
207
|
+
|
|
208
|
+
if (prop in target) return Reflect.get(target, prop, receiver);
|
|
209
|
+
|
|
210
|
+
throw new TypeError(`constructor function '${prop}' undefined !`);
|
|
211
|
+
},
|
|
212
|
+
}
|
|
213
|
+
);
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Runtime-guarded, readonly type map (Object).
|
|
217
|
+
* Accessing an unknown key throws at runtime.
|
|
218
|
+
*
|
|
219
|
+
* @type {Readonly<{
|
|
220
|
+
* bi: "bigint";
|
|
221
|
+
* bigint: "bigint";
|
|
222
|
+
* bool: "boolean";
|
|
223
|
+
* boolean: "boolean";
|
|
224
|
+
* fn: "function";
|
|
225
|
+
* fun: "function";
|
|
226
|
+
* func: "function";
|
|
227
|
+
* function: "function";
|
|
228
|
+
* nil: null;
|
|
229
|
+
* none: null;
|
|
230
|
+
* null: null;
|
|
231
|
+
* num: "number";
|
|
232
|
+
* number: "number";
|
|
233
|
+
* obj: "object";
|
|
234
|
+
* object: "object";
|
|
235
|
+
* str: "string";
|
|
236
|
+
* string: "string";
|
|
237
|
+
* sym: "symbol";
|
|
238
|
+
* symbol: "symbol";
|
|
239
|
+
* undef: "undefined";
|
|
240
|
+
* undefined: "undefined";
|
|
241
|
+
* }>}
|
|
242
|
+
*/
|
|
243
|
+
|
|
244
|
+
export const T = new Proxy(
|
|
245
|
+
Object.freeze({
|
|
246
|
+
bi: "bigint",
|
|
247
|
+
bigint: "bigint",
|
|
248
|
+
bool: "boolean",
|
|
249
|
+
boolean: "boolean",
|
|
250
|
+
fn: "function",
|
|
251
|
+
fun: "function",
|
|
252
|
+
func: "function",
|
|
253
|
+
function: "function",
|
|
254
|
+
nil: null,
|
|
255
|
+
none: null,
|
|
256
|
+
null: null,
|
|
257
|
+
num: "number",
|
|
258
|
+
number: "number",
|
|
259
|
+
obj: "object",
|
|
260
|
+
object: "object",
|
|
261
|
+
str: "string",
|
|
262
|
+
string: "string",
|
|
263
|
+
sym: "symbol",
|
|
264
|
+
symbol: "symbol",
|
|
265
|
+
undef: "undefined",
|
|
266
|
+
undefined: "undefined",
|
|
267
|
+
}),
|
|
268
|
+
|
|
269
|
+
{
|
|
270
|
+
get(target, prop, receiver) {
|
|
271
|
+
if (typeof prop !== "string")
|
|
272
|
+
throw new TypeError("operation not allowed");
|
|
273
|
+
|
|
274
|
+
if (prop in target) return Reflect.get(target, prop, receiver);
|
|
275
|
+
|
|
276
|
+
throw new TypeError(`type '${prop}' undefined !`);
|
|
277
|
+
},
|
|
278
|
+
}
|
|
279
|
+
);
|
|
280
|
+
|
|
151
281
|
/**
|
|
152
282
|
* Compares a value or a list of values against a given type (or list of types),
|
|
153
283
|
* similar to using `typeof x === y`, but in a more flexible and readable way.
|
|
@@ -166,7 +296,7 @@ export const undef = "undefined";
|
|
|
166
296
|
function checkType(data, dataType, logic, match, strict) {
|
|
167
297
|
/** @type {Set<string | null | undefined>} */
|
|
168
298
|
|
|
169
|
-
|
|
299
|
+
const setOfTypes = new Set([
|
|
170
300
|
and,
|
|
171
301
|
bi,
|
|
172
302
|
bool,
|
|
@@ -182,7 +312,8 @@ function checkType(data, dataType, logic, match, strict) {
|
|
|
182
312
|
]);
|
|
183
313
|
|
|
184
314
|
if (!(dataType instanceof Array)) {
|
|
185
|
-
if (!setOfTypes.has(dataType))
|
|
315
|
+
if (!setOfTypes.has(dataType))
|
|
316
|
+
throw new TypeError(`type '${String(dataType)}' undefined !`);
|
|
186
317
|
|
|
187
318
|
if (!(data instanceof Array)) {
|
|
188
319
|
if (data === null) return dataType === null;
|
|
@@ -256,7 +387,7 @@ function checkType(data, dataType, logic, match, strict) {
|
|
|
256
387
|
// 1 step directly in all cases
|
|
257
388
|
|
|
258
389
|
if (!setOfTypes.has(dtype))
|
|
259
|
-
throw new TypeError(`type ${dtype} undefined !`);
|
|
390
|
+
throw new TypeError(`type '${String(dtype)}' undefined !`);
|
|
260
391
|
|
|
261
392
|
if (data === null) {
|
|
262
393
|
if (dtype === null) return true;
|
|
@@ -288,7 +419,7 @@ function checkType(data, dataType, logic, match, strict) {
|
|
|
288
419
|
outerLoop: for (let dt of data) {
|
|
289
420
|
for (let dtype of dataType) {
|
|
290
421
|
if (!setOfTypes.has(dtype))
|
|
291
|
-
throw new TypeError(`type ${dtype} undefined !`);
|
|
422
|
+
throw new TypeError(`type '${String(dtype)}' undefined !`);
|
|
292
423
|
|
|
293
424
|
if (dt === null) {
|
|
294
425
|
if (dtype === null) continue outerLoop;
|
|
@@ -334,7 +465,7 @@ function checkType(data, dataType, logic, match, strict) {
|
|
|
334
465
|
// // // the same as this
|
|
335
466
|
|
|
336
467
|
// // if (!setOfTypes.has(dtype))
|
|
337
|
-
// // throw new TypeError(`type ${dtype} undefined !`);
|
|
468
|
+
// // throw new TypeError(`type '${String(dtype)}' undefined !`);
|
|
338
469
|
|
|
339
470
|
// // if (data[0] === null) {
|
|
340
471
|
// // if (dtype === null) return true;
|
|
@@ -375,7 +506,7 @@ function checkType(data, dataType, logic, match, strict) {
|
|
|
375
506
|
// // // the same as this
|
|
376
507
|
|
|
377
508
|
// // // if (!setOfTypes.has(dataType[0]))
|
|
378
|
-
// // // throw new TypeError(
|
|
509
|
+
// // // throw new TypeError(`type '${String(dataType[0])}' undefined !`);
|
|
379
510
|
|
|
380
511
|
// // // if (dt === null) {
|
|
381
512
|
// // // if (dataType[0] !== null) return false;
|
|
@@ -442,7 +573,7 @@ function checkType(data, dataType, logic, match, strict) {
|
|
|
442
573
|
for (let dt of data) {
|
|
443
574
|
for (let dtype of dataType) {
|
|
444
575
|
if (!setOfTypes.has(dtype))
|
|
445
|
-
throw new TypeError(`type ${dtype} undefined !`);
|
|
576
|
+
throw new TypeError(`type '${String(dtype)}' undefined !`);
|
|
446
577
|
|
|
447
578
|
if (dt === null) {
|
|
448
579
|
if (dtype === null) return true;
|
|
@@ -484,7 +615,7 @@ function checkType(data, dataType, logic, match, strict) {
|
|
|
484
615
|
// // // the same as this
|
|
485
616
|
|
|
486
617
|
// // // if (!setOfTypes.has(dtype))
|
|
487
|
-
// // // throw new TypeError(`type ${dtype} undefined !`);
|
|
618
|
+
// // // throw new TypeError(`type '${String(dtype)}' undefined !`);
|
|
488
619
|
|
|
489
620
|
// // // if (data[0] === null) {
|
|
490
621
|
// // // if (dtype === null) return true;
|
|
@@ -527,7 +658,7 @@ function checkType(data, dataType, logic, match, strict) {
|
|
|
527
658
|
// // // the same as this
|
|
528
659
|
|
|
529
660
|
// // // if (!setOfTypes.has(dataType[0]))
|
|
530
|
-
// // // throw new TypeError(
|
|
661
|
+
// // // throw new TypeError(`type '${String(dataType[0])}' undefined !`);
|
|
531
662
|
|
|
532
663
|
// // // if (dt === null) {
|
|
533
664
|
// // // if (dataType[0] === null) return true;
|
|
@@ -646,7 +777,7 @@ function checkType(data, dataType, logic, match, strict) {
|
|
|
646
777
|
// 1 step directly in all cases
|
|
647
778
|
|
|
648
779
|
if (!setOfTypes.has(dataType[i]))
|
|
649
|
-
throw new TypeError(`type ${dataType[i]} undefined !`);
|
|
780
|
+
throw new TypeError(`type '${String(dataType[i])}' undefined !`);
|
|
650
781
|
|
|
651
782
|
if (data[i] === null) {
|
|
652
783
|
if (dataType[i] !== null) return false;
|
|
@@ -679,7 +810,7 @@ function checkType(data, dataType, logic, match, strict) {
|
|
|
679
810
|
// 1 step directly in all cases
|
|
680
811
|
|
|
681
812
|
if (!setOfTypes.has(dataType[i]))
|
|
682
|
-
throw new TypeError(`type ${dataType[i]} undefined !`);
|
|
813
|
+
throw new TypeError(`type '${String(dataType[i])}' undefined !`);
|
|
683
814
|
|
|
684
815
|
if (data[i] === null) {
|
|
685
816
|
if (dataType[i] === null) return true;
|
|
@@ -714,7 +845,7 @@ function checkType(data, dataType, logic, match, strict) {
|
|
|
714
845
|
// 1 step directly in all cases
|
|
715
846
|
|
|
716
847
|
if (!setOfTypes.has(dataType[i]))
|
|
717
|
-
throw new TypeError(`type ${dataType[i]} undefined !`);
|
|
848
|
+
throw new TypeError(`type '${String(dataType[i])}' undefined !`);
|
|
718
849
|
|
|
719
850
|
if (data[i] === null) {
|
|
720
851
|
if (dataType[i] !== null) return false;
|
|
@@ -746,7 +877,7 @@ function checkType(data, dataType, logic, match, strict) {
|
|
|
746
877
|
// 1 step directly in all cases
|
|
747
878
|
|
|
748
879
|
if (!setOfTypes.has(dataType[i]))
|
|
749
|
-
throw new TypeError(`type ${dataType[i]} undefined !`);
|
|
880
|
+
throw new TypeError(`type '${String(dataType[i])}' undefined !`);
|
|
750
881
|
|
|
751
882
|
if (data[i] === null) {
|
|
752
883
|
if (dataType[i] === null) return true;
|
|
@@ -802,7 +933,7 @@ function isinstancesof(objs, type, logic, match, strict) {
|
|
|
802
933
|
// return objs instanceof type;
|
|
803
934
|
|
|
804
935
|
throw new TypeError(
|
|
805
|
-
`instancesof expected at least : 2 objects as first argument 'objs' or 2
|
|
936
|
+
`instancesof expected at least : 2 objects as first argument 'objs' or 2 constructor functions as second argument 'type', got 1`
|
|
806
937
|
);
|
|
807
938
|
|
|
808
939
|
// comment this condition to enable the 1/4 of (one to one)
|
|
@@ -820,7 +951,7 @@ function isinstancesof(objs, type, logic, match, strict) {
|
|
|
820
951
|
if (!(obj instanceof type)) return false;
|
|
821
952
|
} catch (error) {
|
|
822
953
|
throw new TypeError(
|
|
823
|
-
"second argument of 'instancesof' is not a
|
|
954
|
+
"second argument of 'instancesof' is not a constructor function or array of constructor functions"
|
|
824
955
|
);
|
|
825
956
|
}
|
|
826
957
|
|
|
@@ -841,7 +972,7 @@ function isinstancesof(objs, type, logic, match, strict) {
|
|
|
841
972
|
if (obj instanceof type) return true;
|
|
842
973
|
} catch (error) {
|
|
843
974
|
throw new TypeError(
|
|
844
|
-
"second argument of 'instancesof' is not a
|
|
975
|
+
"second argument of 'instancesof' is not a constructor function or array of constructor functions"
|
|
845
976
|
);
|
|
846
977
|
}
|
|
847
978
|
|
|
@@ -862,7 +993,7 @@ function isinstancesof(objs, type, logic, match, strict) {
|
|
|
862
993
|
|
|
863
994
|
if (type.length < 2)
|
|
864
995
|
throw new TypeError(
|
|
865
|
-
`instancesof expected at least 2
|
|
996
|
+
`instancesof expected at least 2 constructor functions as second argument 'type', got 1`
|
|
866
997
|
);
|
|
867
998
|
|
|
868
999
|
for (let dt of type) {
|
|
@@ -872,7 +1003,7 @@ function isinstancesof(objs, type, logic, match, strict) {
|
|
|
872
1003
|
if (objs instanceof dt) return true;
|
|
873
1004
|
} catch (error) {
|
|
874
1005
|
throw new TypeError(
|
|
875
|
-
"second argument of 'instancesof' is not a
|
|
1006
|
+
"second argument of 'instancesof' is not a constructor function or array of constructor functions"
|
|
876
1007
|
);
|
|
877
1008
|
}
|
|
878
1009
|
|
|
@@ -889,7 +1020,7 @@ function isinstancesof(objs, type, logic, match, strict) {
|
|
|
889
1020
|
|
|
890
1021
|
if (objs.length < 2 && type.length < 2)
|
|
891
1022
|
throw new TypeError(
|
|
892
|
-
`instancesof expected at least : 2 objects as first argument 'objs' or 2
|
|
1023
|
+
`instancesof expected at least : 2 objects as first argument 'objs' or 2 constructor functions as second argument 'type', got 1`
|
|
893
1024
|
);
|
|
894
1025
|
|
|
895
1026
|
if (not(match)) {
|
|
@@ -902,7 +1033,7 @@ function isinstancesof(objs, type, logic, match, strict) {
|
|
|
902
1033
|
if (obj instanceof dt) continue outerLoop;
|
|
903
1034
|
} catch (error) {
|
|
904
1035
|
throw new TypeError(
|
|
905
|
-
"second argument of 'instancesof' is not a
|
|
1036
|
+
"second argument of 'instancesof' is not a constructor function or array of constructor functions"
|
|
906
1037
|
);
|
|
907
1038
|
}
|
|
908
1039
|
}
|
|
@@ -1017,7 +1148,7 @@ function isinstancesof(objs, type, logic, match, strict) {
|
|
|
1017
1148
|
if (obj instanceof dt) return true;
|
|
1018
1149
|
} catch (error) {
|
|
1019
1150
|
throw new TypeError(
|
|
1020
|
-
"second argument of 'instancesof' is not a
|
|
1151
|
+
"second argument of 'instancesof' is not a constructor function or array of constructor functions"
|
|
1021
1152
|
);
|
|
1022
1153
|
}
|
|
1023
1154
|
}
|
|
@@ -1161,7 +1292,7 @@ function isinstancesof(objs, type, logic, match, strict) {
|
|
|
1161
1292
|
if (objs.length !== type.length) {
|
|
1162
1293
|
if (strict)
|
|
1163
1294
|
throw new TypeError(
|
|
1164
|
-
"length of
|
|
1295
|
+
"length of constructor functions array must match the number of objects"
|
|
1165
1296
|
);
|
|
1166
1297
|
|
|
1167
1298
|
/** @type {number} */
|
|
@@ -1182,7 +1313,7 @@ function isinstancesof(objs, type, logic, match, strict) {
|
|
|
1182
1313
|
if (!(objs[i] instanceof type[i])) return false;
|
|
1183
1314
|
} catch (error) {
|
|
1184
1315
|
throw new TypeError(
|
|
1185
|
-
"second argument of 'instancesof' is not a
|
|
1316
|
+
"second argument of 'instancesof' is not a constructor function or array of constructor functions"
|
|
1186
1317
|
);
|
|
1187
1318
|
}
|
|
1188
1319
|
|
|
@@ -1206,7 +1337,7 @@ function isinstancesof(objs, type, logic, match, strict) {
|
|
|
1206
1337
|
if (objs[i] instanceof type[i]) return true;
|
|
1207
1338
|
} catch (error) {
|
|
1208
1339
|
throw new TypeError(
|
|
1209
|
-
"second argument of 'instancesof' is not a
|
|
1340
|
+
"second argument of 'instancesof' is not a constructor function or array of constructor functions"
|
|
1210
1341
|
);
|
|
1211
1342
|
}
|
|
1212
1343
|
|
|
@@ -1232,7 +1363,7 @@ function isinstancesof(objs, type, logic, match, strict) {
|
|
|
1232
1363
|
if (!(objs[i] instanceof type[i])) return false;
|
|
1233
1364
|
} catch (error) {
|
|
1234
1365
|
throw new TypeError(
|
|
1235
|
-
"second argument of 'instancesof' is not a
|
|
1366
|
+
"second argument of 'instancesof' is not a constructor function or array of constructor functions"
|
|
1236
1367
|
);
|
|
1237
1368
|
}
|
|
1238
1369
|
|
|
@@ -1255,7 +1386,7 @@ function isinstancesof(objs, type, logic, match, strict) {
|
|
|
1255
1386
|
if (objs[i] instanceof type[i]) return true;
|
|
1256
1387
|
} catch (error) {
|
|
1257
1388
|
throw new TypeError(
|
|
1258
|
-
"second argument of 'instancesof' is not a
|
|
1389
|
+
"second argument of 'instancesof' is not a constructor function or array of constructor functions"
|
|
1259
1390
|
);
|
|
1260
1391
|
}
|
|
1261
1392
|
|
|
@@ -1311,7 +1442,7 @@ export function instancesof(
|
|
|
1311
1442
|
) {
|
|
1312
1443
|
/** @type {number} */
|
|
1313
1444
|
|
|
1314
|
-
|
|
1445
|
+
const len = arguments.length;
|
|
1315
1446
|
|
|
1316
1447
|
// ================================================================
|
|
1317
1448
|
|
|
@@ -1413,7 +1544,7 @@ export function isType(
|
|
|
1413
1544
|
) {
|
|
1414
1545
|
/** @type {number} */
|
|
1415
1546
|
|
|
1416
|
-
|
|
1547
|
+
const len = arguments.length;
|
|
1417
1548
|
|
|
1418
1549
|
// ================================================================
|
|
1419
1550
|
|
|
@@ -1485,7 +1616,7 @@ export function lengths(...objs) {
|
|
|
1485
1616
|
|
|
1486
1617
|
/** @type {Array<number>} */
|
|
1487
1618
|
|
|
1488
|
-
|
|
1619
|
+
const lens = [objs[0].length];
|
|
1489
1620
|
|
|
1490
1621
|
/** @type {boolean} */
|
|
1491
1622
|
|
|
@@ -1550,14 +1681,18 @@ export function log(...data) {
|
|
|
1550
1681
|
export function not(obj) {
|
|
1551
1682
|
/** @type {number} */
|
|
1552
1683
|
|
|
1553
|
-
|
|
1684
|
+
const len = arguments.length;
|
|
1554
1685
|
|
|
1555
1686
|
if (len !== 1)
|
|
1556
1687
|
throw new TypeError(`not() takes exactly one argument (${len} given)`);
|
|
1557
1688
|
|
|
1558
|
-
|
|
1689
|
+
/** @type {*} */
|
|
1690
|
+
|
|
1691
|
+
const val = obj?.valueOf();
|
|
1692
|
+
|
|
1693
|
+
if (!(val instanceof Object) || val instanceof Function) return !val;
|
|
1559
1694
|
|
|
1560
|
-
return
|
|
1695
|
+
return val.length === 0 || !val; // handling the true object case
|
|
1561
1696
|
}
|
|
1562
1697
|
|
|
1563
1698
|
/**
|
|
@@ -1583,7 +1718,7 @@ export function print(...data) {
|
|
|
1583
1718
|
export function printf(data) {
|
|
1584
1719
|
if (arguments.length > 1) throw new TypeError('unexpected token ","');
|
|
1585
1720
|
|
|
1586
|
-
process.stdout.write(`${data}`);
|
|
1721
|
+
process.stdout.write(`${String(data)}`);
|
|
1587
1722
|
}
|
|
1588
1723
|
|
|
1589
1724
|
/**
|
|
@@ -1745,7 +1880,7 @@ export function typesof(...objs) {
|
|
|
1745
1880
|
|
|
1746
1881
|
/** @type {Array<string>} */
|
|
1747
1882
|
|
|
1748
|
-
|
|
1883
|
+
const types = [typeof objs[0]];
|
|
1749
1884
|
|
|
1750
1885
|
/** @type {boolean} */
|
|
1751
1886
|
|
|
@@ -1832,15 +1967,15 @@ function main() {
|
|
|
1832
1967
|
|
|
1833
1968
|
/** @type {Number} */
|
|
1834
1969
|
|
|
1835
|
-
|
|
1970
|
+
const a = new Number(3);
|
|
1836
1971
|
|
|
1837
1972
|
/** @type {Number} */
|
|
1838
1973
|
|
|
1839
|
-
|
|
1974
|
+
const b = new Number(7);
|
|
1840
1975
|
|
|
1841
1976
|
/** @type {String} */
|
|
1842
1977
|
|
|
1843
|
-
|
|
1978
|
+
const c = new String("test");
|
|
1844
1979
|
|
|
1845
1980
|
// one to one (disabled)
|
|
1846
1981
|
|
|
@@ -1906,15 +2041,15 @@ function main() {
|
|
|
1906
2041
|
|
|
1907
2042
|
/** @type {number} */
|
|
1908
2043
|
|
|
1909
|
-
|
|
2044
|
+
const x = 3; // or Number(3)
|
|
1910
2045
|
|
|
1911
2046
|
/** @type {number} */
|
|
1912
2047
|
|
|
1913
|
-
|
|
2048
|
+
const y = 7; // or Number(7)
|
|
1914
2049
|
|
|
1915
2050
|
/** @type {number} */
|
|
1916
2051
|
|
|
1917
|
-
|
|
2052
|
+
const z = "test"; // or String("test")
|
|
1918
2053
|
|
|
1919
2054
|
// one to one
|
|
1920
2055
|
|
|
@@ -1986,7 +2121,7 @@ function main() {
|
|
|
1986
2121
|
// two args +
|
|
1987
2122
|
|
|
1988
2123
|
console.log(lengths([1, 2, 3], [1, 2, 3])); // [ 3, true ]
|
|
1989
|
-
console.log(lengths([1, 2, 3], "
|
|
2124
|
+
console.log(lengths([1, 2, 3], "test")); // [ [3, 4], false ]
|
|
1990
2125
|
|
|
1991
2126
|
console.log("--------------!--------------");
|
|
1992
2127
|
|
|
@@ -2038,7 +2173,7 @@ function main() {
|
|
|
2038
2173
|
// two args +
|
|
2039
2174
|
|
|
2040
2175
|
console.log(typesof(1, 2)); // [ number, true ]
|
|
2041
|
-
console.log(typesof(1, "
|
|
2176
|
+
console.log(typesof(1, "test")); // [ [number, string], false ]
|
|
2042
2177
|
|
|
2043
2178
|
console.log("------------------------------");
|
|
2044
2179
|
|