@ls-stack/utils 3.36.0 → 3.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.
@@ -0,0 +1,57 @@
1
+ // src/typedStrings.ts
2
+ function stringContains(str, substring) {
3
+ return str.includes(substring);
4
+ }
5
+ function stringStartsWith(str, substring) {
6
+ return str.startsWith(substring);
7
+ }
8
+ function stringEndsWith(str, substring) {
9
+ return str.endsWith(substring);
10
+ }
11
+ function splitTypedString(str, separator) {
12
+ return str.split(separator);
13
+ }
14
+ function splitTypedStringAt(str, separator, splitAtNSeparatorPos = 1) {
15
+ const parts = str.split(separator);
16
+ let leftPart = parts[0];
17
+ let rightPart = parts.slice(1).join(separator);
18
+ if (leftPart === void 0) {
19
+ throw new Error("String does not contain the separator");
20
+ }
21
+ if (splitAtNSeparatorPos > 1) {
22
+ leftPart = parts.slice(0, splitAtNSeparatorPos).join(separator);
23
+ rightPart = parts.slice(splitAtNSeparatorPos).join(separator);
24
+ }
25
+ return [leftPart, rightPart];
26
+ }
27
+ function isNonEmptyString(str) {
28
+ return str.length > 0;
29
+ }
30
+ function asNonEmptyStringOrThrow(str) {
31
+ if (isNonEmptyString(str)) {
32
+ return str;
33
+ }
34
+ throw new Error("String is empty");
35
+ }
36
+ function asNonEmptyStringOrNull(str) {
37
+ if (isNonEmptyString(str)) {
38
+ return str;
39
+ }
40
+ return null;
41
+ }
42
+ function assertStringIsNonEmpty(str) {
43
+ if (!isNonEmptyString(str)) {
44
+ throw new Error("String is empty");
45
+ }
46
+ }
47
+ export {
48
+ asNonEmptyStringOrNull,
49
+ asNonEmptyStringOrThrow,
50
+ assertStringIsNonEmpty,
51
+ isNonEmptyString,
52
+ splitTypedString,
53
+ splitTypedStringAt,
54
+ stringContains,
55
+ stringEndsWith,
56
+ stringStartsWith
57
+ };
@@ -21,6 +21,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
21
21
  var typingFnUtils_exports = {};
22
22
  __export(typingFnUtils_exports, {
23
23
  asNonPartial: () => asNonPartial,
24
+ asPartialUndefinedValues: () => asPartialUndefinedValues,
24
25
  asType: () => asType,
25
26
  isObjKey: () => isObjKey,
26
27
  isSubTypeOf: () => isSubTypeOf,
@@ -62,9 +63,13 @@ function isObjKey(key, obj) {
62
63
  }
63
64
  function unionsAreTheSame(_diff) {
64
65
  }
66
+ function asPartialUndefinedValues(value) {
67
+ return value;
68
+ }
65
69
  // Annotate the CommonJS export names for ESM import in node:
66
70
  0 && (module.exports = {
67
71
  asNonPartial,
72
+ asPartialUndefinedValues,
68
73
  asType,
69
74
  isObjKey,
70
75
  isSubTypeOf,
@@ -1,3 +1,4 @@
1
+ import { PartialPossiblyUndefinedValues } from './typeUtils.cjs';
1
2
  import { NonPartial } from './typingUtils.cjs';
2
3
 
3
4
  declare function asNonPartial<T extends Record<string, unknown>>(obj: T): NonPartial<T>;
@@ -60,5 +61,6 @@ type UnionDiff<T, U> = [
60
61
  * @param _diff - null if unions are identical, or an object describing the errors
61
62
  */
62
63
  declare function unionsAreTheSame<T, U>(_diff: UnionDiff<T, U>): void;
64
+ declare function asPartialUndefinedValues<T extends Record<string, unknown>>(value: PartialPossiblyUndefinedValues<T>): T;
63
65
 
64
- export { asNonPartial, asType, isObjKey, isSubTypeOf, narrowStringToUnion, typeOnRightExtendsLeftType, typedObjectEntries, typedObjectKeys, unionsAreTheSame };
66
+ export { asNonPartial, asPartialUndefinedValues, asType, isObjKey, isSubTypeOf, narrowStringToUnion, typeOnRightExtendsLeftType, typedObjectEntries, typedObjectKeys, unionsAreTheSame };
@@ -1,3 +1,4 @@
1
+ import { PartialPossiblyUndefinedValues } from './typeUtils.js';
1
2
  import { NonPartial } from './typingUtils.js';
2
3
 
3
4
  declare function asNonPartial<T extends Record<string, unknown>>(obj: T): NonPartial<T>;
@@ -60,5 +61,6 @@ type UnionDiff<T, U> = [
60
61
  * @param _diff - null if unions are identical, or an object describing the errors
61
62
  */
62
63
  declare function unionsAreTheSame<T, U>(_diff: UnionDiff<T, U>): void;
64
+ declare function asPartialUndefinedValues<T extends Record<string, unknown>>(value: PartialPossiblyUndefinedValues<T>): T;
63
65
 
64
- export { asNonPartial, asType, isObjKey, isSubTypeOf, narrowStringToUnion, typeOnRightExtendsLeftType, typedObjectEntries, typedObjectKeys, unionsAreTheSame };
66
+ export { asNonPartial, asPartialUndefinedValues, asType, isObjKey, isSubTypeOf, narrowStringToUnion, typeOnRightExtendsLeftType, typedObjectEntries, typedObjectKeys, unionsAreTheSame };
@@ -30,8 +30,12 @@ function isObjKey(key, obj) {
30
30
  }
31
31
  function unionsAreTheSame(_diff) {
32
32
  }
33
+ function asPartialUndefinedValues(value) {
34
+ return value;
35
+ }
33
36
  export {
34
37
  asNonPartial,
38
+ asPartialUndefinedValues,
35
39
  asType,
36
40
  isObjKey,
37
41
  isSubTypeOf,
@@ -48,6 +48,7 @@
48
48
  - [time](time.md)
49
49
  - [timers](timers.md)
50
50
  - [tsResult](tsResult/README.md)
51
+ - [typedStrings](typedStrings.md)
51
52
  - [typeGuards](typeGuards.md)
52
53
  - [typeUtils](typeUtils/README.md)
53
54
  - [typeUtils.typesTest](typeUtils.typesTest.md)
package/docs/modules.md CHANGED
@@ -48,6 +48,7 @@
48
48
  - [time](time.md)
49
49
  - [timers](timers.md)
50
50
  - [tsResult](tsResult/README.md)
51
+ - [typedStrings](typedStrings.md)
51
52
  - [typeGuards](typeGuards.md)
52
53
  - [typeUtils](typeUtils/README.md)
53
54
  - [typeUtils.typesTest](typeUtils.typesTest.md)
package/docs/testUtils.md CHANGED
@@ -14,7 +14,7 @@
14
14
  function compactSnapshot(value, options): string;
15
15
  ```
16
16
 
17
- Defined in: [packages/utils/src/testUtils.ts:363](https://github.com/lucasols/utils/blob/main/packages/utils/src/testUtils.ts#L363)
17
+ Defined in: [packages/utils/src/testUtils.ts:364](https://github.com/lucasols/utils/blob/main/packages/utils/src/testUtils.ts#L364)
18
18
 
19
19
  Produces a more compact and readable snapshot of a value using yaml.
20
20
  By default booleans are shown as `✅` and `❌`, use `showBooleansAs` to disable/configure this.
@@ -142,6 +142,24 @@ Defined in: [packages/utils/src/typeUtils.ts:7](https://github.com/lucasols/util
142
142
 
143
143
  ***
144
144
 
145
+ ### PartialPossiblyUndefinedValues\<T\>
146
+
147
+ ```ts
148
+ type PartialPossiblyUndefinedValues<T> = Prettify<Partial<Pick<T, KeysWithUndefinedValues<T>>> & Omit<T, KeysWithUndefinedValues<T>>>;
149
+ ```
150
+
151
+ Defined in: [packages/utils/src/typeUtils.ts:156](https://github.com/lucasols/utils/blob/main/packages/utils/src/typeUtils.ts#L156)
152
+
153
+ Marks all possible undefined values as partial at the root level of the object.
154
+
155
+ #### Type Parameters
156
+
157
+ ##### T
158
+
159
+ `T` *extends* `Record`\<`string`, `unknown`\>
160
+
161
+ ***
162
+
145
163
  ### PartialRecord\<K, T\>
146
164
 
147
165
  ```ts
@@ -0,0 +1,458 @@
1
+ [**@ls-stack/utils**](README.md)
2
+
3
+ ***
4
+
5
+ [@ls-stack/utils](modules.md) / typedStrings
6
+
7
+ # typedStrings
8
+
9
+ ## Type Aliases
10
+
11
+ ### NonEmptyString
12
+
13
+ ```ts
14
+ type NonEmptyString = string & object;
15
+ ```
16
+
17
+ Defined in: [packages/utils/src/typedStrings.ts:171](https://github.com/lucasols/utils/blob/main/packages/utils/src/typedStrings.ts#L171)
18
+
19
+ A branded type representing a string that is guaranteed to be non-empty (length > 0).
20
+ This type provides compile-time safety by preventing empty strings from being
21
+ assigned without proper validation.
22
+
23
+ #### Type declaration
24
+
25
+ ##### \_\_nonEmptyString
26
+
27
+ ```ts
28
+ __nonEmptyString: true;
29
+ ```
30
+
31
+ #### Example
32
+
33
+ ```ts
34
+ function processName(name: NonEmptyString) {
35
+ // name is guaranteed to be non-empty
36
+ return name.toUpperCase();
37
+ }
38
+ ```
39
+
40
+ ***
41
+
42
+ ### StringContaining\<T\>
43
+
44
+ ```ts
45
+ type StringContaining<T> = string extends T ? never : `${string}${T}${string}`;
46
+ ```
47
+
48
+ Defined in: [packages/utils/src/typedStrings.ts:12](https://github.com/lucasols/utils/blob/main/packages/utils/src/typedStrings.ts#L12)
49
+
50
+ A type representing a string that contains a specific substring.
51
+ Uses template literal types to ensure type safety at compile time.
52
+
53
+ #### Type Parameters
54
+
55
+ ##### T
56
+
57
+ `T` *extends* `string`
58
+
59
+ The substring that must be contained within the string
60
+
61
+ #### Example
62
+
63
+ ```ts
64
+ type EmailString = StringContaining<'@'>; // string that contains '@'
65
+ const email: EmailString = 'user@example.com'; // ✓ valid
66
+ ```
67
+
68
+ ***
69
+
70
+ ### StringEndingWith\<T\>
71
+
72
+ ```ts
73
+ type StringEndingWith<T> = string extends T ? never : `${string}${T}`;
74
+ ```
75
+
76
+ Defined in: [packages/utils/src/typedStrings.ts:40](https://github.com/lucasols/utils/blob/main/packages/utils/src/typedStrings.ts#L40)
77
+
78
+ A type representing a string that ends with a specific substring.
79
+ Uses template literal types to ensure the string ends with the specified suffix.
80
+
81
+ #### Type Parameters
82
+
83
+ ##### T
84
+
85
+ `T` *extends* `string`
86
+
87
+ The substring that the string must end with
88
+
89
+ #### Example
90
+
91
+ ```ts
92
+ type JavaFile = StringEndingWith<'.java'>; // string ending with '.java'
93
+ const filename: JavaFile = 'HelloWorld.java'; // ✓ valid
94
+ ```
95
+
96
+ ***
97
+
98
+ ### StringStartingWith\<T\>
99
+
100
+ ```ts
101
+ type StringStartingWith<T> = string extends T ? never : `${T}${string}`;
102
+ ```
103
+
104
+ Defined in: [packages/utils/src/typedStrings.ts:26](https://github.com/lucasols/utils/blob/main/packages/utils/src/typedStrings.ts#L26)
105
+
106
+ A type representing a string that starts with a specific substring.
107
+ Uses template literal types to ensure the string begins with the specified prefix.
108
+
109
+ #### Type Parameters
110
+
111
+ ##### T
112
+
113
+ `T` *extends* `string`
114
+
115
+ The substring that the string must start with
116
+
117
+ #### Example
118
+
119
+ ```ts
120
+ type HttpUrl = StringStartingWith<'http'>; // string starting with 'http'
121
+ const url: HttpUrl = 'https://example.com'; // ✓ valid
122
+ ```
123
+
124
+ ## Functions
125
+
126
+ ### asNonEmptyStringOrNull()
127
+
128
+ ```ts
129
+ function asNonEmptyStringOrNull(str): null | NonEmptyString;
130
+ ```
131
+
132
+ Defined in: [packages/utils/src/typedStrings.ts:208](https://github.com/lucasols/utils/blob/main/packages/utils/src/typedStrings.ts#L208)
133
+
134
+ Converts a string to `NonEmptyString` or returns `null` if the string is empty.
135
+ Use this when empty strings should be handled gracefully rather than throwing errors.
136
+
137
+ #### Parameters
138
+
139
+ ##### str
140
+
141
+ `string`
142
+
143
+ The string to convert
144
+
145
+ #### Returns
146
+
147
+ `null` \| [`NonEmptyString`](#nonemptystring)
148
+
149
+ The string as `NonEmptyString` or `null` if empty
150
+
151
+ ***
152
+
153
+ ### asNonEmptyStringOrThrow()
154
+
155
+ ```ts
156
+ function asNonEmptyStringOrThrow(str): NonEmptyString;
157
+ ```
158
+
159
+ Defined in: [packages/utils/src/typedStrings.ts:194](https://github.com/lucasols/utils/blob/main/packages/utils/src/typedStrings.ts#L194)
160
+
161
+ Converts a string to `NonEmptyString` or throws an error if the string is empty.
162
+ Use this when you need to ensure a string is non-empty and want to fail fast.
163
+
164
+ #### Parameters
165
+
166
+ ##### str
167
+
168
+ `string`
169
+
170
+ The string to convert
171
+
172
+ #### Returns
173
+
174
+ [`NonEmptyString`](#nonemptystring)
175
+
176
+ The string as `NonEmptyString`
177
+
178
+ #### Throws
179
+
180
+ Error if the string is empty
181
+
182
+ ***
183
+
184
+ ### assertStringIsNonEmpty()
185
+
186
+ ```ts
187
+ function assertStringIsNonEmpty(str): asserts str is NonEmptyString;
188
+ ```
189
+
190
+ Defined in: [packages/utils/src/typedStrings.ts:222](https://github.com/lucasols/utils/blob/main/packages/utils/src/typedStrings.ts#L222)
191
+
192
+ Assertion function that ensures a string is non-empty.
193
+ Throws an error if the string is empty, otherwise narrows the type to `NonEmptyString`.
194
+
195
+ #### Parameters
196
+
197
+ ##### str
198
+
199
+ `string`
200
+
201
+ The string to assert as non-empty
202
+
203
+ #### Returns
204
+
205
+ `asserts str is NonEmptyString`
206
+
207
+ #### Throws
208
+
209
+ Error if the string is empty
210
+
211
+ ***
212
+
213
+ ### isNonEmptyString()
214
+
215
+ ```ts
216
+ function isNonEmptyString(str): str is NonEmptyString;
217
+ ```
218
+
219
+ Defined in: [packages/utils/src/typedStrings.ts:182](https://github.com/lucasols/utils/blob/main/packages/utils/src/typedStrings.ts#L182)
220
+
221
+ Type guard function that checks if a string is non-empty.
222
+ Narrows the type to `NonEmptyString` when the check passes.
223
+
224
+ #### Parameters
225
+
226
+ ##### str
227
+
228
+ `string`
229
+
230
+ The string to check
231
+
232
+ #### Returns
233
+
234
+ `str is NonEmptyString`
235
+
236
+ `true` if the string has length > 0, `false` otherwise
237
+
238
+ ***
239
+
240
+ ### splitTypedString()
241
+
242
+ ```ts
243
+ function splitTypedString<T>(str, separator): [string, string, ...string[]];
244
+ ```
245
+
246
+ Defined in: [packages/utils/src/typedStrings.ts:103](https://github.com/lucasols/utils/blob/main/packages/utils/src/typedStrings.ts#L103)
247
+
248
+ Splits a typed string by a separator that is guaranteed to exist in the string.
249
+ Returns an array with at least two elements: the parts before and after the first separator,
250
+ plus any additional parts if there are multiple separators.
251
+
252
+ #### Type Parameters
253
+
254
+ ##### T
255
+
256
+ `T` *extends* `string`
257
+
258
+ #### Parameters
259
+
260
+ ##### str
261
+
262
+ A string that contains, starts with, or ends with the separator
263
+
264
+ [`StringContaining`](#stringcontaining)\<`NoInfer`\<`T`\>\> | [`StringStartingWith`](#stringstartingwith)\<`NoInfer`\<`T`\>\> | [`StringEndingWith`](#stringendingwith)\<`NoInfer`\<`T`\>\>
265
+
266
+ ##### separator
267
+
268
+ `T`
269
+
270
+ The separator to split by
271
+
272
+ #### Returns
273
+
274
+ \[`string`, `string`, `...string[]`\]
275
+
276
+ An array with at least two string elements
277
+
278
+ #### Example
279
+
280
+ ```ts
281
+ const path: StringContaining<'/'> = 'src/utils/types.ts';
282
+ const [first, second, ...rest] = splitTypedString(path, '/');
283
+ // first: 'src', second: 'utils', rest: ['types.ts']
284
+ ```
285
+
286
+ ***
287
+
288
+ ### splitTypedStringAt()
289
+
290
+ ```ts
291
+ function splitTypedStringAt<T>(
292
+ str,
293
+ separator,
294
+ splitAtNSeparatorPos): [string, string];
295
+ ```
296
+
297
+ Defined in: [packages/utils/src/typedStrings.ts:129](https://github.com/lucasols/utils/blob/main/packages/utils/src/typedStrings.ts#L129)
298
+
299
+ Splits a typed string at a specific occurrence of the separator.
300
+ Unlike `splitTypedString`, this returns exactly two parts: everything before
301
+ the nth separator and everything after it.
302
+
303
+ #### Type Parameters
304
+
305
+ ##### T
306
+
307
+ `T` *extends* `string`
308
+
309
+ #### Parameters
310
+
311
+ ##### str
312
+
313
+ A string that contains, starts with, or ends with the separator
314
+
315
+ [`StringContaining`](#stringcontaining)\<`NoInfer`\<`T`\>\> | [`StringStartingWith`](#stringstartingwith)\<`NoInfer`\<`T`\>\> | [`StringEndingWith`](#stringendingwith)\<`NoInfer`\<`T`\>\>
316
+
317
+ ##### separator
318
+
319
+ `T`
320
+
321
+ The separator to split by
322
+
323
+ ##### splitAtNSeparatorPos
324
+
325
+ `number` = `1`
326
+
327
+ The position of the separator to split at (1-based)
328
+
329
+ #### Returns
330
+
331
+ \[`string`, `string`\]
332
+
333
+ A tuple with exactly two string elements
334
+
335
+ #### Example
336
+
337
+ ```ts
338
+ const path: StringContaining<'.'> = 'file.name.ext';
339
+ const [name, ext] = splitTypedStringAt(path, '.', 2);
340
+ // name: 'file.name', ext: 'ext'
341
+ ```
342
+
343
+ ***
344
+
345
+ ### stringContains()
346
+
347
+ ```ts
348
+ function stringContains<T>(str, substring): str is StringContaining<T>;
349
+ ```
350
+
351
+ Defined in: [packages/utils/src/typedStrings.ts:51](https://github.com/lucasols/utils/blob/main/packages/utils/src/typedStrings.ts#L51)
352
+
353
+ Type guard function that checks if a string contains a specific substring.
354
+ Narrows the type to `StringContaining<T>` when the check passes.
355
+
356
+ #### Type Parameters
357
+
358
+ ##### T
359
+
360
+ `T` *extends* `string`
361
+
362
+ #### Parameters
363
+
364
+ ##### str
365
+
366
+ `string`
367
+
368
+ The string to check
369
+
370
+ ##### substring
371
+
372
+ `T`
373
+
374
+ The substring to search for
375
+
376
+ #### Returns
377
+
378
+ `str is StringContaining<T>`
379
+
380
+ `true` if the string contains the substring, `false` otherwise
381
+
382
+ ***
383
+
384
+ ### stringEndsWith()
385
+
386
+ ```ts
387
+ function stringEndsWith<T>(str, substring): str is StringEndingWith<T>;
388
+ ```
389
+
390
+ Defined in: [packages/utils/src/typedStrings.ts:81](https://github.com/lucasols/utils/blob/main/packages/utils/src/typedStrings.ts#L81)
391
+
392
+ Type guard function that checks if a string ends with a specific substring.
393
+ Narrows the type to `StringEndingWith<T>` when the check passes.
394
+
395
+ #### Type Parameters
396
+
397
+ ##### T
398
+
399
+ `T` *extends* `string`
400
+
401
+ #### Parameters
402
+
403
+ ##### str
404
+
405
+ `string`
406
+
407
+ The string to check
408
+
409
+ ##### substring
410
+
411
+ `T`
412
+
413
+ The substring to check for at the end
414
+
415
+ #### Returns
416
+
417
+ `str is StringEndingWith<T>`
418
+
419
+ `true` if the string ends with the substring, `false` otherwise
420
+
421
+ ***
422
+
423
+ ### stringStartsWith()
424
+
425
+ ```ts
426
+ function stringStartsWith<T>(str, substring): str is StringStartingWith<T>;
427
+ ```
428
+
429
+ Defined in: [packages/utils/src/typedStrings.ts:66](https://github.com/lucasols/utils/blob/main/packages/utils/src/typedStrings.ts#L66)
430
+
431
+ Type guard function that checks if a string starts with a specific substring.
432
+ Narrows the type to `StringStartingWith<T>` when the check passes.
433
+
434
+ #### Type Parameters
435
+
436
+ ##### T
437
+
438
+ `T` *extends* `string`
439
+
440
+ #### Parameters
441
+
442
+ ##### str
443
+
444
+ `string`
445
+
446
+ The string to check
447
+
448
+ ##### substring
449
+
450
+ `T`
451
+
452
+ The substring to check for at the beginning
453
+
454
+ #### Returns
455
+
456
+ `str is StringStartingWith<T>`
457
+
458
+ `true` if the string starts with the substring, `false` otherwise
@@ -8,13 +8,29 @@
8
8
 
9
9
  ## Type Aliases
10
10
 
11
+ ### KeysWithUndefinedValues\<T\>
12
+
13
+ ```ts
14
+ type KeysWithUndefinedValues<T> = { [K in keyof T]: undefined extends T[K] ? K : never }[keyof T];
15
+ ```
16
+
17
+ Defined in: [packages/utils/src/typeUtils.ts:149](https://github.com/lucasols/utils/blob/main/packages/utils/src/typeUtils.ts#L149)
18
+
19
+ #### Type Parameters
20
+
21
+ ##### T
22
+
23
+ `T` *extends* `Record`\<`string`, `unknown`\>
24
+
25
+ ***
26
+
11
27
  ### UnionDiff\<T, U\>
12
28
 
13
29
  ```ts
14
30
  type UnionDiff<T, U> = [T] extends [U] ? [U] extends [T] ? null : object : [U] extends [T] ? object : object;
15
31
  ```
16
32
 
17
- Defined in: [packages/utils/src/typingFnUtils.ts:92](https://github.com/lucasols/utils/blob/main/packages/utils/src/typingFnUtils.ts#L92)
33
+ Defined in: [packages/utils/src/typingFnUtils.ts:93](https://github.com/lucasols/utils/blob/main/packages/utils/src/typingFnUtils.ts#L93)
18
34
 
19
35
  #### Type Parameters
20
36