@zelgadis87/utils-core 5.2.4 → 5.2.6
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/CHANGELOG.md +15 -0
- package/dist/Optional.d.ts +2 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/utils/arrays.d.ts +8 -2
- package/dist/utils/operations.d.ts +16 -6
- package/esbuild/index.cjs +24 -17
- package/esbuild/index.cjs.map +4 -4
- package/esbuild/index.mjs +22 -15
- package/esbuild/index.mjs.map +4 -4
- package/package.json +1 -1
- package/src/Optional.ts +7 -0
- package/src/utils/arrays.ts +13 -15
- package/src/utils/operations.ts +15 -6
package/package.json
CHANGED
package/src/Optional.ts
CHANGED
|
@@ -153,6 +153,12 @@ export class Optional<T> implements TOptional<T> {
|
|
|
153
153
|
return this as TPresentOptional<T>;
|
|
154
154
|
}
|
|
155
155
|
|
|
156
|
+
public throwIfPresent( errorProducer: TFunction<T, Error> ) {
|
|
157
|
+
if ( this.isEmpty() )
|
|
158
|
+
return this as TEmptyOptional<T>;
|
|
159
|
+
throw errorProducer( this.get() );
|
|
160
|
+
}
|
|
161
|
+
|
|
156
162
|
public mapTo<R>( mapper: TFunction<T, R | null | undefined> ) {
|
|
157
163
|
return this.flatMapTo( t => Optional.ofNullable( mapper( t ) ) );
|
|
158
164
|
}
|
|
@@ -215,6 +221,7 @@ export type TOptional<T> = {
|
|
|
215
221
|
ifEmpty: ( callback: TVoidFunction ) => void;
|
|
216
222
|
ifPresent: ( callback: TConsumer<T> ) => void;
|
|
217
223
|
apply( callbackIfPresent: TConsumer<T>, callbackIfEmpty: TVoidFunction ): void;
|
|
224
|
+
throwIfPresent: ( errorGenerator: TFunction<T, Error> ) => TEmptyOptional<T>;
|
|
218
225
|
|
|
219
226
|
/** @deprecated[2025.07.25]: Replace with {@link orElseReturn} (drop-in replacement) */
|
|
220
227
|
orElse: ( newValue: T ) => T;
|
package/src/utils/arrays.ts
CHANGED
|
@@ -149,21 +149,6 @@ export function listToDict<T, K extends string, V>( arr: T[], mapFn: TTransforme
|
|
|
149
149
|
}, {} satisfies Partial<Record<K, V>> );
|
|
150
150
|
}
|
|
151
151
|
|
|
152
|
-
export function countArrayBy<T, K extends string>( arr: T[], mapFn: TTransformer<T, K> ): Partial<Record<K, number>> {
|
|
153
|
-
return arr.reduce( ( dict: Partial<Record<K, number>>, cur: T ) => {
|
|
154
|
-
const value = mapFn( cur );
|
|
155
|
-
const count = ( dict[ value ] ?? 0 ) + 1;
|
|
156
|
-
return { ...dict, [ value ]: count };
|
|
157
|
-
}, {} satisfies Partial<Record<K, number>> );
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
export function sumArrayBy<T>( arr: T[], mapFn: TTransformer<T, number> ): number {
|
|
161
|
-
return arr.reduce( ( sum: number, cur: T ) => {
|
|
162
|
-
const value = mapFn( cur );
|
|
163
|
-
return sum + value;
|
|
164
|
-
}, 0 );
|
|
165
|
-
}
|
|
166
|
-
|
|
167
152
|
export function shallowArrayEquals( a: unknown[], b: unknown[] ): boolean {
|
|
168
153
|
if ( a === b ) return true;
|
|
169
154
|
if ( a.length !== b.length ) return false;
|
|
@@ -193,3 +178,16 @@ export function unzip<T, R>( arr: [ T, R ][] ): [ T[], R[] ] {
|
|
|
193
178
|
return [ [ ...ts, t ], [ ...rs, r ] ];
|
|
194
179
|
}, [ [], [] ] as [ T[], R[] ] );
|
|
195
180
|
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Gets the element at the specified index in the array, wrapped in an Optional.
|
|
184
|
+
* Returns an empty Optional if the index is out of bounds (negative or >= array length).
|
|
185
|
+
* @param arr - The array to get the element from
|
|
186
|
+
* @param index - The index of the element to retrieve
|
|
187
|
+
* @returns An Optional containing the element at the index, or empty if index is out of bounds
|
|
188
|
+
*/
|
|
189
|
+
export function arrayGet<T>( arr: TReadableArray<T>, index: number ) {
|
|
190
|
+
if ( index < 0 || index >= arr.length )
|
|
191
|
+
return Optional.empty();
|
|
192
|
+
return Optional.of( arr[ index ] );
|
|
193
|
+
}
|
package/src/utils/operations.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
|
|
2
|
-
export type TOperation<T, E
|
|
2
|
+
export type TOperation<T, E = Error> = {
|
|
3
3
|
success: false;
|
|
4
4
|
error: E;
|
|
5
5
|
} | {
|
|
@@ -7,13 +7,13 @@ export type TOperation<T, E extends Error = Error> = {
|
|
|
7
7
|
data: T;
|
|
8
8
|
};
|
|
9
9
|
|
|
10
|
-
export type TAsyncOperation<T, E
|
|
10
|
+
export type TAsyncOperation<T, E = Error> = Promise<TOperation<T, E>>;
|
|
11
11
|
|
|
12
|
-
export type TOperationTuple<T, E
|
|
12
|
+
export type TOperationTuple<T, E = Error> = [ T, undefined ] | [ undefined, E ];
|
|
13
13
|
|
|
14
|
-
export type TAsyncOperationTuple<T, E
|
|
14
|
+
export type TAsyncOperationTuple<T, E = Error> = Promise<TOperationTuple<T, E>>;
|
|
15
15
|
|
|
16
|
-
export type TValidation<T, E
|
|
16
|
+
export type TValidation<T, E = Error> = {
|
|
17
17
|
success: false;
|
|
18
18
|
errors: E[];
|
|
19
19
|
} | {
|
|
@@ -21,4 +21,13 @@ export type TValidation<T, E extends Error = Error> = {
|
|
|
21
21
|
data: T;
|
|
22
22
|
};
|
|
23
23
|
|
|
24
|
-
export type TAsyncValidation<T, E
|
|
24
|
+
export type TAsyncValidation<T, E = Error> = Promise<TValidation<T, E>>;
|
|
25
|
+
|
|
26
|
+
export const Operation = {
|
|
27
|
+
ok: <const T>( data: T ) => {
|
|
28
|
+
return { success: true, data } as const satisfies TOperation<T, unknown>;
|
|
29
|
+
},
|
|
30
|
+
ko: <const E>( error: E ) => {
|
|
31
|
+
return { success: false, error } as const satisfies TOperation<unknown, E>;
|
|
32
|
+
},
|
|
33
|
+
}
|