@synnaxlabs/x 0.15.0 → 0.15.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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@synnaxlabs/x",
3
3
  "private": false,
4
- "version": "0.15.0",
4
+ "version": "0.15.1",
5
5
  "type": "module",
6
6
  "description": "Common Utilities for Synnax Labs",
7
7
  "repository": "https://github.com/synnaxlabs/synnax/tree/main/x/go",
@@ -16,7 +16,6 @@
16
16
  "dependencies": {
17
17
  "async-mutex": "^0.4.0",
18
18
  "js-convert-case": "^4.2.0",
19
- "msgpackr": "^1.9.9",
20
19
  "nanoid": "^5.0.2",
21
20
  "typescript": "^5.3.3",
22
21
  "zod": "3.22.4"
@@ -7,7 +7,6 @@
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 { pack, unpack } from "msgpackr";
11
10
  import { type ZodSchema, type z } from "zod";
12
11
 
13
12
  import { Case } from "@/case";
@@ -38,23 +37,6 @@ export interface EncoderDecoder {
38
37
  decode: <P>(data: Uint8Array | ArrayBuffer, schema?: ZodSchema<P>) => P;
39
38
  }
40
39
 
41
- /** MsgpackEncoderDecoder is a msgpack implementation of EncoderDecoder. */
42
- export class MsgpackEncoderDecoder implements EncoderDecoder {
43
- contentType = "application/msgpack";
44
-
45
- encode(payload: unknown): ArrayBuffer {
46
- return pack(Case.toSnake(payload));
47
- }
48
-
49
- decode<P extends z.ZodTypeAny>(
50
- data: Uint8Array | ArrayBuffer,
51
- schema?: P,
52
- ): z.output<P> {
53
- const unpacked = Case.toCamel(unpack(new Uint8Array(data)));
54
- return schema != null ? schema.parse(unpacked) : (unpacked as P);
55
- }
56
- }
57
-
58
40
  /** JSONEncoderDecoder is a JSON implementation of EncoderDecoder. */
59
41
  export class JSONEncoderDecoder implements EncoderDecoder {
60
42
  contentType = "application/json";
@@ -88,7 +70,4 @@ export class JSONEncoderDecoder implements EncoderDecoder {
88
70
  static registerCustomType(): void {}
89
71
  }
90
72
 
91
- export const ENCODERS: EncoderDecoder[] = [
92
- new MsgpackEncoderDecoder(),
93
- new JSONEncoderDecoder(),
94
- ];
73
+ export const ENCODERS: EncoderDecoder[] = [new JSONEncoderDecoder()];
@@ -529,6 +529,49 @@ describe("Series", () => {
529
529
  ]);
530
530
  });
531
531
  });
532
+
533
+ describe("as", () => {
534
+ describe("number", () => {
535
+ it("should correctly interpret the series as numeric", () => {
536
+ const s = new Series([1, 2, 3]);
537
+ const s2 = s.as("number");
538
+ expect(s2.at(0)).toEqual(1);
539
+ });
540
+ it("should throw an error if the series is not numeric", () => {
541
+ const s = new Series(["a", "b", "c"]);
542
+ expect(() => {
543
+ s.as("number");
544
+ }).toThrow();
545
+ });
546
+ });
547
+ describe("string", () => {
548
+ it("should correctly interpret the series as a string", () => {
549
+ const s = new Series(["apple", "banana", "carrot"]);
550
+ const s2 = s.as("string");
551
+ expect(s2.at(0)).toEqual("apple");
552
+ });
553
+ it("should throw an error if the series is not a string", () => {
554
+ const s = new Series([1, 2, 3]);
555
+ expect(() => {
556
+ s.as("string");
557
+ }).toThrow();
558
+ });
559
+ });
560
+ describe("bigint", () => {
561
+ it("should correctly interpret the series as a bigint", () => {
562
+ const s = new Series([BigInt(1), BigInt(2), BigInt(3)]);
563
+ console.log(s.dataType);
564
+ const s2 = s.as("bigint");
565
+ expect(s2.at(0)).toEqual(BigInt(1));
566
+ });
567
+ it("should throw an error if the series is not a bigint", () => {
568
+ const s = new Series([1, 2, 3]);
569
+ expect(() => {
570
+ s.as("bigint");
571
+ }).toThrow();
572
+ });
573
+ });
574
+ });
532
575
  });
533
576
 
534
577
  describe("MultiSeries", () => {
@@ -577,20 +577,35 @@ export class Series<T extends TelemValue = TelemValue> {
577
577
  }
578
578
  }
579
579
 
580
- as(dataType: "string"): Series<string>;
580
+ as(jsType: "string"): Series<string>;
581
581
 
582
- as(dataType: "number"): Series<number>;
582
+ as(jsType: "number"): Series<number>;
583
583
 
584
- as(dataType: "bigint"): Series<bigint>;
584
+ as(jsType: "bigint"): Series<bigint>;
585
585
 
586
- as(dataType: "numeric"): Series<NumericTelemValue>;
587
-
588
- as<T extends TelemValue>(dataType: CrudeDataType): Series<T> {
589
- if (!new DataType(dataType).equals(this.dataType))
590
- throw new Error(
591
- `cannot convert series of type ${this.dataType.toString()} to ${dataType.toString()}`,
592
- );
593
- return this as unknown as Series<T>;
586
+ as<T extends TelemValue>(jsType: "string" | "number" | "bigint"): Series<T> {
587
+ if (jsType === "string") {
588
+ if (!this.dataType.equals(DataType.STRING))
589
+ throw new Error(
590
+ `cannot convert series of type ${this.dataType.toString()} to string`,
591
+ );
592
+ return this as unknown as Series<T>;
593
+ }
594
+ if (jsType === "number") {
595
+ if (!this.dataType.isNumeric)
596
+ throw new Error(
597
+ `cannot convert series of type ${this.dataType.toString()} to number`,
598
+ );
599
+ return this as unknown as Series<T>;
600
+ }
601
+ if (jsType === "bigint") {
602
+ if (!this.dataType.equals(DataType.INT64))
603
+ throw new Error(
604
+ `cannot convert series of type ${this.dataType.toString()} to bigint`,
605
+ );
606
+ return this as unknown as Series<T>;
607
+ }
608
+ throw new Error(`cannot convert series to ${jsType as string}`);
594
609
  }
595
610
 
596
611
  get digest(): SeriesDigest {
@@ -748,6 +763,8 @@ export const addSamples = (
748
763
  ): NumericTelemValue => {
749
764
  if (typeof a === "bigint" && typeof b === "bigint") return a + b;
750
765
  if (typeof a === "number" && typeof b === "number") return a + b;
766
+ if (b === 0) return a;
767
+ if (a === 0) return b;
751
768
  return Number(a) + Number(b);
752
769
  };
753
770
 
@@ -764,13 +781,11 @@ export class MultiSeries<T extends TelemValue = TelemValue> implements Iterable<
764
781
  this.series = series;
765
782
  }
766
783
 
767
- as(dataType: "string"): MultiSeries<string>;
768
-
769
- as(dataType: "number"): MultiSeries<number>;
784
+ as(jsType: "string"): MultiSeries<string>;
770
785
 
771
- as(dataType: "bigint"): MultiSeries<bigint>;
786
+ as(jsType: "number"): MultiSeries<number>;
772
787
 
773
- as(dataType: "numeric"): MultiSeries<NumericTelemValue>;
788
+ as(jsType: "bigint"): MultiSeries<bigint>;
774
789
 
775
790
  as<T extends TelemValue>(dataType: CrudeDataType): MultiSeries<T> {
776
791
  if (!new DataType(dataType).equals(this.dataType))
@@ -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 { object, z } from "zod";
10
+ import { z } from "zod";
11
11
 
12
12
  import { type Stringer } from "@/primitive";
13
13
  import { addSamples } from "@/telem/series";
@@ -1049,6 +1049,18 @@ export class DataType extends String implements Stringer {
1049
1049
  return this.equals(DataType.JSON) || this.equals(DataType.STRING);
1050
1050
  }
1051
1051
 
1052
+ get isNumeric(): boolean {
1053
+ return !this.isVariable && !this.equals(DataType.UUID);
1054
+ }
1055
+
1056
+ get isInteger(): boolean {
1057
+ return this.toString().startsWith("int");
1058
+ }
1059
+
1060
+ get isFloat(): boolean {
1061
+ return this.toString().startsWith("float");
1062
+ }
1063
+
1052
1064
  get density(): Density {
1053
1065
  const v = DataType.DENSITIES.get(this.toString());
1054
1066
  if (v == null) throw new Error(`unable to find density for ${this.valueOf()}`);