@ls-stack/utils 3.40.0 → 3.42.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.
- package/dist/arrayUtils.d.cts +70 -57
- package/dist/arrayUtils.d.ts +70 -57
- package/dist/assertions.d.cts +128 -117
- package/dist/assertions.d.ts +128 -117
- package/dist/asyncQueue.cjs +117 -112
- package/dist/asyncQueue.d.cts +217 -215
- package/dist/asyncQueue.d.ts +217 -215
- package/dist/asyncQueue.js +117 -112
- package/dist/awaitDebounce.d.cts +26 -23
- package/dist/awaitDebounce.d.ts +26 -23
- package/dist/cache.cjs +2 -1
- package/dist/cache.d.cts +15 -9
- package/dist/cache.d.ts +15 -9
- package/dist/cache.js +2 -1
- package/dist/{chunk-ADM37GSC.js → chunk-7L4KCZJJ.js} +12 -10
- package/dist/{chunk-KW55OTUG.js → chunk-B3KFV2MH.js} +2 -2
- package/dist/chunk-GMJTLFM6.js +60 -0
- package/dist/{chunk-GHAQOUA6.js → chunk-Y45CE75W.js} +22 -1
- package/dist/concurrentCalls.d.cts +8 -4
- package/dist/concurrentCalls.d.ts +8 -4
- package/dist/concurrentCalls.js +1 -1
- package/dist/debounce.d.cts +12 -20
- package/dist/debounce.d.ts +12 -20
- package/dist/dedent.d.cts +31 -31
- package/dist/dedent.d.ts +31 -31
- package/dist/deepEqual.d.cts +10 -9
- package/dist/deepEqual.d.ts +10 -9
- package/dist/filterObjectOrArrayKeys.d.cts +44 -19
- package/dist/filterObjectOrArrayKeys.d.ts +44 -19
- package/dist/getAutoIncrementId.d.cts +25 -23
- package/dist/getAutoIncrementId.d.ts +25 -23
- package/dist/getCompositeKey.d.cts +2 -1
- package/dist/getCompositeKey.d.ts +2 -1
- package/dist/getValueStableKey.d.cts +4 -3
- package/dist/getValueStableKey.d.ts +4 -3
- package/dist/mutationUtils.d.cts +2 -2
- package/dist/mutationUtils.d.ts +2 -2
- package/dist/objUtils.cjs +51 -2
- package/dist/objUtils.d.cts +8 -3
- package/dist/objUtils.d.ts +8 -3
- package/dist/objUtils.js +11 -3
- package/dist/parallelAsyncCalls.cjs +2 -1
- package/dist/parallelAsyncCalls.d.cts +5 -4
- package/dist/parallelAsyncCalls.d.ts +5 -4
- package/dist/parallelAsyncCalls.js +2 -1
- package/dist/retryOnError.d.cts +23 -20
- package/dist/retryOnError.d.ts +23 -20
- package/dist/runShellCmd.d.cts +40 -40
- package/dist/runShellCmd.d.ts +40 -40
- package/dist/safeJson.d.cts +6 -2
- package/dist/safeJson.d.ts +6 -2
- package/dist/saferTyping.d.cts +21 -14
- package/dist/saferTyping.d.ts +21 -14
- package/dist/shallowEqual.d.cts +1 -1
- package/dist/shallowEqual.d.ts +1 -1
- package/dist/stringUtils.cjs +2 -2
- package/dist/stringUtils.d.cts +6 -7
- package/dist/stringUtils.d.ts +6 -7
- package/dist/stringUtils.js +1 -1
- package/dist/testUtils.cjs +11 -9
- package/dist/testUtils.d.cts +32 -14
- package/dist/testUtils.d.ts +32 -14
- package/dist/testUtils.js +4 -3
- package/dist/throttle.d.cts +57 -48
- package/dist/throttle.d.ts +57 -48
- package/dist/timers.d.cts +68 -63
- package/dist/timers.d.ts +68 -63
- package/dist/tsResult.d.cts +7 -6
- package/dist/tsResult.d.ts +7 -6
- package/dist/typeGuards.d.cts +65 -64
- package/dist/typeGuards.d.ts +65 -64
- package/dist/typeUtils.d.cts +18 -4
- package/dist/typeUtils.d.ts +18 -4
- package/dist/typedStrings.d.cts +68 -57
- package/dist/typedStrings.d.ts +68 -57
- package/dist/typingFnUtils.cjs +15 -0
- package/dist/typingFnUtils.d.cts +34 -7
- package/dist/typingFnUtils.d.ts +34 -7
- package/dist/typingFnUtils.js +18 -35
- package/dist/typingTestUtils.cjs +7 -1
- package/dist/typingTestUtils.d.cts +52 -11
- package/dist/typingTestUtils.d.ts +52 -11
- package/dist/typingTestUtils.js +7 -1
- package/dist/typingUtils.d.cts +4 -1
- package/dist/typingUtils.d.ts +4 -1
- package/dist/yamlStringify.cjs +11 -9
- package/dist/yamlStringify.js +2 -2
- package/package.json +5 -1
- package/docs/README.md +0 -72
- package/docs/_media/modules.md +0 -58
- package/docs/arrayUtils/-internal-.md +0 -179
- package/docs/arrayUtils/README.md +0 -550
- package/docs/assertions/-internal-.md +0 -63
- package/docs/assertions/README.md +0 -565
- package/docs/asyncQueue/-internal-.md +0 -297
- package/docs/asyncQueue/README.md +0 -1485
- package/docs/awaitDebounce.md +0 -66
- package/docs/cache/-internal-.md +0 -168
- package/docs/cache/README.md +0 -360
- package/docs/castValues.md +0 -47
- package/docs/concurrentCalls/-internal-.md +0 -490
- package/docs/concurrentCalls/README.md +0 -299
- package/docs/consoleFmt.md +0 -115
- package/docs/conversions.md +0 -27
- package/docs/createThrottleController/-internal-.md +0 -73
- package/docs/createThrottleController/README.md +0 -31
- package/docs/debounce.md +0 -188
- package/docs/dedent/-internal-.md +0 -17
- package/docs/dedent/README.md +0 -204
- package/docs/deepEqual.md +0 -94
- package/docs/enhancedMap.md +0 -358
- package/docs/exhaustiveMatch/-internal-.md +0 -39
- package/docs/exhaustiveMatch/README.md +0 -146
- package/docs/filterObjectOrArrayKeys.md +0 -109
- package/docs/getAutoIncrementId.md +0 -93
- package/docs/getCompositeKey.md +0 -39
- package/docs/getValueStableKey.md +0 -57
- package/docs/hash.md +0 -31
- package/docs/interpolate/-internal-.md +0 -61
- package/docs/interpolate/README.md +0 -62
- package/docs/keepPrevIfUnchanged.md +0 -43
- package/docs/levenshtein.md +0 -93
- package/docs/main.md +0 -21
- package/docs/mathUtils.md +0 -137
- package/docs/modules.md +0 -58
- package/docs/mutationUtils.md +0 -44
- package/docs/objUtils.md +0 -237
- package/docs/parallelAsyncCalls/-internal-.md +0 -347
- package/docs/parallelAsyncCalls/README.md +0 -45
- package/docs/promiseUtils/-internal-.md +0 -69
- package/docs/promiseUtils/README.md +0 -31
- package/docs/retryOnError/-internal-.md +0 -111
- package/docs/retryOnError/README.md +0 -168
- package/docs/runShellCmd/-internal-.md +0 -111
- package/docs/runShellCmd/README.md +0 -201
- package/docs/safeJson.md +0 -51
- package/docs/saferTyping.md +0 -228
- package/docs/serializeXML.md +0 -100
- package/docs/shallowEqual.md +0 -33
- package/docs/sleep.md +0 -27
- package/docs/stringUtils/-internal-.md +0 -17
- package/docs/stringUtils/README.md +0 -270
- package/docs/testUtils.md +0 -382
- package/docs/throttle/-internal-.md +0 -47
- package/docs/throttle/README.md +0 -178
- package/docs/time.md +0 -274
- package/docs/timers.md +0 -256
- package/docs/tsResult/-internal-.md +0 -327
- package/docs/tsResult/README.md +0 -702
- package/docs/typeGuards.md +0 -399
- package/docs/typeUtils/-internal-.md +0 -99
- package/docs/typeUtils/README.md +0 -195
- package/docs/typeUtils.typesTest.md +0 -7
- package/docs/typedStrings.md +0 -458
- package/docs/typingFnUtils/-internal-.md +0 -43
- package/docs/typingFnUtils/README.md +0 -317
- package/docs/typingTestUtils.md +0 -172
- package/docs/typingUtils.md +0 -135
- package/docs/yamlStringify.md +0 -83
package/dist/typeUtils.d.ts
CHANGED
|
@@ -4,6 +4,10 @@ type PartialRecord<K extends keyof any, T> = {
|
|
|
4
4
|
type NonPartial<T> = {
|
|
5
5
|
[K in keyof Required<T>]: T[K];
|
|
6
6
|
};
|
|
7
|
+
/**
|
|
8
|
+
* @deprecated Use `ObjKeysWithValuesOfType` from `@ls-stack/utils/typeUtils`
|
|
9
|
+
* instead
|
|
10
|
+
*/
|
|
7
11
|
type ObjKeysWithValuesOfType<Obj extends Record<PropertyKey, unknown>, ValueType> = {
|
|
8
12
|
[K in keyof Obj]: Obj[K] extends ValueType ? K : never;
|
|
9
13
|
}[keyof Obj];
|
|
@@ -26,12 +30,14 @@ type DeepReplaceValueImpl<T, ReplaceType, NewType, SkipPaths extends string | un
|
|
|
26
30
|
[K in keyof T]: DeepReplaceValueImpl<T[K], ReplaceType, NewType, SkipPaths, SkipType, BuildPath<Path, K>>;
|
|
27
31
|
} : T;
|
|
28
32
|
/**
|
|
29
|
-
* Replaces all values that extends `ReplaceType` with `NewType` in a deeply
|
|
33
|
+
* Replaces all values that extends `ReplaceType` with `NewType` in a deeply
|
|
34
|
+
* nested object or array.
|
|
30
35
|
*
|
|
31
36
|
* @template T - The object or array to replace values in.
|
|
32
37
|
* @template ReplaceType - The type to replace.
|
|
33
38
|
* @template NewType - The new type to replace with.
|
|
34
|
-
* @template SkipPaths - The paths to skip in transverse. e.g. 'a.b.c' |
|
|
39
|
+
* @template SkipPaths - The paths to skip in transverse. e.g. 'a.b.c' |
|
|
40
|
+
* 'array[*].b'
|
|
35
41
|
* @template SkipTypes - The types to skip in transverse and replace.
|
|
36
42
|
*/
|
|
37
43
|
type DeepReplaceValue<T, ReplaceType, NewType, SkipPaths extends string | undefined = undefined, SkipTypes = DefaultSkipTransverseDeepReplace> = DeepReplaceValueImpl<T, ReplaceType, NewType, SkipPaths, SkipTypes>;
|
|
@@ -39,8 +45,16 @@ type KeysWithUndefinedValues<T extends Record<string, unknown>> = {
|
|
|
39
45
|
[K in keyof T]: undefined extends T[K] ? K : never;
|
|
40
46
|
}[keyof T];
|
|
41
47
|
/**
|
|
42
|
-
* Marks all possible undefined values as partial at the root level of the
|
|
48
|
+
* Marks all possible undefined values as partial at the root level of the
|
|
49
|
+
* object.
|
|
43
50
|
*/
|
|
44
51
|
type PartialPossiblyUndefinedValues<T extends Record<string, unknown>> = Prettify<Partial<Pick<T, KeysWithUndefinedValues<T>>> & Omit<T, KeysWithUndefinedValues<T>>>;
|
|
52
|
+
type PickUndefinedKeys<T> = {
|
|
53
|
+
[K in keyof T]: undefined extends T[K] ? K : never;
|
|
54
|
+
}[keyof T];
|
|
55
|
+
type PickRequiredKeys<T> = {
|
|
56
|
+
[K in keyof T]: undefined extends T[K] ? never : K;
|
|
57
|
+
}[keyof T];
|
|
58
|
+
type MakeUndefinedKeysOptional<T> = Prettify<Partial<Pick<T, PickUndefinedKeys<T>>> & Pick<T, PickRequiredKeys<T>>>;
|
|
45
59
|
|
|
46
|
-
export type { DeepPrettify, DeepReplaceValue, DefaultSkipTransverseDeepReplace, IsAny, NonPartial, ObjKeysWithValuesOfType, PartialPossiblyUndefinedValues, PartialRecord, Prettify };
|
|
60
|
+
export type { DeepPrettify, DeepReplaceValue, DefaultSkipTransverseDeepReplace, IsAny, MakeUndefinedKeysOptional, NonPartial, ObjKeysWithValuesOfType, PartialPossiblyUndefinedValues, PartialRecord, Prettify };
|
package/dist/typedStrings.d.cts
CHANGED
|
@@ -1,37 +1,41 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* A type representing a string that contains a specific substring.
|
|
3
|
-
*
|
|
2
|
+
* A type representing a string that contains a specific substring. Uses
|
|
3
|
+
* template literal types to ensure type safety at compile time.
|
|
4
4
|
*
|
|
5
|
-
* @template T - The substring that must be contained within the string
|
|
6
5
|
* @example
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
6
|
+
* ```ts
|
|
7
|
+
* type EmailString = StringContaining<'@'>; // string that contains '@'
|
|
8
|
+
* const email: EmailString = 'user@example.com'; // ✓ valid
|
|
9
|
+
* ```;
|
|
10
|
+
*
|
|
11
|
+
* @template T - The substring that must be contained within the string
|
|
11
12
|
*/
|
|
12
13
|
type StringContaining<T extends string> = string extends T ? never : `${string}${T}${string}`;
|
|
13
14
|
/**
|
|
14
|
-
* A type representing a string that starts with a specific substring.
|
|
15
|
-
*
|
|
15
|
+
* A type representing a string that starts with a specific substring. Uses
|
|
16
|
+
* template literal types to ensure the string begins with the specified
|
|
17
|
+
* prefix.
|
|
16
18
|
*
|
|
17
|
-
* @template T - The substring that the string must start with
|
|
18
19
|
* @example
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
20
|
+
* ```ts
|
|
21
|
+
* type HttpUrl = StringStartingWith<'http'>; // string starting with 'http'
|
|
22
|
+
* const url: HttpUrl = 'https://example.com'; // ✓ valid
|
|
23
|
+
* ```;
|
|
24
|
+
*
|
|
25
|
+
* @template T - The substring that the string must start with
|
|
23
26
|
*/
|
|
24
27
|
type StringStartingWith<T extends string> = string extends T ? never : `${T}${string}`;
|
|
25
28
|
/**
|
|
26
|
-
* A type representing a string that ends with a specific substring.
|
|
27
|
-
*
|
|
29
|
+
* A type representing a string that ends with a specific substring. Uses
|
|
30
|
+
* template literal types to ensure the string ends with the specified suffix.
|
|
28
31
|
*
|
|
29
|
-
* @template T - The substring that the string must end with
|
|
30
32
|
* @example
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
33
|
+
* ```ts
|
|
34
|
+
* type JavaFile = StringEndingWith<'.java'>; // string ending with '.java'
|
|
35
|
+
* const filename: JavaFile = 'HelloWorld.java'; // ✓ valid
|
|
36
|
+
* ```;
|
|
37
|
+
*
|
|
38
|
+
* @template T - The substring that the string must end with
|
|
35
39
|
*/
|
|
36
40
|
type StringEndingWith<T extends string> = string extends T ? never : `${string}${T}`;
|
|
37
41
|
/**
|
|
@@ -62,70 +66,76 @@ declare function stringStartsWith<T extends string>(str: string, substring: T):
|
|
|
62
66
|
*/
|
|
63
67
|
declare function stringEndsWith<T extends string>(str: string, substring: T): str is StringEndingWith<T>;
|
|
64
68
|
/**
|
|
65
|
-
* Splits a typed string by a separator that is guaranteed to exist in the
|
|
66
|
-
* Returns an array with at least two elements: the parts before and
|
|
67
|
-
* plus any additional parts if there are multiple
|
|
69
|
+
* Splits a typed string by a separator that is guaranteed to exist in the
|
|
70
|
+
* string. Returns an array with at least two elements: the parts before and
|
|
71
|
+
* after the first separator, plus any additional parts if there are multiple
|
|
72
|
+
* separators.
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* ```ts
|
|
76
|
+
* const path: StringContaining<'/'> = 'src/utils/types.ts';
|
|
77
|
+
* const [first, second, ...rest] = splitTypedString(path, '/');
|
|
78
|
+
* // first: 'src', second: 'utils', rest: ['types.ts']
|
|
79
|
+
* ```;
|
|
68
80
|
*
|
|
69
81
|
* @param str - A string that contains, starts with, or ends with the separator
|
|
70
82
|
* @param separator - The separator to split by
|
|
71
83
|
* @returns An array with at least two string elements
|
|
72
|
-
* @example
|
|
73
|
-
* ```ts
|
|
74
|
-
* const path: StringContaining<'/'> = 'src/utils/types.ts';
|
|
75
|
-
* const [first, second, ...rest] = splitTypedString(path, '/');
|
|
76
|
-
* // first: 'src', second: 'utils', rest: ['types.ts']
|
|
77
|
-
* ```
|
|
78
84
|
*/
|
|
79
85
|
declare function splitTypedString<T extends string>(str: StringContaining<NoInfer<T>> | StringStartingWith<NoInfer<T>> | StringEndingWith<NoInfer<T>>, separator: T): [string, string, ...string[]];
|
|
80
86
|
/**
|
|
81
|
-
* Splits a typed string at a specific occurrence of the separator.
|
|
82
|
-
*
|
|
83
|
-
*
|
|
87
|
+
* Splits a typed string at a specific occurrence of the separator. Unlike
|
|
88
|
+
* `splitTypedString`, this returns exactly two parts: everything before the nth
|
|
89
|
+
* separator and everything after it.
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* ```ts
|
|
93
|
+
* const path: StringContaining<'.'> = 'file.name.ext';
|
|
94
|
+
* const [name, ext] = splitTypedStringAt(path, '.', 2);
|
|
95
|
+
* // name: 'file.name', ext: 'ext'
|
|
96
|
+
* ```;
|
|
84
97
|
*
|
|
85
98
|
* @param str - A string that contains, starts with, or ends with the separator
|
|
86
99
|
* @param separator - The separator to split by
|
|
87
|
-
* @param splitAtNSeparatorPos - The position of the separator to split at
|
|
100
|
+
* @param splitAtNSeparatorPos - The position of the separator to split at
|
|
101
|
+
* (1-based)
|
|
88
102
|
* @returns A tuple with exactly two string elements
|
|
89
|
-
* @example
|
|
90
|
-
* ```ts
|
|
91
|
-
* const path: StringContaining<'.'> = 'file.name.ext';
|
|
92
|
-
* const [name, ext] = splitTypedStringAt(path, '.', 2);
|
|
93
|
-
* // name: 'file.name', ext: 'ext'
|
|
94
|
-
* ```
|
|
95
103
|
*/
|
|
96
104
|
declare function splitTypedStringAt<T extends string>(str: StringContaining<NoInfer<T>> | StringStartingWith<NoInfer<T>> | StringEndingWith<NoInfer<T>>, separator: T,
|
|
97
105
|
/**
|
|
98
106
|
* The position of the separator to split at.
|
|
107
|
+
*
|
|
99
108
|
* @default 1 - split at the first separator
|
|
100
109
|
*/
|
|
101
110
|
splitAtNSeparatorPos?: number): [string, string];
|
|
102
111
|
/**
|
|
103
|
-
* A branded type representing a string that is guaranteed to be non-empty
|
|
104
|
-
* This type provides compile-time safety by preventing empty
|
|
105
|
-
* assigned without proper validation.
|
|
112
|
+
* A branded type representing a string that is guaranteed to be non-empty
|
|
113
|
+
* (length > 0). This type provides compile-time safety by preventing empty
|
|
114
|
+
* strings from being assigned without proper validation.
|
|
106
115
|
*
|
|
107
116
|
* @example
|
|
108
|
-
*
|
|
109
|
-
*
|
|
110
|
-
*
|
|
111
|
-
*
|
|
112
|
-
*
|
|
113
|
-
*
|
|
117
|
+
* ```ts
|
|
118
|
+
* function processName(name: NonEmptyString) {
|
|
119
|
+
* // name is guaranteed to be non-empty
|
|
120
|
+
* return name.toUpperCase();
|
|
121
|
+
* }
|
|
122
|
+
* ```;
|
|
114
123
|
*/
|
|
115
124
|
type NonEmptyString = string & {
|
|
116
125
|
__nonEmptyString: true;
|
|
117
126
|
};
|
|
118
127
|
/**
|
|
119
|
-
* Type guard function that checks if a string is non-empty.
|
|
120
|
-
*
|
|
128
|
+
* Type guard function that checks if a string is non-empty. Narrows the type to
|
|
129
|
+
* `NonEmptyString` when the check passes.
|
|
121
130
|
*
|
|
122
131
|
* @param str - The string to check
|
|
123
132
|
* @returns `true` if the string has length > 0, `false` otherwise
|
|
124
133
|
*/
|
|
125
134
|
declare function isNonEmptyString(str: string): str is NonEmptyString;
|
|
126
135
|
/**
|
|
127
|
-
* Converts a string to `NonEmptyString` or throws an error if the string is
|
|
128
|
-
* Use this when you need to ensure a string is non-empty and want to
|
|
136
|
+
* Converts a string to `NonEmptyString` or throws an error if the string is
|
|
137
|
+
* empty. Use this when you need to ensure a string is non-empty and want to
|
|
138
|
+
* fail fast.
|
|
129
139
|
*
|
|
130
140
|
* @param str - The string to convert
|
|
131
141
|
* @returns The string as `NonEmptyString`
|
|
@@ -133,16 +143,17 @@ declare function isNonEmptyString(str: string): str is NonEmptyString;
|
|
|
133
143
|
*/
|
|
134
144
|
declare function asNonEmptyStringOrThrow(str: string): NonEmptyString;
|
|
135
145
|
/**
|
|
136
|
-
* Converts a string to `NonEmptyString` or returns `null` if the string is
|
|
137
|
-
* Use this when empty strings should be handled gracefully rather than
|
|
146
|
+
* Converts a string to `NonEmptyString` or returns `null` if the string is
|
|
147
|
+
* empty. Use this when empty strings should be handled gracefully rather than
|
|
148
|
+
* throwing errors.
|
|
138
149
|
*
|
|
139
150
|
* @param str - The string to convert
|
|
140
151
|
* @returns The string as `NonEmptyString` or `null` if empty
|
|
141
152
|
*/
|
|
142
153
|
declare function asNonEmptyStringOrNull(str: string): NonEmptyString | null;
|
|
143
154
|
/**
|
|
144
|
-
* Assertion function that ensures a string is non-empty.
|
|
145
|
-
*
|
|
155
|
+
* Assertion function that ensures a string is non-empty. Throws an error if the
|
|
156
|
+
* string is empty, otherwise narrows the type to `NonEmptyString`.
|
|
146
157
|
*
|
|
147
158
|
* @param str - The string to assert as non-empty
|
|
148
159
|
* @throws Error if the string is empty
|
package/dist/typedStrings.d.ts
CHANGED
|
@@ -1,37 +1,41 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* A type representing a string that contains a specific substring.
|
|
3
|
-
*
|
|
2
|
+
* A type representing a string that contains a specific substring. Uses
|
|
3
|
+
* template literal types to ensure type safety at compile time.
|
|
4
4
|
*
|
|
5
|
-
* @template T - The substring that must be contained within the string
|
|
6
5
|
* @example
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
6
|
+
* ```ts
|
|
7
|
+
* type EmailString = StringContaining<'@'>; // string that contains '@'
|
|
8
|
+
* const email: EmailString = 'user@example.com'; // ✓ valid
|
|
9
|
+
* ```;
|
|
10
|
+
*
|
|
11
|
+
* @template T - The substring that must be contained within the string
|
|
11
12
|
*/
|
|
12
13
|
type StringContaining<T extends string> = string extends T ? never : `${string}${T}${string}`;
|
|
13
14
|
/**
|
|
14
|
-
* A type representing a string that starts with a specific substring.
|
|
15
|
-
*
|
|
15
|
+
* A type representing a string that starts with a specific substring. Uses
|
|
16
|
+
* template literal types to ensure the string begins with the specified
|
|
17
|
+
* prefix.
|
|
16
18
|
*
|
|
17
|
-
* @template T - The substring that the string must start with
|
|
18
19
|
* @example
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
20
|
+
* ```ts
|
|
21
|
+
* type HttpUrl = StringStartingWith<'http'>; // string starting with 'http'
|
|
22
|
+
* const url: HttpUrl = 'https://example.com'; // ✓ valid
|
|
23
|
+
* ```;
|
|
24
|
+
*
|
|
25
|
+
* @template T - The substring that the string must start with
|
|
23
26
|
*/
|
|
24
27
|
type StringStartingWith<T extends string> = string extends T ? never : `${T}${string}`;
|
|
25
28
|
/**
|
|
26
|
-
* A type representing a string that ends with a specific substring.
|
|
27
|
-
*
|
|
29
|
+
* A type representing a string that ends with a specific substring. Uses
|
|
30
|
+
* template literal types to ensure the string ends with the specified suffix.
|
|
28
31
|
*
|
|
29
|
-
* @template T - The substring that the string must end with
|
|
30
32
|
* @example
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
33
|
+
* ```ts
|
|
34
|
+
* type JavaFile = StringEndingWith<'.java'>; // string ending with '.java'
|
|
35
|
+
* const filename: JavaFile = 'HelloWorld.java'; // ✓ valid
|
|
36
|
+
* ```;
|
|
37
|
+
*
|
|
38
|
+
* @template T - The substring that the string must end with
|
|
35
39
|
*/
|
|
36
40
|
type StringEndingWith<T extends string> = string extends T ? never : `${string}${T}`;
|
|
37
41
|
/**
|
|
@@ -62,70 +66,76 @@ declare function stringStartsWith<T extends string>(str: string, substring: T):
|
|
|
62
66
|
*/
|
|
63
67
|
declare function stringEndsWith<T extends string>(str: string, substring: T): str is StringEndingWith<T>;
|
|
64
68
|
/**
|
|
65
|
-
* Splits a typed string by a separator that is guaranteed to exist in the
|
|
66
|
-
* Returns an array with at least two elements: the parts before and
|
|
67
|
-
* plus any additional parts if there are multiple
|
|
69
|
+
* Splits a typed string by a separator that is guaranteed to exist in the
|
|
70
|
+
* string. Returns an array with at least two elements: the parts before and
|
|
71
|
+
* after the first separator, plus any additional parts if there are multiple
|
|
72
|
+
* separators.
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* ```ts
|
|
76
|
+
* const path: StringContaining<'/'> = 'src/utils/types.ts';
|
|
77
|
+
* const [first, second, ...rest] = splitTypedString(path, '/');
|
|
78
|
+
* // first: 'src', second: 'utils', rest: ['types.ts']
|
|
79
|
+
* ```;
|
|
68
80
|
*
|
|
69
81
|
* @param str - A string that contains, starts with, or ends with the separator
|
|
70
82
|
* @param separator - The separator to split by
|
|
71
83
|
* @returns An array with at least two string elements
|
|
72
|
-
* @example
|
|
73
|
-
* ```ts
|
|
74
|
-
* const path: StringContaining<'/'> = 'src/utils/types.ts';
|
|
75
|
-
* const [first, second, ...rest] = splitTypedString(path, '/');
|
|
76
|
-
* // first: 'src', second: 'utils', rest: ['types.ts']
|
|
77
|
-
* ```
|
|
78
84
|
*/
|
|
79
85
|
declare function splitTypedString<T extends string>(str: StringContaining<NoInfer<T>> | StringStartingWith<NoInfer<T>> | StringEndingWith<NoInfer<T>>, separator: T): [string, string, ...string[]];
|
|
80
86
|
/**
|
|
81
|
-
* Splits a typed string at a specific occurrence of the separator.
|
|
82
|
-
*
|
|
83
|
-
*
|
|
87
|
+
* Splits a typed string at a specific occurrence of the separator. Unlike
|
|
88
|
+
* `splitTypedString`, this returns exactly two parts: everything before the nth
|
|
89
|
+
* separator and everything after it.
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* ```ts
|
|
93
|
+
* const path: StringContaining<'.'> = 'file.name.ext';
|
|
94
|
+
* const [name, ext] = splitTypedStringAt(path, '.', 2);
|
|
95
|
+
* // name: 'file.name', ext: 'ext'
|
|
96
|
+
* ```;
|
|
84
97
|
*
|
|
85
98
|
* @param str - A string that contains, starts with, or ends with the separator
|
|
86
99
|
* @param separator - The separator to split by
|
|
87
|
-
* @param splitAtNSeparatorPos - The position of the separator to split at
|
|
100
|
+
* @param splitAtNSeparatorPos - The position of the separator to split at
|
|
101
|
+
* (1-based)
|
|
88
102
|
* @returns A tuple with exactly two string elements
|
|
89
|
-
* @example
|
|
90
|
-
* ```ts
|
|
91
|
-
* const path: StringContaining<'.'> = 'file.name.ext';
|
|
92
|
-
* const [name, ext] = splitTypedStringAt(path, '.', 2);
|
|
93
|
-
* // name: 'file.name', ext: 'ext'
|
|
94
|
-
* ```
|
|
95
103
|
*/
|
|
96
104
|
declare function splitTypedStringAt<T extends string>(str: StringContaining<NoInfer<T>> | StringStartingWith<NoInfer<T>> | StringEndingWith<NoInfer<T>>, separator: T,
|
|
97
105
|
/**
|
|
98
106
|
* The position of the separator to split at.
|
|
107
|
+
*
|
|
99
108
|
* @default 1 - split at the first separator
|
|
100
109
|
*/
|
|
101
110
|
splitAtNSeparatorPos?: number): [string, string];
|
|
102
111
|
/**
|
|
103
|
-
* A branded type representing a string that is guaranteed to be non-empty
|
|
104
|
-
* This type provides compile-time safety by preventing empty
|
|
105
|
-
* assigned without proper validation.
|
|
112
|
+
* A branded type representing a string that is guaranteed to be non-empty
|
|
113
|
+
* (length > 0). This type provides compile-time safety by preventing empty
|
|
114
|
+
* strings from being assigned without proper validation.
|
|
106
115
|
*
|
|
107
116
|
* @example
|
|
108
|
-
*
|
|
109
|
-
*
|
|
110
|
-
*
|
|
111
|
-
*
|
|
112
|
-
*
|
|
113
|
-
*
|
|
117
|
+
* ```ts
|
|
118
|
+
* function processName(name: NonEmptyString) {
|
|
119
|
+
* // name is guaranteed to be non-empty
|
|
120
|
+
* return name.toUpperCase();
|
|
121
|
+
* }
|
|
122
|
+
* ```;
|
|
114
123
|
*/
|
|
115
124
|
type NonEmptyString = string & {
|
|
116
125
|
__nonEmptyString: true;
|
|
117
126
|
};
|
|
118
127
|
/**
|
|
119
|
-
* Type guard function that checks if a string is non-empty.
|
|
120
|
-
*
|
|
128
|
+
* Type guard function that checks if a string is non-empty. Narrows the type to
|
|
129
|
+
* `NonEmptyString` when the check passes.
|
|
121
130
|
*
|
|
122
131
|
* @param str - The string to check
|
|
123
132
|
* @returns `true` if the string has length > 0, `false` otherwise
|
|
124
133
|
*/
|
|
125
134
|
declare function isNonEmptyString(str: string): str is NonEmptyString;
|
|
126
135
|
/**
|
|
127
|
-
* Converts a string to `NonEmptyString` or throws an error if the string is
|
|
128
|
-
* Use this when you need to ensure a string is non-empty and want to
|
|
136
|
+
* Converts a string to `NonEmptyString` or throws an error if the string is
|
|
137
|
+
* empty. Use this when you need to ensure a string is non-empty and want to
|
|
138
|
+
* fail fast.
|
|
129
139
|
*
|
|
130
140
|
* @param str - The string to convert
|
|
131
141
|
* @returns The string as `NonEmptyString`
|
|
@@ -133,16 +143,17 @@ declare function isNonEmptyString(str: string): str is NonEmptyString;
|
|
|
133
143
|
*/
|
|
134
144
|
declare function asNonEmptyStringOrThrow(str: string): NonEmptyString;
|
|
135
145
|
/**
|
|
136
|
-
* Converts a string to `NonEmptyString` or returns `null` if the string is
|
|
137
|
-
* Use this when empty strings should be handled gracefully rather than
|
|
146
|
+
* Converts a string to `NonEmptyString` or returns `null` if the string is
|
|
147
|
+
* empty. Use this when empty strings should be handled gracefully rather than
|
|
148
|
+
* throwing errors.
|
|
138
149
|
*
|
|
139
150
|
* @param str - The string to convert
|
|
140
151
|
* @returns The string as `NonEmptyString` or `null` if empty
|
|
141
152
|
*/
|
|
142
153
|
declare function asNonEmptyStringOrNull(str: string): NonEmptyString | null;
|
|
143
154
|
/**
|
|
144
|
-
* Assertion function that ensures a string is non-empty.
|
|
145
|
-
*
|
|
155
|
+
* Assertion function that ensures a string is non-empty. Throws an error if the
|
|
156
|
+
* string is empty, otherwise narrows the type to `NonEmptyString`.
|
|
146
157
|
*
|
|
147
158
|
* @param str - The string to assert as non-empty
|
|
148
159
|
* @throws Error if the string is empty
|
package/dist/typingFnUtils.cjs
CHANGED
|
@@ -23,9 +23,12 @@ __export(typingFnUtils_exports, {
|
|
|
23
23
|
asNonPartial: () => asNonPartial,
|
|
24
24
|
asPartialUndefinedValues: () => asPartialUndefinedValues,
|
|
25
25
|
asType: () => asType,
|
|
26
|
+
isNonEmptyArray: () => isNonEmptyArray,
|
|
26
27
|
isObjKey: () => isObjKey,
|
|
27
28
|
isSubTypeOf: () => isSubTypeOf,
|
|
28
29
|
narrowStringToUnion: () => narrowStringToUnion,
|
|
30
|
+
objectHasKey: () => objectHasKey,
|
|
31
|
+
strictTypedObjectEntries: () => strictTypedObjectEntries,
|
|
29
32
|
typeOnRightExtendsLeftType: () => typeOnRightExtendsLeftType,
|
|
30
33
|
typedObjectEntries: () => typedObjectEntries,
|
|
31
34
|
typedObjectKeys: () => typedObjectKeys,
|
|
@@ -38,6 +41,9 @@ function asNonPartial(obj) {
|
|
|
38
41
|
function typedObjectEntries(obj) {
|
|
39
42
|
return Object.entries(obj);
|
|
40
43
|
}
|
|
44
|
+
function strictTypedObjectEntries(obj) {
|
|
45
|
+
return Object.entries(obj);
|
|
46
|
+
}
|
|
41
47
|
function typedObjectKeys(obj) {
|
|
42
48
|
return Object.keys(obj);
|
|
43
49
|
}
|
|
@@ -66,14 +72,23 @@ function unionsAreTheSame(_diff) {
|
|
|
66
72
|
function asPartialUndefinedValues(value) {
|
|
67
73
|
return value;
|
|
68
74
|
}
|
|
75
|
+
function isNonEmptyArray(array) {
|
|
76
|
+
return array.length > 0;
|
|
77
|
+
}
|
|
78
|
+
function objectHasKey(obj, key) {
|
|
79
|
+
return key in obj;
|
|
80
|
+
}
|
|
69
81
|
// Annotate the CommonJS export names for ESM import in node:
|
|
70
82
|
0 && (module.exports = {
|
|
71
83
|
asNonPartial,
|
|
72
84
|
asPartialUndefinedValues,
|
|
73
85
|
asType,
|
|
86
|
+
isNonEmptyArray,
|
|
74
87
|
isObjKey,
|
|
75
88
|
isSubTypeOf,
|
|
76
89
|
narrowStringToUnion,
|
|
90
|
+
objectHasKey,
|
|
91
|
+
strictTypedObjectEntries,
|
|
77
92
|
typeOnRightExtendsLeftType,
|
|
78
93
|
typedObjectEntries,
|
|
79
94
|
typedObjectKeys,
|
package/dist/typingFnUtils.d.cts
CHANGED
|
@@ -3,24 +3,37 @@ import { NonPartial } from './typingUtils.cjs';
|
|
|
3
3
|
|
|
4
4
|
declare function asNonPartial<T extends Record<string, unknown>>(obj: T): NonPartial<T>;
|
|
5
5
|
/**
|
|
6
|
-
*
|
|
6
|
+
* A wrapper to Object.entries with a better typing inference
|
|
7
|
+
*
|
|
7
8
|
* @param obj
|
|
8
9
|
*/
|
|
9
10
|
declare function typedObjectEntries<T extends Record<string, unknown>>(obj: T): NonNullable<{
|
|
10
11
|
[K in keyof T]: [K, T[K]];
|
|
11
12
|
}[keyof T]>[];
|
|
12
13
|
/**
|
|
13
|
-
*
|
|
14
|
+
* A wrapper to Object.entries with a better typing inference, but with strict
|
|
15
|
+
* typing narrowing keys to strings.
|
|
16
|
+
*
|
|
17
|
+
* @param obj
|
|
18
|
+
*/
|
|
19
|
+
declare function strictTypedObjectEntries<T extends Record<string, unknown>>(obj: T): NonNullable<{
|
|
20
|
+
[K in keyof T]: [K & string, T[K]];
|
|
21
|
+
}[keyof T]>[];
|
|
22
|
+
/**
|
|
23
|
+
* A wrapper to Object.keys with a better typing inference
|
|
24
|
+
*
|
|
14
25
|
* @param obj
|
|
15
26
|
*/
|
|
16
27
|
declare function typedObjectKeys<T extends Record<string, unknown>>(obj: T): (keyof T)[];
|
|
17
28
|
/**
|
|
18
|
-
*
|
|
29
|
+
* A safe way to cast types, use to substitute the `as Type`
|
|
30
|
+
*
|
|
19
31
|
* @param value
|
|
20
32
|
*/
|
|
21
33
|
declare function asType<T = unknown>(value: T): T;
|
|
22
34
|
/**
|
|
23
|
-
*
|
|
35
|
+
* Narrow a string to a union of strings
|
|
36
|
+
*
|
|
24
37
|
* @param key
|
|
25
38
|
* @param union
|
|
26
39
|
*/
|
|
@@ -33,10 +46,11 @@ declare function narrowStringToUnion<const T extends string>(key: string | undef
|
|
|
33
46
|
* @returns Returns undefined, only used for type checking
|
|
34
47
|
*/
|
|
35
48
|
declare function typeOnRightExtendsLeftType<BaseType, SubType extends BaseType>(): unknown;
|
|
36
|
-
/** @deprecated
|
|
49
|
+
/** @deprecated Use typeOnRightExtendsLeftType instead */
|
|
37
50
|
declare const isSubTypeOf: typeof typeOnRightExtendsLeftType;
|
|
38
51
|
/**
|
|
39
52
|
* Type helper to narrow a string to a key of an object.
|
|
53
|
+
*
|
|
40
54
|
* @param key
|
|
41
55
|
* @param obj
|
|
42
56
|
*/
|
|
@@ -58,9 +72,22 @@ type UnionDiff<T, U> = [
|
|
|
58
72
|
*
|
|
59
73
|
* @template T - The first union type (left side)
|
|
60
74
|
* @template U - The second union type (right side)
|
|
61
|
-
* @param _diff -
|
|
75
|
+
* @param _diff - Null if unions are identical, or an object describing the
|
|
76
|
+
* errors
|
|
62
77
|
*/
|
|
63
78
|
declare function unionsAreTheSame<T, U>(_diff: UnionDiff<T, U>): void;
|
|
64
79
|
declare function asPartialUndefinedValues<T extends Record<string, unknown>>(value: PartialPossiblyUndefinedValues<T>): T;
|
|
80
|
+
/** A type representing an array that is guaranteed to have at least one element. */
|
|
81
|
+
type NonEmptyArray<T> = [T, ...T[]];
|
|
82
|
+
/**
|
|
83
|
+
* Type guard to check if an array has at least one element.
|
|
84
|
+
*
|
|
85
|
+
* @param array - The array to check
|
|
86
|
+
* @returns True if the array is non-empty, false otherwise
|
|
87
|
+
*/
|
|
88
|
+
declare function isNonEmptyArray<T>(array: T[]): array is NonEmptyArray<T>;
|
|
89
|
+
declare function objectHasKey<T extends string>(obj: object, key: T): obj is object & {
|
|
90
|
+
[K in T]: unknown;
|
|
91
|
+
};
|
|
65
92
|
|
|
66
|
-
export { asNonPartial, asPartialUndefinedValues, asType, isObjKey, isSubTypeOf, narrowStringToUnion, typeOnRightExtendsLeftType, typedObjectEntries, typedObjectKeys, unionsAreTheSame };
|
|
93
|
+
export { type NonEmptyArray, asNonPartial, asPartialUndefinedValues, asType, isNonEmptyArray, isObjKey, isSubTypeOf, narrowStringToUnion, objectHasKey, strictTypedObjectEntries, typeOnRightExtendsLeftType, typedObjectEntries, typedObjectKeys, unionsAreTheSame };
|
package/dist/typingFnUtils.d.ts
CHANGED
|
@@ -3,24 +3,37 @@ import { NonPartial } from './typingUtils.js';
|
|
|
3
3
|
|
|
4
4
|
declare function asNonPartial<T extends Record<string, unknown>>(obj: T): NonPartial<T>;
|
|
5
5
|
/**
|
|
6
|
-
*
|
|
6
|
+
* A wrapper to Object.entries with a better typing inference
|
|
7
|
+
*
|
|
7
8
|
* @param obj
|
|
8
9
|
*/
|
|
9
10
|
declare function typedObjectEntries<T extends Record<string, unknown>>(obj: T): NonNullable<{
|
|
10
11
|
[K in keyof T]: [K, T[K]];
|
|
11
12
|
}[keyof T]>[];
|
|
12
13
|
/**
|
|
13
|
-
*
|
|
14
|
+
* A wrapper to Object.entries with a better typing inference, but with strict
|
|
15
|
+
* typing narrowing keys to strings.
|
|
16
|
+
*
|
|
17
|
+
* @param obj
|
|
18
|
+
*/
|
|
19
|
+
declare function strictTypedObjectEntries<T extends Record<string, unknown>>(obj: T): NonNullable<{
|
|
20
|
+
[K in keyof T]: [K & string, T[K]];
|
|
21
|
+
}[keyof T]>[];
|
|
22
|
+
/**
|
|
23
|
+
* A wrapper to Object.keys with a better typing inference
|
|
24
|
+
*
|
|
14
25
|
* @param obj
|
|
15
26
|
*/
|
|
16
27
|
declare function typedObjectKeys<T extends Record<string, unknown>>(obj: T): (keyof T)[];
|
|
17
28
|
/**
|
|
18
|
-
*
|
|
29
|
+
* A safe way to cast types, use to substitute the `as Type`
|
|
30
|
+
*
|
|
19
31
|
* @param value
|
|
20
32
|
*/
|
|
21
33
|
declare function asType<T = unknown>(value: T): T;
|
|
22
34
|
/**
|
|
23
|
-
*
|
|
35
|
+
* Narrow a string to a union of strings
|
|
36
|
+
*
|
|
24
37
|
* @param key
|
|
25
38
|
* @param union
|
|
26
39
|
*/
|
|
@@ -33,10 +46,11 @@ declare function narrowStringToUnion<const T extends string>(key: string | undef
|
|
|
33
46
|
* @returns Returns undefined, only used for type checking
|
|
34
47
|
*/
|
|
35
48
|
declare function typeOnRightExtendsLeftType<BaseType, SubType extends BaseType>(): unknown;
|
|
36
|
-
/** @deprecated
|
|
49
|
+
/** @deprecated Use typeOnRightExtendsLeftType instead */
|
|
37
50
|
declare const isSubTypeOf: typeof typeOnRightExtendsLeftType;
|
|
38
51
|
/**
|
|
39
52
|
* Type helper to narrow a string to a key of an object.
|
|
53
|
+
*
|
|
40
54
|
* @param key
|
|
41
55
|
* @param obj
|
|
42
56
|
*/
|
|
@@ -58,9 +72,22 @@ type UnionDiff<T, U> = [
|
|
|
58
72
|
*
|
|
59
73
|
* @template T - The first union type (left side)
|
|
60
74
|
* @template U - The second union type (right side)
|
|
61
|
-
* @param _diff -
|
|
75
|
+
* @param _diff - Null if unions are identical, or an object describing the
|
|
76
|
+
* errors
|
|
62
77
|
*/
|
|
63
78
|
declare function unionsAreTheSame<T, U>(_diff: UnionDiff<T, U>): void;
|
|
64
79
|
declare function asPartialUndefinedValues<T extends Record<string, unknown>>(value: PartialPossiblyUndefinedValues<T>): T;
|
|
80
|
+
/** A type representing an array that is guaranteed to have at least one element. */
|
|
81
|
+
type NonEmptyArray<T> = [T, ...T[]];
|
|
82
|
+
/**
|
|
83
|
+
* Type guard to check if an array has at least one element.
|
|
84
|
+
*
|
|
85
|
+
* @param array - The array to check
|
|
86
|
+
* @returns True if the array is non-empty, false otherwise
|
|
87
|
+
*/
|
|
88
|
+
declare function isNonEmptyArray<T>(array: T[]): array is NonEmptyArray<T>;
|
|
89
|
+
declare function objectHasKey<T extends string>(obj: object, key: T): obj is object & {
|
|
90
|
+
[K in T]: unknown;
|
|
91
|
+
};
|
|
65
92
|
|
|
66
|
-
export { asNonPartial, asPartialUndefinedValues, asType, isObjKey, isSubTypeOf, narrowStringToUnion, typeOnRightExtendsLeftType, typedObjectEntries, typedObjectKeys, unionsAreTheSame };
|
|
93
|
+
export { type NonEmptyArray, asNonPartial, asPartialUndefinedValues, asType, isNonEmptyArray, isObjKey, isSubTypeOf, narrowStringToUnion, objectHasKey, strictTypedObjectEntries, typeOnRightExtendsLeftType, typedObjectEntries, typedObjectKeys, unionsAreTheSame };
|