defuss-runtime 1.1.0 → 1.2.1

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/dist/index.cjs CHANGED
@@ -270,42 +270,77 @@ function wait(ms) {
270
270
  }
271
271
  function createTimeoutPromise(timeoutMs, operation, timeoutCallback) {
272
272
  return new Promise((resolve, reject) => {
273
+ let completed = false;
273
274
  const timeoutId = setTimeout(() => {
274
- timeoutCallback?.(timeoutMs);
275
- reject(new Error(`Timeout after ${timeoutMs}ms`));
275
+ if (!completed) {
276
+ completed = true;
277
+ timeoutCallback?.(timeoutMs);
278
+ reject(new Error(`Timeout after ${timeoutMs}ms`));
279
+ }
276
280
  }, timeoutMs);
277
281
  Promise.resolve().then(async () => {
278
282
  try {
279
283
  const result = await operation();
280
- clearTimeout(timeoutId);
281
- resolve(result);
284
+ if (!completed) {
285
+ completed = true;
286
+ clearTimeout(timeoutId);
287
+ resolve(result);
288
+ }
282
289
  } catch (error) {
283
- clearTimeout(timeoutId);
284
- reject(error);
290
+ if (!completed) {
291
+ completed = true;
292
+ clearTimeout(timeoutId);
293
+ reject(error);
294
+ }
285
295
  }
286
296
  });
287
297
  });
288
298
  }
289
299
  async function waitForWithPolling(check, timeout, interval = 1) {
290
- return createTimeoutPromise(timeout, () => {
291
- return new Promise((resolve, reject) => {
292
- const timer = setInterval(() => {
293
- try {
294
- const result = check();
295
- if (result != null) {
296
- clearInterval(timer);
297
- resolve(result);
300
+ const start = Date.now();
301
+ let timer;
302
+ const cleanup = () => {
303
+ if (timer) {
304
+ clearInterval(timer);
305
+ }
306
+ };
307
+ try {
308
+ return await createTimeoutPromise(timeout, () => {
309
+ return new Promise((resolve, reject) => {
310
+ let pollCount = 0;
311
+ timer = setInterval(() => {
312
+ pollCount++;
313
+ const elapsed = Date.now() - start;
314
+ try {
315
+ const result = check();
316
+ if (result != null) {
317
+ cleanup();
318
+ resolve(result);
319
+ }
320
+ } catch (err) {
321
+ console.log(
322
+ `\u274C waitForWithPolling() error on poll #${pollCount}:`,
323
+ err
324
+ );
325
+ cleanup();
326
+ reject(err);
298
327
  }
299
- } catch (err) {
300
- clearInterval(timer);
301
- reject(err);
302
- }
303
- }, interval);
328
+ }, interval);
329
+ });
304
330
  });
305
- });
331
+ } catch (error) {
332
+ cleanup();
333
+ throw error;
334
+ }
306
335
  }
307
336
  async function waitForRef(ref, timeout) {
308
- return waitForWithPolling(() => ref.current, timeout);
337
+ return waitForWithPolling(() => {
338
+ if (ref.orphan) {
339
+ console.log("\u26A0\uFE0F waitForRef() - ref is marked as orphaned, failing fast");
340
+ throw new Error("Ref has been orphaned from component unmount");
341
+ }
342
+ return ref.current;
343
+ }, timeout);
309
344
  }
310
345
 
311
346
  const asString = (value) => {
@@ -387,92 +422,149 @@ const asInteger = (value) => {
387
422
  return asNumber(Number.parseInt(asString(number), 10).toFixed(0));
388
423
  };
389
424
 
390
- const isTrue = (value) => {
391
- return value === true;
425
+ const isAfter = (value, minDate, inclusive = false, message) => {
426
+ const isValid = value instanceof Date && (inclusive ? value.getTime() >= minDate.getTime() : value.getTime() > minDate.getTime());
427
+ return isValid ? true : message || false;
392
428
  };
393
429
 
394
- const isFalse = (value) => {
395
- return value === false;
430
+ const isArray = (value, message) => {
431
+ const isValid = Array.isArray(value);
432
+ return isValid ? true : message || false;
396
433
  };
397
434
 
398
- const isTruthy = (value) => {
399
- return !!value;
435
+ const isBefore = (value, maxDate, inclusive = false, message) => {
436
+ const isValid = value instanceof Date && (inclusive ? value.getTime() <= maxDate.getTime() : value.getTime() < maxDate.getTime());
437
+ return isValid ? true : message || false;
400
438
  };
401
439
 
402
- const isFalsy = (value) => {
403
- return !value;
440
+ const isBoolean = (value, message) => {
441
+ const isValid = typeof value === "boolean";
442
+ return isValid ? true : message || false;
404
443
  };
405
444
 
406
- const isAfter = (value, minDate, inclusive = false) => {
407
- return value instanceof Date && (inclusive ? value.getTime() >= minDate.getTime() : value.getTime() > minDate.getTime());
445
+ const isDate = (value, message) => {
446
+ const isValid = value instanceof Date && !Number.isNaN(value.getDate());
447
+ return isValid ? true : message || false;
408
448
  };
409
449
 
410
- const isArray = (value) => Array.isArray(value);
411
-
412
- const isBefore = (value, maxDate, inclusive = false) => value instanceof Date && (inclusive ? value.getTime() <= maxDate.getTime() : value.getTime() < maxDate.getTime());
413
-
414
- const isBoolean = (value) => typeof value === "boolean";
415
-
416
- const isDate = (value) => value instanceof Date && !Number.isNaN(value.getDate());
417
-
418
- const isDefined = (value) => typeof value !== "undefined";
450
+ const isDefined = (value, message) => {
451
+ const isValid = typeof value !== "undefined";
452
+ return isValid ? true : message || false;
453
+ };
419
454
 
420
- const isString = (value) => typeof value === "string";
455
+ const isString = (value, message) => {
456
+ const isValid = typeof value === "string";
457
+ return isValid ? true : message || false;
458
+ };
421
459
 
422
- const isEmail = (value) => isString(value) && /(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\\x01\-\\x08\\x0b\\x0c\\x0e\-\\x1f\\x21\\x23\-\\x5b\\x5d\-\\x7f]|\\[\\x01-\\x09\\x0b\\x0c\\x0e\-\\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01\-\\x08\\x0b\\x0c\\x0e\-\\x1f\\x21\-\\x5a\\x53\-\\x7f]|\\[\\x01\-\\x09\\x0b\\x0c\\x0e\-\\x7f])+)\])/.test(
423
- value
424
- );
460
+ const isEmail = (value, message) => {
461
+ const stringResult = isString(value);
462
+ const isStringValid = stringResult === true;
463
+ const isValid = isStringValid && /(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\\x01\-\\x08\\x0b\\x0c\\x0e\-\\x1f\\x21\\x23\-\\x5b\\x5d\-\\x7f]|\\[\\x01-\\x09\\x0b\\x0c\\x0e\-\\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01\-\\x08\\x0b\\x0c\\x0e\-\\x1f\\x21\-\\x5a\\x53\-\\x7f]|\\[\\x01\-\\x09\\x0b\\x0c\\x0e\-\\x7f])+)\])/.test(
464
+ value
465
+ );
466
+ return isValid ? true : message || false;
467
+ };
425
468
 
426
- const isEmpty = (value) => {
427
- if (value === null || value === void 0) return true;
428
- if (typeof value === "string") return value === "";
429
- if (Array.isArray(value)) return value.length === 0;
430
- if (value instanceof Date) return false;
431
- if (typeof value === "object") return Object.keys(value).length === 0;
432
- return false;
469
+ const isEmpty = (value, message) => {
470
+ let isValid = false;
471
+ if (value === null || value === void 0) isValid = true;
472
+ else if (typeof value === "string") isValid = value === "";
473
+ else if (Array.isArray(value)) isValid = value.length === 0;
474
+ else if (value instanceof Date) isValid = false;
475
+ else if (typeof value === "object") isValid = Object.keys(value).length === 0;
476
+ else isValid = false;
477
+ return isValid ? true : message || false;
433
478
  };
434
479
 
435
- const is = (value, valueB) => value === valueB;
480
+ const is = (value, valueB, message) => {
481
+ const isValid = value === valueB;
482
+ return isValid ? true : message || false;
483
+ };
436
484
 
437
- const isSafeNumber = (value) => typeof value === "number" && !Number.isNaN(value) && Number.isFinite(value);
485
+ const isSafeNumber = (value, message) => {
486
+ const isValid = typeof value === "number" && !Number.isNaN(value) && Number.isFinite(value);
487
+ return isValid ? true : message || false;
488
+ };
438
489
 
439
- const isGreaterThan = (value, minValue, includeEqual = false) => isSafeNumber(value) && (includeEqual ? value >= minValue : value > minValue);
490
+ const isGreaterThan = (value, minValue, includeEqual = false, message) => {
491
+ const safeNumberResult = isSafeNumber(value);
492
+ const isSafeNum = safeNumberResult === true;
493
+ const isValid = isSafeNum && (includeEqual ? value >= minValue : value > minValue);
494
+ return isValid ? true : message || false;
495
+ };
440
496
 
441
- const isSafeNumeric = (value) => {
442
- if (typeof value === "number") return isSafeNumber(value);
443
- if (typeof value === "string") {
444
- if (value.trim() === "") return false;
445
- const num = Number(value);
446
- return isSafeNumber(num);
497
+ const isSafeNumeric = (value, message) => {
498
+ let isValid = false;
499
+ if (typeof value === "number") {
500
+ const result = isSafeNumber(value);
501
+ isValid = result === true;
502
+ } else if (typeof value === "string") {
503
+ if (value.trim() === "") {
504
+ isValid = false;
505
+ } else {
506
+ const num = Number(value);
507
+ const result = isSafeNumber(num);
508
+ isValid = result === true;
509
+ }
510
+ } else {
511
+ isValid = false;
447
512
  }
448
- return false;
513
+ return isValid ? true : message || false;
449
514
  };
450
515
 
451
- const isObject = (value) => value !== null && typeof value === "object" && !Array.isArray(value);
516
+ const isObject = (value, message) => {
517
+ const isValid = value !== null && typeof value === "object" && !Array.isArray(value);
518
+ return isValid ? true : message || false;
519
+ };
452
520
 
453
- const isOneOf = (value, options) => options.includes(value);
521
+ const isOneOf = (value, options, message) => {
522
+ const isValid = options.includes(value);
523
+ return isValid ? true : message || false;
524
+ };
454
525
 
455
- const isPhoneNumber = (value) => isString(value) && /\+(9[976]\d|8[987530]\d|6[987]\d|5[90]\d|42\d|3[875]\d|2[98654321]\d|9[8543210]|8[6421]|6[6543210]|5[87654321]|4[987654310]|3[9643210]|2[70]|7|1)\d{1,14}$/.test(
456
- value
457
- );
526
+ const isPhoneNumber = (value, message) => {
527
+ const isValid = isString(value) && /\+(9[976]\d|8[987530]\d|6[987]\d|5[90]\d|42\d|3[875]\d|2[98654321]\d|9[8543210]|8[6421]|6[6543210]|5[87654321]|4[987654310]|3[9643210]|2[70]|7|1)\d{1,14}$/.test(
528
+ value
529
+ );
530
+ return isValid ? true : message || false;
531
+ };
458
532
 
459
- const isRequired = (value) => !!value;
533
+ const isRequired = (value, message) => {
534
+ const isValid = !!value;
535
+ return isValid ? true : message || false;
536
+ };
460
537
 
461
- const isSlug = (value) => isString(value) && /^[a-z0-9-]+$/.test(value);
538
+ const isSlug = (value, message) => {
539
+ const stringResult = isString(value);
540
+ const isStringValid = stringResult === true;
541
+ const isValid = isStringValid && /^[a-z0-9-]+$/.test(value);
542
+ return isValid ? true : message || false;
543
+ };
462
544
 
463
- const isLessThan = (value, maxValue, includeEqual = false) => isSafeNumber(value) && (includeEqual ? value <= maxValue : value < maxValue);
545
+ const isLessThan = (value, maxValue, includeEqual = false, message) => {
546
+ const safeNumberResult = isSafeNumber(value);
547
+ const isSafeNum = safeNumberResult === true;
548
+ const isValid = isSafeNum && (includeEqual ? value <= maxValue : value < maxValue);
549
+ return isValid ? true : message || false;
550
+ };
464
551
 
465
- const isUrl = (value) => {
466
- if (!isString(value)) return false;
552
+ const isUrl = (value, message) => {
553
+ const stringResult = isString(value);
554
+ if (stringResult !== true) return message || false;
467
555
  try {
468
556
  new URL(value);
469
557
  return true;
470
558
  } catch (_) {
471
- return false;
559
+ return message || false;
472
560
  }
473
561
  };
474
562
 
475
- const isUrlPath = (value) => isString(value) && value.length > 0 && /^[a-z0-9\-_\/]+$/.test(value);
563
+ const isUrlPath = (value, message) => {
564
+ const stringCheck = isString(value);
565
+ const isValid = stringCheck === true && value.length > 0 && /^[a-z0-9\-_\/]+$/.test(value);
566
+ return isValid ? true : message || false;
567
+ };
476
568
 
477
569
  const getDateValue = (date) => ({
478
570
  year: date.getFullYear(),
@@ -484,24 +576,110 @@ const getDateValue = (date) => ({
484
576
  millisecond: date.getMilliseconds()
485
577
  });
486
578
 
487
- const isLongerThan = (value, minLength, includeEqual = false) => {
488
- if (typeof value !== "string") return false;
489
- return includeEqual ? value.length >= minLength : value.length > minLength;
579
+ const isLongerThan = (value, minLength, includeEqual = false, message) => {
580
+ if (typeof value !== "string") return message || false;
581
+ const isValid = includeEqual ? value.length >= minLength : value.length > minLength;
582
+ return isValid ? true : message || false;
583
+ };
584
+
585
+ const isShorterThan = (value, maxLength, includeEqual = false, message) => {
586
+ if (typeof value !== "string") return message || false;
587
+ const isValid = includeEqual ? value.length <= maxLength : value.length < maxLength;
588
+ return isValid ? true : message || false;
589
+ };
590
+
591
+ const hasPattern = (value, pattern, message) => {
592
+ if (typeof value !== "string") return message || false;
593
+ const isValid = pattern.test(value);
594
+ return isValid ? true : message || false;
595
+ };
596
+
597
+ const hasDateFormat = (value, message) => {
598
+ if (typeof value === "bigint" || typeof value === "symbol") {
599
+ return message || false;
600
+ }
601
+ if (typeof value === "number") {
602
+ try {
603
+ const date = new Date(value);
604
+ const isValid = !Number.isNaN(date.getTime());
605
+ return isValid ? true : message || false;
606
+ } catch {
607
+ return message || false;
608
+ }
609
+ }
610
+ const str = asString(value);
611
+ if (!str) {
612
+ return message || false;
613
+ }
614
+ try {
615
+ const date = new Date(str);
616
+ const isValid = !Number.isNaN(date.getTime());
617
+ return isValid ? true : message || false;
618
+ } catch {
619
+ return message || false;
620
+ }
621
+ };
622
+
623
+ const isInteger = (value, message) => {
624
+ const isValid = isSafeNumber(value) && Number.isInteger(value);
625
+ return isValid ? true : message || false;
626
+ };
627
+
628
+ const isEqual = (value, valueB, message) => {
629
+ const isValid = equalsJSON(value, valueB);
630
+ return isValid ? true : message || false;
631
+ };
632
+
633
+ const isTrue = (value, message) => {
634
+ const isValid = value === true;
635
+ return isValid ? true : message || false;
490
636
  };
491
637
 
492
- const isShorterThan = (value, maxLength, includeEqual = false) => {
493
- if (typeof value !== "string") return false;
494
- return includeEqual ? value.length <= maxLength : value.length < maxLength;
638
+ const isFalse = (value, message) => {
639
+ const isValid = value === false;
640
+ return isValid ? true : message || false;
495
641
  };
496
642
 
497
- const hasPattern = (value, pattern) => {
498
- if (typeof value !== "string") return false;
499
- return pattern.test(value);
643
+ const isTruthy = (value, message) => {
644
+ const isValid = !!value;
645
+ return isValid ? true : message || false;
500
646
  };
501
647
 
502
- const isInteger = (value) => isSafeNumber(value) && Number.isInteger(value);
648
+ const isFalsy = (value, message) => {
649
+ const isValid = !value;
650
+ return isValid ? true : message || false;
651
+ };
652
+
653
+ const isInstanceOf = (value, someConstructorFunction, message) => {
654
+ if (typeof someConstructorFunction !== "function") {
655
+ throw new TypeError("Expected a constructor function");
656
+ }
657
+ const isValid = value instanceof someConstructorFunction && value.constructor === someConstructorFunction;
658
+ return isValid ? true : message || false;
659
+ };
503
660
 
504
- const isEqual = (value, valueB) => equalsJSON(value, valueB);
661
+ const isTypeOf = (value, type, message) => {
662
+ let isValid = false;
663
+ if (type === "string") {
664
+ isValid = typeof value === "string";
665
+ } else if (type === "number") {
666
+ isValid = typeof value === "number";
667
+ } else if (type === "boolean") {
668
+ isValid = typeof value === "boolean";
669
+ } else if (type === "object") {
670
+ isValid = typeof value === "object" && value !== null;
671
+ } else if (type === "function") {
672
+ isValid = typeof value === "function";
673
+ } else if (type === "undefined") {
674
+ isValid = typeof value === "undefined";
675
+ }
676
+ return isValid ? true : message || false;
677
+ };
678
+
679
+ const isNull = (value, message) => {
680
+ const isValid = value === null;
681
+ return isValid ? true : message || false;
682
+ };
505
683
 
506
684
  function preload(url, as = "image") {
507
685
  const urls = Array.isArray(url) ? url : [url];
@@ -536,6 +714,7 @@ exports.equalsJSON = equalsJSON;
536
714
  exports.getAllKeysFromPath = getAllKeysFromPath;
537
715
  exports.getByPath = getByPath;
538
716
  exports.getDateValue = getDateValue;
717
+ exports.hasDateFormat = hasDateFormat;
539
718
  exports.hasPattern = hasPattern;
540
719
  exports.hexToBinary = hexToBinary;
541
720
  exports.is = is;
@@ -551,9 +730,11 @@ exports.isEqual = isEqual;
551
730
  exports.isFalse = isFalse;
552
731
  exports.isFalsy = isFalsy;
553
732
  exports.isGreaterThan = isGreaterThan;
733
+ exports.isInstanceOf = isInstanceOf;
554
734
  exports.isInteger = isInteger;
555
735
  exports.isLessThan = isLessThan;
556
736
  exports.isLongerThan = isLongerThan;
737
+ exports.isNull = isNull;
557
738
  exports.isObject = isObject;
558
739
  exports.isOneOf = isOneOf;
559
740
  exports.isPathAccessor = isPathAccessor;
@@ -566,6 +747,7 @@ exports.isSlug = isSlug;
566
747
  exports.isString = isString;
567
748
  exports.isTrue = isTrue;
568
749
  exports.isTruthy = isTruthy;
750
+ exports.isTypeOf = isTypeOf;
569
751
  exports.isUrl = isUrl;
570
752
  exports.isUrlPath = isUrlPath;
571
753
  exports.omit = omit;