@synnaxlabs/x 0.42.0 → 0.42.3
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/.turbo/turbo-build.log +7 -7
- package/dist/deep.cjs +1 -1
- package/dist/deep.js +84 -74
- package/dist/index.cjs +3 -3
- package/dist/index.js +281 -280
- package/dist/src/color/color.d.ts +14 -3
- package/dist/src/color/color.d.ts.map +1 -1
- package/dist/src/color/gradient.d.ts +6 -2
- package/dist/src/color/gradient.d.ts.map +1 -1
- package/dist/src/color/palette.d.ts +10 -2
- package/dist/src/color/palette.d.ts.map +1 -1
- package/dist/src/deep/merge.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/color/color.spec.ts +8 -0
- package/src/color/color.ts +9 -3
- package/src/deep/merge.spec.ts +60 -0
- package/src/deep/merge.ts +12 -1
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
2
|
declare const hexZ: z.ZodString;
|
|
3
|
+
declare const legacyObjectZ: z.ZodObject<{
|
|
4
|
+
rgba255: z.ZodTuple<[z.ZodNumber, z.ZodNumber, z.ZodNumber, z.ZodNumber], null>;
|
|
5
|
+
}, {}>;
|
|
3
6
|
/** A color in RGBA format. */
|
|
4
7
|
export type RGBA = [number, number, number, number];
|
|
5
8
|
/** A color in HSLA format. */
|
|
@@ -10,6 +13,8 @@ export type RGB = [number, number, number];
|
|
|
10
13
|
export type Hex = z.infer<typeof hexZ>;
|
|
11
14
|
/** A color in RGBA format. Used as the standard representation of a color in this package. */
|
|
12
15
|
export type Color = RGBA;
|
|
16
|
+
/** A legacy color object. */
|
|
17
|
+
type LegacyObject = z.infer<typeof legacyObjectZ>;
|
|
13
18
|
/** @returns true if the given color can be parsed into a valid color object. */
|
|
14
19
|
export declare const isCrude: (color: unknown) => color is Crude;
|
|
15
20
|
/** @returns true if the color is a true Color type. */
|
|
@@ -18,7 +23,7 @@ export declare const isColor: (color: unknown) => color is Color;
|
|
|
18
23
|
* An unparsed representation of a color i.e. a value that can be converted into
|
|
19
24
|
* a Color object.
|
|
20
25
|
*/
|
|
21
|
-
export type Crude = Hex | RGBA | Color | string | RGB;
|
|
26
|
+
export type Crude = Hex | RGBA | Color | string | RGB | LegacyObject;
|
|
22
27
|
/**
|
|
23
28
|
* Converts a crude color to its most meaningful CSS format.
|
|
24
29
|
* @returns undefined if the color is undefined.
|
|
@@ -111,13 +116,19 @@ export declare const isDark: (color: Crude) => boolean;
|
|
|
111
116
|
/** @returns true if the color is light i.e. the luminance is greater than or equal to 0.5. */
|
|
112
117
|
export declare const isLight: (color: Crude) => boolean;
|
|
113
118
|
/** A zod schema to parse color values from various crude representations. */
|
|
114
|
-
export declare const colorZ: z.ZodPipe<z.ZodUnion<readonly [z.ZodString, z.ZodTuple<[z.ZodNumber, z.ZodNumber, z.ZodNumber, z.ZodNumber], null>, z.ZodTuple<[z.ZodNumber, z.ZodNumber, z.ZodNumber], null
|
|
119
|
+
export declare const colorZ: z.ZodPipe<z.ZodUnion<readonly [z.ZodString, z.ZodTuple<[z.ZodNumber, z.ZodNumber, z.ZodNumber, z.ZodNumber], null>, z.ZodTuple<[z.ZodNumber, z.ZodNumber, z.ZodNumber], null>, z.ZodObject<{
|
|
120
|
+
rgba255: z.ZodTuple<[z.ZodNumber, z.ZodNumber, z.ZodNumber, z.ZodNumber], null>;
|
|
121
|
+
}, {}>]>, z.ZodTransform<RGBA, string | [number, number, number, number] | {
|
|
122
|
+
rgba255: [number, number, number, number];
|
|
123
|
+
} | [number, number, number]>>;
|
|
115
124
|
/** A totally zero color with no alpha. */
|
|
116
125
|
export declare const ZERO: Color;
|
|
117
126
|
/** @returns a color parsed from an HSLA tuple. */
|
|
118
127
|
export declare const fromHSLA: (hsla: RGBA) => RGBA;
|
|
119
128
|
/** A zod schema for a crude color representation. */
|
|
120
|
-
export declare const crudeZ: z.ZodUnion<readonly [z.ZodString, z.ZodTuple<[z.ZodNumber, z.ZodNumber, z.ZodNumber, z.ZodNumber], null>, z.ZodString, z.ZodTuple<[z.ZodNumber, z.ZodNumber, z.ZodNumber], null
|
|
129
|
+
export declare const crudeZ: z.ZodUnion<readonly [z.ZodString, z.ZodTuple<[z.ZodNumber, z.ZodNumber, z.ZodNumber, z.ZodNumber], null>, z.ZodString, z.ZodTuple<[z.ZodNumber, z.ZodNumber, z.ZodNumber], null>, z.ZodObject<{
|
|
130
|
+
rgba255: z.ZodTuple<[z.ZodNumber, z.ZodNumber, z.ZodNumber, z.ZodNumber], null>;
|
|
131
|
+
}, {}>]>;
|
|
121
132
|
/** The color black. */
|
|
122
133
|
export declare const BLACK: RGBA;
|
|
123
134
|
/** The color white. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"color.d.ts","sourceRoot":"","sources":["../../../src/color/color.ts"],"names":[],"mappings":"AASA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAGxB,QAAA,MAAM,IAAI,aAA6B,CAAC;
|
|
1
|
+
{"version":3,"file":"color.d.ts","sourceRoot":"","sources":["../../../src/color/color.ts"],"names":[],"mappings":"AASA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAGxB,QAAA,MAAM,IAAI,aAA6B,CAAC;AAKxC,QAAA,MAAM,aAAa;;MAA+B,CAAC;AAEnD,8BAA8B;AAC9B,MAAM,MAAM,IAAI,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;AACpD,8BAA8B;AAC9B,MAAM,MAAM,IAAI,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;AACpD,6BAA6B;AAC7B,MAAM,MAAM,GAAG,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;AAC3C,6BAA6B;AAC7B,MAAM,MAAM,GAAG,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,IAAI,CAAC,CAAC;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AI,MAAM,IAAI,KAAG,IAmBrC,CAAC;AA2CF,qDAAqD;AACrD,eAAO,MAAM,MAAM;;QAA0D,CAAC;AAE9E,uBAAuB;AACvB,eAAO,MAAM,KAAK,MAAuB,CAAC;AAC1C,uBAAuB;AACvB,eAAO,MAAM,KAAK,MAAuB,CAAC"}
|
|
@@ -2,14 +2,18 @@ import { z } from 'zod';
|
|
|
2
2
|
import { Color } from './color';
|
|
3
3
|
export declare const stopZ: z.ZodObject<{
|
|
4
4
|
key: z.ZodString;
|
|
5
|
-
color: z.ZodUnion<readonly [z.ZodString, z.ZodTuple<[z.ZodNumber, z.ZodNumber, z.ZodNumber, z.ZodNumber], null>, z.ZodString, z.ZodTuple<[z.ZodNumber, z.ZodNumber, z.ZodNumber], null
|
|
5
|
+
color: z.ZodUnion<readonly [z.ZodString, z.ZodTuple<[z.ZodNumber, z.ZodNumber, z.ZodNumber, z.ZodNumber], null>, z.ZodString, z.ZodTuple<[z.ZodNumber, z.ZodNumber, z.ZodNumber], null>, z.ZodObject<{
|
|
6
|
+
rgba255: z.ZodTuple<[z.ZodNumber, z.ZodNumber, z.ZodNumber, z.ZodNumber], null>;
|
|
7
|
+
}, {}>]>;
|
|
6
8
|
position: z.ZodNumber;
|
|
7
9
|
switched: z.ZodOptional<z.ZodBoolean>;
|
|
8
10
|
}, {}>;
|
|
9
11
|
export type Stop = z.infer<typeof stopZ>;
|
|
10
12
|
export declare const gradientZ: z.ZodArray<z.ZodObject<{
|
|
11
13
|
key: z.ZodString;
|
|
12
|
-
color: z.ZodUnion<readonly [z.ZodString, z.ZodTuple<[z.ZodNumber, z.ZodNumber, z.ZodNumber, z.ZodNumber], null>, z.ZodString, z.ZodTuple<[z.ZodNumber, z.ZodNumber, z.ZodNumber], null
|
|
14
|
+
color: z.ZodUnion<readonly [z.ZodString, z.ZodTuple<[z.ZodNumber, z.ZodNumber, z.ZodNumber, z.ZodNumber], null>, z.ZodString, z.ZodTuple<[z.ZodNumber, z.ZodNumber, z.ZodNumber], null>, z.ZodObject<{
|
|
15
|
+
rgba255: z.ZodTuple<[z.ZodNumber, z.ZodNumber, z.ZodNumber, z.ZodNumber], null>;
|
|
16
|
+
}, {}>]>;
|
|
13
17
|
position: z.ZodNumber;
|
|
14
18
|
switched: z.ZodOptional<z.ZodBoolean>;
|
|
15
19
|
}, {}>>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"gradient.d.ts","sourceRoot":"","sources":["../../../src/color/gradient.ts"],"names":[],"mappings":"AASA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,EAGL,KAAK,KAAK,EAMX,MAAM,eAAe,CAAC;AAEvB,eAAO,MAAM,KAAK
|
|
1
|
+
{"version":3,"file":"gradient.d.ts","sourceRoot":"","sources":["../../../src/color/gradient.ts"],"names":[],"mappings":"AASA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,EAGL,KAAK,KAAK,EAMX,MAAM,eAAe,CAAC;AAEvB,eAAO,MAAM,KAAK;;;;;;;MAKhB,CAAC;AAEH,MAAM,MAAM,IAAI,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,KAAK,CAAC,CAAC;AAEzC,eAAO,MAAM,SAAS;;;;;;;OAAiB,CAAC;AAExC,MAAM,MAAM,QAAQ,GAAG,IAAI,EAAE,CAAC;AAE9B,eAAO,MAAM,YAAY,GAAI,UAAU,QAAQ,EAAE,UAAU,MAAM,KAAG,KA0CnE,CAAC"}
|
|
@@ -2,7 +2,11 @@ import { z } from 'zod';
|
|
|
2
2
|
declare const styleZ: z.ZodObject<{
|
|
3
3
|
key: z.ZodString;
|
|
4
4
|
name: z.ZodString;
|
|
5
|
-
color: z.ZodPipe<z.ZodUnion<readonly [z.ZodString, z.ZodTuple<[z.ZodNumber, z.ZodNumber, z.ZodNumber, z.ZodNumber], null>, z.ZodTuple<[z.ZodNumber, z.ZodNumber, z.ZodNumber], null
|
|
5
|
+
color: z.ZodPipe<z.ZodUnion<readonly [z.ZodString, z.ZodTuple<[z.ZodNumber, z.ZodNumber, z.ZodNumber, z.ZodNumber], null>, z.ZodTuple<[z.ZodNumber, z.ZodNumber, z.ZodNumber], null>, z.ZodObject<{
|
|
6
|
+
rgba255: z.ZodTuple<[z.ZodNumber, z.ZodNumber, z.ZodNumber, z.ZodNumber], null>;
|
|
7
|
+
}, {}>]>, z.ZodTransform<import('./color').RGBA, string | [number, number, number, number] | {
|
|
8
|
+
rgba255: [number, number, number, number];
|
|
9
|
+
} | [number, number, number]>>;
|
|
6
10
|
}, {}>;
|
|
7
11
|
export type Style = z.infer<typeof styleZ>;
|
|
8
12
|
export declare const paletteZ: z.ZodObject<{
|
|
@@ -11,7 +15,11 @@ export declare const paletteZ: z.ZodObject<{
|
|
|
11
15
|
swatches: z.ZodArray<z.ZodObject<{
|
|
12
16
|
key: z.ZodString;
|
|
13
17
|
name: z.ZodString;
|
|
14
|
-
color: z.ZodPipe<z.ZodUnion<readonly [z.ZodString, z.ZodTuple<[z.ZodNumber, z.ZodNumber, z.ZodNumber, z.ZodNumber], null>, z.ZodTuple<[z.ZodNumber, z.ZodNumber, z.ZodNumber], null
|
|
18
|
+
color: z.ZodPipe<z.ZodUnion<readonly [z.ZodString, z.ZodTuple<[z.ZodNumber, z.ZodNumber, z.ZodNumber, z.ZodNumber], null>, z.ZodTuple<[z.ZodNumber, z.ZodNumber, z.ZodNumber], null>, z.ZodObject<{
|
|
19
|
+
rgba255: z.ZodTuple<[z.ZodNumber, z.ZodNumber, z.ZodNumber, z.ZodNumber], null>;
|
|
20
|
+
}, {}>]>, z.ZodTransform<import('./color').RGBA, string | [number, number, number, number] | {
|
|
21
|
+
rgba255: [number, number, number, number];
|
|
22
|
+
} | [number, number, number]>>;
|
|
15
23
|
}, {}>>;
|
|
16
24
|
}, {}>;
|
|
17
25
|
export type Palette = z.infer<typeof paletteZ>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"palette.d.ts","sourceRoot":"","sources":["../../../src/color/palette.ts"],"names":[],"mappings":"AASA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAIxB,QAAA,MAAM,MAAM
|
|
1
|
+
{"version":3,"file":"palette.d.ts","sourceRoot":"","sources":["../../../src/color/palette.ts"],"names":[],"mappings":"AASA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAIxB,QAAA,MAAM,MAAM;;;;;;;;MAIV,CAAC;AAEH,MAAM,MAAM,KAAK,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,MAAM,CAAC,CAAC;AAE3C,eAAO,MAAM,QAAQ;;;;;;;;;;;;MAInB,CAAC;AAEH,MAAM,MAAM,OAAO,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,QAAQ,CAAC,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"merge.d.ts","sourceRoot":"","sources":["../../../src/deep/merge.ts"],"names":[],"mappings":"AASA,OAAO,EAAE,KAAK,CAAC,EAAE,MAAM,KAAK,CAAC;AAE7B,OAAO,EAAE,KAAK,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAG9C;;;;;GAKG;AACH,eAAO,MAAM,QAAQ,GAAI,CAAC,EAAE,MAAM,CAAC,EAAE,GAAG,WAAW,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,KAAG,CAiBtE,CAAC;AAEF,eAAO,MAAM,kBAAkB,GAAI,CAAC,EAAE,CAAC,EACrC,MAAM,CAAC,EACP,UAAU,CAAC,EACX,QAAQ,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,KACnB,
|
|
1
|
+
{"version":3,"file":"merge.d.ts","sourceRoot":"","sources":["../../../src/deep/merge.ts"],"names":[],"mappings":"AASA,OAAO,EAAE,KAAK,CAAC,EAAE,MAAM,KAAK,CAAC;AAE7B,OAAO,EAAE,KAAK,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAG9C;;;;;GAKG;AACH,eAAO,MAAM,QAAQ,GAAI,CAAC,EAAE,MAAM,CAAC,EAAE,GAAG,WAAW,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,KAAG,CAiBtE,CAAC;AAEF,eAAO,MAAM,kBAAkB,GAAI,CAAC,EAAE,CAAC,EACrC,MAAM,CAAC,EACP,UAAU,CAAC,EACX,QAAQ,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,KACnB,CAwCF,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@synnaxlabs/x",
|
|
3
|
-
"version": "0.42.
|
|
3
|
+
"version": "0.42.3",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Common Utilities for Synnax Labs",
|
|
6
6
|
"repository": "https://github.com/synnaxlabs/synnax/tree/main/x/go",
|
|
@@ -26,9 +26,9 @@
|
|
|
26
26
|
"typescript": "^5.8.3",
|
|
27
27
|
"vite": "^6.2.5",
|
|
28
28
|
"vitest": "^3.1.1",
|
|
29
|
+
"@synnaxlabs/vite-plugin": "0.0.1",
|
|
29
30
|
"eslint-config-synnaxlabs": "0.0.1",
|
|
30
|
-
"@synnaxlabs/tsconfig": "0.0.2"
|
|
31
|
-
"@synnaxlabs/vite-plugin": "0.0.1"
|
|
31
|
+
"@synnaxlabs/tsconfig": "0.0.2"
|
|
32
32
|
},
|
|
33
33
|
"main": "./dist/index.cjs",
|
|
34
34
|
"module": "./dist/index.js",
|
package/src/color/color.spec.ts
CHANGED
|
@@ -63,6 +63,14 @@ describe("color.Color", () => {
|
|
|
63
63
|
expect(color.gValue(c)).toEqual(44);
|
|
64
64
|
expect(color.bValue(c)).toEqual(38);
|
|
65
65
|
});
|
|
66
|
+
|
|
67
|
+
test("from legacy object", () => {
|
|
68
|
+
const c = color.construct({ rgba255: [122, 44, 38, 0.5] });
|
|
69
|
+
expect(color.rValue(c)).toEqual(122);
|
|
70
|
+
expect(color.gValue(c)).toEqual(44);
|
|
71
|
+
expect(color.bValue(c)).toEqual(38);
|
|
72
|
+
expect(color.aValue(c)).toEqual(0.5);
|
|
73
|
+
});
|
|
66
74
|
});
|
|
67
75
|
|
|
68
76
|
describe("to hex", () => {
|
package/src/color/color.ts
CHANGED
|
@@ -15,6 +15,7 @@ const rgbValueZ = z.number().min(0).max(255);
|
|
|
15
15
|
const alphaZ = z.number().min(0).max(1);
|
|
16
16
|
const rgbaZ = z.tuple([rgbValueZ, rgbValueZ, rgbValueZ, alphaZ]);
|
|
17
17
|
const rgbZ = z.tuple([rgbValueZ, rgbValueZ, rgbValueZ]);
|
|
18
|
+
const legacyObjectZ = z.object({ rgba255: rgbaZ });
|
|
18
19
|
|
|
19
20
|
/** A color in RGBA format. */
|
|
20
21
|
export type RGBA = [number, number, number, number];
|
|
@@ -26,6 +27,8 @@ export type RGB = [number, number, number];
|
|
|
26
27
|
export type Hex = z.infer<typeof hexZ>;
|
|
27
28
|
/** A color in RGBA format. Used as the standard representation of a color in this package. */
|
|
28
29
|
export type Color = RGBA;
|
|
30
|
+
/** A legacy color object. */
|
|
31
|
+
type LegacyObject = z.infer<typeof legacyObjectZ>;
|
|
29
32
|
|
|
30
33
|
/** @returns true if the given color can be parsed into a valid color object. */
|
|
31
34
|
export const isCrude = (color: unknown): color is Crude =>
|
|
@@ -39,7 +42,7 @@ export const isColor = (color: unknown): color is Color =>
|
|
|
39
42
|
* An unparsed representation of a color i.e. a value that can be converted into
|
|
40
43
|
* a Color object.
|
|
41
44
|
*/
|
|
42
|
-
export type Crude = Hex | RGBA | Color | string | RGB;
|
|
45
|
+
export type Crude = Hex | RGBA | Color | string | RGB | LegacyObject;
|
|
43
46
|
|
|
44
47
|
/**
|
|
45
48
|
* Converts a crude color to its most meaningful CSS format.
|
|
@@ -78,6 +81,7 @@ export const construct = (color: Crude, alpha: number = 1): Color => {
|
|
|
78
81
|
if (color.length === 3) return [...color, alpha] as RGBA;
|
|
79
82
|
return color;
|
|
80
83
|
}
|
|
84
|
+
if (typeof color === "object" && "rgba255" in color) return color.rgba255;
|
|
81
85
|
throw new Error(`Invalid color: ${JSON.stringify(color)}`);
|
|
82
86
|
};
|
|
83
87
|
|
|
@@ -223,7 +227,9 @@ export const isDark = (color: Crude): boolean => luminance(color) < 0.5;
|
|
|
223
227
|
export const isLight = (color: Crude): boolean => !isDark(color);
|
|
224
228
|
|
|
225
229
|
/** A zod schema to parse color values from various crude representations. */
|
|
226
|
-
export const colorZ = z
|
|
230
|
+
export const colorZ = z
|
|
231
|
+
.union([hexZ, rgbaZ, rgbZ, legacyObjectZ])
|
|
232
|
+
.transform((v) => construct(v));
|
|
227
233
|
|
|
228
234
|
/** @returns a color parsed from a hex string with an alpha value. */
|
|
229
235
|
const fromHex = (hex_: string, alpha: number = 1): RGBA => {
|
|
@@ -309,7 +315,7 @@ const rgbaToHSLA = (rgba: RGBA): HSLA => {
|
|
|
309
315
|
};
|
|
310
316
|
|
|
311
317
|
/** A zod schema for a crude color representation. */
|
|
312
|
-
export const crudeZ = z.union([hexZ, rgbaZ, z.string(), rgbZ]);
|
|
318
|
+
export const crudeZ = z.union([hexZ, rgbaZ, z.string(), rgbZ, legacyObjectZ]);
|
|
313
319
|
|
|
314
320
|
/** The color black. */
|
|
315
321
|
export const BLACK = construct("#000000");
|
package/src/deep/merge.spec.ts
CHANGED
|
@@ -202,4 +202,64 @@ describe("deepMerge", () => {
|
|
|
202
202
|
b: 2,
|
|
203
203
|
});
|
|
204
204
|
});
|
|
205
|
+
|
|
206
|
+
it("should work with zod unions", () => {
|
|
207
|
+
const schema1 = z.object({
|
|
208
|
+
a: z.number(),
|
|
209
|
+
});
|
|
210
|
+
const schema2 = z.object({
|
|
211
|
+
b: z.number(),
|
|
212
|
+
});
|
|
213
|
+
const schema = z.union([schema1, schema2]);
|
|
214
|
+
const base = {
|
|
215
|
+
a: 1,
|
|
216
|
+
};
|
|
217
|
+
const override = {
|
|
218
|
+
b: 2,
|
|
219
|
+
};
|
|
220
|
+
expect(deep.overrideValidItems(base, override, schema)).toEqual({
|
|
221
|
+
a: 1,
|
|
222
|
+
b: 2,
|
|
223
|
+
});
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
it("should work with extensions", () => {
|
|
227
|
+
const schema = z.object({
|
|
228
|
+
a: z.number(),
|
|
229
|
+
});
|
|
230
|
+
const extension = z.object({
|
|
231
|
+
b: z.string(),
|
|
232
|
+
});
|
|
233
|
+
const extendedSchema = schema.extend(extension);
|
|
234
|
+
const base = {
|
|
235
|
+
a: 1,
|
|
236
|
+
};
|
|
237
|
+
const override = {
|
|
238
|
+
b: "2",
|
|
239
|
+
};
|
|
240
|
+
expect(deep.overrideValidItems(base, override, extendedSchema)).toEqual({
|
|
241
|
+
a: 1,
|
|
242
|
+
b: "2",
|
|
243
|
+
});
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
it("should work with intersection", () => {
|
|
247
|
+
const schema1 = z.object({
|
|
248
|
+
a: z.number(),
|
|
249
|
+
});
|
|
250
|
+
const schema2 = z.object({
|
|
251
|
+
b: z.string(),
|
|
252
|
+
c: z.number(),
|
|
253
|
+
});
|
|
254
|
+
const schema = schema1.and(schema2);
|
|
255
|
+
const base = {};
|
|
256
|
+
const override = {
|
|
257
|
+
b: "2",
|
|
258
|
+
c: 3,
|
|
259
|
+
};
|
|
260
|
+
expect(deep.overrideValidItems(base, override, schema)).toEqual({
|
|
261
|
+
b: "2",
|
|
262
|
+
c: 3,
|
|
263
|
+
});
|
|
264
|
+
});
|
|
205
265
|
});
|
package/src/deep/merge.ts
CHANGED
|
@@ -47,10 +47,21 @@ export const overrideValidItems = <A, B>(
|
|
|
47
47
|
overrideObj: any,
|
|
48
48
|
currentSchema: any,
|
|
49
49
|
): any => {
|
|
50
|
+
if (currentSchema.def?.type === "union")
|
|
51
|
+
return currentSchema.def.options.reduce(
|
|
52
|
+
(acc: any, option: any) => mergeValidFields(acc, overrideObj, option),
|
|
53
|
+
baseObj,
|
|
54
|
+
);
|
|
55
|
+
if (currentSchema.def?.type === "intersection") {
|
|
56
|
+
const out = mergeValidFields(baseObj, overrideObj, currentSchema.def.left);
|
|
57
|
+
const right = mergeValidFields(out, overrideObj, currentSchema.def.right);
|
|
58
|
+
return right;
|
|
59
|
+
}
|
|
60
|
+
|
|
50
61
|
// Iterate over each property in the override object
|
|
51
62
|
for (const key in overrideObj) {
|
|
52
63
|
const overrideValue = overrideObj[key];
|
|
53
|
-
const shape = currentSchema
|
|
64
|
+
const shape = currentSchema?.shape;
|
|
54
65
|
if (shape?.[key]) {
|
|
55
66
|
const result = shape[key].safeParse(overrideValue);
|
|
56
67
|
// Check if parsing succeeded
|