@ls-stack/utils 3.37.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.
- package/dist/typeUtils.d.cts +8 -1
- package/dist/typeUtils.d.ts +8 -1
- package/dist/typedStrings.cjs +90 -0
- package/dist/typedStrings.d.cts +152 -0
- package/dist/typedStrings.d.ts +152 -0
- package/dist/typedStrings.js +57 -0
- package/dist/typingFnUtils.cjs +5 -0
- package/dist/typingFnUtils.d.cts +3 -1
- package/dist/typingFnUtils.d.ts +3 -1
- package/dist/typingFnUtils.js +4 -0
- package/docs/_media/modules.md +1 -0
- package/docs/modules.md +1 -0
- package/docs/typeUtils/README.md +18 -0
- package/docs/typedStrings.md +458 -0
- package/docs/typingFnUtils/-internal-.md +17 -1
- package/docs/typingFnUtils/README.md +35 -9
- package/package.json +5 -1
package/dist/typeUtils.d.cts
CHANGED
|
@@ -35,5 +35,12 @@ type DeepReplaceValueImpl<T, ReplaceType, NewType, SkipPaths extends string | un
|
|
|
35
35
|
* @template SkipTypes - The types to skip in transverse and replace.
|
|
36
36
|
*/
|
|
37
37
|
type DeepReplaceValue<T, ReplaceType, NewType, SkipPaths extends string | undefined = undefined, SkipTypes = DefaultSkipTransverseDeepReplace> = DeepReplaceValueImpl<T, ReplaceType, NewType, SkipPaths, SkipTypes>;
|
|
38
|
+
type KeysWithUndefinedValues<T extends Record<string, unknown>> = {
|
|
39
|
+
[K in keyof T]: undefined extends T[K] ? K : never;
|
|
40
|
+
}[keyof T];
|
|
41
|
+
/**
|
|
42
|
+
* Marks all possible undefined values as partial at the root level of the object.
|
|
43
|
+
*/
|
|
44
|
+
type PartialPossiblyUndefinedValues<T extends Record<string, unknown>> = Prettify<Partial<Pick<T, KeysWithUndefinedValues<T>>> & Omit<T, KeysWithUndefinedValues<T>>>;
|
|
38
45
|
|
|
39
|
-
export type { DeepPrettify, DeepReplaceValue, DefaultSkipTransverseDeepReplace, IsAny, NonPartial, ObjKeysWithValuesOfType, PartialRecord, Prettify };
|
|
46
|
+
export type { DeepPrettify, DeepReplaceValue, DefaultSkipTransverseDeepReplace, IsAny, NonPartial, ObjKeysWithValuesOfType, PartialPossiblyUndefinedValues, PartialRecord, Prettify };
|
package/dist/typeUtils.d.ts
CHANGED
|
@@ -35,5 +35,12 @@ type DeepReplaceValueImpl<T, ReplaceType, NewType, SkipPaths extends string | un
|
|
|
35
35
|
* @template SkipTypes - The types to skip in transverse and replace.
|
|
36
36
|
*/
|
|
37
37
|
type DeepReplaceValue<T, ReplaceType, NewType, SkipPaths extends string | undefined = undefined, SkipTypes = DefaultSkipTransverseDeepReplace> = DeepReplaceValueImpl<T, ReplaceType, NewType, SkipPaths, SkipTypes>;
|
|
38
|
+
type KeysWithUndefinedValues<T extends Record<string, unknown>> = {
|
|
39
|
+
[K in keyof T]: undefined extends T[K] ? K : never;
|
|
40
|
+
}[keyof T];
|
|
41
|
+
/**
|
|
42
|
+
* Marks all possible undefined values as partial at the root level of the object.
|
|
43
|
+
*/
|
|
44
|
+
type PartialPossiblyUndefinedValues<T extends Record<string, unknown>> = Prettify<Partial<Pick<T, KeysWithUndefinedValues<T>>> & Omit<T, KeysWithUndefinedValues<T>>>;
|
|
38
45
|
|
|
39
|
-
export type { DeepPrettify, DeepReplaceValue, DefaultSkipTransverseDeepReplace, IsAny, NonPartial, ObjKeysWithValuesOfType, PartialRecord, Prettify };
|
|
46
|
+
export type { DeepPrettify, DeepReplaceValue, DefaultSkipTransverseDeepReplace, IsAny, NonPartial, ObjKeysWithValuesOfType, PartialPossiblyUndefinedValues, PartialRecord, Prettify };
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/typedStrings.ts
|
|
21
|
+
var typedStrings_exports = {};
|
|
22
|
+
__export(typedStrings_exports, {
|
|
23
|
+
asNonEmptyStringOrNull: () => asNonEmptyStringOrNull,
|
|
24
|
+
asNonEmptyStringOrThrow: () => asNonEmptyStringOrThrow,
|
|
25
|
+
assertStringIsNonEmpty: () => assertStringIsNonEmpty,
|
|
26
|
+
isNonEmptyString: () => isNonEmptyString,
|
|
27
|
+
splitTypedString: () => splitTypedString,
|
|
28
|
+
splitTypedStringAt: () => splitTypedStringAt,
|
|
29
|
+
stringContains: () => stringContains,
|
|
30
|
+
stringEndsWith: () => stringEndsWith,
|
|
31
|
+
stringStartsWith: () => stringStartsWith
|
|
32
|
+
});
|
|
33
|
+
module.exports = __toCommonJS(typedStrings_exports);
|
|
34
|
+
function stringContains(str, substring) {
|
|
35
|
+
return str.includes(substring);
|
|
36
|
+
}
|
|
37
|
+
function stringStartsWith(str, substring) {
|
|
38
|
+
return str.startsWith(substring);
|
|
39
|
+
}
|
|
40
|
+
function stringEndsWith(str, substring) {
|
|
41
|
+
return str.endsWith(substring);
|
|
42
|
+
}
|
|
43
|
+
function splitTypedString(str, separator) {
|
|
44
|
+
return str.split(separator);
|
|
45
|
+
}
|
|
46
|
+
function splitTypedStringAt(str, separator, splitAtNSeparatorPos = 1) {
|
|
47
|
+
const parts = str.split(separator);
|
|
48
|
+
let leftPart = parts[0];
|
|
49
|
+
let rightPart = parts.slice(1).join(separator);
|
|
50
|
+
if (leftPart === void 0) {
|
|
51
|
+
throw new Error("String does not contain the separator");
|
|
52
|
+
}
|
|
53
|
+
if (splitAtNSeparatorPos > 1) {
|
|
54
|
+
leftPart = parts.slice(0, splitAtNSeparatorPos).join(separator);
|
|
55
|
+
rightPart = parts.slice(splitAtNSeparatorPos).join(separator);
|
|
56
|
+
}
|
|
57
|
+
return [leftPart, rightPart];
|
|
58
|
+
}
|
|
59
|
+
function isNonEmptyString(str) {
|
|
60
|
+
return str.length > 0;
|
|
61
|
+
}
|
|
62
|
+
function asNonEmptyStringOrThrow(str) {
|
|
63
|
+
if (isNonEmptyString(str)) {
|
|
64
|
+
return str;
|
|
65
|
+
}
|
|
66
|
+
throw new Error("String is empty");
|
|
67
|
+
}
|
|
68
|
+
function asNonEmptyStringOrNull(str) {
|
|
69
|
+
if (isNonEmptyString(str)) {
|
|
70
|
+
return str;
|
|
71
|
+
}
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
function assertStringIsNonEmpty(str) {
|
|
75
|
+
if (!isNonEmptyString(str)) {
|
|
76
|
+
throw new Error("String is empty");
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
80
|
+
0 && (module.exports = {
|
|
81
|
+
asNonEmptyStringOrNull,
|
|
82
|
+
asNonEmptyStringOrThrow,
|
|
83
|
+
assertStringIsNonEmpty,
|
|
84
|
+
isNonEmptyString,
|
|
85
|
+
splitTypedString,
|
|
86
|
+
splitTypedStringAt,
|
|
87
|
+
stringContains,
|
|
88
|
+
stringEndsWith,
|
|
89
|
+
stringStartsWith
|
|
90
|
+
});
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A type representing a string that contains a specific substring.
|
|
3
|
+
* Uses template literal types to ensure type safety at compile time.
|
|
4
|
+
*
|
|
5
|
+
* @template T - The substring that must be contained within the string
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* type EmailString = StringContaining<'@'>; // string that contains '@'
|
|
9
|
+
* const email: EmailString = 'user@example.com'; // ✓ valid
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
12
|
+
type StringContaining<T extends string> = string extends T ? never : `${string}${T}${string}`;
|
|
13
|
+
/**
|
|
14
|
+
* A type representing a string that starts with a specific substring.
|
|
15
|
+
* Uses template literal types to ensure the string begins with the specified prefix.
|
|
16
|
+
*
|
|
17
|
+
* @template T - The substring that the string must start with
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* type HttpUrl = StringStartingWith<'http'>; // string starting with 'http'
|
|
21
|
+
* const url: HttpUrl = 'https://example.com'; // ✓ valid
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
type StringStartingWith<T extends string> = string extends T ? never : `${T}${string}`;
|
|
25
|
+
/**
|
|
26
|
+
* A type representing a string that ends with a specific substring.
|
|
27
|
+
* Uses template literal types to ensure the string ends with the specified suffix.
|
|
28
|
+
*
|
|
29
|
+
* @template T - The substring that the string must end with
|
|
30
|
+
* @example
|
|
31
|
+
* ```ts
|
|
32
|
+
* type JavaFile = StringEndingWith<'.java'>; // string ending with '.java'
|
|
33
|
+
* const filename: JavaFile = 'HelloWorld.java'; // ✓ valid
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
type StringEndingWith<T extends string> = string extends T ? never : `${string}${T}`;
|
|
37
|
+
/**
|
|
38
|
+
* Type guard function that checks if a string contains a specific substring.
|
|
39
|
+
* Narrows the type to `StringContaining<T>` when the check passes.
|
|
40
|
+
*
|
|
41
|
+
* @param str - The string to check
|
|
42
|
+
* @param substring - The substring to search for
|
|
43
|
+
* @returns `true` if the string contains the substring, `false` otherwise
|
|
44
|
+
*/
|
|
45
|
+
declare function stringContains<T extends string>(str: string, substring: T): str is StringContaining<T>;
|
|
46
|
+
/**
|
|
47
|
+
* Type guard function that checks if a string starts with a specific substring.
|
|
48
|
+
* Narrows the type to `StringStartingWith<T>` when the check passes.
|
|
49
|
+
*
|
|
50
|
+
* @param str - The string to check
|
|
51
|
+
* @param substring - The substring to check for at the beginning
|
|
52
|
+
* @returns `true` if the string starts with the substring, `false` otherwise
|
|
53
|
+
*/
|
|
54
|
+
declare function stringStartsWith<T extends string>(str: string, substring: T): str is StringStartingWith<T>;
|
|
55
|
+
/**
|
|
56
|
+
* Type guard function that checks if a string ends with a specific substring.
|
|
57
|
+
* Narrows the type to `StringEndingWith<T>` when the check passes.
|
|
58
|
+
*
|
|
59
|
+
* @param str - The string to check
|
|
60
|
+
* @param substring - The substring to check for at the end
|
|
61
|
+
* @returns `true` if the string ends with the substring, `false` otherwise
|
|
62
|
+
*/
|
|
63
|
+
declare function stringEndsWith<T extends string>(str: string, substring: T): str is StringEndingWith<T>;
|
|
64
|
+
/**
|
|
65
|
+
* Splits a typed string by a separator that is guaranteed to exist in the string.
|
|
66
|
+
* Returns an array with at least two elements: the parts before and after the first separator,
|
|
67
|
+
* plus any additional parts if there are multiple separators.
|
|
68
|
+
*
|
|
69
|
+
* @param str - A string that contains, starts with, or ends with the separator
|
|
70
|
+
* @param separator - The separator to split by
|
|
71
|
+
* @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
|
+
*/
|
|
79
|
+
declare function splitTypedString<T extends string>(str: StringContaining<NoInfer<T>> | StringStartingWith<NoInfer<T>> | StringEndingWith<NoInfer<T>>, separator: T): [string, string, ...string[]];
|
|
80
|
+
/**
|
|
81
|
+
* Splits a typed string at a specific occurrence of the separator.
|
|
82
|
+
* Unlike `splitTypedString`, this returns exactly two parts: everything before
|
|
83
|
+
* the nth separator and everything after it.
|
|
84
|
+
*
|
|
85
|
+
* @param str - A string that contains, starts with, or ends with the separator
|
|
86
|
+
* @param separator - The separator to split by
|
|
87
|
+
* @param splitAtNSeparatorPos - The position of the separator to split at (1-based)
|
|
88
|
+
* @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
|
+
*/
|
|
96
|
+
declare function splitTypedStringAt<T extends string>(str: StringContaining<NoInfer<T>> | StringStartingWith<NoInfer<T>> | StringEndingWith<NoInfer<T>>, separator: T,
|
|
97
|
+
/**
|
|
98
|
+
* The position of the separator to split at.
|
|
99
|
+
* @default 1 - split at the first separator
|
|
100
|
+
*/
|
|
101
|
+
splitAtNSeparatorPos?: number): [string, string];
|
|
102
|
+
/**
|
|
103
|
+
* A branded type representing a string that is guaranteed to be non-empty (length > 0).
|
|
104
|
+
* This type provides compile-time safety by preventing empty strings from being
|
|
105
|
+
* assigned without proper validation.
|
|
106
|
+
*
|
|
107
|
+
* @example
|
|
108
|
+
* ```ts
|
|
109
|
+
* function processName(name: NonEmptyString) {
|
|
110
|
+
* // name is guaranteed to be non-empty
|
|
111
|
+
* return name.toUpperCase();
|
|
112
|
+
* }
|
|
113
|
+
* ```
|
|
114
|
+
*/
|
|
115
|
+
type NonEmptyString = string & {
|
|
116
|
+
__nonEmptyString: true;
|
|
117
|
+
};
|
|
118
|
+
/**
|
|
119
|
+
* Type guard function that checks if a string is non-empty.
|
|
120
|
+
* Narrows the type to `NonEmptyString` when the check passes.
|
|
121
|
+
*
|
|
122
|
+
* @param str - The string to check
|
|
123
|
+
* @returns `true` if the string has length > 0, `false` otherwise
|
|
124
|
+
*/
|
|
125
|
+
declare function isNonEmptyString(str: string): str is NonEmptyString;
|
|
126
|
+
/**
|
|
127
|
+
* Converts a string to `NonEmptyString` or throws an error if the string is empty.
|
|
128
|
+
* Use this when you need to ensure a string is non-empty and want to fail fast.
|
|
129
|
+
*
|
|
130
|
+
* @param str - The string to convert
|
|
131
|
+
* @returns The string as `NonEmptyString`
|
|
132
|
+
* @throws Error if the string is empty
|
|
133
|
+
*/
|
|
134
|
+
declare function asNonEmptyStringOrThrow(str: string): NonEmptyString;
|
|
135
|
+
/**
|
|
136
|
+
* Converts a string to `NonEmptyString` or returns `null` if the string is empty.
|
|
137
|
+
* Use this when empty strings should be handled gracefully rather than throwing errors.
|
|
138
|
+
*
|
|
139
|
+
* @param str - The string to convert
|
|
140
|
+
* @returns The string as `NonEmptyString` or `null` if empty
|
|
141
|
+
*/
|
|
142
|
+
declare function asNonEmptyStringOrNull(str: string): NonEmptyString | null;
|
|
143
|
+
/**
|
|
144
|
+
* Assertion function that ensures a string is non-empty.
|
|
145
|
+
* Throws an error if the string is empty, otherwise narrows the type to `NonEmptyString`.
|
|
146
|
+
*
|
|
147
|
+
* @param str - The string to assert as non-empty
|
|
148
|
+
* @throws Error if the string is empty
|
|
149
|
+
*/
|
|
150
|
+
declare function assertStringIsNonEmpty(str: string): asserts str is NonEmptyString;
|
|
151
|
+
|
|
152
|
+
export { type NonEmptyString, type StringContaining, type StringEndingWith, type StringStartingWith, asNonEmptyStringOrNull, asNonEmptyStringOrThrow, assertStringIsNonEmpty, isNonEmptyString, splitTypedString, splitTypedStringAt, stringContains, stringEndsWith, stringStartsWith };
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A type representing a string that contains a specific substring.
|
|
3
|
+
* Uses template literal types to ensure type safety at compile time.
|
|
4
|
+
*
|
|
5
|
+
* @template T - The substring that must be contained within the string
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* type EmailString = StringContaining<'@'>; // string that contains '@'
|
|
9
|
+
* const email: EmailString = 'user@example.com'; // ✓ valid
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
12
|
+
type StringContaining<T extends string> = string extends T ? never : `${string}${T}${string}`;
|
|
13
|
+
/**
|
|
14
|
+
* A type representing a string that starts with a specific substring.
|
|
15
|
+
* Uses template literal types to ensure the string begins with the specified prefix.
|
|
16
|
+
*
|
|
17
|
+
* @template T - The substring that the string must start with
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* type HttpUrl = StringStartingWith<'http'>; // string starting with 'http'
|
|
21
|
+
* const url: HttpUrl = 'https://example.com'; // ✓ valid
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
type StringStartingWith<T extends string> = string extends T ? never : `${T}${string}`;
|
|
25
|
+
/**
|
|
26
|
+
* A type representing a string that ends with a specific substring.
|
|
27
|
+
* Uses template literal types to ensure the string ends with the specified suffix.
|
|
28
|
+
*
|
|
29
|
+
* @template T - The substring that the string must end with
|
|
30
|
+
* @example
|
|
31
|
+
* ```ts
|
|
32
|
+
* type JavaFile = StringEndingWith<'.java'>; // string ending with '.java'
|
|
33
|
+
* const filename: JavaFile = 'HelloWorld.java'; // ✓ valid
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
type StringEndingWith<T extends string> = string extends T ? never : `${string}${T}`;
|
|
37
|
+
/**
|
|
38
|
+
* Type guard function that checks if a string contains a specific substring.
|
|
39
|
+
* Narrows the type to `StringContaining<T>` when the check passes.
|
|
40
|
+
*
|
|
41
|
+
* @param str - The string to check
|
|
42
|
+
* @param substring - The substring to search for
|
|
43
|
+
* @returns `true` if the string contains the substring, `false` otherwise
|
|
44
|
+
*/
|
|
45
|
+
declare function stringContains<T extends string>(str: string, substring: T): str is StringContaining<T>;
|
|
46
|
+
/**
|
|
47
|
+
* Type guard function that checks if a string starts with a specific substring.
|
|
48
|
+
* Narrows the type to `StringStartingWith<T>` when the check passes.
|
|
49
|
+
*
|
|
50
|
+
* @param str - The string to check
|
|
51
|
+
* @param substring - The substring to check for at the beginning
|
|
52
|
+
* @returns `true` if the string starts with the substring, `false` otherwise
|
|
53
|
+
*/
|
|
54
|
+
declare function stringStartsWith<T extends string>(str: string, substring: T): str is StringStartingWith<T>;
|
|
55
|
+
/**
|
|
56
|
+
* Type guard function that checks if a string ends with a specific substring.
|
|
57
|
+
* Narrows the type to `StringEndingWith<T>` when the check passes.
|
|
58
|
+
*
|
|
59
|
+
* @param str - The string to check
|
|
60
|
+
* @param substring - The substring to check for at the end
|
|
61
|
+
* @returns `true` if the string ends with the substring, `false` otherwise
|
|
62
|
+
*/
|
|
63
|
+
declare function stringEndsWith<T extends string>(str: string, substring: T): str is StringEndingWith<T>;
|
|
64
|
+
/**
|
|
65
|
+
* Splits a typed string by a separator that is guaranteed to exist in the string.
|
|
66
|
+
* Returns an array with at least two elements: the parts before and after the first separator,
|
|
67
|
+
* plus any additional parts if there are multiple separators.
|
|
68
|
+
*
|
|
69
|
+
* @param str - A string that contains, starts with, or ends with the separator
|
|
70
|
+
* @param separator - The separator to split by
|
|
71
|
+
* @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
|
+
*/
|
|
79
|
+
declare function splitTypedString<T extends string>(str: StringContaining<NoInfer<T>> | StringStartingWith<NoInfer<T>> | StringEndingWith<NoInfer<T>>, separator: T): [string, string, ...string[]];
|
|
80
|
+
/**
|
|
81
|
+
* Splits a typed string at a specific occurrence of the separator.
|
|
82
|
+
* Unlike `splitTypedString`, this returns exactly two parts: everything before
|
|
83
|
+
* the nth separator and everything after it.
|
|
84
|
+
*
|
|
85
|
+
* @param str - A string that contains, starts with, or ends with the separator
|
|
86
|
+
* @param separator - The separator to split by
|
|
87
|
+
* @param splitAtNSeparatorPos - The position of the separator to split at (1-based)
|
|
88
|
+
* @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
|
+
*/
|
|
96
|
+
declare function splitTypedStringAt<T extends string>(str: StringContaining<NoInfer<T>> | StringStartingWith<NoInfer<T>> | StringEndingWith<NoInfer<T>>, separator: T,
|
|
97
|
+
/**
|
|
98
|
+
* The position of the separator to split at.
|
|
99
|
+
* @default 1 - split at the first separator
|
|
100
|
+
*/
|
|
101
|
+
splitAtNSeparatorPos?: number): [string, string];
|
|
102
|
+
/**
|
|
103
|
+
* A branded type representing a string that is guaranteed to be non-empty (length > 0).
|
|
104
|
+
* This type provides compile-time safety by preventing empty strings from being
|
|
105
|
+
* assigned without proper validation.
|
|
106
|
+
*
|
|
107
|
+
* @example
|
|
108
|
+
* ```ts
|
|
109
|
+
* function processName(name: NonEmptyString) {
|
|
110
|
+
* // name is guaranteed to be non-empty
|
|
111
|
+
* return name.toUpperCase();
|
|
112
|
+
* }
|
|
113
|
+
* ```
|
|
114
|
+
*/
|
|
115
|
+
type NonEmptyString = string & {
|
|
116
|
+
__nonEmptyString: true;
|
|
117
|
+
};
|
|
118
|
+
/**
|
|
119
|
+
* Type guard function that checks if a string is non-empty.
|
|
120
|
+
* Narrows the type to `NonEmptyString` when the check passes.
|
|
121
|
+
*
|
|
122
|
+
* @param str - The string to check
|
|
123
|
+
* @returns `true` if the string has length > 0, `false` otherwise
|
|
124
|
+
*/
|
|
125
|
+
declare function isNonEmptyString(str: string): str is NonEmptyString;
|
|
126
|
+
/**
|
|
127
|
+
* Converts a string to `NonEmptyString` or throws an error if the string is empty.
|
|
128
|
+
* Use this when you need to ensure a string is non-empty and want to fail fast.
|
|
129
|
+
*
|
|
130
|
+
* @param str - The string to convert
|
|
131
|
+
* @returns The string as `NonEmptyString`
|
|
132
|
+
* @throws Error if the string is empty
|
|
133
|
+
*/
|
|
134
|
+
declare function asNonEmptyStringOrThrow(str: string): NonEmptyString;
|
|
135
|
+
/**
|
|
136
|
+
* Converts a string to `NonEmptyString` or returns `null` if the string is empty.
|
|
137
|
+
* Use this when empty strings should be handled gracefully rather than throwing errors.
|
|
138
|
+
*
|
|
139
|
+
* @param str - The string to convert
|
|
140
|
+
* @returns The string as `NonEmptyString` or `null` if empty
|
|
141
|
+
*/
|
|
142
|
+
declare function asNonEmptyStringOrNull(str: string): NonEmptyString | null;
|
|
143
|
+
/**
|
|
144
|
+
* Assertion function that ensures a string is non-empty.
|
|
145
|
+
* Throws an error if the string is empty, otherwise narrows the type to `NonEmptyString`.
|
|
146
|
+
*
|
|
147
|
+
* @param str - The string to assert as non-empty
|
|
148
|
+
* @throws Error if the string is empty
|
|
149
|
+
*/
|
|
150
|
+
declare function assertStringIsNonEmpty(str: string): asserts str is NonEmptyString;
|
|
151
|
+
|
|
152
|
+
export { type NonEmptyString, type StringContaining, type StringEndingWith, type StringStartingWith, asNonEmptyStringOrNull, asNonEmptyStringOrThrow, assertStringIsNonEmpty, isNonEmptyString, splitTypedString, splitTypedStringAt, stringContains, stringEndsWith, stringStartsWith };
|
|
@@ -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
|
+
};
|
package/dist/typingFnUtils.cjs
CHANGED
|
@@ -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,
|
package/dist/typingFnUtils.d.cts
CHANGED
|
@@ -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 };
|
package/dist/typingFnUtils.d.ts
CHANGED
|
@@ -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 };
|
package/dist/typingFnUtils.js
CHANGED
package/docs/_media/modules.md
CHANGED
package/docs/modules.md
CHANGED
package/docs/typeUtils/README.md
CHANGED
|
@@ -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:
|
|
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
|
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
const isSubTypeOf: <BaseType, SubType>() => unknown = typeOnRightExtendsLeftType;
|
|
19
19
|
```
|
|
20
20
|
|
|
21
|
-
Defined in: [packages/utils/src/typingFnUtils.ts:
|
|
21
|
+
Defined in: [packages/utils/src/typingFnUtils.ts:79](https://github.com/lucasols/utils/blob/main/packages/utils/src/typingFnUtils.ts#L79)
|
|
22
22
|
|
|
23
23
|
Type helper to check if a type is a subtype of another type.
|
|
24
24
|
|
|
@@ -54,7 +54,7 @@ use typeOnRightExtendsLeftType instead
|
|
|
54
54
|
function asNonPartial<T>(obj): NonPartial<T>;
|
|
55
55
|
```
|
|
56
56
|
|
|
57
|
-
Defined in: [packages/utils/src/typingFnUtils.ts:
|
|
57
|
+
Defined in: [packages/utils/src/typingFnUtils.ts:4](https://github.com/lucasols/utils/blob/main/packages/utils/src/typingFnUtils.ts#L4)
|
|
58
58
|
|
|
59
59
|
#### Type Parameters
|
|
60
60
|
|
|
@@ -74,13 +74,39 @@ Defined in: [packages/utils/src/typingFnUtils.ts:3](https://github.com/lucasols/
|
|
|
74
74
|
|
|
75
75
|
***
|
|
76
76
|
|
|
77
|
+
### asPartialUndefinedValues()
|
|
78
|
+
|
|
79
|
+
```ts
|
|
80
|
+
function asPartialUndefinedValues<T>(value): T;
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
Defined in: [packages/utils/src/typingFnUtils.ts:110](https://github.com/lucasols/utils/blob/main/packages/utils/src/typingFnUtils.ts#L110)
|
|
84
|
+
|
|
85
|
+
#### Type Parameters
|
|
86
|
+
|
|
87
|
+
##### T
|
|
88
|
+
|
|
89
|
+
`T` *extends* `Record`\<`string`, `unknown`\>
|
|
90
|
+
|
|
91
|
+
#### Parameters
|
|
92
|
+
|
|
93
|
+
##### value
|
|
94
|
+
|
|
95
|
+
\{ \[P in string \| number \| symbol\]: (Partial\<Pick\<T, KeysWithUndefinedValues\<T\>\>\> & Omit\<T, KeysWithUndefinedValues\<T\>\>)\[P\] \}
|
|
96
|
+
|
|
97
|
+
#### Returns
|
|
98
|
+
|
|
99
|
+
`T`
|
|
100
|
+
|
|
101
|
+
***
|
|
102
|
+
|
|
77
103
|
### asType()
|
|
78
104
|
|
|
79
105
|
```ts
|
|
80
106
|
function asType<T>(value): T;
|
|
81
107
|
```
|
|
82
108
|
|
|
83
|
-
Defined in: [packages/utils/src/typingFnUtils.ts:
|
|
109
|
+
Defined in: [packages/utils/src/typingFnUtils.ts:38](https://github.com/lucasols/utils/blob/main/packages/utils/src/typingFnUtils.ts#L38)
|
|
84
110
|
|
|
85
111
|
a safe way to cast types, use to substitute the `as Type`
|
|
86
112
|
|
|
@@ -108,7 +134,7 @@ a safe way to cast types, use to substitute the `as Type`
|
|
|
108
134
|
function isObjKey<T>(key, obj): key is keyof T;
|
|
109
135
|
```
|
|
110
136
|
|
|
111
|
-
Defined in: [packages/utils/src/typingFnUtils.ts:
|
|
137
|
+
Defined in: [packages/utils/src/typingFnUtils.ts:86](https://github.com/lucasols/utils/blob/main/packages/utils/src/typingFnUtils.ts#L86)
|
|
112
138
|
|
|
113
139
|
Type helper to narrow a string to a key of an object.
|
|
114
140
|
|
|
@@ -140,7 +166,7 @@ Type helper to narrow a string to a key of an object.
|
|
|
140
166
|
function narrowStringToUnion<T>(key, union): undefined | T;
|
|
141
167
|
```
|
|
142
168
|
|
|
143
|
-
Defined in: [packages/utils/src/typingFnUtils.ts:
|
|
169
|
+
Defined in: [packages/utils/src/typingFnUtils.ts:47](https://github.com/lucasols/utils/blob/main/packages/utils/src/typingFnUtils.ts#L47)
|
|
144
170
|
|
|
145
171
|
narrow a string to a union of strings
|
|
146
172
|
|
|
@@ -172,7 +198,7 @@ narrow a string to a union of strings
|
|
|
172
198
|
function typedObjectEntries<T>(obj): NonNullable<{ [K in string | number | symbol]: [K, T[K]] }[keyof T]>[];
|
|
173
199
|
```
|
|
174
200
|
|
|
175
|
-
Defined in: [packages/utils/src/typingFnUtils.ts:
|
|
201
|
+
Defined in: [packages/utils/src/typingFnUtils.ts:14](https://github.com/lucasols/utils/blob/main/packages/utils/src/typingFnUtils.ts#L14)
|
|
176
202
|
|
|
177
203
|
a wrapper to Object.entries with a better typing inference
|
|
178
204
|
|
|
@@ -200,7 +226,7 @@ a wrapper to Object.entries with a better typing inference
|
|
|
200
226
|
function typedObjectKeys<T>(obj): keyof T[];
|
|
201
227
|
```
|
|
202
228
|
|
|
203
|
-
Defined in: [packages/utils/src/typingFnUtils.ts:
|
|
229
|
+
Defined in: [packages/utils/src/typingFnUtils.ts:28](https://github.com/lucasols/utils/blob/main/packages/utils/src/typingFnUtils.ts#L28)
|
|
204
230
|
|
|
205
231
|
a wrapper to Object.keys with a better typing inference
|
|
206
232
|
|
|
@@ -228,7 +254,7 @@ keyof `T`[]
|
|
|
228
254
|
function typeOnRightExtendsLeftType<BaseType, SubType>(): unknown;
|
|
229
255
|
```
|
|
230
256
|
|
|
231
|
-
Defined in: [packages/utils/src/typingFnUtils.ts:
|
|
257
|
+
Defined in: [packages/utils/src/typingFnUtils.ts:71](https://github.com/lucasols/utils/blob/main/packages/utils/src/typingFnUtils.ts#L71)
|
|
232
258
|
|
|
233
259
|
Type helper to check if a type is a subtype of another type.
|
|
234
260
|
|
|
@@ -260,7 +286,7 @@ Returns undefined, only used for type checking
|
|
|
260
286
|
function unionsAreTheSame<T, U>(_diff): void;
|
|
261
287
|
```
|
|
262
288
|
|
|
263
|
-
Defined in: [packages/utils/src/typingFnUtils.ts:
|
|
289
|
+
Defined in: [packages/utils/src/typingFnUtils.ts:108](https://github.com/lucasols/utils/blob/main/packages/utils/src/typingFnUtils.ts#L108)
|
|
264
290
|
|
|
265
291
|
Type helper to compare two union types and determine their relationship.
|
|
266
292
|
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ls-stack/utils",
|
|
3
3
|
"description": "Universal TypeScript utilities for browser and Node.js",
|
|
4
|
-
"version": "3.
|
|
4
|
+
"version": "3.38.0",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"files": [
|
|
7
7
|
"dist",
|
|
@@ -200,6 +200,10 @@
|
|
|
200
200
|
"import": "./dist/typeUtils.typesTest.js",
|
|
201
201
|
"require": "./dist/typeUtils.typesTest.cjs"
|
|
202
202
|
},
|
|
203
|
+
"./typedStrings": {
|
|
204
|
+
"import": "./dist/typedStrings.js",
|
|
205
|
+
"require": "./dist/typedStrings.cjs"
|
|
206
|
+
},
|
|
203
207
|
"./typingFnUtils": {
|
|
204
208
|
"import": "./dist/typingFnUtils.js",
|
|
205
209
|
"require": "./dist/typingFnUtils.cjs"
|