@zelgadis87/utils-core 5.4.3 → 5.4.4
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/.rollup/index.cjs +27 -14
- package/.rollup/index.cjs.map +1 -1
- package/.rollup/index.d.ts +28 -11
- package/.rollup/index.mjs +27 -14
- package/.rollup/index.mjs.map +1 -1
- package/.rollup/tsconfig.tsbuildinfo +1 -1
- package/CHANGELOG.md +6 -0
- package/package.json +1 -1
- package/src/utils/operations.ts +40 -14
package/.rollup/index.d.ts
CHANGED
|
@@ -1069,6 +1069,7 @@ type TValidation<T, E = Error> = {
|
|
|
1069
1069
|
*/
|
|
1070
1070
|
type TAsyncValidation<T, E = Error> = Promise<TValidation<T, E>>;
|
|
1071
1071
|
/**
|
|
1072
|
+
|
|
1072
1073
|
* An error class that aggregates multiple errors from a combined operation.
|
|
1073
1074
|
* Used by `Operation.combine` to represent failures from multiple operations.
|
|
1074
1075
|
*
|
|
@@ -1082,6 +1083,32 @@ declare class OperationAggregateError<E = Error> extends Error {
|
|
|
1082
1083
|
*/
|
|
1083
1084
|
get count(): number;
|
|
1084
1085
|
}
|
|
1086
|
+
/**
|
|
1087
|
+
* Recursively extracts the innermost error type from potentially nested OperationAggregateError.
|
|
1088
|
+
* This enables `Operation.combine` to return accurate error types after flattening.
|
|
1089
|
+
*
|
|
1090
|
+
* @template E - The error type to flatten
|
|
1091
|
+
* @example
|
|
1092
|
+
* TOperationAggregateFlattenedError<string> // string
|
|
1093
|
+
* TOperationAggregateFlattenedError<OperationAggregateError<string>> // string
|
|
1094
|
+
* TOperationAggregateFlattenedError<Error | OperationAggregateError<string>> // Error | string
|
|
1095
|
+
*/
|
|
1096
|
+
type TOperationAggregateFlattenedError<E> = E extends OperationAggregateError<infer Inner> ? TOperationAggregateFlattenedError<Inner> : E;
|
|
1097
|
+
/**
|
|
1098
|
+
* Combines multiple operation results into a single result.
|
|
1099
|
+
* - If all operations succeed, returns success with an array of all data values.
|
|
1100
|
+
* - If any operation fails, returns failure with an `OperationAggregateError` containing all errors.
|
|
1101
|
+
*
|
|
1102
|
+
* When combining operations whose error type is already `OperationAggregateError<E>`,
|
|
1103
|
+
* the errors are automatically flattened to avoid nesting (e.g., combining results from
|
|
1104
|
+
* previous `combine` calls).
|
|
1105
|
+
*
|
|
1106
|
+
* @template T - The success data type of individual operations
|
|
1107
|
+
* @template E - The error type of individual operations (defaults to Error)
|
|
1108
|
+
* @param results - Array of operation results to combine
|
|
1109
|
+
* @returns A single operation with either all data or all aggregated errors (flattened)
|
|
1110
|
+
*/
|
|
1111
|
+
declare function combine<T, E = Error>(results: TReadableArray<TOperation<T, E>>): TOperation<T[], OperationAggregateError<TOperationAggregateFlattenedError<E>>>;
|
|
1085
1112
|
declare const Operation: {
|
|
1086
1113
|
ok: <const T>(data: T) => {
|
|
1087
1114
|
readonly success: true;
|
|
@@ -1118,17 +1145,7 @@ declare const Operation: {
|
|
|
1118
1145
|
* @returns A tuple of [successes, failures]
|
|
1119
1146
|
*/
|
|
1120
1147
|
partition: <T, E = Error>(results: TReadableArray<TOperation<T, E>>) => [TOperationSuccess<T>[], TOperationFailure<E>[]];
|
|
1121
|
-
|
|
1122
|
-
* Combines multiple operation results into a single result.
|
|
1123
|
-
* - If all operations succeed, returns success with an array of all data values.
|
|
1124
|
-
* - If any operation fails, returns failure with an `OperationAggregateError` containing all errors.
|
|
1125
|
-
*
|
|
1126
|
-
* @template T - The success data type of individual operations
|
|
1127
|
-
* @template E - The error type of individual operations (defaults to Error)
|
|
1128
|
-
* @param results - Array of operation results to combine
|
|
1129
|
-
* @returns A single operation with either all data or all aggregated errors
|
|
1130
|
-
*/
|
|
1131
|
-
combine: <T, E = Error>(results: TReadableArray<TOperation<T, E>>) => TOperation<T[], OperationAggregateError<E>>;
|
|
1148
|
+
combine: typeof combine;
|
|
1132
1149
|
};
|
|
1133
1150
|
|
|
1134
1151
|
declare function withTryCatch<R>(fn: TFunction<void, R>, errMapFn?: TTransformer<Error, Error>): TOperationTuple<R, Error>;
|
package/.rollup/index.mjs
CHANGED
|
@@ -1274,6 +1274,7 @@ function wrapWithString(str, start, end = start) {
|
|
|
1274
1274
|
}
|
|
1275
1275
|
|
|
1276
1276
|
/**
|
|
1277
|
+
|
|
1277
1278
|
* An error class that aggregates multiple errors from a combined operation.
|
|
1278
1279
|
* Used by `Operation.combine` to represent failures from multiple operations.
|
|
1279
1280
|
*
|
|
@@ -1293,6 +1294,31 @@ class OperationAggregateError extends Error {
|
|
|
1293
1294
|
return this.errors.length;
|
|
1294
1295
|
}
|
|
1295
1296
|
}
|
|
1297
|
+
/**
|
|
1298
|
+
* Combines multiple operation results into a single result.
|
|
1299
|
+
* - If all operations succeed, returns success with an array of all data values.
|
|
1300
|
+
* - If any operation fails, returns failure with an `OperationAggregateError` containing all errors.
|
|
1301
|
+
*
|
|
1302
|
+
* When combining operations whose error type is already `OperationAggregateError<E>`,
|
|
1303
|
+
* the errors are automatically flattened to avoid nesting (e.g., combining results from
|
|
1304
|
+
* previous `combine` calls).
|
|
1305
|
+
*
|
|
1306
|
+
* @template T - The success data type of individual operations
|
|
1307
|
+
* @template E - The error type of individual operations (defaults to Error)
|
|
1308
|
+
* @param results - Array of operation results to combine
|
|
1309
|
+
* @returns A single operation with either all data or all aggregated errors (flattened)
|
|
1310
|
+
*/
|
|
1311
|
+
function combine$1(results) {
|
|
1312
|
+
const [successes, failures] = partition(results, r => r.success);
|
|
1313
|
+
if (failures.length === 0) {
|
|
1314
|
+
return { success: true, data: successes.map(r => r.data) };
|
|
1315
|
+
}
|
|
1316
|
+
const allErrors = failures.flatMap(r => {
|
|
1317
|
+
const error = r.error;
|
|
1318
|
+
return error instanceof OperationAggregateError ? error.errors : [error];
|
|
1319
|
+
});
|
|
1320
|
+
return { success: false, error: new OperationAggregateError(allErrors) };
|
|
1321
|
+
}
|
|
1296
1322
|
const Operation = {
|
|
1297
1323
|
ok: (data) => {
|
|
1298
1324
|
return { success: true, data };
|
|
@@ -1333,20 +1359,7 @@ const Operation = {
|
|
|
1333
1359
|
partition: (results) => {
|
|
1334
1360
|
return partition(results, r => r.success);
|
|
1335
1361
|
},
|
|
1336
|
-
|
|
1337
|
-
* Combines multiple operation results into a single result.
|
|
1338
|
-
* - If all operations succeed, returns success with an array of all data values.
|
|
1339
|
-
* - If any operation fails, returns failure with an `OperationAggregateError` containing all errors.
|
|
1340
|
-
*
|
|
1341
|
-
* @template T - The success data type of individual operations
|
|
1342
|
-
* @template E - The error type of individual operations (defaults to Error)
|
|
1343
|
-
* @param results - Array of operation results to combine
|
|
1344
|
-
* @returns A single operation with either all data or all aggregated errors
|
|
1345
|
-
*/
|
|
1346
|
-
combine: (results) => {
|
|
1347
|
-
const [successes, failures] = Operation.partition(results);
|
|
1348
|
-
return failures.length === 0 ? Operation.ok(successes.map(r => r.data)) : Operation.ko(new OperationAggregateError(failures.map(r => r.error)));
|
|
1349
|
-
},
|
|
1362
|
+
combine: combine$1,
|
|
1350
1363
|
};
|
|
1351
1364
|
|
|
1352
1365
|
function asPromise(promisable) {
|