@synnaxlabs/x 0.37.0 → 0.38.0

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 (122) hide show
  1. package/.turbo/turbo-build.log +35 -31
  2. package/dist/box-BZ6d2d5A.cjs +1 -0
  3. package/dist/box-Cto-5Uxu.js +201 -0
  4. package/dist/box.cjs +1 -1
  5. package/dist/box.js +1 -1
  6. package/dist/compare.cjs +1 -1
  7. package/dist/compare.js +1 -1
  8. package/dist/external-C-dNgNQw.cjs +1 -0
  9. package/dist/external-Cax-LfQW.cjs +1 -0
  10. package/dist/external-DqPrWKvU.js +47 -0
  11. package/dist/{external-DKQKvgIi.js → external-vFGUdZf6.js} +9 -9
  12. package/dist/index-BG3Scw3G.cjs +1 -0
  13. package/dist/{index-HQonyH7n.js → index-BVC_8Cg9.js} +1 -1
  14. package/dist/index-D4NCYiQB.js +19 -0
  15. package/dist/index-udOjA9d-.cjs +1 -0
  16. package/dist/index.cjs +2 -2
  17. package/dist/index.js +56 -54
  18. package/dist/kv.cjs +1 -1
  19. package/dist/kv.js +2 -2
  20. package/dist/link.cjs +1 -0
  21. package/dist/link.js +10 -0
  22. package/dist/position-CvSNZkSD.cjs +1 -0
  23. package/dist/position-GeF1oEYk.js +85 -0
  24. package/dist/position.cjs +1 -1
  25. package/dist/position.js +1 -1
  26. package/dist/runtime.cjs +1 -1
  27. package/dist/runtime.js +1 -1
  28. package/dist/{scale-BESJN9LG.cjs → scale-BTgf0Mr-.cjs} +1 -1
  29. package/dist/{scale-CV3O6tkY.js → scale-Dh1UNRoC.js} +2 -2
  30. package/dist/scale.cjs +1 -1
  31. package/dist/scale.js +1 -1
  32. package/dist/series-BMhEEJZL.cjs +11 -0
  33. package/dist/{series-C_B2hAp9.js → series-DxDIugLj.js} +295 -285
  34. package/dist/spatial.cjs +1 -1
  35. package/dist/spatial.js +4 -4
  36. package/dist/src/deep/path.d.ts.map +1 -1
  37. package/dist/src/index.d.ts +1 -0
  38. package/dist/src/index.d.ts.map +1 -1
  39. package/dist/src/kv/external.d.ts +3 -0
  40. package/dist/src/kv/external.d.ts.map +1 -0
  41. package/dist/src/kv/index.d.ts +1 -1
  42. package/dist/src/kv/index.d.ts.map +1 -1
  43. package/dist/src/kv/mock.d.ts +24 -0
  44. package/dist/src/kv/mock.d.ts.map +1 -0
  45. package/dist/src/kv/mock.spec.d.ts +2 -0
  46. package/dist/src/kv/mock.spec.d.ts.map +1 -0
  47. package/dist/src/kv/types.d.ts +18 -18
  48. package/dist/src/kv/types.d.ts.map +1 -1
  49. package/dist/src/link/index.d.ts +2 -0
  50. package/dist/src/link/index.d.ts.map +1 -0
  51. package/dist/src/link/link.d.ts +2 -0
  52. package/dist/src/link/link.d.ts.map +1 -0
  53. package/dist/src/link/link.spec.d.ts +2 -0
  54. package/dist/src/link/link.spec.d.ts.map +1 -0
  55. package/dist/src/runtime/os.d.ts.map +1 -1
  56. package/dist/src/telem/index.d.ts +1 -1
  57. package/dist/src/telem/index.d.ts.map +1 -1
  58. package/dist/src/telem/series.d.ts +1 -0
  59. package/dist/src/telem/series.d.ts.map +1 -1
  60. package/dist/src/telem/telem.d.ts +2 -0
  61. package/dist/src/telem/telem.d.ts.map +1 -1
  62. package/dist/src/unique/index.d.ts +2 -0
  63. package/dist/src/unique/index.d.ts.map +1 -0
  64. package/dist/src/unique/unique.d.ts +42 -0
  65. package/dist/src/unique/unique.d.ts.map +1 -0
  66. package/dist/src/unique/unique.spec.d.ts +2 -0
  67. package/dist/src/unique/unique.spec.d.ts.map +1 -0
  68. package/dist/src/zodutil/index.d.ts.map +1 -1
  69. package/dist/src/zodutil/zodutil.d.ts +1 -16
  70. package/dist/src/zodutil/zodutil.d.ts.map +1 -1
  71. package/dist/telem.cjs +1 -1
  72. package/dist/telem.js +1 -1
  73. package/dist/unique.cjs +1 -1
  74. package/dist/unique.js +2 -2
  75. package/dist/url.cjs +1 -1
  76. package/dist/url.js +5 -5
  77. package/dist/{xy-LADI2wVU.cjs → xy-Budz-qz-.cjs} +1 -1
  78. package/dist/{xy-DQdccWlc.js → xy-DxjPL2DZ.js} +4 -4
  79. package/dist/xy.cjs +1 -1
  80. package/dist/xy.js +1 -1
  81. package/dist/zodutil-BfrF8jE3.js +23 -0
  82. package/dist/zodutil-DFJyyQd2.cjs +1 -0
  83. package/dist/zodutil.cjs +1 -1
  84. package/dist/zodutil.js +1 -1
  85. package/package.json +5 -7
  86. package/src/caseconv/caseconv.spec.ts +4 -20
  87. package/src/compare/compare.ts +2 -2
  88. package/src/deep/path.ts +1 -1
  89. package/src/index.ts +1 -0
  90. package/src/{unique.ts → kv/external.ts} +2 -1
  91. package/src/kv/index.ts +1 -1
  92. package/src/kv/mock.spec.ts +101 -0
  93. package/src/kv/mock.ts +58 -0
  94. package/src/kv/types.ts +22 -28
  95. package/src/link/index.ts +10 -0
  96. package/src/link/link.spec.ts +68 -0
  97. package/src/link/link.ts +21 -0
  98. package/src/runtime/os.ts +1 -1
  99. package/src/telem/index.ts +1 -1
  100. package/src/telem/series.spec.ts +21 -0
  101. package/src/telem/series.ts +15 -8
  102. package/src/telem/telem.ts +34 -10
  103. package/src/unique/index.ts +10 -0
  104. package/src/unique/unique.spec.ts +192 -0
  105. package/src/unique/unique.ts +67 -0
  106. package/src/zodutil/index.ts +1 -1
  107. package/src/zodutil/zodutil.ts +1 -29
  108. package/tsconfig.tsbuildinfo +1 -1
  109. package/vite.config.ts +6 -8
  110. package/dist/box-BHIyGhI_.cjs +0 -1
  111. package/dist/box-CfhGfJJH.js +0 -201
  112. package/dist/external-CghVMqCA.cjs +0 -1
  113. package/dist/index-h-QAL9T1.cjs +0 -1
  114. package/dist/position-BXFz7I9G.js +0 -85
  115. package/dist/position-CFiSGTz9.cjs +0 -1
  116. package/dist/series-BbxR21uO.cjs +0 -11
  117. package/dist/src/unique.d.ts +0 -2
  118. package/dist/src/unique.d.ts.map +0 -1
  119. package/dist/types-BpAJW2TM.js +0 -11
  120. package/dist/types-zRwnQ1hc.cjs +0 -1
  121. package/dist/zodutil-BRjUdYAv.cjs +0 -1
  122. package/dist/zodutil-DI4gVZkT.js +0 -27
@@ -0,0 +1,192 @@
1
+ // Copyright 2024 Synnax Labs, Inc.
2
+ //
3
+ // Use of this software is governed by the Business Source License included in the file
4
+ // licenses/BSL.txt.
5
+ //
6
+ // As of the Change Date specified in that file, in accordance with the Business Source
7
+ // License, use of this software will be governed by the Apache License, Version 2.0,
8
+ // included in the file licenses/APL.txt.
9
+
10
+ import { describe, expect, it } from "vitest";
11
+
12
+ import { unique } from "@/unique";
13
+
14
+ describe("unique", () => {
15
+ it("removes duplicate primitive values", () => {
16
+ const result = unique.unique([1, 2, 2, 3, 4, 4, 5]);
17
+ expect(result).toEqual([1, 2, 3, 4, 5]);
18
+ });
19
+
20
+ it("works with strings", () => {
21
+ const result = unique.unique(["a", "b", "a", "c", "b"]);
22
+ expect(result).toEqual(["a", "b", "c"]);
23
+ });
24
+
25
+ it("works with mixed types", () => {
26
+ const result = unique.unique([1, "1", 2, "2", 1, "1"]);
27
+ expect(result).toEqual([1, "1", 2, "2"]);
28
+ });
29
+
30
+ it("handles an empty array", () => {
31
+ const result = unique.unique([]);
32
+ expect(result).toEqual([]);
33
+ });
34
+
35
+ it("works with readonly arrays", () => {
36
+ const values: readonly number[] = [1, 1, 2, 3];
37
+ const result = unique.unique(values);
38
+ expect(result).toEqual([1, 2, 3]);
39
+ });
40
+ });
41
+
42
+ describe("by", () => {
43
+ interface IDTestCase {
44
+ id: number;
45
+ }
46
+
47
+ it("removes duplicates based on a key function and keeps the first instance by default", () => {
48
+ const result = unique.by(
49
+ [
50
+ { id: 1, name: "A" },
51
+ { id: 2, name: "B" },
52
+ { id: 1, name: "C" },
53
+ ],
54
+ (value: IDTestCase) => value.id,
55
+ );
56
+ expect(result).toEqual([
57
+ { id: 1, name: "A" },
58
+ { id: 2, name: "B" },
59
+ ]);
60
+ });
61
+
62
+ it("removes duplicates based on a key function and keeps the first instance when keepFirst is true", () => {
63
+ const result = unique.by(
64
+ [
65
+ { id: 1, name: "A" },
66
+ { id: 2, name: "B" },
67
+ { id: 1, name: "C" },
68
+ ],
69
+ (value: IDTestCase) => value.id,
70
+ true,
71
+ );
72
+ expect(result).toEqual([
73
+ { id: 1, name: "A" },
74
+ { id: 2, name: "B" },
75
+ ]);
76
+ });
77
+
78
+ it("removes duplicates based on a key function and keeps the last instance when keepFirst is false", () => {
79
+ const result = unique.by(
80
+ [
81
+ { id: 1, name: "A" },
82
+ { id: 2, name: "B" },
83
+ { id: 1, name: "C" },
84
+ ],
85
+ (value: IDTestCase) => value.id,
86
+ false,
87
+ );
88
+ expect(result).toEqual([
89
+ { id: 2, name: "B" },
90
+ { id: 1, name: "C" },
91
+ ]);
92
+ });
93
+
94
+ interface ValueTestCase {
95
+ value: string;
96
+ }
97
+
98
+ it("works with a custom key function", () => {
99
+ const result = unique.by(
100
+ [{ value: "apple" }, { value: "banana" }, { value: "apple" }],
101
+ (v: ValueTestCase) => v.value,
102
+ );
103
+ expect(result).toEqual([{ value: "apple" }, { value: "banana" }]);
104
+ });
105
+
106
+ it("handles an empty array", () => {
107
+ const result = unique.by([], (v: unknown) => v);
108
+ expect(result).toEqual([]);
109
+ });
110
+
111
+ it("works with readonly arrays and keeps the first instance by default", () => {
112
+ const values: readonly { id: number; name: string }[] = [
113
+ { id: 1, name: "A" },
114
+ { id: 1, name: "B" },
115
+ ];
116
+ const result = unique.by(values, (v: IDTestCase) => v.id);
117
+ expect(result).toEqual([{ id: 1, name: "A" }]);
118
+ });
119
+
120
+ it("works with readonly arrays and keeps the last instance when keepFirst is false", () => {
121
+ const values: readonly { id: number; name: string }[] = [
122
+ { id: 1, name: "A" },
123
+ { id: 1, name: "B" },
124
+ ];
125
+ const result = unique.by(values, (v: IDTestCase) => v.id, false);
126
+ expect(result).toEqual([{ id: 1, name: "B" }]);
127
+ });
128
+
129
+ interface ComplexTestCase {
130
+ id: number;
131
+ nested: { value: string };
132
+ }
133
+
134
+ it("works with complex keys and keeps the first instance by default", () => {
135
+ const result = unique.by(
136
+ [
137
+ { id: 1, nested: { value: "A" } },
138
+ { id: 1, nested: { value: "B" } },
139
+ { id: 1, nested: { value: "A", otherKey: "4" } },
140
+ ],
141
+ (v: ComplexTestCase) => `${v.id}-${v.nested.value}`,
142
+ );
143
+ expect(result).toEqual([
144
+ { id: 1, nested: { value: "A" } },
145
+ { id: 1, nested: { value: "B" } },
146
+ ]);
147
+ });
148
+
149
+ it("works with complex keys and keeps the last instance when keepFirst is false", () => {
150
+ const result = unique.by(
151
+ [
152
+ { id: 1, nested: { value: "A" } },
153
+ { id: 1, nested: { value: "B" } },
154
+ { id: 1, nested: { value: "A", otherKey: "4" } },
155
+ ],
156
+ (v: ComplexTestCase) => `${v.id}-${v.nested.value}`,
157
+ false,
158
+ );
159
+ expect(result).toEqual([
160
+ { id: 1, nested: { value: "B" } },
161
+ { id: 1, nested: { value: "A", otherKey: "4" } },
162
+ ]);
163
+ });
164
+
165
+ it("handles cases where all keys are unique", () => {
166
+ const result = unique.by(
167
+ [
168
+ { id: 1, name: "A" },
169
+ { id: 2, name: "B" },
170
+ { id: 3, name: "C" },
171
+ ],
172
+ (v: IDTestCase) => v.id,
173
+ );
174
+ expect(result).toEqual([
175
+ { id: 1, name: "A" },
176
+ { id: 2, name: "B" },
177
+ { id: 3, name: "C" },
178
+ ]);
179
+ });
180
+
181
+ it("handles cases where all values are identical", () => {
182
+ const result = unique.by(
183
+ [
184
+ { id: 1, name: "A" },
185
+ { id: 1, name: "A" },
186
+ { id: 1, name: "A" },
187
+ ],
188
+ (v: IDTestCase) => v.id,
189
+ );
190
+ expect(result).toEqual([{ id: 1, name: "A" }]);
191
+ });
192
+ });
@@ -0,0 +1,67 @@
1
+ // Copyright 2024 Synnax Labs, Inc.
2
+ //
3
+ // Use of this software is governed by the Business Source License included in the file
4
+ // licenses/BSL.txt.
5
+ //
6
+ // As of the Change Date specified in that file, in accordance with the Business Source
7
+ // License, use of this software will be governed by the Apache License, Version 2.0,
8
+ // included in the file licenses/APL.txt.
9
+
10
+ /**
11
+ * Removes duplicate values from an array, preserving the order of the first occurrence
12
+ * of each unique value.
13
+ *
14
+ * @param values - An array or readonly array of values to deduplicate.
15
+ * @returns A new array containing only unique values.
16
+ *
17
+ * @example
18
+ * ```typescript
19
+ * unique([1, 2, 2, 3, 4, 4, 5]); // [1, 2, 3, 4, 5]
20
+ * ```
21
+ */
22
+ export const unique = <V>(values: V[] | readonly V[]): V[] => [...new Set(values)];
23
+
24
+ /**
25
+ * Removes duplicate values from an array based on a key function, preserving either
26
+ * the first or last occurrence of each unique key. If
27
+ *
28
+ * @param values - An array or readonly array of values to deduplicate.
29
+ * @param key - A function that generates a unique key for each value.
30
+ * @param keepFirst - An optional boolean indicating whether to keep the first instance
31
+ * (`true`, default) or the last instance (`false`) of each unique key.
32
+ * @returns A new array containing only unique values based on the generated keys.
33
+ *
34
+ * @example
35
+ * // Default behavior (keep first instance):
36
+ * by(
37
+ * [{ id: 1, name: "A" }, { id: 2, name: "B" }, { id: 1, name: "C" }],
38
+ * (value) => value.id
39
+ * );
40
+ * // Result: [{ id: 1, name: "A" }, { id: 2, name: "B" }]
41
+ *
42
+ * @example
43
+ * // Keep last instance:
44
+ * by(
45
+ * [{ id: 1, name: "A" }, { id: 2, name: "B" }, { id: 1, name: "C" }],
46
+ * (value) => value.id,
47
+ * false
48
+ * );
49
+ * // Result: [{ id: 2, name: "B" }, { id: 1, name: "C" }]
50
+ */
51
+ export const by = <V>(
52
+ values: V[] | readonly V[],
53
+ key: (value: V) => unknown,
54
+ keepFirst: boolean = true,
55
+ ): V[] => {
56
+ const map = new Map<unknown, V>();
57
+ values.forEach((v) => {
58
+ const k = key(v);
59
+ if (map.has(k)) {
60
+ if (keepFirst) return;
61
+ map.delete(k);
62
+ }
63
+ // different delete and set operations for keepLast so order is preserved
64
+ map.set(k, v);
65
+ });
66
+ return Array.from(map.values());
67
+ };
@@ -7,4 +7,4 @@
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
- export * as zodutil from "./zodutil";
10
+ export * as zodutil from "@/zodutil/zodutil";
@@ -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 { z, type ZodSchema } from "zod";
10
+ import { z } from "zod";
11
11
 
12
12
  import { deep } from "@/deep";
13
13
  import { type UnknownRecord } from "@/record";
@@ -43,32 +43,4 @@ export const getFieldSchema: deep.TypedGet<z.ZodTypeAny, z.ZodTypeAny> = ((
43
43
  { ...options, getter: sourceTypeGetter } as deep.GetOptions<boolean | undefined>,
44
44
  ) as z.ZodTypeAny | null) as deep.TypedGet<z.ZodTypeAny, z.ZodTypeAny>;
45
45
 
46
- /**
47
- * Creates a transformer function that validates and transforms input values based on
48
- * provided schemas. The first schema to successfully validate the input value is used
49
- * in the transformation. If no schema is found that validates the input, the
50
- * transformer function returns null.
51
- *
52
- * @template Input - The type of the input value.
53
- * @template Output - The type of the output value.
54
- * @param transform - The function to transform the input value to the output value.
55
- * @param schemas - An array of Zod schemas to validate the input value against.
56
- * @returns A function that takes an unknown value, validates it against the schemas,
57
- * and uses the first valid schema to transform the input type. If no schema can
58
- * validate the input, the function returns null.
59
- */
60
- export const transformer =
61
- <Input, Output>(
62
- transform: (input: Input) => Output,
63
- schemas: ZodSchema<Input>[],
64
- ): ((value: unknown) => Output | null) =>
65
- (value) => {
66
- const matchingSchema = schemas.find((schema) => {
67
- const res = schema.safeParse(value);
68
- return res.success;
69
- });
70
- if (matchingSchema == null) return null;
71
- return transform(matchingSchema.parse(value));
72
- };
73
-
74
46
  export const bigInt = z.bigint().or(z.string().transform(BigInt));