@postxl/utils 0.1.8

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 (57) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +3 -0
  3. package/dist/DefaultMap.d.ts +11 -0
  4. package/dist/DefaultMap.js +26 -0
  5. package/dist/NestedMap.d.ts +16 -0
  6. package/dist/NestedMap.js +73 -0
  7. package/dist/TypedMapping.d.ts +26 -0
  8. package/dist/TypedMapping.js +33 -0
  9. package/dist/array.d.ts +6 -0
  10. package/dist/array.js +20 -0
  11. package/dist/async.d.ts +3 -0
  12. package/dist/async.js +40 -0
  13. package/dist/buffer.d.ts +4 -0
  14. package/dist/buffer.js +15 -0
  15. package/dist/check-port.d.ts +4 -0
  16. package/dist/check-port.js +27 -0
  17. package/dist/datetime.d.ts +4 -0
  18. package/dist/datetime.js +15 -0
  19. package/dist/dictionary.d.ts +8 -0
  20. package/dist/dictionary.js +44 -0
  21. package/dist/format.d.ts +1 -0
  22. package/dist/format.js +6 -0
  23. package/dist/group-by.d.ts +8 -0
  24. package/dist/group-by.js +65 -0
  25. package/dist/index.d.ts +25 -0
  26. package/dist/index.js +41 -0
  27. package/dist/is-object.d.ts +12 -0
  28. package/dist/is-object.js +24 -0
  29. package/dist/map.d.ts +22 -0
  30. package/dist/map.js +75 -0
  31. package/dist/omit.d.ts +4 -0
  32. package/dist/omit.js +10 -0
  33. package/dist/pagination.d.ts +6 -0
  34. package/dist/pagination.js +13 -0
  35. package/dist/random.d.ts +4 -0
  36. package/dist/random.js +16 -0
  37. package/dist/remove-secrets.d.ts +11 -0
  38. package/dist/remove-secrets.js +61 -0
  39. package/dist/remove-undefined.d.ts +9 -0
  40. package/dist/remove-undefined.js +21 -0
  41. package/dist/result.d.ts +24 -0
  42. package/dist/result.js +49 -0
  43. package/dist/sort.d.ts +17 -0
  44. package/dist/sort.js +63 -0
  45. package/dist/string-colors.d.ts +44 -0
  46. package/dist/string-colors.js +72 -0
  47. package/dist/string.d.ts +115 -0
  48. package/dist/string.js +322 -0
  49. package/dist/types.d.ts +78 -0
  50. package/dist/types.js +38 -0
  51. package/dist/uniq.d.ts +1 -0
  52. package/dist/uniq.js +15 -0
  53. package/dist/zod-excel.decoders.d.ts +151 -0
  54. package/dist/zod-excel.decoders.js +397 -0
  55. package/dist/zod.d.ts +8 -0
  56. package/dist/zod.js +17 -0
  57. package/package.json +33 -0
@@ -0,0 +1,397 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.excelDateNullableDecoder = exports.excelDateDecoder = exports.excelBooleanNullableDecoder = exports.excelBooleanDecoder = exports.excelBooleanStrictDecoder = exports.excelBigIntNullableDecoder = exports.excelBigIntDecoder = exports.excelBigIntStrictDecoder = exports.excelIntNullableDecoder = exports.excelIntDecoder = exports.excelIntStrictDecoder = exports.excelNumberNullableDecoder = exports.excelNumberDecoder = exports.excelNumberStrictDecoder = exports.excelStringNullableDecoder = exports.excelStringDecoder = exports.excelStringStrictDecoder = void 0;
7
+ const zod_1 = __importDefault(require("zod"));
8
+ const types_1 = require("./types");
9
+ /**
10
+ * Transformer that verifies that the number is an integer
11
+ */
12
+ const intValidator = (x, ctx) => {
13
+ if (x % 1 !== 0) {
14
+ ctx.addIssue({
15
+ code: zod_1.default.ZodIssueCode.custom,
16
+ message: `Not an integer: ${x}`,
17
+ });
18
+ return zod_1.default.NEVER;
19
+ }
20
+ return x;
21
+ };
22
+ /**
23
+ * Converts any transformer to a nullable transformer, i.e. if the input is
24
+ * null or undefined, the output will be nullable. Else the original transformer
25
+ * is applied
26
+ */
27
+ const nullableTransformer = (transformer) => (x, ctx) => {
28
+ if (x === null || x === undefined) {
29
+ return null;
30
+ }
31
+ return transformer(x, ctx);
32
+ };
33
+ /**
34
+ * A decoder that transforms Excel strings to JS strings, also handling numbers and boolean. Will not parse any other type!
35
+ *
36
+ * Background: In Excel, entering a number in a cell will automatically convert it to a number -
37
+ * and xlPort will return it as a number. However, often we want to treat it as a string - this decoder
38
+ * hence accepts both strings and numbers, and converts numbers to strings.
39
+ *
40
+ * It'll perform casting based on the following rules:
41
+ * - If the value is a number, it'll be converted to a string
42
+ * - If the value is a string, it'll be returned as-is
43
+ * - If the value is boolean, it'll return 'true' or 'false'
44
+ *
45
+ * Any other type will throw an error!
46
+ */
47
+ exports.excelStringStrictDecoder = zod_1.default.union([zod_1.default.string(), zod_1.default.number(), zod_1.default.boolean()]).transform((x) => {
48
+ if (typeof x === 'number') {
49
+ return x.toString();
50
+ }
51
+ if (typeof x === 'boolean') {
52
+ return x ? 'true' : 'false';
53
+ }
54
+ return x;
55
+ });
56
+ /**
57
+ * A decoder that transforms Excel strings to JS strings, also handling numbers and any other potential type
58
+ *
59
+ * Background: In Excel, entering a number in a cell will automatically convert it to a number -
60
+ * and xlPort will return it as a number. However, often we want to treat it as a string - this decoder
61
+ * hence accepts both strings and numbers, and converts numbers to strings.
62
+ *
63
+ * It'll perform casting based on the following rules:
64
+ * - If the value is a number, it'll be converted to a string
65
+ * - If the value is a string, it'll be returned as-is
66
+ * - If the value is boolean, it'll return 'true' or 'false'
67
+ * - If the value is null or undefined, it'll return an empty string
68
+ * - If the value is any other type, it'll return an empty string
69
+ */
70
+ exports.excelStringDecoder = zod_1.default.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()]).transform((x) => {
71
+ if (x === null || x === undefined) {
72
+ return '';
73
+ }
74
+ if (typeof x === 'number') {
75
+ return x.toString();
76
+ }
77
+ if (typeof x === 'boolean') {
78
+ return x ? 'true' : 'false';
79
+ }
80
+ if (typeof x === 'string') {
81
+ return x;
82
+ }
83
+ return '';
84
+ });
85
+ /**
86
+ * A decoder that transforms Excel strings to JS strings, also handling numbers and null
87
+ *
88
+ * Background: In Excel, entering a number in a cell will automatically convert it to a number -
89
+ * and xlPort will return it as a number. However, often we want to treat it as a string - this decoder
90
+ * hence accepts both strings and numbers, and converts numbers to strings.
91
+ *
92
+ * It'll perform casting based on the following rules:
93
+ * - If the value is a number, it'll be converted to a string
94
+ * - If the value is a string, it'll be returned as-is
95
+ * - If the value is boolean, it'll return 'true' or 'false'
96
+ * - If the value is null or undefined, it'll return null
97
+ * - If the value is any other type, it'll return null
98
+ */
99
+ exports.excelStringNullableDecoder = zod_1.default.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()]).transform((x) => {
100
+ if (x === null || x === undefined) {
101
+ return null;
102
+ }
103
+ if (typeof x === 'number') {
104
+ return x.toString();
105
+ }
106
+ if (typeof x === 'boolean') {
107
+ return x ? 'true' : 'false';
108
+ }
109
+ if (typeof x === 'string') {
110
+ return x;
111
+ }
112
+ return null;
113
+ });
114
+ /**
115
+ * A decoder that transforms Excel numbers to JS numbers, also handling strings and boolean. Will not parse any other type!
116
+ *
117
+ * It'll perform casting based on the following rules:
118
+ * - If the value is a number, it'll be returned as-is
119
+ * - If the value is a string, it'll try to parse it to a number. Blank strings will be converted to 0. Non-numeric strings will throw an error.
120
+ * - If the value is boolean, it'll return 1 or 0
121
+ * - If the value is any other type, it'll throw an error
122
+ *
123
+ */
124
+ exports.excelNumberStrictDecoder = zod_1.default.union([zod_1.default.string(), zod_1.default.number(), zod_1.default.boolean()]).transform((x, ctx) => {
125
+ if (typeof x === 'string') {
126
+ if (x.trim() === '') {
127
+ return 0;
128
+ }
129
+ const parsed = parseFloat(x);
130
+ if (isNaN(parsed)) {
131
+ ctx.addIssue({
132
+ code: zod_1.default.ZodIssueCode.custom,
133
+ message: `Not a number: ${x}`,
134
+ });
135
+ return zod_1.default.NEVER;
136
+ }
137
+ return parsed;
138
+ }
139
+ if (typeof x === 'boolean') {
140
+ return x ? 1 : 0;
141
+ }
142
+ return x;
143
+ });
144
+ /**
145
+ * A decoder that transforms Excel numbers to JS numbers, also handling strings, boolean and other types.
146
+
147
+ * It'll perform casting based on the following rules:
148
+ * - If the value is a number, it'll be returned as-is
149
+ * - If the value is a string, it'll try to parse it to a number. Blank strings and non-numerical strings will be converted to 0.
150
+ * - If the value is boolean, it'll return 1 or 0
151
+ * - If the value is any other type, it'll return 0
152
+ */
153
+ exports.excelNumberDecoder = zod_1.default.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()]).transform((x) => {
154
+ if (typeof x === 'string') {
155
+ if (x.trim() === '') {
156
+ return 0;
157
+ }
158
+ const parsed = parseFloat(x);
159
+ if (isNaN(parsed)) {
160
+ return 0;
161
+ }
162
+ return parsed;
163
+ }
164
+ if (typeof x === 'boolean') {
165
+ return x ? 1 : 0;
166
+ }
167
+ if (typeof x === 'number') {
168
+ return x;
169
+ }
170
+ return 0;
171
+ });
172
+ /**
173
+ * A decoder that transforms Excel numbers to JS numbers, also handling strings, boolean and other types.
174
+
175
+ * It'll perform casting based on the following rules:
176
+ * - If the value is a number, it'll be returned as-is
177
+ * - If the value is a string, it'll try to parse it to a number. Blank strings and non-numerical strings will be converted to 0.
178
+ * - If the value is boolean, it'll return 1 or 0
179
+ * - If the value is any other type, it'll return null
180
+ */
181
+ exports.excelNumberNullableDecoder = zod_1.default.union([zod_1.default.string(), zod_1.default.null(), zod_1.default.number(), zod_1.default.undefined(), zod_1.default.any()]).transform((x) => {
182
+ if (typeof x === 'string') {
183
+ if (x.trim() === '') {
184
+ return null;
185
+ }
186
+ const parsed = parseFloat(x);
187
+ if (isNaN(parsed)) {
188
+ return null;
189
+ }
190
+ return parsed;
191
+ }
192
+ if (typeof x === 'boolean') {
193
+ return x ? 1 : 0;
194
+ }
195
+ if (typeof x === 'number') {
196
+ return x;
197
+ }
198
+ return null;
199
+ });
200
+ /**
201
+ * A decoder that transforms Excel numbers to JS integers, also handling strings and boolean. Other types will throw an error.
202
+ *
203
+ * If the number is not an integer, an error will be thrown.
204
+ */
205
+ exports.excelIntStrictDecoder = exports.excelNumberStrictDecoder.transform(intValidator);
206
+ /**
207
+ * A decoder that transforms Excel numbers to JS integers, also handling strings and boolean. Other types will be converted to 0.
208
+ *
209
+ * If the number is not an integer, an error will be thrown.
210
+ */
211
+ exports.excelIntDecoder = exports.excelNumberDecoder.transform(intValidator);
212
+ /**
213
+ * A decoder that transforms Excel numbers to JS integers, also handling strings and boolean. Other types will be converted to null.
214
+ *
215
+ * If the number is not an integer, an error will be thrown.
216
+ */
217
+ exports.excelIntNullableDecoder = exports.excelNumberNullableDecoder.transform(nullableTransformer(intValidator));
218
+ // TODO: Add BigInt tests (to be investigated, how Excel handles BigInts)
219
+ const bigIntTransformer = (x, ctx) => {
220
+ if (x % 1 !== 0) {
221
+ ctx.addIssue({
222
+ code: zod_1.default.ZodIssueCode.custom,
223
+ message: `Not an integer: ${x}`,
224
+ });
225
+ return zod_1.default.NEVER;
226
+ }
227
+ return BigInt(x);
228
+ };
229
+ /**
230
+ * A decoder that transforms Excel numbers to JS BigInts, also handling strings and boolean. Other types will throw an error.
231
+ *
232
+ * If the number is not a (big) integer, an error will be thrown.
233
+ */
234
+ exports.excelBigIntStrictDecoder = exports.excelNumberStrictDecoder.transform(bigIntTransformer);
235
+ /**
236
+ * A decoder that transforms Excel numbers to JS BigInts, also handling strings and boolean. Other types will be converted to 0.
237
+ *
238
+ * If the number is not a (big) integer, an error will be thrown.
239
+ */
240
+ exports.excelBigIntDecoder = exports.excelNumberDecoder.transform(bigIntTransformer);
241
+ /**
242
+ * A decoder that transforms Excel numbers to JS BigInts, also handling strings and boolean. Other types will be converted to null.
243
+ *
244
+ * If the number is not a (big) integer, an error will be thrown.
245
+ */
246
+ exports.excelBigIntNullableDecoder = exports.excelNumberNullableDecoder.transform(nullableTransformer(bigIntTransformer));
247
+ /**
248
+ * A decoder that transforms Excel booleans to JS booleans, also handling numbers and strings.
249
+ *
250
+ * It'll perform casting based on the following rules:
251
+ * - If the value is a number, it'll return true if it's not 0
252
+ * - If the value is a string, it'll return true if it's 'true' or '1', false if it's 'false' or '0', and throw an error otherwise
253
+ * - If the value is boolean, it'll return it as-is
254
+ * - If the value is any other type, it'll throw an error
255
+ */
256
+ exports.excelBooleanStrictDecoder = zod_1.default.union([zod_1.default.boolean(), zod_1.default.number(), zod_1.default.string()]).transform((x, ctx) => {
257
+ if (typeof x === 'number') {
258
+ return x !== 0;
259
+ }
260
+ if (typeof x === 'string') {
261
+ switch (x.toLowerCase()) {
262
+ case 'true':
263
+ case '1':
264
+ return true;
265
+ case 'false':
266
+ case '0':
267
+ return false;
268
+ default:
269
+ ctx.addIssue({
270
+ code: zod_1.default.ZodIssueCode.custom,
271
+ message: `Not a boolean: ${x}`,
272
+ });
273
+ return zod_1.default.NEVER;
274
+ }
275
+ }
276
+ if (typeof x === 'boolean') {
277
+ return x;
278
+ }
279
+ throw new types_1.ExhaustiveSwitchCheck(x);
280
+ });
281
+ /**
282
+ * A decoder that transforms Excel booleans to JS booleans, also handling numbers and strings.
283
+ *
284
+ * It'll perform casting based on the following rules:
285
+ * - If the value is a number, it'll return true if it's not 0
286
+ * - If the value is a string, it'll return true if it's 'true' or '1', false otherwise
287
+ * - If the value is boolean, it'll return it as-is
288
+ * - If the value is any other type, it'll return false
289
+ */
290
+ exports.excelBooleanDecoder = zod_1.default.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()]).transform((x) => {
291
+ if (typeof x === 'number') {
292
+ return x !== 0;
293
+ }
294
+ if (typeof x === 'string') {
295
+ switch (x.toLowerCase()) {
296
+ case 'true':
297
+ case '1':
298
+ return true;
299
+ case 'false':
300
+ case '0':
301
+ return false;
302
+ default:
303
+ return false;
304
+ }
305
+ }
306
+ if (typeof x === 'boolean') {
307
+ return x;
308
+ }
309
+ return false;
310
+ });
311
+ /**
312
+ * A decoder that transforms Excel booleans to JS booleans, also handling numbers and strings.
313
+ *
314
+ * It'll perform casting based on the following rules:
315
+ * - If the value is a number, it'll return true if it's not 0
316
+ * - If the value is a string, it'll return true if it's 'true' or '1', false if it's 'false' or '0', and null otherwise
317
+ * - If the value is boolean, it'll return it as-is
318
+ * - If the value is any other type, it'll return null
319
+ */
320
+ exports.excelBooleanNullableDecoder = zod_1.default.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()]).transform((x) => {
321
+ if (typeof x === 'number') {
322
+ return x !== 0;
323
+ }
324
+ if (typeof x === 'string') {
325
+ switch (x.toLowerCase()) {
326
+ case 'true':
327
+ case '1':
328
+ return true;
329
+ case 'false':
330
+ case '0':
331
+ return false;
332
+ default:
333
+ return null;
334
+ }
335
+ }
336
+ if (typeof x === 'boolean') {
337
+ return x;
338
+ }
339
+ return null;
340
+ });
341
+ /**
342
+ * A decoder that transforms Excel dates to JS Dates
343
+ */
344
+ exports.excelDateDecoder = zod_1.default
345
+ .union([zod_1.default.number(), zod_1.default.string()])
346
+ .transform((x, ctx) => {
347
+ if (typeof x === 'number') {
348
+ // Excel number is a float with 1 representing Jan 1, 1900. Each increment is a day.
349
+ // As Excel incorrectly ignores the 1900 leap year, we need to correct for that
350
+ const date = new Date(Date.UTC(1900, 0, 1));
351
+ date.setDate(date.getDate() + x - 2);
352
+ const fractionalDay = x - Math.floor(x);
353
+ date.setMilliseconds(date.getMilliseconds() + fractionalDay * 24 * 60 * 60 * 1000);
354
+ return date;
355
+ }
356
+ const result = new Date(x);
357
+ if (isNaN(result.getTime())) {
358
+ ctx.addIssue({
359
+ code: zod_1.default.ZodIssueCode.custom,
360
+ message: `Not a date: ${x}`,
361
+ });
362
+ return zod_1.default.NEVER;
363
+ }
364
+ return result;
365
+ });
366
+ /**
367
+ * A decoder that transforms nullable Excel dates to JS Dates
368
+ */
369
+ exports.excelDateNullableDecoder = zod_1.default.union([zod_1.default.string(), zod_1.default.number(), zod_1.default.null(), zod_1.default.undefined(), zod_1.default.any()]).transform((x, ctx) => {
370
+ if (x === null || x === undefined) {
371
+ return null;
372
+ }
373
+ if (typeof x === 'number') {
374
+ // Excel number is a float with 1 representing Jan 1, 1900. Each increment is a day.
375
+ // As Excel incorrectly ignores the 1900 leap year, we need to correct for that
376
+ const date = new Date(Date.UTC(1900, 0, 1));
377
+ date.setDate(date.getDate() + x - 2);
378
+ const fractionalDay = x - Math.floor(x);
379
+ date.setMilliseconds(date.getMilliseconds() + fractionalDay * 24 * 60 * 60 * 1000);
380
+ return date;
381
+ }
382
+ if (typeof x === 'string') {
383
+ if (x.trim() === '') {
384
+ return null;
385
+ }
386
+ const result = new Date(x);
387
+ if (isNaN(result.getTime())) {
388
+ ctx.addIssue({
389
+ code: zod_1.default.ZodIssueCode.custom,
390
+ message: `Not a date: ${x}`,
391
+ });
392
+ return zod_1.default.NEVER;
393
+ }
394
+ return result;
395
+ }
396
+ return null;
397
+ });
package/dist/zod.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ import { z } from 'zod';
2
+ export type Transformer<Input, Output> = (value: Input, ctx: z.RefinementCtx) => Output | z.ZodNever;
3
+ type ReturnTypeOfLast<T extends any[]> = T extends [...any, infer L] ? L extends (...args: any) => any ? ReturnType<L> : never : never;
4
+ /**
5
+ * Pipes a list of Zod transformers together in a type-safe way. Returns early if any of the transformers returns `z.NEVER`.
6
+ */
7
+ export declare function pipeZodTransformers<Initial, Fns extends [Transformer<any, any>, ...Transformer<any, any>[]]>(initialValue: Initial, ctx: z.RefinementCtx, fns: Fns): ReturnTypeOfLast<Fns>;
8
+ export {};
package/dist/zod.js ADDED
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.pipeZodTransformers = pipeZodTransformers;
4
+ const zod_1 = require("zod");
5
+ /**
6
+ * Pipes a list of Zod transformers together in a type-safe way. Returns early if any of the transformers returns `z.NEVER`.
7
+ */
8
+ function pipeZodTransformers(initialValue, ctx, fns) {
9
+ let result = initialValue;
10
+ for (const fn of fns) {
11
+ result = fn(result, ctx);
12
+ if (result === zod_1.z.NEVER) {
13
+ return zod_1.z.NEVER;
14
+ }
15
+ }
16
+ return result;
17
+ }
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@postxl/utils",
3
+ "version": "0.1.8",
4
+ "main": "./dist/index.js",
5
+ "typings": "./dist/index.d.ts",
6
+ "scripts": {
7
+ "build": "tsc -b tsconfig.build.json",
8
+ "lint": "eslint .",
9
+ "prettier:check": "prettier --check \"**/*.{ts,tsx}\" --config ../../prettier.config.js",
10
+ "test:jest": "jest",
11
+ "test:watch": "jest --watch",
12
+ "test:coverage": "jest --coverage",
13
+ "test:types": "tsc --noEmit",
14
+ "watch": "tsc -b tsconfig.build.json -w"
15
+ },
16
+ "files": [
17
+ "dist"
18
+ ],
19
+ "dependencies": {
20
+ "zod": "3.24.1"
21
+ },
22
+ "devDependencies": {
23
+ "tsconfig-paths": "4.2.0"
24
+ },
25
+ "wallaby": {
26
+ "env": {
27
+ "type": "node",
28
+ "params": {
29
+ "runner": "--experimental-vm-modules"
30
+ }
31
+ }
32
+ }
33
+ }