@ls-stack/utils 3.37.0 → 3.39.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,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
+ };
@@ -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)