@postxl/utils 0.1.10 → 0.1.11
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/types.d.ts +15 -0
- package/dist/types.js +18 -0
- package/dist/zod-excel.decoders.d.ts +4 -4
- package/dist/zod-excel.decoders.js +22 -10
- package/package.json +1 -1
package/dist/types.d.ts
CHANGED
|
@@ -53,6 +53,21 @@ export type DeepPartial<T> = T extends object ? {
|
|
|
53
53
|
* // Bar is { a?: string, b?: number, c: boolean }
|
|
54
54
|
*/
|
|
55
55
|
export type Optional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
|
|
56
|
+
/**
|
|
57
|
+
* Allows inline type assertions.
|
|
58
|
+
*
|
|
59
|
+
* Example:
|
|
60
|
+
* ```
|
|
61
|
+
* type Original = { a: number }
|
|
62
|
+
* type WithExtra = Original & { b: string }
|
|
63
|
+
*
|
|
64
|
+
* function addExtra(obj: Original): asserts obj is WithExtra {
|
|
65
|
+
* assertType(obj)
|
|
66
|
+
* obj.b = 'new value' // ✅ No TypeScript error!
|
|
67
|
+
* }
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
export declare function assertType<T>(value: unknown): asserts value is T;
|
|
56
71
|
/**
|
|
57
72
|
* Applies a type assertion to an entire array and hence asserts the array.
|
|
58
73
|
*
|
package/dist/types.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ExhaustiveSwitchCheck = void 0;
|
|
4
|
+
exports.assertType = assertType;
|
|
4
5
|
exports.assertArray = assertArray;
|
|
5
6
|
/**
|
|
6
7
|
* Makes a type check that is only valid when all cases of a switch
|
|
@@ -12,6 +13,23 @@ class ExhaustiveSwitchCheck extends Error {
|
|
|
12
13
|
}
|
|
13
14
|
}
|
|
14
15
|
exports.ExhaustiveSwitchCheck = ExhaustiveSwitchCheck;
|
|
16
|
+
/**
|
|
17
|
+
* Allows inline type assertions.
|
|
18
|
+
*
|
|
19
|
+
* Example:
|
|
20
|
+
* ```
|
|
21
|
+
* type Original = { a: number }
|
|
22
|
+
* type WithExtra = Original & { b: string }
|
|
23
|
+
*
|
|
24
|
+
* function addExtra(obj: Original): asserts obj is WithExtra {
|
|
25
|
+
* assertType(obj)
|
|
26
|
+
* obj.b = 'new value' // ✅ No TypeScript error!
|
|
27
|
+
* }
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
function assertType(value) {
|
|
31
|
+
return value;
|
|
32
|
+
}
|
|
15
33
|
/**
|
|
16
34
|
* Applies a type assertion to an entire array and hence asserts the array.
|
|
17
35
|
*
|
|
@@ -74,7 +74,7 @@ export declare const excelNumberDecoder: z.ZodEffects<z.ZodUnion<[z.ZodString, z
|
|
|
74
74
|
* - If the value is boolean, it'll return 1 or 0
|
|
75
75
|
* - If the value is any other type, it'll return null
|
|
76
76
|
*/
|
|
77
|
-
export declare const excelNumberNullableDecoder: z.ZodEffects<z.ZodUnion<[z.ZodString, z.ZodNull, z.ZodNumber, z.ZodUndefined, z.ZodAny]>, number | null, any>;
|
|
77
|
+
export declare const excelNumberNullableDecoder: z.ZodEffects<z.ZodUnion<[z.ZodString, z.ZodNull, z.ZodNumber, z.ZodNull, z.ZodUndefined, z.ZodAny]>, number | null, any>;
|
|
78
78
|
/**
|
|
79
79
|
* A decoder that transforms Excel numbers to JS integers, also handling strings and boolean. Other types will throw an error.
|
|
80
80
|
*
|
|
@@ -92,7 +92,7 @@ export declare const excelIntDecoder: z.ZodEffects<z.ZodEffects<z.ZodUnion<[z.Zo
|
|
|
92
92
|
*
|
|
93
93
|
* If the number is not an integer, an error will be thrown.
|
|
94
94
|
*/
|
|
95
|
-
export declare const excelIntNullableDecoder: z.ZodEffects<z.ZodEffects<z.ZodUnion<[z.ZodString, z.ZodNull, z.ZodNumber, z.ZodUndefined, z.ZodAny]>, number | null, any>, number | null, any>;
|
|
95
|
+
export declare const excelIntNullableDecoder: z.ZodEffects<z.ZodEffects<z.ZodUnion<[z.ZodString, z.ZodNull, z.ZodNumber, z.ZodNull, z.ZodUndefined, z.ZodAny]>, number | null, any>, number | null, any>;
|
|
96
96
|
/**
|
|
97
97
|
* A decoder that transforms Excel numbers to JS BigInts, also handling strings and boolean. Other types will throw an error.
|
|
98
98
|
*
|
|
@@ -110,7 +110,7 @@ export declare const excelBigIntDecoder: z.ZodEffects<z.ZodEffects<z.ZodUnion<[z
|
|
|
110
110
|
*
|
|
111
111
|
* If the number is not a (big) integer, an error will be thrown.
|
|
112
112
|
*/
|
|
113
|
-
export declare const excelBigIntNullableDecoder: z.ZodEffects<z.ZodEffects<z.ZodUnion<[z.ZodString, z.ZodNull, z.ZodNumber, z.ZodUndefined, z.ZodAny]>, number | null, any>, bigint | null, any>;
|
|
113
|
+
export declare const excelBigIntNullableDecoder: z.ZodEffects<z.ZodEffects<z.ZodUnion<[z.ZodString, z.ZodNull, z.ZodNumber, z.ZodNull, z.ZodUndefined, z.ZodAny]>, number | null, any>, bigint | null, any>;
|
|
114
114
|
/**
|
|
115
115
|
* A decoder that transforms Excel booleans to JS booleans, also handling numbers and strings.
|
|
116
116
|
*
|
|
@@ -144,7 +144,7 @@ export declare const excelBooleanNullableDecoder: z.ZodEffects<z.ZodUnion<[z.Zod
|
|
|
144
144
|
/**
|
|
145
145
|
* A decoder that transforms Excel dates to JS Dates
|
|
146
146
|
*/
|
|
147
|
-
export declare const excelDateDecoder: z.ZodEffects<z.ZodUnion<[z.ZodNumber, z.ZodString]>, Date, string | number>;
|
|
147
|
+
export declare const excelDateDecoder: z.ZodEffects<z.ZodUnion<[z.ZodDate, z.ZodNumber, z.ZodString]>, Date, string | number | Date>;
|
|
148
148
|
/**
|
|
149
149
|
* A decoder that transforms nullable Excel dates to JS Dates
|
|
150
150
|
*/
|
|
@@ -67,7 +67,9 @@ exports.excelStringStrictDecoder = zod_1.default.union([zod_1.default.string(),
|
|
|
67
67
|
* - If the value is null or undefined, it'll return an empty string
|
|
68
68
|
* - If the value is any other type, it'll return an empty string
|
|
69
69
|
*/
|
|
70
|
-
exports.excelStringDecoder = zod_1.default
|
|
70
|
+
exports.excelStringDecoder = zod_1.default
|
|
71
|
+
.union([zod_1.default.string(), zod_1.default.number(), zod_1.default.boolean(), zod_1.default.null(), zod_1.default.undefined(), zod_1.default.any()])
|
|
72
|
+
.transform((x) => {
|
|
71
73
|
if (x === null || x === undefined) {
|
|
72
74
|
return '';
|
|
73
75
|
}
|
|
@@ -96,7 +98,9 @@ exports.excelStringDecoder = zod_1.default.union([zod_1.default.string(), zod_1.
|
|
|
96
98
|
* - If the value is null or undefined, it'll return null
|
|
97
99
|
* - If the value is any other type, it'll return null
|
|
98
100
|
*/
|
|
99
|
-
exports.excelStringNullableDecoder = zod_1.default
|
|
101
|
+
exports.excelStringNullableDecoder = zod_1.default
|
|
102
|
+
.union([zod_1.default.string(), zod_1.default.number(), zod_1.default.boolean(), zod_1.default.null(), zod_1.default.undefined(), zod_1.default.any()])
|
|
103
|
+
.transform((x) => {
|
|
100
104
|
if (x === null || x === undefined) {
|
|
101
105
|
return null;
|
|
102
106
|
}
|
|
@@ -150,7 +154,9 @@ exports.excelNumberStrictDecoder = zod_1.default.union([zod_1.default.string(),
|
|
|
150
154
|
* - If the value is boolean, it'll return 1 or 0
|
|
151
155
|
* - If the value is any other type, it'll return 0
|
|
152
156
|
*/
|
|
153
|
-
exports.excelNumberDecoder = zod_1.default
|
|
157
|
+
exports.excelNumberDecoder = zod_1.default
|
|
158
|
+
.union([zod_1.default.string(), zod_1.default.number(), zod_1.default.boolean(), zod_1.default.null(), zod_1.default.undefined(), zod_1.default.any()])
|
|
159
|
+
.transform((x) => {
|
|
154
160
|
if (typeof x === 'string') {
|
|
155
161
|
if (x.trim() === '') {
|
|
156
162
|
return 0;
|
|
@@ -178,7 +184,9 @@ exports.excelNumberDecoder = zod_1.default.union([zod_1.default.string(), zod_1.
|
|
|
178
184
|
* - If the value is boolean, it'll return 1 or 0
|
|
179
185
|
* - If the value is any other type, it'll return null
|
|
180
186
|
*/
|
|
181
|
-
exports.excelNumberNullableDecoder = zod_1.default
|
|
187
|
+
exports.excelNumberNullableDecoder = zod_1.default
|
|
188
|
+
.union([zod_1.default.string(), zod_1.default.null(), zod_1.default.number(), zod_1.default.null(), zod_1.default.undefined(), zod_1.default.any()])
|
|
189
|
+
.transform((x) => {
|
|
182
190
|
if (typeof x === 'string') {
|
|
183
191
|
if (x.trim() === '') {
|
|
184
192
|
return null;
|
|
@@ -287,7 +295,9 @@ exports.excelBooleanStrictDecoder = zod_1.default.union([zod_1.default.boolean()
|
|
|
287
295
|
* - If the value is boolean, it'll return it as-is
|
|
288
296
|
* - If the value is any other type, it'll return false
|
|
289
297
|
*/
|
|
290
|
-
exports.excelBooleanDecoder = zod_1.default
|
|
298
|
+
exports.excelBooleanDecoder = zod_1.default
|
|
299
|
+
.union([zod_1.default.boolean(), zod_1.default.number(), zod_1.default.null(), zod_1.default.string(), zod_1.default.undefined(), zod_1.default.any()])
|
|
300
|
+
.transform((x) => {
|
|
291
301
|
if (typeof x === 'number') {
|
|
292
302
|
return x !== 0;
|
|
293
303
|
}
|
|
@@ -317,7 +327,9 @@ exports.excelBooleanDecoder = zod_1.default.union([zod_1.default.boolean(), zod_
|
|
|
317
327
|
* - If the value is boolean, it'll return it as-is
|
|
318
328
|
* - If the value is any other type, it'll return null
|
|
319
329
|
*/
|
|
320
|
-
exports.excelBooleanNullableDecoder = zod_1.default
|
|
330
|
+
exports.excelBooleanNullableDecoder = zod_1.default
|
|
331
|
+
.union([zod_1.default.boolean(), zod_1.default.number(), zod_1.default.null(), zod_1.default.string(), zod_1.default.undefined(), zod_1.default.any()])
|
|
332
|
+
.transform((x) => {
|
|
321
333
|
if (typeof x === 'number') {
|
|
322
334
|
return x !== 0;
|
|
323
335
|
}
|
|
@@ -341,9 +353,7 @@ exports.excelBooleanNullableDecoder = zod_1.default.union([zod_1.default.boolean
|
|
|
341
353
|
/**
|
|
342
354
|
* A decoder that transforms Excel dates to JS Dates
|
|
343
355
|
*/
|
|
344
|
-
exports.excelDateDecoder = zod_1.default
|
|
345
|
-
.union([zod_1.default.number(), zod_1.default.string()])
|
|
346
|
-
.transform((x, ctx) => {
|
|
356
|
+
exports.excelDateDecoder = zod_1.default.union([zod_1.default.date(), zod_1.default.number(), zod_1.default.string()]).transform((x, ctx) => {
|
|
347
357
|
if (typeof x === 'number') {
|
|
348
358
|
// Excel number is a float with 1 representing Jan 1, 1900. Each increment is a day.
|
|
349
359
|
// As Excel incorrectly ignores the 1900 leap year, we need to correct for that
|
|
@@ -366,7 +376,9 @@ exports.excelDateDecoder = zod_1.default
|
|
|
366
376
|
/**
|
|
367
377
|
* A decoder that transforms nullable Excel dates to JS Dates
|
|
368
378
|
*/
|
|
369
|
-
exports.excelDateNullableDecoder = zod_1.default
|
|
379
|
+
exports.excelDateNullableDecoder = zod_1.default
|
|
380
|
+
.union([zod_1.default.string(), zod_1.default.number(), zod_1.default.null(), zod_1.default.undefined(), zod_1.default.any()])
|
|
381
|
+
.transform((x, ctx) => {
|
|
370
382
|
if (x === null || x === undefined) {
|
|
371
383
|
return null;
|
|
372
384
|
}
|