@budsbox/lib-es 2.0.0 → 2.2.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/array.d.ts +18 -8
- package/dist/array.js +16 -4
- package/dist/class-name.d.ts +2 -0
- package/dist/class-name.js +2 -0
- package/dist/guards.d.ts +83 -33
- package/dist/guards.js +15 -14
- package/dist/logical.d.ts +18 -16
- package/dist/logical.js +14 -12
- package/dist/random.d.ts +19 -0
- package/dist/random.js +35 -0
- package/dist/string.d.ts +69 -1
- package/dist/string.js +51 -1
- package/dist/types.d.ts +93 -7
- package/package.json +7 -7
package/dist/array.d.ts
CHANGED
|
@@ -1,12 +1,22 @@
|
|
|
1
|
+
import type { TupleN } from '@budsbox/lib-types';
|
|
2
|
+
import type { FValue } from './types.js';
|
|
1
3
|
/**
|
|
2
4
|
* Ensures that the provided value is returned as an array. If the value is already an array,
|
|
3
5
|
* it is returned as-is. If the value is not an array, it is wrapped in a new array.
|
|
4
6
|
*
|
|
5
7
|
* @param value - The value to be checked and converted into an array if necessary.
|
|
6
|
-
* @
|
|
8
|
+
* @returns An array containing the original value or the original array if it was already an array.
|
|
7
9
|
*/
|
|
8
|
-
export declare function ensureArray<T>(value: T | readonly T[]): readonly T[];
|
|
9
10
|
export declare function ensureArray<T>(value: T | T[]): T[];
|
|
11
|
+
export declare function ensureArray<T>(value: readonly T[] | T): readonly T[];
|
|
12
|
+
/**
|
|
13
|
+
* Creates an array of a fixed length with all elements initialized to the specified value.
|
|
14
|
+
*
|
|
15
|
+
* @param n - The length of the array to create.
|
|
16
|
+
* @param value - The value to fill the array with. Defaults to `null`.
|
|
17
|
+
* @returns A tuple of the specified length with all elements set to the given value.
|
|
18
|
+
*/
|
|
19
|
+
export declare function nArray<N extends number, T = null>(n: N, value?: FValue<number, T>): TupleN<N, T>;
|
|
10
20
|
/**
|
|
11
21
|
* Removes duplicate elements from the provided array while preserving the order of the first occurrence of each element.
|
|
12
22
|
*
|
|
@@ -19,9 +29,9 @@ export declare const dedupe: <T>(items: readonly T[]) => T[];
|
|
|
19
29
|
* Creates an array of unique values from the combined elements of the input arrays.
|
|
20
30
|
*
|
|
21
31
|
* @param arrays - The arrays to be merged and deduplicated.
|
|
22
|
-
* @
|
|
32
|
+
* @returns An array containing unique elements from all input arrays.
|
|
23
33
|
*/
|
|
24
|
-
export declare function union<T>(...arrays: ReadonlyArray<T |
|
|
34
|
+
export declare function union<T>(...arrays: ReadonlyArray<readonly T[] | T>): T[];
|
|
25
35
|
/**
|
|
26
36
|
* Computes the intersection of multiple arrays, returning an array that contains
|
|
27
37
|
* all elements that are present in every input array.
|
|
@@ -30,14 +40,14 @@ export declare function union<T>(...arrays: ReadonlyArray<T | readonly T[]>): T[
|
|
|
30
40
|
* Each array or element will be checked for intersection.
|
|
31
41
|
* @returns An array containing elements that are present in all input arrays.
|
|
32
42
|
*/
|
|
33
|
-
export declare function intersection<T>(...arrays: ReadonlyArray<T |
|
|
43
|
+
export declare function intersection<T>(...arrays: ReadonlyArray<readonly T[] | T>): T[];
|
|
34
44
|
/**
|
|
35
45
|
* Computes the difference between a source array and one or more arrays of exclusions.
|
|
36
46
|
* Returns a new array containing elements from the source array that are not present in any of the exclusion arrays.
|
|
37
47
|
*
|
|
38
|
-
* @param source The source array to compare against.
|
|
39
|
-
* @param excludes Arrays containing elements to be excluded from the source array.
|
|
40
|
-
* @
|
|
48
|
+
* @param source - The source array to compare against.
|
|
49
|
+
* @param excludes - Arrays containing elements to be excluded from the source array.
|
|
50
|
+
* @returns A new array containing elements from the source array that are not in the exclusion arrays.
|
|
41
51
|
*/
|
|
42
52
|
export declare function diff<T>(source: readonly T[], ...excludes: ReadonlyArray<readonly T[]>): T[];
|
|
43
53
|
//# sourceMappingURL=array.d.ts.map
|
package/dist/array.js
CHANGED
|
@@ -1,6 +1,18 @@
|
|
|
1
|
+
import { isFunction } from '#guards';
|
|
1
2
|
export function ensureArray(value) {
|
|
2
3
|
return Array.isArray(value) ? value : [value];
|
|
3
4
|
}
|
|
5
|
+
export function nArray(n, value = null) {
|
|
6
|
+
// eslint-disable-next-line jsdoc/require-jsdoc
|
|
7
|
+
if (isFunction(value)) {
|
|
8
|
+
const newArray = new Array(n);
|
|
9
|
+
for (let i = 0; i < newArray.length; i++) {
|
|
10
|
+
newArray[i] = value(i);
|
|
11
|
+
}
|
|
12
|
+
return newArray;
|
|
13
|
+
}
|
|
14
|
+
return new Array(n).fill(value);
|
|
15
|
+
}
|
|
4
16
|
/**
|
|
5
17
|
* Removes duplicate elements from the provided array while preserving the order of the first occurrence of each element.
|
|
6
18
|
*
|
|
@@ -13,7 +25,7 @@ export const dedupe = (items) => Array.from(new Set(items));
|
|
|
13
25
|
* Creates an array of unique values from the combined elements of the input arrays.
|
|
14
26
|
*
|
|
15
27
|
* @param arrays - The arrays to be merged and deduplicated.
|
|
16
|
-
* @
|
|
28
|
+
* @returns An array containing unique elements from all input arrays.
|
|
17
29
|
*/
|
|
18
30
|
export function union(...arrays) {
|
|
19
31
|
return dedupe(arrays.flatMap((v) => v));
|
|
@@ -43,9 +55,9 @@ export function intersection(...arrays) {
|
|
|
43
55
|
* Computes the difference between a source array and one or more arrays of exclusions.
|
|
44
56
|
* Returns a new array containing elements from the source array that are not present in any of the exclusion arrays.
|
|
45
57
|
*
|
|
46
|
-
* @param source The source array to compare against.
|
|
47
|
-
* @param excludes Arrays containing elements to be excluded from the source array.
|
|
48
|
-
* @
|
|
58
|
+
* @param source - The source array to compare against.
|
|
59
|
+
* @param excludes - Arrays containing elements to be excluded from the source array.
|
|
60
|
+
* @returns A new array containing elements from the source array that are not in the exclusion arrays.
|
|
49
61
|
*/
|
|
50
62
|
export function diff(source, ...excludes) {
|
|
51
63
|
const set = new Set(excludes.flatMap((v) => v));
|
package/dist/guards.d.ts
CHANGED
|
@@ -1,67 +1,74 @@
|
|
|
1
1
|
import type { Def, Nil, NonNil, Undef } from '@budsbox/lib-types';
|
|
2
|
+
import type { Predicate, TypeGuard } from './types.js';
|
|
2
3
|
/**
|
|
3
4
|
* Checks if the provided value is `undefined`.
|
|
4
5
|
*
|
|
5
6
|
* @param value - The value to check for `undefined`.
|
|
6
|
-
* @
|
|
7
|
+
* @returns `true` if the value is `undefined`, otherwise `false`.
|
|
7
8
|
*/
|
|
8
9
|
export declare function isUndef(value: unknown): value is Undef;
|
|
9
10
|
/**
|
|
10
11
|
* Checks if a given value is defined (not `undefined`).
|
|
11
12
|
*
|
|
12
13
|
* @param value - The value to check.
|
|
13
|
-
* @
|
|
14
|
+
* @returns Whether the value is defined.
|
|
14
15
|
*/
|
|
15
16
|
export declare function isDef<U>(value: U): value is Def<U>;
|
|
16
17
|
/**
|
|
17
18
|
* Checks if the provided value is `null` or `undefined`.
|
|
18
19
|
*
|
|
19
20
|
* @param value - The value to be checked.
|
|
20
|
-
* @
|
|
21
|
+
* @returns Returns `true` if the value is `null` or `undefined`, otherwise `false`.
|
|
21
22
|
*/
|
|
22
23
|
export declare function isNil(value: unknown): value is Nil;
|
|
23
24
|
/**
|
|
24
25
|
* Checks if the provided value is not null or undefined.
|
|
25
26
|
*
|
|
26
27
|
* @param value - The value to be checked.
|
|
27
|
-
* @
|
|
28
|
+
* @returns Returns true if the value is not null or undefined; otherwise, false.
|
|
28
29
|
*/
|
|
29
30
|
export declare function isNotNil<T>(value: T): value is NonNil<T>;
|
|
31
|
+
/**
|
|
32
|
+
* Checks if the provided value is not null or undefined.
|
|
33
|
+
*
|
|
34
|
+
* @param value - The value to be checked.
|
|
35
|
+
* @returns Returns true if the value is not null or undefined; otherwise, false.
|
|
36
|
+
*/
|
|
30
37
|
export declare function isNotNil(value: unknown): value is NonNil;
|
|
31
38
|
/**
|
|
32
39
|
* Checks if the provided value is strictly equal to true.
|
|
33
40
|
*
|
|
34
41
|
* @param value - The value to check.
|
|
35
|
-
* @
|
|
42
|
+
* @returns Returns true if the value is strictly true, otherwise false.
|
|
36
43
|
*/
|
|
37
44
|
export declare function isTrue(value: unknown): value is true;
|
|
38
45
|
/**
|
|
39
46
|
* Determines if the provided value is strictly `false`.
|
|
40
47
|
*
|
|
41
48
|
* @param value - The value to be checked.
|
|
42
|
-
* @
|
|
49
|
+
* @returns Returns `true` if the value is `false`, otherwise returns `false`.
|
|
43
50
|
*/
|
|
44
51
|
export declare function isFalse(value: unknown): value is false;
|
|
45
52
|
/**
|
|
46
53
|
* Determines if the given value is truly, i.e., converts to true when used in a boolean context.
|
|
47
54
|
*
|
|
48
55
|
* @param value - The value to be tested for truthiness.
|
|
49
|
-
* @
|
|
56
|
+
* @returns Returns true if the value is truthy, false otherwise.
|
|
50
57
|
*/
|
|
51
58
|
export declare function isTruly(value: unknown): boolean;
|
|
52
59
|
/**
|
|
53
60
|
* Determines if a given value is falsy.
|
|
54
61
|
* A value is considered falsy if it evaluates to false when coerced to a boolean.
|
|
55
62
|
*
|
|
56
|
-
* @param value The value to be tested.
|
|
57
|
-
* @
|
|
63
|
+
* @param value - The value to be tested.
|
|
64
|
+
* @returns True if the value is falsy, otherwise false.
|
|
58
65
|
*/
|
|
59
66
|
export declare function isFalsy(value: unknown): boolean;
|
|
60
67
|
/**
|
|
61
68
|
* Checks if the given value is an object.
|
|
62
69
|
*
|
|
63
70
|
* @param value - The value to check.
|
|
64
|
-
* @
|
|
71
|
+
* @returns True if the value is an object, false otherwise.
|
|
65
72
|
*/
|
|
66
73
|
export declare function isObject(value: unknown): value is object;
|
|
67
74
|
/**
|
|
@@ -73,73 +80,116 @@ export declare function isObject(value: unknown): value is object;
|
|
|
73
80
|
*
|
|
74
81
|
* @param value - The value to check.
|
|
75
82
|
* @param allowEmpty - Determines whether empty records are allowed.
|
|
76
|
-
* @
|
|
83
|
+
* @returns A boolean indicating whether the value is a record.
|
|
77
84
|
*/
|
|
78
85
|
export declare function isRecord(value: unknown, allowEmpty?: boolean): value is Record<PropertyKey, unknown>;
|
|
79
86
|
/**
|
|
80
87
|
* Checks if the given value is an array.
|
|
81
88
|
*
|
|
82
|
-
* @
|
|
89
|
+
* @typeParam T - Type of the value to be checked.
|
|
83
90
|
* @param value - The value to check.
|
|
84
|
-
* @
|
|
91
|
+
* @returns True if the value is an array, otherwise false.
|
|
85
92
|
*/
|
|
86
93
|
export declare function isArray<T>(value: T | readonly T[]): value is readonly T[];
|
|
87
94
|
/**
|
|
88
95
|
* Checks if the provided value is an array.
|
|
89
96
|
*
|
|
90
|
-
* @
|
|
97
|
+
* @typeParam T - type of the value to be checked.
|
|
91
98
|
* @param value - The value to be checked.
|
|
92
|
-
* @
|
|
99
|
+
* @returns True if the value is an array, otherwise false.
|
|
93
100
|
*/
|
|
94
101
|
export declare function isArray<T>(value: T | T[]): value is T[];
|
|
95
102
|
/**
|
|
96
103
|
* Checks if the given value is an array.
|
|
97
104
|
*
|
|
98
105
|
* @param value - The value to be checked.
|
|
99
|
-
* @
|
|
106
|
+
* @returns Returns true if the value is an array, otherwise false.
|
|
100
107
|
*/
|
|
101
108
|
export declare function isArray(value: unknown): value is unknown[];
|
|
102
109
|
/**
|
|
103
110
|
* Determines if the provided value is of type Function.
|
|
104
111
|
*
|
|
105
112
|
* @param value - The value to be checked.
|
|
106
|
-
* @
|
|
113
|
+
* @returns True if the value is a function; otherwise, false.
|
|
107
114
|
*/
|
|
108
|
-
export declare function isFunction(value: unknown): value is
|
|
115
|
+
export declare function isFunction<TFn extends CallableFunction = CallableFunction>(value: unknown): value is TFn;
|
|
109
116
|
/**
|
|
110
117
|
* Determines if the provided value is a string.
|
|
111
118
|
*
|
|
112
119
|
* @param value - The value to check.
|
|
113
|
-
* @
|
|
120
|
+
* @returns True if the value is a string, otherwise false.
|
|
114
121
|
*/
|
|
115
122
|
export declare function isString(value: unknown): value is string;
|
|
116
123
|
/**
|
|
117
124
|
* Checks if the provided value is a number and not NaN.
|
|
118
125
|
*
|
|
119
126
|
* @param value - The value to be checked.
|
|
120
|
-
* @
|
|
127
|
+
* @returns Returns true if the value is a number and not NaN, otherwise false.
|
|
121
128
|
*/
|
|
122
129
|
export declare function isNumber(value: unknown): value is number;
|
|
123
130
|
/**
|
|
124
131
|
* Checks if the given value is of type boolean.
|
|
125
132
|
*
|
|
126
133
|
* @param value - The value to check.
|
|
127
|
-
* @
|
|
134
|
+
* @returns A boolean indicating whether the value is a boolean or not.
|
|
128
135
|
*/
|
|
129
136
|
export declare function isBoolean(value: unknown): value is boolean;
|
|
130
|
-
export declare function hasProp(source: Nil, prop: PropertyKey, test?: (propValue: unknown) => boolean): false;
|
|
131
137
|
/**
|
|
132
|
-
*
|
|
138
|
+
* Determines if a given property exists on a source object and optionally
|
|
139
|
+
* evaluates it with a provided predicate function.
|
|
140
|
+
*
|
|
141
|
+
* @param source - The object to check for the property existence. Can be `null` or `undefined`.
|
|
142
|
+
* @param prop - The property key to check on the source.
|
|
143
|
+
* @param test - Optional predicate function used to evaluate the property's value.
|
|
144
|
+
* @returns Returns `false` since the source is `null` or `undefined` in this function signature.
|
|
145
|
+
*/
|
|
146
|
+
export declare function hasProp(source: Nil, prop: PropertyKey, test?: Predicate<unknown>): false;
|
|
147
|
+
/**
|
|
148
|
+
* Checks if a given property exists on the provided object.
|
|
149
|
+
*
|
|
150
|
+
* @param source - The object to check for the property.
|
|
151
|
+
* @param prop - The property key to check for existence on the object.
|
|
152
|
+
* @returns A boolean indicating whether the property exists on the object.
|
|
153
|
+
*/
|
|
154
|
+
export declare function hasProp<TKey extends PropertyKey>(source: NonNil, prop: TKey): source is Record<TKey, unknown>;
|
|
155
|
+
/**
|
|
156
|
+
* Checks if the given object has a specific property that meets the criteria
|
|
157
|
+
* defined by a type guard function.
|
|
158
|
+
*
|
|
159
|
+
* @param source - The object to check for the property.
|
|
160
|
+
* @param prop - The key of the property to check for in the object.
|
|
161
|
+
* @param test - A type guard function that tests the property value against a type.
|
|
162
|
+
* @returns Returns `true` if the object has the specified property and the value passes
|
|
163
|
+
* the type guard, otherwise returns `false`.
|
|
164
|
+
*/
|
|
165
|
+
export declare function hasProp<TValue extends NonNil, TKey extends keyof TValue, TNarrowed extends TValue[TKey]>(source: TValue, prop: TKey, test: TypeGuard<Required<TValue>[TKey], TNarrowed>): source is TValue & Record<TKey, TNarrowed>;
|
|
166
|
+
/**
|
|
167
|
+
* Checks if the given `source` object has a property specified by `prop` and verifies
|
|
168
|
+
* that the value of the property satisfies the condition defined by the `test` function.
|
|
169
|
+
*
|
|
170
|
+
* @param source - The object to be checked for the specified property.
|
|
171
|
+
* @param prop - The key of the property to check for existence in the `source`.
|
|
172
|
+
* @param test - A type guard function used to validate the type of the property's value.
|
|
173
|
+
* @returns A boolean indicating whether the `source` has the specified property and the value
|
|
174
|
+
* satisfies the type guard test.
|
|
175
|
+
*/
|
|
176
|
+
export declare function hasProp<TKey extends PropertyKey, TNarrowed>(source: unknown, prop: TKey, test: TypeGuard<unknown, TNarrowed>): source is Record<TKey, TNarrowed>;
|
|
177
|
+
/**
|
|
178
|
+
* Checks if a given property exists on a source object and satisfies a specified test condition.
|
|
179
|
+
*
|
|
180
|
+
* @param source - The source object to check for the property.
|
|
181
|
+
* @param prop - The property key to check in the source object.
|
|
182
|
+
* @param test - A predicate function to test the value of the specified property.
|
|
183
|
+
* @returns True if the property exists on the source object and the test predicate returns true; otherwise, false.
|
|
184
|
+
*/
|
|
185
|
+
export declare function hasProp<TValue extends NonNil, TKey extends keyof TValue>(source: TValue, prop: TKey, test: Predicate<Required<TValue>[TKey]>): boolean;
|
|
186
|
+
/**
|
|
187
|
+
* Checks if a given property exists on the specified source object and optionally validates it using a predicate function.
|
|
133
188
|
*
|
|
134
|
-
* @param source - The
|
|
135
|
-
* @param prop - The property to check for existence.
|
|
136
|
-
* @
|
|
189
|
+
* @param source - The object on which the property check is performed.
|
|
190
|
+
* @param prop - The property key to check for existence in the source object.
|
|
191
|
+
* @param test - An optional predicate function to validate the property value.
|
|
192
|
+
* @returns Returns true if the property exists on the source object and the predicate (if provided) evaluates to true; otherwise, false.
|
|
137
193
|
*/
|
|
138
|
-
export declare function hasProp
|
|
139
|
-
export declare function hasProp<Key extends PropertyKey>(source: unknown, prop: Key): source is Record<Key, unknown>;
|
|
140
|
-
export declare function hasProp<T, Narrowed extends T, Key extends PropertyKey>(source: Record<PropertyKey, T>, prop: Key, test: (propValue: T) => propValue is Narrowed): source is Record<Key, Narrowed>;
|
|
141
|
-
export declare function hasProp<T, Narrowed, Key extends PropertyKey>(source: Record<PropertyKey, T>, prop: Key, test: (propValue: unknown) => propValue is Narrowed): source is Record<Key, T & Narrowed>;
|
|
142
|
-
export declare function hasProp<Key extends PropertyKey, T>(source: unknown, prop: Key, test: (propValue: unknown) => propValue is T): source is {
|
|
143
|
-
[K in Key]: T;
|
|
144
|
-
};
|
|
194
|
+
export declare function hasProp(source: unknown, prop: PropertyKey, test?: Predicate<unknown>): boolean;
|
|
145
195
|
//# sourceMappingURL=guards.d.ts.map
|
package/dist/guards.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Checks if the provided value is `undefined`.
|
|
3
3
|
*
|
|
4
4
|
* @param value - The value to check for `undefined`.
|
|
5
|
-
* @
|
|
5
|
+
* @returns `true` if the value is `undefined`, otherwise `false`.
|
|
6
6
|
*/
|
|
7
7
|
export function isUndef(value) {
|
|
8
8
|
return value === undefined;
|
|
@@ -11,7 +11,7 @@ export function isUndef(value) {
|
|
|
11
11
|
* Checks if a given value is defined (not `undefined`).
|
|
12
12
|
*
|
|
13
13
|
* @param value - The value to be checked.
|
|
14
|
-
* @
|
|
14
|
+
* @returns Returns true if the value is defined, otherwise false.
|
|
15
15
|
*/
|
|
16
16
|
export function isDef(value) {
|
|
17
17
|
return value !== undefined;
|
|
@@ -20,7 +20,7 @@ export function isDef(value) {
|
|
|
20
20
|
* Checks if the provided value is `null` or `undefined`.
|
|
21
21
|
*
|
|
22
22
|
* @param value - The value to be checked.
|
|
23
|
-
* @
|
|
23
|
+
* @returns Returns `true` if the value is `null` or `undefined`, otherwise `false`.
|
|
24
24
|
*/
|
|
25
25
|
export function isNil(value) {
|
|
26
26
|
return value == null;
|
|
@@ -32,7 +32,7 @@ export function isNotNil(value) {
|
|
|
32
32
|
* Checks if the provided value is strictly equal to true.
|
|
33
33
|
*
|
|
34
34
|
* @param value - The value to check.
|
|
35
|
-
* @
|
|
35
|
+
* @returns Returns true if the value is strictly true, otherwise false.
|
|
36
36
|
*/
|
|
37
37
|
export function isTrue(value) {
|
|
38
38
|
return value === true;
|
|
@@ -41,7 +41,7 @@ export function isTrue(value) {
|
|
|
41
41
|
* Determines if the provided value is strictly `false`.
|
|
42
42
|
*
|
|
43
43
|
* @param value - The value to be checked.
|
|
44
|
-
* @
|
|
44
|
+
* @returns Returns `true` if the value is `false`, otherwise returns `false`.
|
|
45
45
|
*/
|
|
46
46
|
export function isFalse(value) {
|
|
47
47
|
return value === false;
|
|
@@ -50,7 +50,7 @@ export function isFalse(value) {
|
|
|
50
50
|
* Determines if the given value is truly, i.e., converts to true when used in a boolean context.
|
|
51
51
|
*
|
|
52
52
|
* @param value - The value to be tested for truthiness.
|
|
53
|
-
* @
|
|
53
|
+
* @returns Returns true if the value is truthy, false otherwise.
|
|
54
54
|
*/
|
|
55
55
|
export function isTruly(value) {
|
|
56
56
|
return Boolean(value);
|
|
@@ -59,8 +59,8 @@ export function isTruly(value) {
|
|
|
59
59
|
* Determines if a given value is falsy.
|
|
60
60
|
* A value is considered falsy if it evaluates to false when coerced to a boolean.
|
|
61
61
|
*
|
|
62
|
-
* @param value The value to be tested.
|
|
63
|
-
* @
|
|
62
|
+
* @param value - The value to be tested.
|
|
63
|
+
* @returns True if the value is falsy, otherwise false.
|
|
64
64
|
*/
|
|
65
65
|
export function isFalsy(value) {
|
|
66
66
|
return !isTruly(value);
|
|
@@ -69,7 +69,7 @@ export function isFalsy(value) {
|
|
|
69
69
|
* Checks if the given value is an object.
|
|
70
70
|
*
|
|
71
71
|
* @param value - The value to check.
|
|
72
|
-
* @
|
|
72
|
+
* @returns True if the value is an object, false otherwise.
|
|
73
73
|
*/
|
|
74
74
|
export function isObject(value) {
|
|
75
75
|
return value != null && typeof value === 'object';
|
|
@@ -83,7 +83,7 @@ export function isObject(value) {
|
|
|
83
83
|
*
|
|
84
84
|
* @param value - The value to check.
|
|
85
85
|
* @param allowEmpty - Determines whether empty records are allowed.
|
|
86
|
-
* @
|
|
86
|
+
* @returns A boolean indicating whether the value is a record.
|
|
87
87
|
*/
|
|
88
88
|
export function isRecord(value, allowEmpty = false) {
|
|
89
89
|
return isObject(value) && (allowEmpty || Object.keys(value).length > 0);
|
|
@@ -95,8 +95,9 @@ export function isArray(value) {
|
|
|
95
95
|
* Determines if the provided value is of type Function.
|
|
96
96
|
*
|
|
97
97
|
* @param value - The value to be checked.
|
|
98
|
-
* @
|
|
98
|
+
* @returns True if the value is a function; otherwise, false.
|
|
99
99
|
*/
|
|
100
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-parameters
|
|
100
101
|
export function isFunction(value) {
|
|
101
102
|
return typeof value === 'function';
|
|
102
103
|
}
|
|
@@ -104,7 +105,7 @@ export function isFunction(value) {
|
|
|
104
105
|
* Determines if the provided value is a string.
|
|
105
106
|
*
|
|
106
107
|
* @param value - The value to check.
|
|
107
|
-
* @
|
|
108
|
+
* @returns True if the value is a string, otherwise false.
|
|
108
109
|
*/
|
|
109
110
|
export function isString(value) {
|
|
110
111
|
return typeof value === 'string';
|
|
@@ -113,7 +114,7 @@ export function isString(value) {
|
|
|
113
114
|
* Checks if the provided value is a number and not NaN.
|
|
114
115
|
*
|
|
115
116
|
* @param value - The value to be checked.
|
|
116
|
-
* @
|
|
117
|
+
* @returns Returns true if the value is a number and not NaN, otherwise false.
|
|
117
118
|
*/
|
|
118
119
|
export function isNumber(value) {
|
|
119
120
|
return typeof value === 'number' && !Number.isNaN(value);
|
|
@@ -122,7 +123,7 @@ export function isNumber(value) {
|
|
|
122
123
|
* Checks if the given value is of type boolean.
|
|
123
124
|
*
|
|
124
125
|
* @param value - The value to check.
|
|
125
|
-
* @
|
|
126
|
+
* @returns A boolean indicating whether the value is a boolean or not.
|
|
126
127
|
*/
|
|
127
128
|
export function isBoolean(value) {
|
|
128
129
|
return typeof value === 'boolean';
|
package/dist/logical.d.ts
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { FValueFalse, FValueTrue, TestFn } from './types.js';
|
|
2
2
|
import { isNotNil, isTrue } from '#guards';
|
|
3
3
|
/**
|
|
4
4
|
* Functional If
|
|
5
5
|
*
|
|
6
|
-
* Evaluates a value with a given test and returns a value (or executes a function and returns
|
|
7
|
-
*
|
|
8
|
-
* @typeParam TrueType - The type of the value or the return type of the function if the test's result is true.
|
|
9
|
-
* @typeParam FalseType - The type of the value or the return type of the function if the test's result is true, defaults to undefined.
|
|
6
|
+
* Evaluates a value with a given test and returns a value (or executes a function and returns its result) based on the result.
|
|
10
7
|
*
|
|
8
|
+
* @typeParam TValue - The type of the value to be tested.
|
|
9
|
+
* @typeParam TTestFn - The type of the test function that takes the value as a parameter and returns a boolean.
|
|
10
|
+
* @typeParam TTrue - The type of the value or the return type of the function if the test's result is true.
|
|
11
|
+
* @typeParam TFalse - The type of the value or the return type of the function if the test's result is true.
|
|
11
12
|
* @param value - The value to test against the provided test function.
|
|
12
13
|
* @param test - A function to evaluate the provided value.
|
|
13
14
|
* @param onTrue - A function or value to execute or return if the test evaluates to true.
|
|
14
15
|
* @param onFalse - A function or value to execute or return if the test evaluates to false.
|
|
15
|
-
* @
|
|
16
|
-
*
|
|
16
|
+
* @returns The value or the result of the function based on the test result
|
|
17
17
|
* @example
|
|
18
18
|
* ```ts
|
|
19
19
|
* const result1 = fif(10, (n) => n > 5, 'Greater', 'Smaller');
|
|
@@ -23,29 +23,31 @@ import { isNotNil, isTrue } from '#guards';
|
|
|
23
23
|
* // result2 is 'Below 3'
|
|
24
24
|
* ```
|
|
25
25
|
*/
|
|
26
|
-
export declare function fif<
|
|
26
|
+
export declare function fif<TValue, TTestFn extends TestFn<TValue>, TTrue, TFalse = undefined>(value: TValue, test: TTestFn, onTrue: FValueTrue<TValue, TTestFn, TTrue>, onFalse?: FValueFalse<TValue, TTestFn, TFalse>): TTrue | TFalse;
|
|
27
27
|
/**
|
|
28
28
|
* Functional If (Static)
|
|
29
29
|
*
|
|
30
30
|
* Evaluates a condition and returns a value or executes a function based on the result.
|
|
31
31
|
*
|
|
32
|
-
* @typeParam
|
|
33
|
-
* @typeParam
|
|
34
|
-
*
|
|
32
|
+
* @typeParam TTrue - The type of the value or the return type of the function if the condition is true.
|
|
33
|
+
* @typeParam TFalse - The type of the value or the return type of the function if condition is true.
|
|
35
34
|
* @param condition - The condition to evaluate.
|
|
36
35
|
* @param onTrue - The value or function to return/execute if the condition is true. If it's a function, it receives `true` as an argument.
|
|
37
36
|
* @param onFalse - The value or function to return/execute if the condition is false. If it's a function, it receives `false` as an argument.
|
|
38
|
-
* @
|
|
37
|
+
* @returns The value or the result of the function based on the evaluated condition.
|
|
39
38
|
*/
|
|
40
|
-
export declare function fifs<
|
|
39
|
+
export declare function fifs<TTrue, TFalse = undefined>(condition: boolean, onTrue: FValueTrue<boolean, typeof isTrue, TTrue>, onFalse?: FValueFalse<boolean, typeof isTrue, TFalse>): TTrue | TFalse;
|
|
41
40
|
/**
|
|
42
|
-
* Ensures that the provided value is not null or undefined
|
|
41
|
+
* Ensures that the provided value is not null or undefined
|
|
43
42
|
* and returns a value (or executes a function and returns it's result) based on the result.
|
|
44
43
|
*
|
|
44
|
+
* @typeParam TValue - The type of the value to be checked.
|
|
45
|
+
* @typeParam TTrue - The type of the value or the return type of the function if the value is not null or undefined.
|
|
46
|
+
* @typeParam TFalse - The type of the value or the return type of the function if the value is null or undefined.
|
|
45
47
|
* @param value - The value to be checked.
|
|
46
48
|
* @param onTrue - Callback function to be executed if the value is not null or undefined.
|
|
47
49
|
* @param onFalse - (Optional) Callback function to be executed if the value is null or undefined.
|
|
48
|
-
* @
|
|
50
|
+
* @returns The result of the appropriate callback function based on the evaluation of the value.
|
|
49
51
|
*/
|
|
50
|
-
export declare function sure<
|
|
52
|
+
export declare function sure<TValue, TTrue, TFalse = undefined>(value: TValue, onTrue: FValueTrue<TValue, typeof isNotNil, TTrue>, onFalse?: FValueFalse<TValue, typeof isNotNil, TFalse>): TTrue | TFalse;
|
|
51
53
|
//# sourceMappingURL=logical.d.ts.map
|
package/dist/logical.js
CHANGED
|
@@ -2,17 +2,17 @@ import { isFunction, isNotNil, isTrue } from '#guards';
|
|
|
2
2
|
/**
|
|
3
3
|
* Functional If
|
|
4
4
|
*
|
|
5
|
-
* Evaluates a value with a given test and returns a value (or executes a function and returns
|
|
6
|
-
*
|
|
7
|
-
* @typeParam TrueType - The type of the value or the return type of the function if the test's result is true.
|
|
8
|
-
* @typeParam FalseType - The type of the value or the return type of the function if the test's result is true, defaults to undefined.
|
|
5
|
+
* Evaluates a value with a given test and returns a value (or executes a function and returns its result) based on the result.
|
|
9
6
|
*
|
|
7
|
+
* @typeParam TValue - The type of the value to be tested.
|
|
8
|
+
* @typeParam TTestFn - The type of the test function that takes the value as a parameter and returns a boolean.
|
|
9
|
+
* @typeParam TTrue - The type of the value or the return type of the function if the test's result is true.
|
|
10
|
+
* @typeParam TFalse - The type of the value or the return type of the function if the test's result is true.
|
|
10
11
|
* @param value - The value to test against the provided test function.
|
|
11
12
|
* @param test - A function to evaluate the provided value.
|
|
12
13
|
* @param onTrue - A function or value to execute or return if the test evaluates to true.
|
|
13
14
|
* @param onFalse - A function or value to execute or return if the test evaluates to false.
|
|
14
|
-
* @
|
|
15
|
-
*
|
|
15
|
+
* @returns The value or the result of the function based on the test result
|
|
16
16
|
* @example
|
|
17
17
|
* ```ts
|
|
18
18
|
* const result1 = fif(10, (n) => n > 5, 'Greater', 'Smaller');
|
|
@@ -38,25 +38,27 @@ export function fif(value, test, onTrue, onFalse) {
|
|
|
38
38
|
*
|
|
39
39
|
* Evaluates a condition and returns a value or executes a function based on the result.
|
|
40
40
|
*
|
|
41
|
-
* @typeParam
|
|
42
|
-
* @typeParam
|
|
43
|
-
*
|
|
41
|
+
* @typeParam TTrue - The type of the value or the return type of the function if the condition is true.
|
|
42
|
+
* @typeParam TFalse - The type of the value or the return type of the function if condition is true.
|
|
44
43
|
* @param condition - The condition to evaluate.
|
|
45
44
|
* @param onTrue - The value or function to return/execute if the condition is true. If it's a function, it receives `true` as an argument.
|
|
46
45
|
* @param onFalse - The value or function to return/execute if the condition is false. If it's a function, it receives `false` as an argument.
|
|
47
|
-
* @
|
|
46
|
+
* @returns The value or the result of the function based on the evaluated condition.
|
|
48
47
|
*/
|
|
49
48
|
export function fifs(condition, onTrue, onFalse) {
|
|
50
49
|
return fif(condition, isTrue, onTrue, onFalse);
|
|
51
50
|
}
|
|
52
51
|
/**
|
|
53
|
-
* Ensures that the provided value is not null or undefined
|
|
52
|
+
* Ensures that the provided value is not null or undefined
|
|
54
53
|
* and returns a value (or executes a function and returns it's result) based on the result.
|
|
55
54
|
*
|
|
55
|
+
* @typeParam TValue - The type of the value to be checked.
|
|
56
|
+
* @typeParam TTrue - The type of the value or the return type of the function if the value is not null or undefined.
|
|
57
|
+
* @typeParam TFalse - The type of the value or the return type of the function if the value is null or undefined.
|
|
56
58
|
* @param value - The value to be checked.
|
|
57
59
|
* @param onTrue - Callback function to be executed if the value is not null or undefined.
|
|
58
60
|
* @param onFalse - (Optional) Callback function to be executed if the value is null or undefined.
|
|
59
|
-
* @
|
|
61
|
+
* @returns The result of the appropriate callback function based on the evaluation of the value.
|
|
60
62
|
*/
|
|
61
63
|
export function sure(value, onTrue, onFalse) {
|
|
62
64
|
return fif(value, isNotNil, onTrue, onFalse);
|
package/dist/random.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { RNG } from './types.js';
|
|
2
|
+
export type { RNG };
|
|
3
|
+
/**
|
|
4
|
+
* Generates a hash value for the given string using the FNV-1a hashing algorithm.
|
|
5
|
+
*
|
|
6
|
+
* @param seed - The input string to hash.
|
|
7
|
+
* @returns The 32-bit unsigned integer hash of the input string.
|
|
8
|
+
*/
|
|
9
|
+
export declare function fnv1aHash(seed: string): number;
|
|
10
|
+
/**
|
|
11
|
+
* Generates a pseudo-random number generator (PRNG) based on the Mulberry32 algorithm.
|
|
12
|
+
*
|
|
13
|
+
* @param seed - The seed for the random number generator. It can be a string or a numeric value.
|
|
14
|
+
* If a string is provided, it will be hashed into a numeric value before use.
|
|
15
|
+
* @returns An object containing the seed and a `next` function that produces the next pseudo-random
|
|
16
|
+
* number in the sequence as a floating-point value in the range [0, 1).
|
|
17
|
+
*/
|
|
18
|
+
export declare function mulberry32(seed: string | number): RNG;
|
|
19
|
+
//# sourceMappingURL=random.d.ts.map
|
package/dist/random.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { isString } from '#guards';
|
|
2
|
+
/**
|
|
3
|
+
* Generates a hash value for the given string using the FNV-1a hashing algorithm.
|
|
4
|
+
*
|
|
5
|
+
* @param seed - The input string to hash.
|
|
6
|
+
* @returns The 32-bit unsigned integer hash of the input string.
|
|
7
|
+
*/
|
|
8
|
+
export function fnv1aHash(seed) {
|
|
9
|
+
let h = 2166136261 >>> 0;
|
|
10
|
+
for (let i = 0; i < seed.length; i += 1) {
|
|
11
|
+
h ^= seed.charCodeAt(i);
|
|
12
|
+
h = Math.imul(h, 16777619);
|
|
13
|
+
}
|
|
14
|
+
return h >>> 0;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Generates a pseudo-random number generator (PRNG) based on the Mulberry32 algorithm.
|
|
18
|
+
*
|
|
19
|
+
* @param seed - The seed for the random number generator. It can be a string or a numeric value.
|
|
20
|
+
* If a string is provided, it will be hashed into a numeric value before use.
|
|
21
|
+
* @returns An object containing the seed and a `next` function that produces the next pseudo-random
|
|
22
|
+
* number in the sequence as a floating-point value in the range [0, 1).
|
|
23
|
+
*/
|
|
24
|
+
export function mulberry32(seed) {
|
|
25
|
+
seed = isString(seed) ? fnv1aHash(seed) : seed;
|
|
26
|
+
let t = seed >>> 0;
|
|
27
|
+
const next = () => {
|
|
28
|
+
t += 0x6d2b79f5;
|
|
29
|
+
let x = Math.imul(t ^ (t >>> 15), 1 | t);
|
|
30
|
+
x ^= x + Math.imul(x ^ (x >>> 7), 61 | x);
|
|
31
|
+
return ((x ^ (x >>> 14)) >>> 0) / 4294967296;
|
|
32
|
+
};
|
|
33
|
+
return { seed, next };
|
|
34
|
+
}
|
|
35
|
+
//# sourceMappingURL=random.js.map
|
package/dist/string.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { CamelCase, DelimiterCase, KebabCase, PascalCase, SnakeCase } from 'type-fest';
|
|
2
|
-
import type { Nil } from '@budsbox/lib-types';
|
|
2
|
+
import type { Maybe, Nil, Undef } from '@budsbox/lib-types';
|
|
3
3
|
import type { ParsedPackageName } from './types.js';
|
|
4
4
|
export type { ParsedPackageName };
|
|
5
5
|
/**
|
|
@@ -25,6 +25,64 @@ export declare function serializePackageName(parsedPackageName: Readonly<ParsedP
|
|
|
25
25
|
* @returns The string representation of the package name if valid, or an empty string if `allowNil` is true and the input is Nil.
|
|
26
26
|
*/
|
|
27
27
|
export declare function serializePackageName(parsedPackageName: Readonly<ParsedPackageName> | Nil, allowNil: true): string;
|
|
28
|
+
/**
|
|
29
|
+
* Resolves the package name based on the provided identifier and options.
|
|
30
|
+
* Can return either a string representing the package name or a parsed package name object.
|
|
31
|
+
*
|
|
32
|
+
* @param ident - The package identifier, either as a string or a parsed package name object.
|
|
33
|
+
* @param options - Optional settings to modify the resolution behavior.
|
|
34
|
+
* Includes an optional `baseScope` to use as a default scope and a `parsed` flag
|
|
35
|
+
* to indicate whether the result should be returned as a parsed package name object.
|
|
36
|
+
* @returns The resolved package name as a string if `parsed` is false or not specified,
|
|
37
|
+
* or as a `ParsedPackageName` object if `parsed` is true.
|
|
38
|
+
*/
|
|
39
|
+
export declare function resolvePackageName<TParsed extends boolean = false>(ident: string | Readonly<ParsedPackageName>, options?: Readonly<{
|
|
40
|
+
baseScope?: Undef<string>;
|
|
41
|
+
parsed?: TParsed;
|
|
42
|
+
}>): TParsed extends true ? ParsedPackageName : string;
|
|
43
|
+
/**
|
|
44
|
+
* Options for formatting the package name
|
|
45
|
+
*/
|
|
46
|
+
export interface PackageNameFormatOptions {
|
|
47
|
+
/**
|
|
48
|
+
* The root identifier for the package.
|
|
49
|
+
*/
|
|
50
|
+
root?: Maybe<string>;
|
|
51
|
+
/**
|
|
52
|
+
* The parent package name.
|
|
53
|
+
*/
|
|
54
|
+
parent?: Maybe<string>;
|
|
55
|
+
/**
|
|
56
|
+
* The path to the package, relative to its parent.
|
|
57
|
+
*/
|
|
58
|
+
relCwd?: Undef<string>;
|
|
59
|
+
/**
|
|
60
|
+
* The delimiter used to separate path chunks.
|
|
61
|
+
*/
|
|
62
|
+
pathDelimiter?: Undef<string>;
|
|
63
|
+
/**
|
|
64
|
+
* The delimiter used to separate parents name from the base name of the package.
|
|
65
|
+
*/
|
|
66
|
+
nameDelimiter?: Undef<string>;
|
|
67
|
+
/**
|
|
68
|
+
* Array of path chunks to be excluded when constructing the path.
|
|
69
|
+
*/
|
|
70
|
+
excludePathChunks?: Undef<readonly string[]>;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Formats a package name by combining the base name with processed parent and directory path details.
|
|
74
|
+
*
|
|
75
|
+
* @param base - The base name of the package, without the scope or any prefixes.
|
|
76
|
+
* @param options - An object providing configuration options for formatting the package name.
|
|
77
|
+
* @param options.root - The root identifier for the package. Defaults to `null`.
|
|
78
|
+
* @param options.parent - The parent package name. Defaults to the value of `root`.
|
|
79
|
+
* @param options.relCwd - The path to the package, relative to its parent. Defaults to an empty string.
|
|
80
|
+
* @param options.pathDelimiter - The delimiter used to separate path chunks. Defaults to `'-'`.
|
|
81
|
+
* @param options.nameDelimiter - The delimiter used to separate parents name from the base name of the package. Defaults to `'_'`.
|
|
82
|
+
* @param options.excludePathChunks - Array of path chunks to be excluded when constructing the path. Defaults to `['packages']`.
|
|
83
|
+
* @returns The formatted package name as a string.
|
|
84
|
+
*/
|
|
85
|
+
export declare function formatPackageName(base: string, { root, parent, relCwd, pathDelimiter, nameDelimiter, excludePathChunks, }?: Readonly<PackageNameFormatOptions>): string;
|
|
28
86
|
/**
|
|
29
87
|
* Converts the provided value to a string representation suitable for debugging purposes.
|
|
30
88
|
*
|
|
@@ -51,6 +109,16 @@ export declare function clampWS(str: string): string;
|
|
|
51
109
|
* @returns The combined path as a single string, with proper slash formatting.
|
|
52
110
|
*/
|
|
53
111
|
export declare function joinPath(...parts: ReadonlyArray<string | number | boolean | null | undefined>): string;
|
|
112
|
+
/**
|
|
113
|
+
* Splits a given Unix-like path into an array of its components based on the "/" delimiter.
|
|
114
|
+
* Optionally keeps empty chunks in the result.
|
|
115
|
+
*
|
|
116
|
+
* @param path - The file path to be split into an array of components.
|
|
117
|
+
* @param keepEmptyChunks - A boolean indicating whether empty strings (resulting from consecutive delimiters)
|
|
118
|
+
* should be preserved in the output array. Defaults to `false`.
|
|
119
|
+
* @returns An array of strings representing the components of the path.
|
|
120
|
+
*/
|
|
121
|
+
export declare function splitPath(path: string, keepEmptyChunks?: boolean): string[];
|
|
54
122
|
/**
|
|
55
123
|
* Transforms a given string into camelCase format.
|
|
56
124
|
*
|
package/dist/string.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { isNil, isNotNil } from './guards.js';
|
|
1
|
+
import { isNil, isNotNil, isString } from './guards.js';
|
|
2
2
|
/**
|
|
3
3
|
* Parses a package name string to extract its scope and name.
|
|
4
4
|
*
|
|
@@ -20,6 +20,43 @@ export function serializePackageName(parsedPackageName, allowNil = false) {
|
|
|
20
20
|
const { scope, name } = parsedPackageName ?? { scope: null, name: '' };
|
|
21
21
|
return joinPath(scope?.replace(/^@?/, '@'), name);
|
|
22
22
|
}
|
|
23
|
+
export function resolvePackageName(ident, { baseScope, parsed = false, } = {}) {
|
|
24
|
+
const { scope, name } = isString(ident) ? parsePackageName(ident) : ident;
|
|
25
|
+
const parsedIdent = {
|
|
26
|
+
scope: scope ?? baseScope ?? null,
|
|
27
|
+
name,
|
|
28
|
+
};
|
|
29
|
+
return parsed ? parsedIdent : serializePackageName(parsedIdent);
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Formats a package name by combining the base name with processed parent and directory path details.
|
|
33
|
+
*
|
|
34
|
+
* @param base - The base name of the package, without the scope or any prefixes.
|
|
35
|
+
* @param options - An object providing configuration options for formatting the package name.
|
|
36
|
+
* @param options.root - The root identifier for the package. Defaults to `null`.
|
|
37
|
+
* @param options.parent - The parent package name. Defaults to the value of `root`.
|
|
38
|
+
* @param options.relCwd - The path to the package, relative to its parent. Defaults to an empty string.
|
|
39
|
+
* @param options.pathDelimiter - The delimiter used to separate path chunks. Defaults to `'-'`.
|
|
40
|
+
* @param options.nameDelimiter - The delimiter used to separate parents name from the base name of the package. Defaults to `'_'`.
|
|
41
|
+
* @param options.excludePathChunks - Array of path chunks to be excluded when constructing the path. Defaults to `['packages']`.
|
|
42
|
+
* @returns The formatted package name as a string.
|
|
43
|
+
*/
|
|
44
|
+
export function formatPackageName(base, { root = null, parent = root, relCwd = '', pathDelimiter = '-', nameDelimiter = '_', excludePathChunks = ['packages'], } = {}) {
|
|
45
|
+
const topLevel = parent === root;
|
|
46
|
+
const exclude = new Set(excludePathChunks);
|
|
47
|
+
const pathChunks = splitPath(relCwd).filter((chunk) => !exclude.has(chunk));
|
|
48
|
+
if (pathChunks.at(-1) === base) {
|
|
49
|
+
pathChunks.pop();
|
|
50
|
+
}
|
|
51
|
+
const { scope, name: parentName } = parsePackageName(parent ?? '');
|
|
52
|
+
return serializePackageName({
|
|
53
|
+
scope,
|
|
54
|
+
name: [
|
|
55
|
+
...(topLevel ? [] : [parentName]),
|
|
56
|
+
[...pathChunks, base].join(pathDelimiter),
|
|
57
|
+
].join(nameDelimiter),
|
|
58
|
+
});
|
|
59
|
+
}
|
|
23
60
|
/**
|
|
24
61
|
* Converts the provided value to a string representation suitable for debugging purposes.
|
|
25
62
|
*
|
|
@@ -61,6 +98,19 @@ export function joinPath(...parts) {
|
|
|
61
98
|
String(part)
|
|
62
99
|
: `${acc.replace(/\/+$/, '')}/${String(part).replace(/^\/+/, '')}`, '');
|
|
63
100
|
}
|
|
101
|
+
/**
|
|
102
|
+
* Splits a given Unix-like path into an array of its components based on the "/" delimiter.
|
|
103
|
+
* Optionally keeps empty chunks in the result.
|
|
104
|
+
*
|
|
105
|
+
* @param path - The file path to be split into an array of components.
|
|
106
|
+
* @param keepEmptyChunks - A boolean indicating whether empty strings (resulting from consecutive delimiters)
|
|
107
|
+
* should be preserved in the output array. Defaults to `false`.
|
|
108
|
+
* @returns An array of strings representing the components of the path.
|
|
109
|
+
*/
|
|
110
|
+
export function splitPath(path, keepEmptyChunks = false) {
|
|
111
|
+
const splitted = path.split(/\/+/);
|
|
112
|
+
return keepEmptyChunks ? splitted : (splitted.filter((chunk) => chunk.length > 0));
|
|
113
|
+
}
|
|
64
114
|
export function camelCase(name) {
|
|
65
115
|
return clampWS(name).replace(/[-_\s]+([^-_\s])/g, (_, suffix) => suffix.toUpperCase());
|
|
66
116
|
}
|
package/dist/types.d.ts
CHANGED
|
@@ -2,18 +2,104 @@
|
|
|
2
2
|
* The `FValue` type represents a union type that can be either a value of type `T` or a function
|
|
3
3
|
* that takes a parameter of type `A` and returns a value of type `T`.
|
|
4
4
|
*
|
|
5
|
-
* @
|
|
6
|
-
* @
|
|
5
|
+
* @typeParam A - The type of the parameter for the function.
|
|
6
|
+
* @typeParam T - The type of the value or the return type of the function.
|
|
7
7
|
*/
|
|
8
8
|
export type FValue<A, T> = T | ((value: A) => T);
|
|
9
|
+
/**
|
|
10
|
+
* A type representing a type guard function that determines if a given value of type `T`
|
|
11
|
+
* conforms to a more specific type `V`, where `V` is a subtype of `T`.
|
|
12
|
+
*
|
|
13
|
+
* This function should be implemented to return `true` if the provided value matches
|
|
14
|
+
* the more specific type `V`, and `false` otherwise. By using a type guard, TypeScript can
|
|
15
|
+
* narrow the type of variable within the scope of a condition.
|
|
16
|
+
*
|
|
17
|
+
* @typeParam T - The broader type against which the value will be checked.
|
|
18
|
+
* @typeParam V - The narrowed type that `T` is tested against. V must extend T.
|
|
19
|
+
* @param value - The value of type `T` to be tested against the narrowed type `V`.
|
|
20
|
+
* @returns A boolean value indicating whether the input value is of type `V`.
|
|
21
|
+
*/
|
|
9
22
|
export type TypeGuard<T, V extends T> = (value: T) => value is V;
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
23
|
+
/**
|
|
24
|
+
* A type definition for a function that performs a boolean test on a given value.
|
|
25
|
+
*
|
|
26
|
+
* This generic type accepts a parameter `T`, which represents the type of the input
|
|
27
|
+
* that the function will evaluate. The function returns a boolean indicating the
|
|
28
|
+
* result of the test.
|
|
29
|
+
*
|
|
30
|
+
* @typeParam T - The type of the input value that the function will evaluate.
|
|
31
|
+
* @param value - The input value to test.
|
|
32
|
+
* @returns A boolean indicating the result of the test.
|
|
33
|
+
*/
|
|
34
|
+
export type Predicate<T> = (value: T) => boolean;
|
|
35
|
+
/**
|
|
36
|
+
* Represents a type that can either be a `TypeGuard` or a `TestFn`.
|
|
37
|
+
*
|
|
38
|
+
* `TestParam` is a utility type that allows defining parameters used in testing or
|
|
39
|
+
* type-checking procedures. It accommodates both type guard functions and test
|
|
40
|
+
* functions for more comprehensive type handling.
|
|
41
|
+
*
|
|
42
|
+
* @typeParam TValue - The base type to be tested or narrowed. Defaults to `any`.
|
|
43
|
+
* @typeParam TNarrowed - A subtype of `ValueType` that represents the narrowed type.
|
|
44
|
+
* Defaults to `ValueType`.
|
|
45
|
+
*/
|
|
46
|
+
export type TestFn<TValue = any, TNarrowed extends TValue = TValue> = Predicate<TValue> | TypeGuard<TValue, TNarrowed>;
|
|
47
|
+
/**
|
|
48
|
+
* Represents a type that evaluates to a specific result based on the provided test conditions.
|
|
49
|
+
*
|
|
50
|
+
* @typeParam TValue - The type of value being tested.
|
|
51
|
+
* @typeParam TTestFn - The type of test being applied. This could be a type guard or a test function.
|
|
52
|
+
* @typeParam TBranch - A boolean that determines the branch type. If true, the resulting type will
|
|
53
|
+
* focus on the narrowed type; otherwise, it excludes the narrowed type.
|
|
54
|
+
*
|
|
55
|
+
* The type resolves based on the following rules:
|
|
56
|
+
* 1. If `TestType` is a type guard:
|
|
57
|
+
* - If `BranchType` is `true`, the resulting type includes only the portion of `ValueType` that
|
|
58
|
+
* conforms to the narrowed type.
|
|
59
|
+
* - If `BranchType` is `false`, the resulting type excludes the portion of `ValueType` that
|
|
60
|
+
* conforms to the narrowed type.
|
|
61
|
+
* 2. If `TestType` is a test function, the resulting type will be the original type inferred by
|
|
62
|
+
* the test function.
|
|
63
|
+
* 3. If neither condition applies, the resulting type will be `never`.
|
|
64
|
+
*/
|
|
65
|
+
export type TestFnResult<TValue, TTestFn extends TestFn, TBranch extends boolean> = TTestFn extends TypeGuard<unknown, infer TNarrowed> ? TBranch extends true ? Extract<TValue, TNarrowed> : Exclude<TValue, TNarrowed> : TTestFn extends Predicate<infer TOriginal> ? TOriginal : never;
|
|
66
|
+
/**
|
|
67
|
+
* A type definition that represents a resolved value when a certain test condition is true.
|
|
68
|
+
*
|
|
69
|
+
* @typeParam ValueType - Represents the type of the input value being tested.
|
|
70
|
+
* @typeParam TestType - Extends the `TestParam` type and defines the parameters for the test condition.
|
|
71
|
+
* @typeParam ReturnType - Specifies the type of the resulting value after the test condition is satisfied.
|
|
72
|
+
*
|
|
73
|
+
* This type combines the test result with a return type, capturing the transformed value
|
|
74
|
+
* or operation result when the test evaluates to `true`.
|
|
75
|
+
*/
|
|
76
|
+
export type FValueTrue<TValue, TTestFn extends TestFn, TResult> = FValue<TestFnResult<TValue, TTestFn, true>, TResult>;
|
|
77
|
+
/**
|
|
78
|
+
* Represents a type that maps a test result to a specific return type when the test evaluates to false.
|
|
79
|
+
*
|
|
80
|
+
* This type is a utility that processes the result of a test operation (`TestResultType`)
|
|
81
|
+
* when the provided test condition evaluates as false, associating it with a specific return type (`ReturnType`).
|
|
82
|
+
*
|
|
83
|
+
* @typeParam ValueType - The type of the primary value being tested.
|
|
84
|
+
* @typeParam TestType - A type extending `TestParam` representing the condition being tested.
|
|
85
|
+
* @typeParam ReturnType - The type of the result or output when the test condition evaluates to false.
|
|
86
|
+
*/
|
|
87
|
+
export type FValueFalse<TValue, TTestFn extends TestFn, TResult> = FValue<TestFnResult<TValue, TTestFn, false>, TResult>;
|
|
88
|
+
/**
|
|
89
|
+
* Represents a parsed Node.js (npm) package name split into its optional scope and the bare name.
|
|
90
|
+
*/
|
|
15
91
|
export interface ParsedPackageName {
|
|
92
|
+
/**
|
|
93
|
+
* The scope part of the package name (organization/user), with or without the leading "@" and trail "/".
|
|
94
|
+
* If the package is unscoped, this will be `null` or `undefined`.
|
|
95
|
+
*
|
|
96
|
+
* @example "scope" for "@scope/pkg", or "undefined" for "pkg"
|
|
97
|
+
*/
|
|
16
98
|
scope?: string | null;
|
|
99
|
+
/**
|
|
100
|
+
* The unscoped package name (the segment after the scope or the whole name if unscoped).
|
|
101
|
+
* For "@scope/pkg", this is "pkg"; for "pkg", this is "pkg".
|
|
102
|
+
*/
|
|
17
103
|
name: string;
|
|
18
104
|
}
|
|
19
105
|
//# sourceMappingURL=types.d.ts.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@budsbox/lib-es",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.2.0",
|
|
4
4
|
"homepage": "https://gitlab.com/budsbox/fe/seed",
|
|
5
5
|
"bugs": {
|
|
6
6
|
"url": "https://gitlab.com/budsbox/fe/seed/-/issues"
|
|
@@ -124,18 +124,18 @@
|
|
|
124
124
|
"prepack": "yarn p:ts:prepack"
|
|
125
125
|
},
|
|
126
126
|
"dependencies": {
|
|
127
|
-
"@budsbox/lib-types": "^1.
|
|
127
|
+
"@budsbox/lib-types": "^1.1.0",
|
|
128
|
+
"@types/node": "^22.15.2",
|
|
128
129
|
"tslib": "^2.8.1",
|
|
129
130
|
"type-fest": "^4.32.0"
|
|
130
131
|
},
|
|
131
132
|
"devDependencies": {
|
|
132
|
-
"@budsbox/eslint": "^1.0
|
|
133
|
-
"@budsbox/eslint_presets-lib": "^1.0.
|
|
134
|
-
"@budsbox/eslint_presets-tools": "^1.0.
|
|
135
|
-
"@budsbox/tsconfigs": "^4.
|
|
133
|
+
"@budsbox/eslint": "^1.2.0",
|
|
134
|
+
"@budsbox/eslint_presets-lib": "^1.0.3",
|
|
135
|
+
"@budsbox/eslint_presets-tools": "^1.0.3",
|
|
136
|
+
"@budsbox/tsconfigs": "^4.3.0",
|
|
136
137
|
"@eslint/js": "^9.26.0",
|
|
137
138
|
"@types/eslint": "^9.6.1",
|
|
138
|
-
"@types/node": "^22.15.2",
|
|
139
139
|
"@typescript-eslint/parser": "^8.32.1",
|
|
140
140
|
"eslint": "^9.26.0",
|
|
141
141
|
"eslint-config-prettier": "^10.1.5",
|