@synnaxlabs/x 0.41.0 → 0.42.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.
Files changed (159) hide show
  1. package/.turbo/turbo-build.log +23 -23
  2. package/dist/binary.cjs +1 -1
  3. package/dist/binary.js +2 -2
  4. package/dist/{bounds-M-SZ3X1Z.cjs → bounds-BQo7rvs9.cjs} +1 -1
  5. package/dist/{bounds-DQrjn60Q.js → bounds-Bn5_l4Z3.js} +10 -9
  6. package/dist/bounds.cjs +1 -1
  7. package/dist/bounds.js +1 -1
  8. package/dist/compare.cjs +1 -1
  9. package/dist/compare.js +1 -1
  10. package/dist/deep.cjs +1 -1
  11. package/dist/deep.js +84 -77
  12. package/dist/{dimensions-PWy5QZoM.cjs → dimensions-D2QGoNXO.cjs} +1 -1
  13. package/dist/dimensions.cjs +1 -1
  14. package/dist/{external-CvWr1nhS.cjs → external-DWQITF5_.cjs} +1 -1
  15. package/dist/index-BywOGO8U.js +1074 -0
  16. package/dist/index-CYYjI7Uf.cjs +1 -0
  17. package/dist/index-C_6NXBlg.cjs +3 -0
  18. package/dist/{index-BVC_8Cg9.js → index-QGplUHuy.js} +1 -1
  19. package/dist/index.cjs +3 -3
  20. package/dist/index.js +702 -243
  21. package/dist/record.js +3 -1
  22. package/dist/{scale-DL9VFGhL.cjs → scale-BtZINJ-A.cjs} +1 -1
  23. package/dist/{scale-DQwBWnwc.js → scale-DfJe9755.js} +1 -1
  24. package/dist/scale.cjs +1 -1
  25. package/dist/scale.js +1 -1
  26. package/dist/{series-D0zxMWxP.js → series-B9JERcqi.js} +571 -492
  27. package/dist/series-DqJ6f97G.cjs +11 -0
  28. package/dist/spatial.cjs +1 -1
  29. package/dist/spatial.js +2 -2
  30. package/dist/src/binary/{encoder.d.ts → codec.d.ts} +14 -8
  31. package/dist/src/binary/codec.d.ts.map +1 -0
  32. package/dist/src/binary/codec.spec.d.ts +2 -0
  33. package/dist/src/binary/codec.spec.d.ts.map +1 -0
  34. package/dist/src/binary/index.d.ts +1 -1
  35. package/dist/src/binary/index.d.ts.map +1 -1
  36. package/dist/src/breaker/breaker.d.ts +14 -21
  37. package/dist/src/breaker/breaker.d.ts.map +1 -1
  38. package/dist/src/change/change.d.ts +5 -18
  39. package/dist/src/change/change.d.ts.map +1 -1
  40. package/dist/src/color/color.d.ts +126 -0
  41. package/dist/src/color/color.d.ts.map +1 -0
  42. package/dist/src/color/color.spec.d.ts +2 -0
  43. package/dist/src/color/color.spec.d.ts.map +1 -0
  44. package/dist/src/color/external.d.ts +5 -0
  45. package/dist/src/color/external.d.ts.map +1 -0
  46. package/dist/src/color/gradient.d.ts +18 -0
  47. package/dist/src/color/gradient.d.ts.map +1 -0
  48. package/dist/src/color/index.d.ts +2 -0
  49. package/dist/src/color/index.d.ts.map +1 -0
  50. package/dist/src/color/palette.d.ts +19 -0
  51. package/dist/src/color/palette.d.ts.map +1 -0
  52. package/dist/src/color/transformColorsToHex.d.ts +6 -0
  53. package/dist/src/color/transformColorsToHex.d.ts.map +1 -0
  54. package/dist/src/control/control.d.ts +69 -74
  55. package/dist/src/control/control.d.ts.map +1 -1
  56. package/dist/src/deep/merge.d.ts +1 -1
  57. package/dist/src/deep/merge.d.ts.map +1 -1
  58. package/dist/src/errors/errors.d.ts +127 -7
  59. package/dist/src/errors/errors.d.ts.map +1 -1
  60. package/dist/src/errors/errors.spec.d.ts +2 -0
  61. package/dist/src/errors/errors.spec.d.ts.map +1 -0
  62. package/dist/src/index.d.ts +4 -0
  63. package/dist/src/index.d.ts.map +1 -1
  64. package/dist/src/jsonrpc/jsonrpc.d.ts +10 -7
  65. package/dist/src/jsonrpc/jsonrpc.d.ts.map +1 -1
  66. package/dist/src/kv/types.d.ts +1 -7
  67. package/dist/src/kv/types.d.ts.map +1 -1
  68. package/dist/src/migrate/migrate.d.ts +1 -1
  69. package/dist/src/notation/notation.d.ts +5 -1
  70. package/dist/src/notation/notation.d.ts.map +1 -1
  71. package/dist/src/record.d.ts +2 -1
  72. package/dist/src/record.d.ts.map +1 -1
  73. package/dist/src/replace.d.ts +2 -0
  74. package/dist/src/replace.d.ts.map +1 -0
  75. package/dist/src/runtime/os.d.ts +9 -1
  76. package/dist/src/runtime/os.d.ts.map +1 -1
  77. package/dist/src/singleton/define.d.ts +9 -0
  78. package/dist/src/singleton/define.d.ts.map +1 -0
  79. package/dist/src/singleton/define.spec.d.ts +2 -0
  80. package/dist/src/singleton/define.spec.d.ts.map +1 -0
  81. package/dist/src/singleton/index.d.ts +2 -0
  82. package/dist/src/singleton/index.d.ts.map +1 -0
  83. package/dist/src/spatial/base.d.ts +74 -70
  84. package/dist/src/spatial/base.d.ts.map +1 -1
  85. package/dist/src/spatial/box/box.d.ts +18 -76
  86. package/dist/src/spatial/box/box.d.ts.map +1 -1
  87. package/dist/src/spatial/dimensions/dimensions.d.ts +5 -29
  88. package/dist/src/spatial/dimensions/dimensions.d.ts.map +1 -1
  89. package/dist/src/spatial/direction/direction.d.ts +9 -1
  90. package/dist/src/spatial/direction/direction.d.ts.map +1 -1
  91. package/dist/src/spatial/location/location.d.ts +43 -22
  92. package/dist/src/spatial/location/location.d.ts.map +1 -1
  93. package/dist/src/spatial/scale/scale.d.ts +12 -120
  94. package/dist/src/spatial/scale/scale.d.ts.map +1 -1
  95. package/dist/src/spatial/xy/xy.d.ts +5 -29
  96. package/dist/src/spatial/xy/xy.d.ts.map +1 -1
  97. package/dist/src/sync/index.d.ts +2 -0
  98. package/dist/src/sync/index.d.ts.map +1 -0
  99. package/dist/src/sync/mutex.d.ts +8 -0
  100. package/dist/src/sync/mutex.d.ts.map +1 -0
  101. package/dist/src/telem/gl.d.ts +4 -1
  102. package/dist/src/telem/gl.d.ts.map +1 -1
  103. package/dist/src/telem/series.d.ts +46 -125
  104. package/dist/src/telem/series.d.ts.map +1 -1
  105. package/dist/src/telem/telem.d.ts +101 -86
  106. package/dist/src/telem/telem.d.ts.map +1 -1
  107. package/dist/src/toArray.d.ts +1 -1
  108. package/dist/src/toArray.d.ts.map +1 -1
  109. package/dist/src/zod/util.d.ts.map +1 -1
  110. package/dist/telem.cjs +1 -1
  111. package/dist/telem.js +1 -1
  112. package/dist/toArray.cjs +1 -1
  113. package/dist/toArray.js +1 -1
  114. package/dist/zod.cjs +1 -1
  115. package/package.json +5 -2
  116. package/src/binary/codec.spec.ts +370 -0
  117. package/src/binary/{encoder.ts → codec.ts} +55 -11
  118. package/src/binary/index.ts +1 -1
  119. package/src/breaker/breaker.spec.ts +16 -25
  120. package/src/breaker/breaker.ts +36 -19
  121. package/src/color/color.spec.ts +673 -0
  122. package/src/color/color.ts +317 -0
  123. package/src/color/external.ts +13 -0
  124. package/src/color/gradient.ts +78 -0
  125. package/src/color/index.ts +10 -0
  126. package/src/color/palette.ts +28 -0
  127. package/src/color/transformColorsToHex.ts +30 -0
  128. package/src/control/control.ts +30 -22
  129. package/src/deep/merge.spec.ts +60 -0
  130. package/src/deep/merge.ts +13 -8
  131. package/src/errors/errors.spec.ts +152 -0
  132. package/src/errors/errors.ts +225 -10
  133. package/src/index.ts +4 -0
  134. package/src/jsonrpc/jsonrpc.ts +12 -8
  135. package/src/migrate/migrate.ts +2 -2
  136. package/src/primitive.ts +1 -1
  137. package/src/record.ts +5 -1
  138. package/src/replace.ts +1 -0
  139. package/src/singleton/define.spec.ts +93 -0
  140. package/src/singleton/define.ts +27 -0
  141. package/src/singleton/index.ts +10 -0
  142. package/src/sync/index.ts +1 -0
  143. package/src/sync/mutex.ts +16 -0
  144. package/src/telem/series.spec.ts +32 -0
  145. package/src/telem/series.ts +54 -19
  146. package/src/telem/telem.spec.ts +151 -10
  147. package/src/telem/telem.ts +126 -73
  148. package/src/toArray.ts +2 -2
  149. package/src/zod/util.spec.ts +17 -1
  150. package/src/zod/util.ts +4 -2
  151. package/tsconfig.tsbuildinfo +1 -1
  152. package/dist/index-BG3Scw3G.cjs +0 -1
  153. package/dist/index-C3QzbIwt.js +0 -101
  154. package/dist/index-CnclyYpG.cjs +0 -3
  155. package/dist/series-BMma2b5q.cjs +0 -11
  156. package/dist/src/binary/encoder.d.ts.map +0 -1
  157. package/dist/src/binary/encoder.spec.d.ts +0 -2
  158. package/dist/src/binary/encoder.spec.d.ts.map +0 -1
  159. package/src/binary/encoder.spec.ts +0 -174
@@ -11,6 +11,7 @@ import { z } from "zod";
11
11
 
12
12
  import { math } from "@/math";
13
13
  import { type Stringer } from "@/primitive";
14
+ import { type bounds } from "@/spatial";
14
15
 
15
16
  export type TZInfo = "UTC" | "local";
16
17
 
@@ -53,17 +54,12 @@ const remainder = <T extends TimeStamp | TimeSpan>(
53
54
  /**
54
55
  * Represents a UTC timestamp. Synnax uses a nanosecond precision int64 timestamp.
55
56
  *
56
- * DISCLAIMER: JavaScript stores all numbers as 64-bit floating point numbers, so we expect a
57
- * expect a precision drop from nanoseconds to quarter microseconds when communicating
58
- * with the server. If this is a problem, please file an issue with the Synnax team.
59
- * Caveat emptor.
60
- *
61
57
  * @param value - The timestamp value to parse. This can be any of the following:
62
58
  *
63
- * 1. A number representing the number of milliseconds since the Unix epoch.
64
- * 2. A javascript Date object.
59
+ * 1. A number representing the number of nanoseconds since the Unix epoch.
60
+ * 2. A JavaScript Date object.
65
61
  * 3. An array of numbers satisfying the DateComponents type, where the first element is the
66
- * year, the second is the month, and the third is the day. To incraase resolution
62
+ * year, the second is the month, and the third is the day. To increase resolution
67
63
  * when using this method, use the add method. It's important to note that this initializes
68
64
  * a timestamp at midnight UTC, regardless of the timezone specified.
69
65
  * 4. An ISO compliant date or date time string. The time zone component is ignored.
@@ -77,7 +73,9 @@ const remainder = <T extends TimeStamp | TimeSpan>(
77
73
  * @example ts = new TimeStamp("2021-01-01T12:30:00Z") // 1/1/2021 at 12:30pm UTC
78
74
  */
79
75
  export class TimeStamp implements Stringer {
76
+ /** The underlying value for the time stamp */
80
77
  private readonly value: bigint;
78
+ /** Makes sure JSON encodes the value and not the class. */
81
79
  readonly encodeValue = true;
82
80
 
83
81
  constructor(value?: CrudeTimeStamp, tzInfo: TZInfo = "UTC") {
@@ -110,10 +108,6 @@ export class TimeStamp implements Stringer {
110
108
  .valueOf();
111
109
  }
112
110
 
113
- encode(): string {
114
- return this.value.toString();
115
- }
116
-
117
111
  valueOf(): bigint {
118
112
  return this.value;
119
113
  }
@@ -353,17 +347,21 @@ export class TimeStamp implements Stringer {
353
347
  return Number(this.valueOf()) / Number(TimeSpan.SECOND.valueOf());
354
348
  }
355
349
 
356
- /**
357
- * @returns The number of milliseconds since the unix epoch.
358
- */
350
+ /** @returns the floating point number of milliseconds since the unix epoch. */
359
351
  get milliseconds(): number {
360
352
  return Number(this.valueOf()) / Number(TimeStamp.MILLISECOND.valueOf());
361
353
  }
362
354
 
355
+ /** @returns the floating point number of microseconds since the unix epoch */
363
356
  get microseconds(): number {
364
357
  return Number(this.valueOf()) / Number(TimeStamp.MICROSECOND.valueOf());
365
358
  }
366
359
 
360
+ /**
361
+ * @returns the floating point number of nanoseconds since the unix epoch.
362
+ * Note that since we're converting to float64, this reduces the resolution
363
+ * to a quarter of a microsecond.
364
+ */
367
365
  get nanoseconds(): number {
368
366
  return Number(this.valueOf());
369
367
  }
@@ -373,6 +371,10 @@ export class TimeStamp implements Stringer {
373
371
  return this.date().getFullYear();
374
372
  }
375
373
 
374
+ /**
375
+ * @returns a copy of the timestamp with the year changed.
376
+ * @param year the value to set the year to.
377
+ */
376
378
  setYear(year: number): TimeStamp {
377
379
  const d = this.date();
378
380
  d.setFullYear(year);
@@ -381,35 +383,49 @@ export class TimeStamp implements Stringer {
381
383
 
382
384
  /** @returns the integer month that the timestamp corresponds to with its year. */
383
385
  get month(): number {
384
- return this.date().getMonth();
386
+ return this.date().getUTCMonth();
385
387
  }
386
388
 
389
+ /**
390
+ * @returns a copy of the timestamp with the month changed.
391
+ * @param month the value to set the month to.
392
+ */
387
393
  setMonth(month: number): TimeStamp {
388
394
  const d = this.date();
389
- d.setMonth(month);
395
+ d.setUTCMonth(month);
390
396
  return new TimeStamp(d);
391
397
  }
392
398
 
393
399
  /** @returns the integer day that the timestamp corresponds to within its month. */
394
400
  get day(): number {
395
- return this.date().getDate();
401
+ return this.date().getUTCDate();
396
402
  }
397
403
 
404
+ /**
405
+ * @returns a copy of the timestamp with the day changed.
406
+ * @param day the value the set the day to.
407
+ */
398
408
  setDay(day: number): TimeStamp {
399
409
  const d = this.date();
400
- d.setDate(day);
410
+ d.setUTCDate(day);
401
411
  return new TimeStamp(d);
402
412
  }
403
413
 
404
- /** @returns the integer hour that the timestamp corresponds to within its day. */
414
+ /**
415
+ * @returns the integer hour that the timestamp corresponds to within its day.
416
+ */
405
417
  get hour(): number {
406
- return this.date().getHours();
418
+ return this.date().getUTCHours();
407
419
  }
408
420
 
421
+ /**
422
+ * @returns a copy of the timestamp with the hour changed.
423
+ * @param hour the value to set the hour to.
424
+ */
409
425
  setHour(hour: number): TimeStamp {
410
426
  const d = this.date();
411
- d.setHours(hour);
412
- return new TimeStamp(d);
427
+ d.setUTCHours(hour);
428
+ return new TimeStamp(d, "UTC");
413
429
  }
414
430
 
415
431
  /** @returns the integer minute that the timestamp corresponds to within its hour. */
@@ -417,32 +433,53 @@ export class TimeStamp implements Stringer {
417
433
  return this.date().getMinutes();
418
434
  }
419
435
 
436
+ /**
437
+ * @returns a copy of the timestamp with the minute changed.
438
+ * @param minute the value to set the minute to.
439
+ */
420
440
  setMinute(minute: number): TimeStamp {
421
441
  const d = this.date();
422
- d.setMinutes(minute);
442
+ d.setUTCMinutes(minute);
423
443
  return new TimeStamp(d);
424
444
  }
425
445
 
446
+ /**
447
+ * @returns the integer second that the timestamp corresponds to within its
448
+ * minute.
449
+ */
426
450
  get second(): number {
427
451
  return this.date().getSeconds();
428
452
  }
429
453
 
454
+ /**
455
+ * @returns a copy of the timestamp with the second changed.
456
+ * @param second the value to set the second to.
457
+ */
430
458
  setSecond(second: number): TimeStamp {
431
459
  const d = this.date();
432
- d.setSeconds(second);
460
+ d.setUTCSeconds(second);
433
461
  return new TimeStamp(d);
434
462
  }
435
463
 
464
+ /**
465
+ * @reutrns the integer millisecond that the timestamp corresponds to within
466
+ * its second.
467
+ */
436
468
  get millisecond(): number {
437
469
  return this.date().getMilliseconds();
438
470
  }
439
471
 
472
+ /**
473
+ * @returns a copy of the timestamp with the milliseconds changed.
474
+ * @param millisecond the value to set the millisecond to.
475
+ */
440
476
  setMillisecond(millisecond: number): TimeStamp {
441
477
  const d = this.date();
442
478
  d.setMilliseconds(millisecond);
443
479
  return new TimeStamp(d);
444
480
  }
445
481
 
482
+ /** @returns the time stamp formatted as an ISO string. */
446
483
  toString(): string {
447
484
  return this.date().toISOString();
448
485
  }
@@ -450,7 +487,7 @@ export class TimeStamp implements Stringer {
450
487
  /**
451
488
  * @returns A new TimeStamp that is the remainder of the TimeStamp divided by the
452
489
  * given span. This is useful in cases where you want only part of a TimeStamp's value
453
- * i.e. the hours, minutes, seconds, milliseconds, microseconds, and nanoseconds but
490
+ * i.e., the hours, minutes, seconds, milliseconds, microseconds, and nanoseconds but
454
491
  * not the days, years, etc.
455
492
  *
456
493
  * @param divisor - The TimeSpan to divide by. Must be an even TimeSpan or TimeStamp. Even
@@ -480,6 +517,7 @@ export class TimeStamp implements Stringer {
480
517
  return new TimeStamp(new Date());
481
518
  }
482
519
 
520
+ /** @returns the maximum value of all the timestamps. */
483
521
  static max(...timestamps: CrudeTimeStamp[]): TimeStamp {
484
522
  let max = TimeStamp.MIN;
485
523
  for (const ts of timestamps) {
@@ -489,6 +527,7 @@ export class TimeStamp implements Stringer {
489
527
  return max;
490
528
  }
491
529
 
530
+ /** @returns the minimum value of all the timestamps. */
492
531
  static min(...timestamps: CrudeTimeStamp[]): TimeStamp {
493
532
  let min = TimeStamp.MAX;
494
533
  for (const ts of timestamps) {
@@ -499,56 +538,56 @@ export class TimeStamp implements Stringer {
499
538
  }
500
539
 
501
540
  /** @returns a new TimeStamp n nanoseconds after the unix epoch */
502
- static nanoseconds(value: number): TimeStamp {
503
- return new TimeStamp(value);
541
+ static nanoseconds(value: number, tzInfo: TZInfo = "UTC"): TimeStamp {
542
+ return new TimeStamp(value, tzInfo);
504
543
  }
505
544
 
506
- /* One nanosecond after the unix epoch */
545
+ /** One nanosecond after the unix epoch */
507
546
  static readonly NANOSECOND = TimeStamp.nanoseconds(1);
508
547
 
509
548
  /** @returns a new TimeStamp n microseconds after the unix epoch */
510
- static microseconds(value: number): TimeStamp {
511
- return TimeStamp.nanoseconds(value * 1000);
549
+ static microseconds(value: number, tzInfo: TZInfo = "UTC"): TimeStamp {
550
+ return TimeStamp.nanoseconds(value * 1000, tzInfo);
512
551
  }
513
552
 
514
553
  /** One microsecond after the unix epoch */
515
554
  static readonly MICROSECOND = TimeStamp.microseconds(1);
516
555
 
517
556
  /** @returns a new TimeStamp n milliseconds after the unix epoch */
518
- static milliseconds(value: number): TimeStamp {
519
- return TimeStamp.microseconds(value * 1000);
557
+ static milliseconds(value: number, tzInfo: TZInfo = "UTC"): TimeStamp {
558
+ return TimeStamp.microseconds(value * 1000, tzInfo);
520
559
  }
521
560
 
522
561
  /** One millisecond after the unix epoch */
523
562
  static readonly MILLISECOND = TimeStamp.milliseconds(1);
524
563
 
525
564
  /** @returns a new TimeStamp n seconds after the unix epoch */
526
- static seconds(value: number): TimeStamp {
527
- return TimeStamp.milliseconds(value * 1000);
565
+ static seconds(value: number, tzInfo: TZInfo = "UTC"): TimeStamp {
566
+ return TimeStamp.milliseconds(value * 1000, tzInfo);
528
567
  }
529
568
 
530
569
  /** One second after the unix epoch */
531
570
  static readonly SECOND = TimeStamp.seconds(1);
532
571
 
533
572
  /** @returns a new TimeStamp n minutes after the unix epoch */
534
- static minutes(value: number): TimeStamp {
535
- return TimeStamp.seconds(value * 60);
573
+ static minutes(value: number, tzInfo: TZInfo = "UTC"): TimeStamp {
574
+ return TimeStamp.seconds(value * 60, tzInfo);
536
575
  }
537
576
 
538
577
  /** One minute after the unix epoch */
539
578
  static readonly MINUTE = TimeStamp.minutes(1);
540
579
 
541
580
  /** @returns a new TimeStamp n hours after the unix epoch */
542
- static hours(value: number): TimeStamp {
543
- return TimeStamp.minutes(value * 60);
581
+ static hours(value: number, tzInfo: TZInfo = "UTC"): TimeStamp {
582
+ return TimeStamp.minutes(value * 60, tzInfo);
544
583
  }
545
584
 
546
585
  /** One hour after the unix epoch */
547
586
  static readonly HOUR = TimeStamp.hours(1);
548
587
 
549
588
  /** @returns a new TimeStamp n days after the unix epoch */
550
- static days(value: number): TimeStamp {
551
- return TimeStamp.hours(value * 24);
589
+ static days(value: number, tzInfo: TZInfo = "UTC"): TimeStamp {
590
+ return TimeStamp.hours(value * 24, tzInfo);
552
591
  }
553
592
 
554
593
  /** One day after the unix epoch */
@@ -1078,14 +1117,27 @@ export class TimeRange implements Stringer {
1078
1117
  return new TimeRange(this.end, this.start);
1079
1118
  }
1080
1119
 
1120
+ get numericBounds(): bounds.Bounds<number> {
1121
+ return {
1122
+ lower: Number(this.start.valueOf()),
1123
+ upper: Number(this.end.valueOf()),
1124
+ };
1125
+ }
1126
+
1081
1127
  /**
1082
1128
  * Checks if the TimeRange is equal to the given TimeRange.
1083
1129
  *
1084
1130
  * @param other - The TimeRange to compare to.
1085
1131
  * @returns True if the TimeRange is equal to the given TimeRange.
1086
1132
  */
1087
- equals(other: TimeRange): boolean {
1088
- return this.start.equals(other.start) && this.end.equals(other.end);
1133
+ equals(other: TimeRange, delta: TimeSpan = TimeSpan.ZERO): boolean {
1134
+ if (delta.isZero)
1135
+ return this.start.equals(other.start) && this.end.equals(other.end);
1136
+ let startDist = this.start.sub(other.start).valueOf();
1137
+ let endDist = this.end.sub(other.end).valueOf();
1138
+ if (startDist < 0) startDist = -startDist;
1139
+ if (endDist < 0) endDist = -endDist;
1140
+ return startDist <= delta.valueOf() && endDist <= delta.valueOf();
1089
1141
  }
1090
1142
 
1091
1143
  toString(): string {
@@ -1097,11 +1149,14 @@ export class TimeRange implements Stringer {
1097
1149
  }
1098
1150
 
1099
1151
  /**
1100
- * Checks if if the two time ranges overlap. If the two time ranges are equal, returns
1101
- * true. If the start of one range is equal to the end of the other, returns false.
1102
- * Just follow the rule [start, end) i.e. start is inclusive and end is exclusive.
1152
+ * Checks if the two time ranges overlap. If the two time ranges are equal, returns
1153
+ * true. If the start of one range is equal to the end of the other, it returns false.
1154
+ * Just follow the rule [start, end), i.e., start is inclusive, and the end is exclusive.
1103
1155
  *
1104
1156
  * @param other - The other TimeRange to compare to.
1157
+ * @param delta - A TimeSpan representing the minimum amount of overlap for
1158
+ * overlap to return true. This allows for a slight amount of leeway when
1159
+ * checking for overlap.
1105
1160
  * @returns True if the two TimeRanges overlap, false otherwise.
1106
1161
  */
1107
1162
  overlapsWith(other: TimeRange, delta: TimeSpan = TimeSpan.ZERO): boolean {
@@ -1117,7 +1172,7 @@ export class TimeRange implements Stringer {
1117
1172
  const startOverlap = TimeStamp.max(rng.start, other.start);
1118
1173
  const endOverlap = TimeStamp.min(rng.end, other.end);
1119
1174
 
1120
- // If end of overlap is before start, then they don't overlap at all
1175
+ // If the end of overlap is before the start, then they don't overlap at all
1121
1176
  if (endOverlap.before(startOverlap)) return false;
1122
1177
 
1123
1178
  // Calculate the duration of the overlap
@@ -1127,14 +1182,6 @@ export class TimeRange implements Stringer {
1127
1182
  return overlapDuration.greaterThanOrEqual(delta);
1128
1183
  }
1129
1184
 
1130
- roughlyEquals(other: TimeRange, delta: TimeSpan): boolean {
1131
- let startDist = this.start.sub(other.start).valueOf();
1132
- let endDist = this.end.sub(other.end).valueOf();
1133
- if (startDist < 0) startDist = -startDist;
1134
- if (endDist < 0) endDist = -endDist;
1135
- return startDist <= delta.valueOf() && endDist <= delta.valueOf();
1136
- }
1137
-
1138
1185
  contains(other: TimeRange): boolean;
1139
1186
 
1140
1187
  contains(ts: CrudeTimeStamp): boolean;
@@ -1154,13 +1201,20 @@ export class TimeRange implements Stringer {
1154
1201
  return next;
1155
1202
  }
1156
1203
 
1204
+ static max(...others: TimeRange[]): TimeRange {
1205
+ return new TimeRange(
1206
+ TimeStamp.min(...others.map((o) => o.start)),
1207
+ TimeStamp.max(...others.map((o) => o.end)),
1208
+ );
1209
+ }
1210
+
1157
1211
  /** The maximum possible time range. */
1158
1212
  static readonly MAX = new TimeRange(TimeStamp.MIN, TimeStamp.MAX);
1159
1213
 
1160
1214
  /** The minimum possible time range. */
1161
1215
  static readonly MIN = new TimeRange(TimeStamp.MAX, TimeStamp.MIN);
1162
1216
 
1163
- /** A zero time range. */
1217
+ /** A time range whose start and end are both zero. */
1164
1218
  static readonly ZERO = new TimeRange(TimeStamp.ZERO, TimeStamp.ZERO);
1165
1219
 
1166
1220
  /** A zod schema for validating and transforming time ranges */
@@ -1181,22 +1235,29 @@ export const sortTimeRange = (a: TimeRange, b: TimeRange): -1 | 0 | 1 => {
1181
1235
  };
1182
1236
 
1183
1237
  /** DataType is a string that represents a data type. */
1184
- export class DataType extends String implements Stringer {
1238
+ export class DataType implements Stringer {
1239
+ readonly value: string;
1240
+ readonly encodeValue = true;
1241
+
1242
+ valueOf(): string {
1243
+ return this.value;
1244
+ }
1245
+
1185
1246
  constructor(value: CrudeDataType) {
1186
1247
  if (
1187
1248
  value instanceof DataType ||
1188
1249
  typeof value === "string" ||
1189
1250
  typeof value.valueOf() === "string"
1190
1251
  ) {
1191
- super(value.valueOf());
1252
+ this.value = value.valueOf() as string;
1192
1253
  return;
1193
1254
  }
1194
1255
  const t = DataType.ARRAY_CONSTRUCTOR_DATA_TYPES.get(value.constructor.name);
1195
1256
  if (t != null) {
1196
- super(t.valueOf());
1257
+ this.value = t.valueOf();
1197
1258
  return;
1198
1259
  }
1199
- super(DataType.UNKNOWN.valueOf());
1260
+ this.value = DataType.UNKNOWN.valueOf();
1200
1261
  throw new Error(`unable to find data type for ${value.toString()}`);
1201
1262
  }
1202
1263
 
@@ -1252,6 +1313,7 @@ export class DataType extends String implements Stringer {
1252
1313
  return v;
1253
1314
  }
1254
1315
 
1316
+ /** @returns ture if the data type is an unsigned numeric integer type */
1255
1317
  get isUnsigned(): boolean {
1256
1318
  return (
1257
1319
  this.equals(DataType.UINT8) ||
@@ -1261,6 +1323,7 @@ export class DataType extends String implements Stringer {
1261
1323
  );
1262
1324
  }
1263
1325
 
1326
+ /** @returns true if the data type is a signed numeric integer type */
1264
1327
  get isSigned(): boolean {
1265
1328
  return (
1266
1329
  this.equals(DataType.INT8) ||
@@ -1296,16 +1359,6 @@ export class DataType extends String implements Stringer {
1296
1359
  return this.equals(other);
1297
1360
  }
1298
1361
 
1299
- /**
1300
- * Checks whether the given TypedArray is of the same type as the DataType.
1301
- *
1302
- * @param array - The TypedArray to check.
1303
- * @returns True if the TypedArray is of the same type as the DataType.
1304
- */
1305
- checkArray(array: TypedArray): boolean {
1306
- return array.constructor === this.Array;
1307
- }
1308
-
1309
1362
  toJSON(): string {
1310
1363
  return this.toString();
1311
1364
  }
@@ -1443,7 +1496,7 @@ export class Size extends Number implements Stringer {
1443
1496
  return this.valueOf() > other.valueOf();
1444
1497
  }
1445
1498
 
1446
- /** @returns true if the Size is smaller than the other sisze. */
1499
+ /** @returns true if the Size is smaller than the other size. */
1447
1500
  smallerThan(other: CrudeSize): boolean {
1448
1501
  return this.valueOf() < other.valueOf();
1449
1502
  }
@@ -1682,9 +1735,9 @@ export const convertDataType = (
1682
1735
  };
1683
1736
 
1684
1737
  export const addSamples = (a: math.Numeric, b: math.Numeric): math.Numeric => {
1738
+ if (b == 0) return a;
1739
+ if (a == 0) return b;
1685
1740
  if (typeof a === "bigint" && typeof b === "bigint") return a + b;
1686
1741
  if (typeof a === "number" && typeof b === "number") return a + b;
1687
- if (b === 0) return a;
1688
- if (a === 0) return b;
1689
1742
  return Number(a) + Number(b);
1690
1743
  };
package/src/toArray.ts CHANGED
@@ -10,5 +10,5 @@
10
10
  export const toArray = <T>(value: T | T[]): T[] =>
11
11
  Array.isArray(value) ? value : [value];
12
12
 
13
- export const nullToArr = <T>(value: T | T[] | null): T[] =>
14
- Array.isArray(value) ? value : value === null ? [] : [value];
13
+ export const nullToArr = <T>(value: T | T[] | null | undefined): T[] =>
14
+ Array.isArray(value) ? value : value == null ? [] : [value];
@@ -7,7 +7,7 @@
7
7
  // License, use of this software will be governed by the Apache License, Version 2.0,
8
8
  // included in the file licenses/APL.txt.
9
9
 
10
- import { describe, expect, it } from "vitest";
10
+ import { describe, expect, it, test } from "vitest";
11
11
  import { z } from "zod";
12
12
 
13
13
  import { zod } from "@/zod";
@@ -97,5 +97,21 @@ describe("zod", () => {
97
97
  expect(v).toBeInstanceOf(z.ZodNumber);
98
98
  });
99
99
  });
100
+
101
+ describe("regression", () => {
102
+ test("reg 1", () => {
103
+ const names = ["one"];
104
+ const schema = z
105
+ .object({
106
+ name: z.string(),
107
+ })
108
+ .refine(({ name }) => !names.includes(name), {
109
+ error: "Already in use",
110
+ });
111
+ const v = zod.getFieldSchema(schema, "name");
112
+ expect(v).toBeInstanceOf(z.ZodString);
113
+ expect(v.isOptional()).toBe(false);
114
+ });
115
+ });
100
116
  });
101
117
  });
package/src/zod/util.ts CHANGED
@@ -26,7 +26,9 @@ const sourceTypeGetter = (obj: unknown, key: string): z.ZodAny | null => {
26
26
  if (obj == null) return null;
27
27
  const v = (obj as Record<string, z.ZodAny>)[key];
28
28
  if (v == null && typeof obj === "object" && "sourceType" in obj) {
29
- const sourceType = (obj as z.ZodEffects<z.ZodTypeAny>).sourceType();
29
+ const sourceType = (
30
+ obj as { sourceType: () => z.ZodObject<z.ZodRawShape> }
31
+ ).sourceType();
30
32
  return (sourceType as unknown as UnknownRecord)[key] as z.ZodAny | null;
31
33
  }
32
34
  return v;
@@ -38,7 +40,7 @@ export const getFieldSchema: deep.TypedGet<z.ZodTypeAny, z.ZodTypeAny> = ((
38
40
  options?: Omit<deep.GetOptions, "getter">,
39
41
  ): z.ZodTypeAny | null =>
40
42
  deep.get<z.ZodTypeAny, z.ZodTypeAny>(
41
- sourceTypeGetter(schema, "shape") as unknown as z.AnyZodObject,
43
+ sourceTypeGetter(schema, "shape") as unknown as z.ZodObject<z.ZodRawShape>,
42
44
  getFieldSchemaPath(path),
43
45
  { ...options, getter: sourceTypeGetter } as deep.GetOptions<boolean | undefined>,
44
46
  )) as deep.TypedGet<z.ZodTypeAny, z.ZodTypeAny>;