@zelgadis87/utils-core 5.2.4 → 5.2.5

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package",
3
3
  "name": "@zelgadis87/utils-core",
4
- "version": "5.2.4",
4
+ "version": "5.2.5",
5
5
  "author": "Zelgadis87",
6
6
  "license": "ISC",
7
7
  "private": false,
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;
@@ -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;
@@ -1,5 +1,5 @@
1
1
 
2
- export type TOperation<T, E extends Error = Error> = {
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 extends Error = Error> = Promise<TOperation<T, E>>;
10
+ export type TAsyncOperation<T, E = Error> = Promise<TOperation<T, E>>;
11
11
 
12
- export type TOperationTuple<T, E extends Error = Error> = [ T, undefined ] | [ undefined, E ];
12
+ export type TOperationTuple<T, E = Error> = [ T, undefined ] | [ undefined, E ];
13
13
 
14
- export type TAsyncOperationTuple<T, E extends Error = Error> = Promise<TOperationTuple<T, E>>;
14
+ export type TAsyncOperationTuple<T, E = Error> = Promise<TOperationTuple<T, E>>;
15
15
 
16
- export type TValidation<T, E extends Error = Error> = {
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 extends Error = Error> = Promise<TValidation<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
+ }