@zelgadis87/utils-core 5.2.3 → 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/CHANGELOG.md +14 -0
- package/dist/Optional.d.ts +2 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/utils/arrays.d.ts +1 -2
- package/dist/utils/operations.d.ts +16 -6
- package/esbuild/index.cjs +23 -17
- package/esbuild/index.cjs.map +4 -4
- package/esbuild/index.mjs +21 -15
- package/esbuild/index.mjs.map +4 -4
- package/package.json +1 -1
- package/src/Optional.ts +7 -0
- package/src/utils/arrays.ts +5 -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;
|
|
@@ -177,6 +162,11 @@ export function findInArray<T>( arr: T[], predicate: TPredicate<T> ): TOptional<
|
|
|
177
162
|
return Optional.ofNullable( arr.find( predicate ) );
|
|
178
163
|
}
|
|
179
164
|
|
|
165
|
+
export function findIndexInArray<T>( arr: T[], predicate: TPredicate<T> ): TOptional<number> {
|
|
166
|
+
const idx = arr.findIndex( predicate );
|
|
167
|
+
return idx === -1 ? Optional.empty() : Optional.of( idx );
|
|
168
|
+
}
|
|
169
|
+
|
|
180
170
|
export function zip<T, R>( ts: T[], rs: R[] ): [ T, R ][] {
|
|
181
171
|
if ( ts.length !== rs.length )
|
|
182
172
|
throw new Error( `Arrays must have the same length. Got ${ts.length} and ${rs.length}` );
|
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
|
+
}
|