@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.
- package/LICENSE +22 -0
- package/README.md +3 -0
- package/dist/DefaultMap.d.ts +11 -0
- package/dist/DefaultMap.js +26 -0
- package/dist/NestedMap.d.ts +16 -0
- package/dist/NestedMap.js +73 -0
- package/dist/TypedMapping.d.ts +26 -0
- package/dist/TypedMapping.js +33 -0
- package/dist/array.d.ts +6 -0
- package/dist/array.js +20 -0
- package/dist/async.d.ts +3 -0
- package/dist/async.js +40 -0
- package/dist/buffer.d.ts +4 -0
- package/dist/buffer.js +15 -0
- package/dist/check-port.d.ts +4 -0
- package/dist/check-port.js +27 -0
- package/dist/datetime.d.ts +4 -0
- package/dist/datetime.js +15 -0
- package/dist/dictionary.d.ts +8 -0
- package/dist/dictionary.js +44 -0
- package/dist/format.d.ts +1 -0
- package/dist/format.js +6 -0
- package/dist/group-by.d.ts +8 -0
- package/dist/group-by.js +65 -0
- package/dist/index.d.ts +25 -0
- package/dist/index.js +41 -0
- package/dist/is-object.d.ts +12 -0
- package/dist/is-object.js +24 -0
- package/dist/map.d.ts +22 -0
- package/dist/map.js +75 -0
- package/dist/omit.d.ts +4 -0
- package/dist/omit.js +10 -0
- package/dist/pagination.d.ts +6 -0
- package/dist/pagination.js +13 -0
- package/dist/random.d.ts +4 -0
- package/dist/random.js +16 -0
- package/dist/remove-secrets.d.ts +11 -0
- package/dist/remove-secrets.js +61 -0
- package/dist/remove-undefined.d.ts +9 -0
- package/dist/remove-undefined.js +21 -0
- package/dist/result.d.ts +24 -0
- package/dist/result.js +49 -0
- package/dist/sort.d.ts +17 -0
- package/dist/sort.js +63 -0
- package/dist/string-colors.d.ts +44 -0
- package/dist/string-colors.js +72 -0
- package/dist/string.d.ts +115 -0
- package/dist/string.js +322 -0
- package/dist/types.d.ts +78 -0
- package/dist/types.js +38 -0
- package/dist/uniq.d.ts +1 -0
- package/dist/uniq.js +15 -0
- package/dist/zod-excel.decoders.d.ts +151 -0
- package/dist/zod-excel.decoders.js +397 -0
- package/dist/zod.d.ts +8 -0
- package/dist/zod.js +17 -0
- package/package.json +33 -0
package/dist/string.js
ADDED
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.upperFirst = upperFirst;
|
|
4
|
+
exports.lowerFirst = lowerFirst;
|
|
5
|
+
exports.pluralize = pluralize;
|
|
6
|
+
exports.isPlural = isPlural;
|
|
7
|
+
exports.capitalizeKeys = capitalizeKeys;
|
|
8
|
+
exports.uncapitalizeKeys = uncapitalizeKeys;
|
|
9
|
+
exports.uncapitalize = uncapitalize;
|
|
10
|
+
exports.capitalize = capitalize;
|
|
11
|
+
exports.toCamelCase = toCamelCase;
|
|
12
|
+
exports.isCamelCase = isCamelCase;
|
|
13
|
+
exports.toPascalCase = toPascalCase;
|
|
14
|
+
exports.isPascalCase = isPascalCase;
|
|
15
|
+
exports.toSnakeCase = toSnakeCase;
|
|
16
|
+
exports.isSnakeCase = isSnakeCase;
|
|
17
|
+
exports.commentLines = commentLines;
|
|
18
|
+
exports.toHumanReadable = toHumanReadable;
|
|
19
|
+
exports.conjugateNames = conjugateNames;
|
|
20
|
+
exports.slugify = slugify;
|
|
21
|
+
exports.isSlug = isSlug;
|
|
22
|
+
function upperFirst(s) {
|
|
23
|
+
return s.charAt(0).toUpperCase() + s.slice(1);
|
|
24
|
+
}
|
|
25
|
+
function lowerFirst(s) {
|
|
26
|
+
return s.charAt(0).toLowerCase() + s.slice(1);
|
|
27
|
+
}
|
|
28
|
+
const IRREGULAR_PLURALS = Object.entries({
|
|
29
|
+
abyss: 'abysses',
|
|
30
|
+
aegis: 'aegises',
|
|
31
|
+
alias: 'aliases',
|
|
32
|
+
alumnus: 'alumni',
|
|
33
|
+
amends: 'amends',
|
|
34
|
+
analysis: 'analyses',
|
|
35
|
+
apparatus: 'apparatuses',
|
|
36
|
+
appendix: 'appendices',
|
|
37
|
+
asbestos: 'asbestoses',
|
|
38
|
+
atlas: 'atlases',
|
|
39
|
+
axis: 'axes',
|
|
40
|
+
basis: 'bases',
|
|
41
|
+
biceps: 'bicepses',
|
|
42
|
+
bus: 'buses',
|
|
43
|
+
caress: 'caresses',
|
|
44
|
+
child: 'children',
|
|
45
|
+
children: 'children',
|
|
46
|
+
circus: 'circuses',
|
|
47
|
+
citrus: 'citruses',
|
|
48
|
+
compass: 'compasses',
|
|
49
|
+
crisis: 'crises',
|
|
50
|
+
criterion: 'criteria',
|
|
51
|
+
crocus: 'crocuses',
|
|
52
|
+
data: 'data',
|
|
53
|
+
datum: 'data',
|
|
54
|
+
diabetes: 'diabetes',
|
|
55
|
+
diagnosis: 'diagnoses',
|
|
56
|
+
dross: 'drosses',
|
|
57
|
+
egress: 'egresses',
|
|
58
|
+
elvis: 'elvises',
|
|
59
|
+
emboss: 'embosses',
|
|
60
|
+
fiss: 'fisses',
|
|
61
|
+
focus: 'foci',
|
|
62
|
+
glass: 'glasses',
|
|
63
|
+
hippocampus: 'hippocampi',
|
|
64
|
+
// While `histories` is the plural of `history`, in a model context, `history` makes more sense:
|
|
65
|
+
// If you have a model called `UserHistory`, calling the table `UserHistories` would be confusing.
|
|
66
|
+
history: 'history',
|
|
67
|
+
hypothesis: 'hypotheses',
|
|
68
|
+
ignis: 'ignises',
|
|
69
|
+
index: 'indices',
|
|
70
|
+
iris: 'irises',
|
|
71
|
+
jesus: 'jesuses',
|
|
72
|
+
kudos: 'kudos',
|
|
73
|
+
lens: 'lenses',
|
|
74
|
+
man: 'men',
|
|
75
|
+
matrix: 'matrices',
|
|
76
|
+
medium: 'media',
|
|
77
|
+
news: 'news',
|
|
78
|
+
oasis: 'oases',
|
|
79
|
+
parenthesis: 'parentheses',
|
|
80
|
+
pass: 'passes',
|
|
81
|
+
phenomenon: 'phenomena',
|
|
82
|
+
prognosis: 'prognoses',
|
|
83
|
+
radius: 'radii',
|
|
84
|
+
ras: 'rasses',
|
|
85
|
+
sepsis: 'sepses',
|
|
86
|
+
species: 'species',
|
|
87
|
+
status: 'statuses',
|
|
88
|
+
suffix: 'suffixes',
|
|
89
|
+
syllabus: 'syllabi',
|
|
90
|
+
synopsis: 'synopses',
|
|
91
|
+
thesis: 'theses',
|
|
92
|
+
tress: 'tresses',
|
|
93
|
+
virus: 'viruses',
|
|
94
|
+
vortex: 'vortices',
|
|
95
|
+
woman: 'women',
|
|
96
|
+
});
|
|
97
|
+
/**
|
|
98
|
+
* Returns a pluralized version of the given string based on the count.
|
|
99
|
+
*/
|
|
100
|
+
function pluralize(s, count = 2) {
|
|
101
|
+
// If there's one of something we simply use the singular form.
|
|
102
|
+
if (count === 1) {
|
|
103
|
+
return s;
|
|
104
|
+
}
|
|
105
|
+
// For plural forms, we first check whether a word is irregular.
|
|
106
|
+
const lower = s.toLowerCase();
|
|
107
|
+
for (const [singular, plural] of IRREGULAR_PLURALS) {
|
|
108
|
+
// We check that items *end with* a given word because we use combined words
|
|
109
|
+
// like `UserHistory` as input values of this function.
|
|
110
|
+
if (lower.endsWith(singular)) {
|
|
111
|
+
return s.slice(0, -singular.length + 1) + plural.slice(1);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
if (lower.endsWith('ss')) {
|
|
115
|
+
return s.slice(0, -2) + 'sses';
|
|
116
|
+
}
|
|
117
|
+
if (
|
|
118
|
+
// e.g. index
|
|
119
|
+
lower.endsWith('x') ||
|
|
120
|
+
// e.g. buzz
|
|
121
|
+
lower.endsWith('z') ||
|
|
122
|
+
// e.g. wish
|
|
123
|
+
lower.endsWith('sh') ||
|
|
124
|
+
// e.g. match
|
|
125
|
+
lower.endsWith('ch')) {
|
|
126
|
+
return s + 'es';
|
|
127
|
+
}
|
|
128
|
+
if (s.endsWith('y') &&
|
|
129
|
+
!s.endsWith('ay') &&
|
|
130
|
+
!s.endsWith('ey') &&
|
|
131
|
+
!s.endsWith('iy') &&
|
|
132
|
+
!s.endsWith('oy') &&
|
|
133
|
+
!s.endsWith('uy')) {
|
|
134
|
+
return s.slice(0, -1) + 'ies';
|
|
135
|
+
}
|
|
136
|
+
if (s.endsWith('s')) {
|
|
137
|
+
return s;
|
|
138
|
+
}
|
|
139
|
+
return s + 's';
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Returns true if the given string is already pluralized.
|
|
143
|
+
*/
|
|
144
|
+
function isPlural(s) {
|
|
145
|
+
const plural = pluralize(s);
|
|
146
|
+
return plural === s;
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Converts all keys of an object to capitalized versions in a type-safe way.
|
|
150
|
+
*/
|
|
151
|
+
function capitalizeKeys(obj) {
|
|
152
|
+
const result = {};
|
|
153
|
+
for (const key in obj) {
|
|
154
|
+
const cKey = `${key.charAt(0).toUpperCase()}${key.slice(1)}`;
|
|
155
|
+
const cVal = obj[key];
|
|
156
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
157
|
+
// @ts-expect-error
|
|
158
|
+
result[cKey] = cVal;
|
|
159
|
+
}
|
|
160
|
+
return result;
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Converts all keys of an object to uncapitalized versions in a type-safe way.
|
|
164
|
+
*/
|
|
165
|
+
function uncapitalizeKeys(obj) {
|
|
166
|
+
const result = {};
|
|
167
|
+
for (const key in obj) {
|
|
168
|
+
const unKey = `${key.charAt(0).toLowerCase()}${key.slice(1)}`;
|
|
169
|
+
const unVal = obj[key];
|
|
170
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
171
|
+
// @ts-expect-error
|
|
172
|
+
result[unKey] = unVal;
|
|
173
|
+
}
|
|
174
|
+
return result;
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Returns the same string with a lowercase first letter.
|
|
178
|
+
*/
|
|
179
|
+
function uncapitalize(str) {
|
|
180
|
+
return str.charAt(0).toLowerCase() + str.slice(1);
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Returns the same string with an uppercase first letter.
|
|
184
|
+
*/
|
|
185
|
+
function capitalize(str) {
|
|
186
|
+
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Returns the camelCase version of the given string.
|
|
190
|
+
*
|
|
191
|
+
* Camel case examples:
|
|
192
|
+
* `toCamelCase('hello world')` -> 'helloWorld',
|
|
193
|
+
* `toCamelCase('hello_world')` -> 'helloWorld',
|
|
194
|
+
* `toCamelCase('helloWorld')` -> 'helloWorld'
|
|
195
|
+
* `toCamelCase('HelloWorld')` -> 'helloWorld'
|
|
196
|
+
* `toCamelCase('hello_world')` -> 'helloWorld'
|
|
197
|
+
*/
|
|
198
|
+
function toCamelCase(str) {
|
|
199
|
+
if (/^[a-z]+(?:[A-Z][a-z]*)*$/.test(str)) {
|
|
200
|
+
return str;
|
|
201
|
+
}
|
|
202
|
+
// Split the string into an array of words
|
|
203
|
+
// We split on spaces, underscores, and hyphens.
|
|
204
|
+
// We also split on single uppercase letters, though not on consecutive uppercase letters.
|
|
205
|
+
const words = str.split(/(?<![A-Z])(?=[A-Z])|[\s_-]+/);
|
|
206
|
+
// Convert the first word to lowercase and capitalize the rest
|
|
207
|
+
const camelCasedWords = words
|
|
208
|
+
.map((word) => (word.toUpperCase() === word ? word.toLowerCase() : word))
|
|
209
|
+
.map((word, index) => (index === 0 ? lowerFirst(word) : upperFirst(word)));
|
|
210
|
+
// Join the words back together into a single string
|
|
211
|
+
const camelCasedStr = camelCasedWords.join('');
|
|
212
|
+
return camelCasedStr;
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Validates if a string is in camelCase format
|
|
216
|
+
*/
|
|
217
|
+
function isCamelCase(str) {
|
|
218
|
+
return str === toCamelCase(str);
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Returns the PascalCase version of the given string.
|
|
222
|
+
*
|
|
223
|
+
* Examples:
|
|
224
|
+
* `toPascalCase('hello world')` -> 'HelloWorld'
|
|
225
|
+
* `toPascalCase('hello_world')` -> 'HelloWorld'
|
|
226
|
+
* `toPascalCase('helloWorld')` -> 'HelloWorld'
|
|
227
|
+
* `toPascalCase('Hello-World')` -> 'HelloWorld'
|
|
228
|
+
* `toPascalCase('HELLO_WORLD')` -> 'HelloWorld'
|
|
229
|
+
*/
|
|
230
|
+
function toPascalCase(str) {
|
|
231
|
+
const s = toCamelCase(str);
|
|
232
|
+
return s.charAt(0).toUpperCase() + s.slice(1);
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Validates if a string is in PascalCase format
|
|
236
|
+
*/
|
|
237
|
+
function isPascalCase(str) {
|
|
238
|
+
return str === toPascalCase(str);
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* Returns the snake_case version of the given string.
|
|
242
|
+
*
|
|
243
|
+
* Examples:
|
|
244
|
+
* `toSnakeCase('hello world')` -> 'hello_world'
|
|
245
|
+
* `toSnakeCase('helloWorld')` -> 'hello_world'
|
|
246
|
+
* `toSnakeCase('HelloWorld')` -> 'hello_world'
|
|
247
|
+
* `toSnakeCase('HELLO_WORLD')` -> 'hello_world'
|
|
248
|
+
*/
|
|
249
|
+
function toSnakeCase(str) {
|
|
250
|
+
const camelCase = toCamelCase(str);
|
|
251
|
+
return camelCase.replace(/([A-Z])/g, '_$1').toLowerCase();
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Validates if a string is in snake_case format
|
|
255
|
+
*/
|
|
256
|
+
function isSnakeCase(str) {
|
|
257
|
+
return str === toSnakeCase(str);
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* Converts each line of a string to a commented line
|
|
261
|
+
*/
|
|
262
|
+
function commentLines(lines) {
|
|
263
|
+
return lines
|
|
264
|
+
.split('\n')
|
|
265
|
+
.map((l) => `// ${l}`)
|
|
266
|
+
.join('\n');
|
|
267
|
+
}
|
|
268
|
+
function toHumanReadable(input) {
|
|
269
|
+
// Replace underscores and hyphens with spaces
|
|
270
|
+
let result = input.replace(/[_-]/g, ' ');
|
|
271
|
+
// Add spaces before capital letters
|
|
272
|
+
result = result.replace(/([a-z])([A-Z])/g, '$1 $2');
|
|
273
|
+
//In case the string was already human readable, return it
|
|
274
|
+
if (result === input) {
|
|
275
|
+
return input;
|
|
276
|
+
}
|
|
277
|
+
//lowercase the first letter of every word
|
|
278
|
+
result = result
|
|
279
|
+
.split(' ')
|
|
280
|
+
.map((s) => s.toLowerCase())
|
|
281
|
+
.join(' ');
|
|
282
|
+
// Capitalize the first letter of the resulting string
|
|
283
|
+
result = upperFirst(result);
|
|
284
|
+
return result;
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* Provide all relevant conjugation of a name
|
|
288
|
+
*/
|
|
289
|
+
function conjugateNames(name) {
|
|
290
|
+
return {
|
|
291
|
+
PascalCase: toPascalCase(name),
|
|
292
|
+
PascalCasePlural: toPascalCase(pluralize(name)),
|
|
293
|
+
camelCase: toCamelCase(name),
|
|
294
|
+
camelCasePlural: toCamelCase(pluralize(name)),
|
|
295
|
+
snake_case: toSnakeCase(name),
|
|
296
|
+
snake_case_plural: toSnakeCase(pluralize(name)),
|
|
297
|
+
pluralized: capitalize(pluralize(name)),
|
|
298
|
+
uncapitalizedPlural: uncapitalize(pluralize(name)),
|
|
299
|
+
uncapitalized: uncapitalize(name),
|
|
300
|
+
capitalized: capitalize(name),
|
|
301
|
+
capitalizedPlural: capitalize(pluralize(name)),
|
|
302
|
+
};
|
|
303
|
+
}
|
|
304
|
+
function slugify(str) {
|
|
305
|
+
return (str
|
|
306
|
+
// replace uppercase letters with hyphens and letter (will be lowercased later)
|
|
307
|
+
.replace(/([A-Z])/g, '-$1')
|
|
308
|
+
.toLowerCase()
|
|
309
|
+
// replace letter followed by number with letter-number
|
|
310
|
+
.replace(/([a-z])(\d)/g, '$1-$2')
|
|
311
|
+
// replace non-alphanumeric characters with hyphens
|
|
312
|
+
.replace(/[^a-z0-9]+/g, '-')
|
|
313
|
+
// remove leading hyphens
|
|
314
|
+
.replace(/^-/g, '')
|
|
315
|
+
// remove trailing hyphens
|
|
316
|
+
.replace(/-$/g, '')
|
|
317
|
+
// replace multiple hyphens with a single hyphen
|
|
318
|
+
.replace(/-+/g, '-'));
|
|
319
|
+
}
|
|
320
|
+
function isSlug(str) {
|
|
321
|
+
return /^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(str);
|
|
322
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
type Without<T, U> = {
|
|
2
|
+
[P in Exclude<keyof T, keyof U>]?: never;
|
|
3
|
+
};
|
|
4
|
+
export type XOR<T, U> = T | U extends object ? (Without<T, U> & U) | (Without<U, T> & T) : T | U;
|
|
5
|
+
export type Exact<T, I> = T extends I ? (Exclude<keyof T, keyof I> extends never ? T : never) : never;
|
|
6
|
+
export type UncapitalizeObjectKeys<T> = {
|
|
7
|
+
[key in keyof T as Uncapitalize<key & string>]: T[key];
|
|
8
|
+
};
|
|
9
|
+
export type Prettify<T> = {
|
|
10
|
+
[K in keyof T]: T[K];
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Distributive Pick - does not collapse unions into a "shared type" only to
|
|
14
|
+
* run Pick on it. Instead, it "picks" from each union item separately.
|
|
15
|
+
*
|
|
16
|
+
* See https://github.com/klimashkin/css-modules-theme/pull/8
|
|
17
|
+
*
|
|
18
|
+
* Example:
|
|
19
|
+
* Pick<{ type: "pick" } | { type: "omit" }, "type">
|
|
20
|
+
* produces { type: "pick" | "omit" }
|
|
21
|
+
*
|
|
22
|
+
* UnionPick<{ type: "pick" } | { type: "omit" }, "type">
|
|
23
|
+
* produces { type: "pick" } | { type: "omit" }
|
|
24
|
+
*/
|
|
25
|
+
export type UnionPick<T, K extends keyof T> = T extends unknown ? Pick<T, K> : never;
|
|
26
|
+
/**
|
|
27
|
+
* Like UnionPick, but for Omit
|
|
28
|
+
*/
|
|
29
|
+
export type UnionOmit<T, K extends keyof T> = T extends unknown ? Omit<T, K> : never;
|
|
30
|
+
/**
|
|
31
|
+
* Makes a type check that is only valid when all cases of a switch
|
|
32
|
+
* statement have been covered.
|
|
33
|
+
*/
|
|
34
|
+
export declare class ExhaustiveSwitchCheck extends Error {
|
|
35
|
+
constructor(val: never);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* A type that represents an array that is guaranteed to have at least one element.
|
|
39
|
+
*/
|
|
40
|
+
export type NonEmptyArray<T> = [T, ...T[]];
|
|
41
|
+
/**
|
|
42
|
+
* Converts a nested object to a nested object where all properties are optional.
|
|
43
|
+
*/
|
|
44
|
+
export type DeepPartial<T> = T extends object ? {
|
|
45
|
+
[P in keyof T]?: DeepPartial<T[P]>;
|
|
46
|
+
} : T;
|
|
47
|
+
/**
|
|
48
|
+
* Changes properties of an object type to optional.
|
|
49
|
+
*
|
|
50
|
+
* Example:
|
|
51
|
+
* type Foo = { a: string, b: number, c: boolean }
|
|
52
|
+
* type Bar = Optional<Foo, 'a' | 'b'>
|
|
53
|
+
* // Bar is { a?: string, b?: number, c: boolean }
|
|
54
|
+
*/
|
|
55
|
+
export type Optional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
|
|
56
|
+
/**
|
|
57
|
+
* Applies a type assertion to an entire array and hence asserts the array.
|
|
58
|
+
*
|
|
59
|
+
* Type assertions are especially useful when you want to modify and object in place - a function
|
|
60
|
+
* with a type assertion lets TypeScript know that the object was modified.
|
|
61
|
+
*
|
|
62
|
+
* Example:
|
|
63
|
+
* ```
|
|
64
|
+
* type Original = { a: number }
|
|
65
|
+
* type WithExtra = Original & { b: string }
|
|
66
|
+
*
|
|
67
|
+
* function addExtra(obj: Original): asserts obj is WithExtra {
|
|
68
|
+
* obj.b = 'new value'
|
|
69
|
+
* }
|
|
70
|
+
*
|
|
71
|
+
* const myObj: Original[] = [{ a: 1 }]
|
|
72
|
+
* console.log(myObj[0].b) // ❌ TypeScript error!
|
|
73
|
+
* assertArray(myObj, addExtra)
|
|
74
|
+
* console.log(myObj[0].b) // ✅ No TypeScript error!
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
export declare function assertArray<I, O extends I>(obj: I[], fn: (i: I) => asserts i is O): asserts obj is O[];
|
|
78
|
+
export {};
|
package/dist/types.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ExhaustiveSwitchCheck = void 0;
|
|
4
|
+
exports.assertArray = assertArray;
|
|
5
|
+
/**
|
|
6
|
+
* Makes a type check that is only valid when all cases of a switch
|
|
7
|
+
* statement have been covered.
|
|
8
|
+
*/
|
|
9
|
+
class ExhaustiveSwitchCheck extends Error {
|
|
10
|
+
constructor(val) {
|
|
11
|
+
super(`Unreachable case: ${JSON.stringify(val)}`);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
exports.ExhaustiveSwitchCheck = ExhaustiveSwitchCheck;
|
|
15
|
+
/**
|
|
16
|
+
* Applies a type assertion to an entire array and hence asserts the array.
|
|
17
|
+
*
|
|
18
|
+
* Type assertions are especially useful when you want to modify and object in place - a function
|
|
19
|
+
* with a type assertion lets TypeScript know that the object was modified.
|
|
20
|
+
*
|
|
21
|
+
* Example:
|
|
22
|
+
* ```
|
|
23
|
+
* type Original = { a: number }
|
|
24
|
+
* type WithExtra = Original & { b: string }
|
|
25
|
+
*
|
|
26
|
+
* function addExtra(obj: Original): asserts obj is WithExtra {
|
|
27
|
+
* obj.b = 'new value'
|
|
28
|
+
* }
|
|
29
|
+
*
|
|
30
|
+
* const myObj: Original[] = [{ a: 1 }]
|
|
31
|
+
* console.log(myObj[0].b) // ❌ TypeScript error!
|
|
32
|
+
* assertArray(myObj, addExtra)
|
|
33
|
+
* console.log(myObj[0].b) // ✅ No TypeScript error!
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
function assertArray(obj, fn) {
|
|
37
|
+
obj.forEach(fn);
|
|
38
|
+
}
|
package/dist/uniq.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function uniq<T>(items: T[], predicate: (item: T) => number): T[];
|
package/dist/uniq.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.uniq = uniq;
|
|
4
|
+
function uniq(items, predicate) {
|
|
5
|
+
const result = [];
|
|
6
|
+
const seen = {};
|
|
7
|
+
for (const item of items) {
|
|
8
|
+
const key = predicate(item);
|
|
9
|
+
if (!seen[key]) {
|
|
10
|
+
seen[key] = true;
|
|
11
|
+
result.push(item);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
return result;
|
|
15
|
+
}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import z from 'zod';
|
|
2
|
+
/**
|
|
3
|
+
* A decoder that transforms Excel strings to JS strings, also handling numbers and boolean. Will not parse any other type!
|
|
4
|
+
*
|
|
5
|
+
* Background: In Excel, entering a number in a cell will automatically convert it to a number -
|
|
6
|
+
* and xlPort will return it as a number. However, often we want to treat it as a string - this decoder
|
|
7
|
+
* hence accepts both strings and numbers, and converts numbers to strings.
|
|
8
|
+
*
|
|
9
|
+
* It'll perform casting based on the following rules:
|
|
10
|
+
* - If the value is a number, it'll be converted to a string
|
|
11
|
+
* - If the value is a string, it'll be returned as-is
|
|
12
|
+
* - If the value is boolean, it'll return 'true' or 'false'
|
|
13
|
+
*
|
|
14
|
+
* Any other type will throw an error!
|
|
15
|
+
*/
|
|
16
|
+
export declare const excelStringStrictDecoder: z.ZodEffects<z.ZodUnion<[z.ZodString, z.ZodNumber, z.ZodBoolean]>, string, string | number | boolean>;
|
|
17
|
+
/**
|
|
18
|
+
* A decoder that transforms Excel strings to JS strings, also handling numbers and any other potential type
|
|
19
|
+
*
|
|
20
|
+
* Background: In Excel, entering a number in a cell will automatically convert it to a number -
|
|
21
|
+
* and xlPort will return it as a number. However, often we want to treat it as a string - this decoder
|
|
22
|
+
* hence accepts both strings and numbers, and converts numbers to strings.
|
|
23
|
+
*
|
|
24
|
+
* It'll perform casting based on the following rules:
|
|
25
|
+
* - If the value is a number, it'll be converted to a string
|
|
26
|
+
* - If the value is a string, it'll be returned as-is
|
|
27
|
+
* - If the value is boolean, it'll return 'true' or 'false'
|
|
28
|
+
* - If the value is null or undefined, it'll return an empty string
|
|
29
|
+
* - If the value is any other type, it'll return an empty string
|
|
30
|
+
*/
|
|
31
|
+
export declare const excelStringDecoder: z.ZodEffects<z.ZodUnion<[z.ZodString, z.ZodNumber, z.ZodBoolean, z.ZodNull, z.ZodUndefined, z.ZodAny]>, string, any>;
|
|
32
|
+
/**
|
|
33
|
+
* A decoder that transforms Excel strings to JS strings, also handling numbers and null
|
|
34
|
+
*
|
|
35
|
+
* Background: In Excel, entering a number in a cell will automatically convert it to a number -
|
|
36
|
+
* and xlPort will return it as a number. However, often we want to treat it as a string - this decoder
|
|
37
|
+
* hence accepts both strings and numbers, and converts numbers to strings.
|
|
38
|
+
*
|
|
39
|
+
* It'll perform casting based on the following rules:
|
|
40
|
+
* - If the value is a number, it'll be converted to a string
|
|
41
|
+
* - If the value is a string, it'll be returned as-is
|
|
42
|
+
* - If the value is boolean, it'll return 'true' or 'false'
|
|
43
|
+
* - If the value is null or undefined, it'll return null
|
|
44
|
+
* - If the value is any other type, it'll return null
|
|
45
|
+
*/
|
|
46
|
+
export declare const excelStringNullableDecoder: z.ZodEffects<z.ZodUnion<[z.ZodString, z.ZodNumber, z.ZodBoolean, z.ZodNull, z.ZodUndefined, z.ZodAny]>, string | null, any>;
|
|
47
|
+
/**
|
|
48
|
+
* A decoder that transforms Excel numbers to JS numbers, also handling strings and boolean. Will not parse any other type!
|
|
49
|
+
*
|
|
50
|
+
* It'll perform casting based on the following rules:
|
|
51
|
+
* - If the value is a number, it'll be returned as-is
|
|
52
|
+
* - 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.
|
|
53
|
+
* - If the value is boolean, it'll return 1 or 0
|
|
54
|
+
* - If the value is any other type, it'll throw an error
|
|
55
|
+
*
|
|
56
|
+
*/
|
|
57
|
+
export declare const excelNumberStrictDecoder: z.ZodEffects<z.ZodUnion<[z.ZodString, z.ZodNumber, z.ZodBoolean]>, number, string | number | boolean>;
|
|
58
|
+
/**
|
|
59
|
+
* A decoder that transforms Excel numbers to JS numbers, also handling strings, boolean and other types.
|
|
60
|
+
|
|
61
|
+
* It'll perform casting based on the following rules:
|
|
62
|
+
* - If the value is a number, it'll be returned as-is
|
|
63
|
+
* - 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.
|
|
64
|
+
* - If the value is boolean, it'll return 1 or 0
|
|
65
|
+
* - If the value is any other type, it'll return 0
|
|
66
|
+
*/
|
|
67
|
+
export declare const excelNumberDecoder: z.ZodEffects<z.ZodUnion<[z.ZodString, z.ZodNumber, z.ZodBoolean, z.ZodNull, z.ZodUndefined, z.ZodAny]>, number, any>;
|
|
68
|
+
/**
|
|
69
|
+
* A decoder that transforms Excel numbers to JS numbers, also handling strings, boolean and other types.
|
|
70
|
+
|
|
71
|
+
* It'll perform casting based on the following rules:
|
|
72
|
+
* - If the value is a number, it'll be returned as-is
|
|
73
|
+
* - 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.
|
|
74
|
+
* - If the value is boolean, it'll return 1 or 0
|
|
75
|
+
* - If the value is any other type, it'll return null
|
|
76
|
+
*/
|
|
77
|
+
export declare const excelNumberNullableDecoder: z.ZodEffects<z.ZodUnion<[z.ZodString, z.ZodNull, z.ZodNumber, z.ZodUndefined, z.ZodAny]>, number | null, any>;
|
|
78
|
+
/**
|
|
79
|
+
* A decoder that transforms Excel numbers to JS integers, also handling strings and boolean. Other types will throw an error.
|
|
80
|
+
*
|
|
81
|
+
* If the number is not an integer, an error will be thrown.
|
|
82
|
+
*/
|
|
83
|
+
export declare const excelIntStrictDecoder: z.ZodEffects<z.ZodEffects<z.ZodUnion<[z.ZodString, z.ZodNumber, z.ZodBoolean]>, number, string | number | boolean>, number, string | number | boolean>;
|
|
84
|
+
/**
|
|
85
|
+
* A decoder that transforms Excel numbers to JS integers, also handling strings and boolean. Other types will be converted to 0.
|
|
86
|
+
*
|
|
87
|
+
* If the number is not an integer, an error will be thrown.
|
|
88
|
+
*/
|
|
89
|
+
export declare const excelIntDecoder: z.ZodEffects<z.ZodEffects<z.ZodUnion<[z.ZodString, z.ZodNumber, z.ZodBoolean, z.ZodNull, z.ZodUndefined, z.ZodAny]>, number, any>, number, any>;
|
|
90
|
+
/**
|
|
91
|
+
* A decoder that transforms Excel numbers to JS integers, also handling strings and boolean. Other types will be converted to null.
|
|
92
|
+
*
|
|
93
|
+
* If the number is not an integer, an error will be thrown.
|
|
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>;
|
|
96
|
+
/**
|
|
97
|
+
* A decoder that transforms Excel numbers to JS BigInts, also handling strings and boolean. Other types will throw an error.
|
|
98
|
+
*
|
|
99
|
+
* If the number is not a (big) integer, an error will be thrown.
|
|
100
|
+
*/
|
|
101
|
+
export declare const excelBigIntStrictDecoder: z.ZodEffects<z.ZodEffects<z.ZodUnion<[z.ZodString, z.ZodNumber, z.ZodBoolean]>, number, string | number | boolean>, bigint, string | number | boolean>;
|
|
102
|
+
/**
|
|
103
|
+
* A decoder that transforms Excel numbers to JS BigInts, also handling strings and boolean. Other types will be converted to 0.
|
|
104
|
+
*
|
|
105
|
+
* If the number is not a (big) integer, an error will be thrown.
|
|
106
|
+
*/
|
|
107
|
+
export declare const excelBigIntDecoder: z.ZodEffects<z.ZodEffects<z.ZodUnion<[z.ZodString, z.ZodNumber, z.ZodBoolean, z.ZodNull, z.ZodUndefined, z.ZodAny]>, number, any>, bigint, any>;
|
|
108
|
+
/**
|
|
109
|
+
* A decoder that transforms Excel numbers to JS BigInts, also handling strings and boolean. Other types will be converted to null.
|
|
110
|
+
*
|
|
111
|
+
* If the number is not a (big) integer, an error will be thrown.
|
|
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>;
|
|
114
|
+
/**
|
|
115
|
+
* A decoder that transforms Excel booleans to JS booleans, also handling numbers and strings.
|
|
116
|
+
*
|
|
117
|
+
* It'll perform casting based on the following rules:
|
|
118
|
+
* - If the value is a number, it'll return true if it's not 0
|
|
119
|
+
* - 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
|
|
120
|
+
* - If the value is boolean, it'll return it as-is
|
|
121
|
+
* - If the value is any other type, it'll throw an error
|
|
122
|
+
*/
|
|
123
|
+
export declare const excelBooleanStrictDecoder: z.ZodEffects<z.ZodUnion<[z.ZodBoolean, z.ZodNumber, z.ZodString]>, boolean, string | number | boolean>;
|
|
124
|
+
/**
|
|
125
|
+
* A decoder that transforms Excel booleans to JS booleans, also handling numbers and strings.
|
|
126
|
+
*
|
|
127
|
+
* It'll perform casting based on the following rules:
|
|
128
|
+
* - If the value is a number, it'll return true if it's not 0
|
|
129
|
+
* - If the value is a string, it'll return true if it's 'true' or '1', false otherwise
|
|
130
|
+
* - If the value is boolean, it'll return it as-is
|
|
131
|
+
* - If the value is any other type, it'll return false
|
|
132
|
+
*/
|
|
133
|
+
export declare const excelBooleanDecoder: z.ZodEffects<z.ZodUnion<[z.ZodBoolean, z.ZodNumber, z.ZodNull, z.ZodString, z.ZodUndefined, z.ZodAny]>, boolean, any>;
|
|
134
|
+
/**
|
|
135
|
+
* A decoder that transforms Excel booleans to JS booleans, also handling numbers and strings.
|
|
136
|
+
*
|
|
137
|
+
* It'll perform casting based on the following rules:
|
|
138
|
+
* - If the value is a number, it'll return true if it's not 0
|
|
139
|
+
* - 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
|
|
140
|
+
* - If the value is boolean, it'll return it as-is
|
|
141
|
+
* - If the value is any other type, it'll return null
|
|
142
|
+
*/
|
|
143
|
+
export declare const excelBooleanNullableDecoder: z.ZodEffects<z.ZodUnion<[z.ZodBoolean, z.ZodNumber, z.ZodNull, z.ZodString, z.ZodUndefined, z.ZodAny]>, boolean | null, any>;
|
|
144
|
+
/**
|
|
145
|
+
* A decoder that transforms Excel dates to JS Dates
|
|
146
|
+
*/
|
|
147
|
+
export declare const excelDateDecoder: z.ZodEffects<z.ZodUnion<[z.ZodNumber, z.ZodString]>, Date, string | number>;
|
|
148
|
+
/**
|
|
149
|
+
* A decoder that transforms nullable Excel dates to JS Dates
|
|
150
|
+
*/
|
|
151
|
+
export declare const excelDateNullableDecoder: z.ZodEffects<z.ZodUnion<[z.ZodString, z.ZodNumber, z.ZodNull, z.ZodUndefined, z.ZodAny]>, Date | null, any>;
|