#Function: collect()
Processes multiple Result or ResultAsync values into a single result. If all results are Success, returns a Success containing all values. If any result is a Failure, returns a Failure containing an array of all errors.
Unlike sequence, which stops at the first error and returns only that error,
collect processes all results and collects all errors.
collect(results)ケースを選ぶと、その流れだけを追えます#Type Param
X
The input type (object or array of Results).
#Examples
Object of Results (all succeed)
import { import Result Result } from '@praha/byethrow';
const const result: Result.Result<{
name: "Alice";
age: 20;
}, never[]>
result = import Result Result .function collect<{
name: Result.Result<"Alice", never>;
age: Result.Result<20, never>;
}>(x: {
name: Result.Result<"Alice", never>;
age: Result.Result<20, never>;
}): Result.Result<{
name: "Alice";
age: 20;
}, never[]> (+4 overloads)
Processes multiple
Result
or
ResultAsync
values into a single result.
If all results are
Success
, returns a
Success
containing all values.
If any result is a
Failure
, returns a
Failure
containing an array of all errors.
Unlike
sequence
, which stops at the first error and returns only that error,
collect processes all results and collects all errors.
@typeParamX - The input type (object or array of Results).@exampleObject of Results (all succeed)
import { Result } from '@praha/byethrow';
const result = Result.collect({
name: Result.succeed('Alice'),
age: Result.succeed(20),
});
// { type: 'Success', value: { name: 'Alice', age: 20 } }
@exampleObject of Results (some fail)
import { Result } from '@praha/byethrow';
const result = Result.collect({
name: Result.succeed('Alice'),
age: Result.fail('Invalid age'),
});
// { type: 'Failure', error: ['Invalid age'] }
@exampleArray of Results (all succeed)
import { Result } from '@praha/byethrow';
const result = Result.collect([
Result.succeed(1),
Result.succeed(2),
Result.succeed(3),
]);
// { type: 'Success', value: [1, 2, 3] }
@exampleArray of Results (some fail)
import { Result } from '@praha/byethrow';
const result = Result.collect([
Result.succeed(1),
Result.fail('error1'),
Result.fail('error2'),
]);
// { type: 'Failure', error: ['error1', 'error2'] }
@exampleUsing a mapper function (all succeed)
import { Result } from '@praha/byethrow';
const result = Result.collect(['1', '2', '3'], (value) => {
const number = Number(value);
return Number.isNaN(number)
? Result.fail(`Invalid number: ${value}`)
: Result.succeed(number);
});
// { type: 'Success', value: [1, 2, 3] }
@exampleUsing a mapper function (some fail)
import { Result } from '@praha/byethrow';
const result = Result.collect(['1', 'invalid', '3'], (value) => {
const number = Number(value);
return Number.isNaN(number)
? Result.fail(`Invalid number: ${value}`)
: Result.succeed(number);
});
// { type: 'Failure', error: ['Invalid number: invalid'] }
@categoryUtilities collect ({
name: Result.Result<"Alice", never> name : import Result Result .function succeed<"Alice">(value: "Alice"): Result.Result<"Alice", never> (+1 overload)Creates a
Success
result from a given value.
Passing a Promise is allowed, but it is recommended to await it before passing to succeed.
@typeParamT - The type of the value to wrap.@returnsA Result containing the given value.@exampleSynchronous Usage
import { Result } from '@praha/byethrow';
const result = Result.succeed(42);
// Result.Result<42, never>
@exampleWith No Value
import { Result } from '@praha/byethrow';
const result = Result.succeed();
// Result.Result<void, never>
@seecollect - For collect multiple Results into a single Result.@seesequence - For sequence multiple Results into a single Result.@categoryCreators succeed ('Alice'),
age: Result.Result<20, never> age : import Result Result .function succeed<20>(value: 20): Result.Result<20, never> (+1 overload)Creates a
Success
result from a given value.
Passing a Promise is allowed, but it is recommended to await it before passing to succeed.
@typeParamT - The type of the value to wrap.@returnsA Result containing the given value.@exampleSynchronous Usage
import { Result } from '@praha/byethrow';
const result = Result.succeed(42);
// Result.Result<42, never>
@exampleWith No Value
import { Result } from '@praha/byethrow';
const result = Result.succeed();
// Result.Result<void, never>
@seecollect - For collect multiple Results into a single Result.@seesequence - For sequence multiple Results into a single Result.@categoryCreators succeed (20),
});
// { type: 'Success', value: { name: 'Alice', age: 20 } }Object of Results (some fail)
import { import Result Result } from '@praha/byethrow';
const const result: Result.Result<{
name: "Alice";
age: never;
}, "Invalid age"[]>
result = import Result Result .function collect<{
name: Result.Result<"Alice", never>;
age: Result.Result<never, "Invalid age">;
}>(x: {
name: Result.Result<"Alice", never>;
age: Result.Result<never, "Invalid age">;
}): Result.Result<{
name: "Alice";
age: never;
}, "Invalid age"[]> (+4 overloads)
Processes multiple
Result
or
ResultAsync
values into a single result.
If all results are
Success
, returns a
Success
containing all values.
If any result is a
Failure
, returns a
Failure
containing an array of all errors.
Unlike
sequence
, which stops at the first error and returns only that error,
collect processes all results and collects all errors.
@typeParamX - The input type (object or array of Results).@exampleObject of Results (all succeed)
import { Result } from '@praha/byethrow';
const result = Result.collect({
name: Result.succeed('Alice'),
age: Result.succeed(20),
});
// { type: 'Success', value: { name: 'Alice', age: 20 } }
@exampleObject of Results (some fail)
import { Result } from '@praha/byethrow';
const result = Result.collect({
name: Result.succeed('Alice'),
age: Result.fail('Invalid age'),
});
// { type: 'Failure', error: ['Invalid age'] }
@exampleArray of Results (all succeed)
import { Result } from '@praha/byethrow';
const result = Result.collect([
Result.succeed(1),
Result.succeed(2),
Result.succeed(3),
]);
// { type: 'Success', value: [1, 2, 3] }
@exampleArray of Results (some fail)
import { Result } from '@praha/byethrow';
const result = Result.collect([
Result.succeed(1),
Result.fail('error1'),
Result.fail('error2'),
]);
// { type: 'Failure', error: ['error1', 'error2'] }
@exampleUsing a mapper function (all succeed)
import { Result } from '@praha/byethrow';
const result = Result.collect(['1', '2', '3'], (value) => {
const number = Number(value);
return Number.isNaN(number)
? Result.fail(`Invalid number: ${value}`)
: Result.succeed(number);
});
// { type: 'Success', value: [1, 2, 3] }
@exampleUsing a mapper function (some fail)
import { Result } from '@praha/byethrow';
const result = Result.collect(['1', 'invalid', '3'], (value) => {
const number = Number(value);
return Number.isNaN(number)
? Result.fail(`Invalid number: ${value}`)
: Result.succeed(number);
});
// { type: 'Failure', error: ['Invalid number: invalid'] }
@categoryUtilities collect ({
name: Result.Result<"Alice", never> name : import Result Result .function succeed<"Alice">(value: "Alice"): Result.Result<"Alice", never> (+1 overload)Creates a
Success
result from a given value.
Passing a Promise is allowed, but it is recommended to await it before passing to succeed.
@typeParamT - The type of the value to wrap.@returnsA Result containing the given value.@exampleSynchronous Usage
import { Result } from '@praha/byethrow';
const result = Result.succeed(42);
// Result.Result<42, never>
@exampleWith No Value
import { Result } from '@praha/byethrow';
const result = Result.succeed();
// Result.Result<void, never>
@seecollect - For collect multiple Results into a single Result.@seesequence - For sequence multiple Results into a single Result.@categoryCreators succeed ('Alice'),
age: Result.Result<never, "Invalid age"> age : import Result Result .function fail<"Invalid age">(error: "Invalid age"): Result.Result<never, "Invalid age"> (+1 overload)Creates a
Failure
result from a given error.
Passing a Promise is allowed, but it is recommended to await it before passing to fail.
@typeParamE - The type of the error to wrap.@returnsA Result containing the given error.@exampleSynchronous Usage
import { Result } from '@praha/byethrow';
const result = Result.fail('Something went wrong');
// Result.Result<never, string>
@exampleWith No Value
import { Result } from '@praha/byethrow';
const result = Result.fail();
// Result.Result<never, void>
@seecollect - For collect multiple Results into a single Result.@seesequence - For sequence multiple Results into a single Result.@categoryCreators fail ('Invalid age'),
});
// { type: 'Failure', error: ['Invalid age'] }Array of Results (all succeed)
import { import Result Result } from '@praha/byethrow';
const const result: Result.Result<[1, 2, 3], never[]> result = import Result Result .function collect<[Result.Result<1, never>, Result.Result<2, never>, Result.Result<3, never>]>(x: [Result.Result<1, never>, Result.Result<2, never>, Result.Result<3, never>]): Result.Result<[1, 2, 3], never[]> (+4 overloads)Processes multiple
Result
or
ResultAsync
values into a single result.
If all results are
Success
, returns a
Success
containing all values.
If any result is a
Failure
, returns a
Failure
containing an array of all errors.
Unlike
sequence
, which stops at the first error and returns only that error,
collect processes all results and collects all errors.
@typeParamX - The input type (object or array of Results).@exampleObject of Results (all succeed)
import { Result } from '@praha/byethrow';
const result = Result.collect({
name: Result.succeed('Alice'),
age: Result.succeed(20),
});
// { type: 'Success', value: { name: 'Alice', age: 20 } }
@exampleObject of Results (some fail)
import { Result } from '@praha/byethrow';
const result = Result.collect({
name: Result.succeed('Alice'),
age: Result.fail('Invalid age'),
});
// { type: 'Failure', error: ['Invalid age'] }
@exampleArray of Results (all succeed)
import { Result } from '@praha/byethrow';
const result = Result.collect([
Result.succeed(1),
Result.succeed(2),
Result.succeed(3),
]);
// { type: 'Success', value: [1, 2, 3] }
@exampleArray of Results (some fail)
import { Result } from '@praha/byethrow';
const result = Result.collect([
Result.succeed(1),
Result.fail('error1'),
Result.fail('error2'),
]);
// { type: 'Failure', error: ['error1', 'error2'] }
@exampleUsing a mapper function (all succeed)
import { Result } from '@praha/byethrow';
const result = Result.collect(['1', '2', '3'], (value) => {
const number = Number(value);
return Number.isNaN(number)
? Result.fail(`Invalid number: ${value}`)
: Result.succeed(number);
});
// { type: 'Success', value: [1, 2, 3] }
@exampleUsing a mapper function (some fail)
import { Result } from '@praha/byethrow';
const result = Result.collect(['1', 'invalid', '3'], (value) => {
const number = Number(value);
return Number.isNaN(number)
? Result.fail(`Invalid number: ${value}`)
: Result.succeed(number);
});
// { type: 'Failure', error: ['Invalid number: invalid'] }
@categoryUtilities collect ([
import Result Result .function succeed<1>(value: 1): Result.Result<1, never> (+1 overload)Creates a
Success
result from a given value.
Passing a Promise is allowed, but it is recommended to await it before passing to succeed.
@typeParamT - The type of the value to wrap.@returnsA Result containing the given value.@exampleSynchronous Usage
import { Result } from '@praha/byethrow';
const result = Result.succeed(42);
// Result.Result<42, never>
@exampleWith No Value
import { Result } from '@praha/byethrow';
const result = Result.succeed();
// Result.Result<void, never>
@seecollect - For collect multiple Results into a single Result.@seesequence - For sequence multiple Results into a single Result.@categoryCreators succeed (1),
import Result Result .function succeed<2>(value: 2): Result.Result<2, never> (+1 overload)Creates a
Success
result from a given value.
Passing a Promise is allowed, but it is recommended to await it before passing to succeed.
@typeParamT - The type of the value to wrap.@returnsA Result containing the given value.@exampleSynchronous Usage
import { Result } from '@praha/byethrow';
const result = Result.succeed(42);
// Result.Result<42, never>
@exampleWith No Value
import { Result } from '@praha/byethrow';
const result = Result.succeed();
// Result.Result<void, never>
@seecollect - For collect multiple Results into a single Result.@seesequence - For sequence multiple Results into a single Result.@categoryCreators succeed (2),
import Result Result .function succeed<3>(value: 3): Result.Result<3, never> (+1 overload)Creates a
Success
result from a given value.
Passing a Promise is allowed, but it is recommended to await it before passing to succeed.
@typeParamT - The type of the value to wrap.@returnsA Result containing the given value.@exampleSynchronous Usage
import { Result } from '@praha/byethrow';
const result = Result.succeed(42);
// Result.Result<42, never>
@exampleWith No Value
import { Result } from '@praha/byethrow';
const result = Result.succeed();
// Result.Result<void, never>
@seecollect - For collect multiple Results into a single Result.@seesequence - For sequence multiple Results into a single Result.@categoryCreators succeed (3),
]);
// { type: 'Success', value: [1, 2, 3] }Array of Results (some fail)
import { import Result Result } from '@praha/byethrow';
const const result: Result.Result<[1, never, never], ("error1" | "error2")[]> result = import Result Result .function collect<[Result.Result<1, never>, Result.Result<never, "error1">, Result.Result<never, "error2">]>(x: [Result.Result<1, never>, Result.Result<never, "error1">, Result.Result<never, "error2">]): Result.Result<[1, never, never], ("error1" | "error2")[]> (+4 overloads)Processes multiple
Result
or
ResultAsync
values into a single result.
If all results are
Success
, returns a
Success
containing all values.
If any result is a
Failure
, returns a
Failure
containing an array of all errors.
Unlike
sequence
, which stops at the first error and returns only that error,
collect processes all results and collects all errors.
@typeParamX - The input type (object or array of Results).@exampleObject of Results (all succeed)
import { Result } from '@praha/byethrow';
const result = Result.collect({
name: Result.succeed('Alice'),
age: Result.succeed(20),
});
// { type: 'Success', value: { name: 'Alice', age: 20 } }
@exampleObject of Results (some fail)
import { Result } from '@praha/byethrow';
const result = Result.collect({
name: Result.succeed('Alice'),
age: Result.fail('Invalid age'),
});
// { type: 'Failure', error: ['Invalid age'] }
@exampleArray of Results (all succeed)
import { Result } from '@praha/byethrow';
const result = Result.collect([
Result.succeed(1),
Result.succeed(2),
Result.succeed(3),
]);
// { type: 'Success', value: [1, 2, 3] }
@exampleArray of Results (some fail)
import { Result } from '@praha/byethrow';
const result = Result.collect([
Result.succeed(1),
Result.fail('error1'),
Result.fail('error2'),
]);
// { type: 'Failure', error: ['error1', 'error2'] }
@exampleUsing a mapper function (all succeed)
import { Result } from '@praha/byethrow';
const result = Result.collect(['1', '2', '3'], (value) => {
const number = Number(value);
return Number.isNaN(number)
? Result.fail(`Invalid number: ${value}`)
: Result.succeed(number);
});
// { type: 'Success', value: [1, 2, 3] }
@exampleUsing a mapper function (some fail)
import { Result } from '@praha/byethrow';
const result = Result.collect(['1', 'invalid', '3'], (value) => {
const number = Number(value);
return Number.isNaN(number)
? Result.fail(`Invalid number: ${value}`)
: Result.succeed(number);
});
// { type: 'Failure', error: ['Invalid number: invalid'] }
@categoryUtilities collect ([
import Result Result .function succeed<1>(value: 1): Result.Result<1, never> (+1 overload)Creates a
Success
result from a given value.
Passing a Promise is allowed, but it is recommended to await it before passing to succeed.
@typeParamT - The type of the value to wrap.@returnsA Result containing the given value.@exampleSynchronous Usage
import { Result } from '@praha/byethrow';
const result = Result.succeed(42);
// Result.Result<42, never>
@exampleWith No Value
import { Result } from '@praha/byethrow';
const result = Result.succeed();
// Result.Result<void, never>
@seecollect - For collect multiple Results into a single Result.@seesequence - For sequence multiple Results into a single Result.@categoryCreators succeed (1),
import Result Result .function fail<"error1">(error: "error1"): Result.Result<never, "error1"> (+1 overload)Creates a
Failure
result from a given error.
Passing a Promise is allowed, but it is recommended to await it before passing to fail.
@typeParamE - The type of the error to wrap.@returnsA Result containing the given error.@exampleSynchronous Usage
import { Result } from '@praha/byethrow';
const result = Result.fail('Something went wrong');
// Result.Result<never, string>
@exampleWith No Value
import { Result } from '@praha/byethrow';
const result = Result.fail();
// Result.Result<never, void>
@seecollect - For collect multiple Results into a single Result.@seesequence - For sequence multiple Results into a single Result.@categoryCreators fail ('error1'),
import Result Result .function fail<"error2">(error: "error2"): Result.Result<never, "error2"> (+1 overload)Creates a
Failure
result from a given error.
Passing a Promise is allowed, but it is recommended to await it before passing to fail.
@typeParamE - The type of the error to wrap.@returnsA Result containing the given error.@exampleSynchronous Usage
import { Result } from '@praha/byethrow';
const result = Result.fail('Something went wrong');
// Result.Result<never, string>
@exampleWith No Value
import { Result } from '@praha/byethrow';
const result = Result.fail();
// Result.Result<never, void>
@seecollect - For collect multiple Results into a single Result.@seesequence - For sequence multiple Results into a single Result.@categoryCreators fail ('error2'),
]);
// { type: 'Failure', error: ['error1', 'error2'] }Using a mapper function (all succeed)
import { import Result Result } from '@praha/byethrow';
const const result: Result.Result<[number, number, number], ("Invalid number: 1" | "Invalid number: 2" | "Invalid number: 3")[]> result = import Result Result .function collect<["1", "2", "3"], (value: "1" | "2" | "3") => Result.Failure<"Invalid number: 1" | "Invalid number: 2" | "Invalid number: 3"> | Result.Success<number>>(x: ["1", "2", "3"], fn: (value: "1" | "2" | "3") => Result.Failure<"Invalid number: 1" | "Invalid number: 2" | "Invalid number: 3"> | Result.Success<number>): Result.Result<[number, number, number], ("Invalid number: 1" | "Invalid number: 2" | "Invalid number: 3")[]> (+4 overloads)Processes multiple
Result
or
ResultAsync
values into a single result.
If all results are
Success
, returns a
Success
containing all values.
If any result is a
Failure
, returns a
Failure
containing an array of all errors.
Unlike
sequence
, which stops at the first error and returns only that error,
collect processes all results and collects all errors.
@typeParamX - The input type (object or array of Results).@exampleObject of Results (all succeed)
import { Result } from '@praha/byethrow';
const result = Result.collect({
name: Result.succeed('Alice'),
age: Result.succeed(20),
});
// { type: 'Success', value: { name: 'Alice', age: 20 } }
@exampleObject of Results (some fail)
import { Result } from '@praha/byethrow';
const result = Result.collect({
name: Result.succeed('Alice'),
age: Result.fail('Invalid age'),
});
// { type: 'Failure', error: ['Invalid age'] }
@exampleArray of Results (all succeed)
import { Result } from '@praha/byethrow';
const result = Result.collect([
Result.succeed(1),
Result.succeed(2),
Result.succeed(3),
]);
// { type: 'Success', value: [1, 2, 3] }
@exampleArray of Results (some fail)
import { Result } from '@praha/byethrow';
const result = Result.collect([
Result.succeed(1),
Result.fail('error1'),
Result.fail('error2'),
]);
// { type: 'Failure', error: ['error1', 'error2'] }
@exampleUsing a mapper function (all succeed)
import { Result } from '@praha/byethrow';
const result = Result.collect(['1', '2', '3'], (value) => {
const number = Number(value);
return Number.isNaN(number)
? Result.fail(`Invalid number: ${value}`)
: Result.succeed(number);
});
// { type: 'Success', value: [1, 2, 3] }
@exampleUsing a mapper function (some fail)
import { Result } from '@praha/byethrow';
const result = Result.collect(['1', 'invalid', '3'], (value) => {
const number = Number(value);
return Number.isNaN(number)
? Result.fail(`Invalid number: ${value}`)
: Result.succeed(number);
});
// { type: 'Failure', error: ['Invalid number: invalid'] }
@categoryUtilities collect (['1', '2', '3'], (value: "1" | "2" | "3" value ) => {
const const number: number number = var Number: NumberConstructor
(value?: any) => number
An object that represents a number of any kind. All JavaScript numbers are 64-bit floating-point numbers.
Number (value: "1" | "2" | "3" value );
return var Number: NumberConstructorAn object that represents a number of any kind. All JavaScript numbers are 64-bit floating-point numbers.
Number .NumberConstructor.isNaN(number: unknown): booleanReturns a Boolean value that indicates whether a value is the reserved value NaN (not a
number). Unlike the global isNaN(), Number.isNaN() doesn't forcefully convert the parameter
to a number. Only values of the type number, that are also NaN, result in true.
@paramnumber A numeric value. isNaN (const number: number number )
? import Result Result .function fail<"Invalid number: 1" | "Invalid number: 2" | "Invalid number: 3">(error: "Invalid number: 1" | "Invalid number: 2" | "Invalid number: 3"): Result.Result<never, "Invalid number: 1" | "Invalid number: 2" | "Invalid number: 3"> (+1 overload)Creates a
Failure
result from a given error.
Passing a Promise is allowed, but it is recommended to await it before passing to fail.
@typeParamE - The type of the error to wrap.@returnsA Result containing the given error.@exampleSynchronous Usage
import { Result } from '@praha/byethrow';
const result = Result.fail('Something went wrong');
// Result.Result<never, string>
@exampleWith No Value
import { Result } from '@praha/byethrow';
const result = Result.fail();
// Result.Result<never, void>
@seecollect - For collect multiple Results into a single Result.@seesequence - For sequence multiple Results into a single Result.@categoryCreators fail (`Invalid number: ${value: "1" | "2" | "3" value }`)
: import Result Result .function succeed<number>(value: number): Result.Result<number, never> (+1 overload)Creates a
Success
result from a given value.
Passing a Promise is allowed, but it is recommended to await it before passing to succeed.
@typeParamT - The type of the value to wrap.@returnsA Result containing the given value.@exampleSynchronous Usage
import { Result } from '@praha/byethrow';
const result = Result.succeed(42);
// Result.Result<42, never>
@exampleWith No Value
import { Result } from '@praha/byethrow';
const result = Result.succeed();
// Result.Result<void, never>
@seecollect - For collect multiple Results into a single Result.@seesequence - For sequence multiple Results into a single Result.@categoryCreators succeed (const number: number number );
});
// { type: 'Success', value: [1, 2, 3] }Using a mapper function (some fail)
import { import Result Result } from '@praha/byethrow';
const const result: Result.Result<[number, number, number], ("Invalid number: 1" | "Invalid number: invalid" | "Invalid number: 3")[]> result = import Result Result .function collect<["1", "invalid", "3"], (value: "1" | "invalid" | "3") => Result.Failure<"Invalid number: 1" | "Invalid number: invalid" | "Invalid number: 3"> | Result.Success<number>>(x: ["1", "invalid", "3"], fn: (value: "1" | "invalid" | "3") => Result.Failure<"Invalid number: 1" | "Invalid number: invalid" | "Invalid number: 3"> | Result.Success<number>): Result.Result<[number, number, number], ("Invalid number: 1" | "Invalid number: invalid" | "Invalid number: 3")[]> (+4 overloads)Processes multiple
Result
or
ResultAsync
values into a single result.
If all results are
Success
, returns a
Success
containing all values.
If any result is a
Failure
, returns a
Failure
containing an array of all errors.
Unlike
sequence
, which stops at the first error and returns only that error,
collect processes all results and collects all errors.
@typeParamX - The input type (object or array of Results).@exampleObject of Results (all succeed)
import { Result } from '@praha/byethrow';
const result = Result.collect({
name: Result.succeed('Alice'),
age: Result.succeed(20),
});
// { type: 'Success', value: { name: 'Alice', age: 20 } }
@exampleObject of Results (some fail)
import { Result } from '@praha/byethrow';
const result = Result.collect({
name: Result.succeed('Alice'),
age: Result.fail('Invalid age'),
});
// { type: 'Failure', error: ['Invalid age'] }
@exampleArray of Results (all succeed)
import { Result } from '@praha/byethrow';
const result = Result.collect([
Result.succeed(1),
Result.succeed(2),
Result.succeed(3),
]);
// { type: 'Success', value: [1, 2, 3] }
@exampleArray of Results (some fail)
import { Result } from '@praha/byethrow';
const result = Result.collect([
Result.succeed(1),
Result.fail('error1'),
Result.fail('error2'),
]);
// { type: 'Failure', error: ['error1', 'error2'] }
@exampleUsing a mapper function (all succeed)
import { Result } from '@praha/byethrow';
const result = Result.collect(['1', '2', '3'], (value) => {
const number = Number(value);
return Number.isNaN(number)
? Result.fail(`Invalid number: ${value}`)
: Result.succeed(number);
});
// { type: 'Success', value: [1, 2, 3] }
@exampleUsing a mapper function (some fail)
import { Result } from '@praha/byethrow';
const result = Result.collect(['1', 'invalid', '3'], (value) => {
const number = Number(value);
return Number.isNaN(number)
? Result.fail(`Invalid number: ${value}`)
: Result.succeed(number);
});
// { type: 'Failure', error: ['Invalid number: invalid'] }
@categoryUtilities collect (['1', 'invalid', '3'], (value: "1" | "invalid" | "3" value ) => {
const const number: number number = var Number: NumberConstructor
(value?: any) => number
An object that represents a number of any kind. All JavaScript numbers are 64-bit floating-point numbers.
Number (value: "1" | "invalid" | "3" value );
return var Number: NumberConstructorAn object that represents a number of any kind. All JavaScript numbers are 64-bit floating-point numbers.
Number .NumberConstructor.isNaN(number: unknown): booleanReturns a Boolean value that indicates whether a value is the reserved value NaN (not a
number). Unlike the global isNaN(), Number.isNaN() doesn't forcefully convert the parameter
to a number. Only values of the type number, that are also NaN, result in true.
@paramnumber A numeric value. isNaN (const number: number number )
? import Result Result .function fail<"Invalid number: 1" | "Invalid number: invalid" | "Invalid number: 3">(error: "Invalid number: 1" | "Invalid number: invalid" | "Invalid number: 3"): Result.Result<never, "Invalid number: 1" | "Invalid number: invalid" | "Invalid number: 3"> (+1 overload)Creates a
Failure
result from a given error.
Passing a Promise is allowed, but it is recommended to await it before passing to fail.
@typeParamE - The type of the error to wrap.@returnsA Result containing the given error.@exampleSynchronous Usage
import { Result } from '@praha/byethrow';
const result = Result.fail('Something went wrong');
// Result.Result<never, string>
@exampleWith No Value
import { Result } from '@praha/byethrow';
const result = Result.fail();
// Result.Result<never, void>
@seecollect - For collect multiple Results into a single Result.@seesequence - For sequence multiple Results into a single Result.@categoryCreators fail (`Invalid number: ${value: "1" | "invalid" | "3" value }`)
: import Result Result .function succeed<number>(value: number): Result.Result<number, never> (+1 overload)Creates a
Success
result from a given value.
Passing a Promise is allowed, but it is recommended to await it before passing to succeed.
@typeParamT - The type of the value to wrap.@returnsA Result containing the given value.@exampleSynchronous Usage
import { Result } from '@praha/byethrow';
const result = Result.succeed(42);
// Result.Result<42, never>
@exampleWith No Value
import { Result } from '@praha/byethrow';
const result = Result.succeed();
// Result.Result<void, never>
@seecollect - For collect multiple Results into a single Result.@seesequence - For sequence multiple Results into a single Result.@categoryCreators succeed (const number: number number );
});
// { type: 'Failure', error: ['Invalid number: invalid'] }#Call Signature
collect<
X>(x):ResultFor<X[keyofX], { [K in string | number | symbol]: InferSuccess<X[K]> },InferFailure<X[keyofX]>[]>
#Type Parameters
#X
X extends Record<string, ResultMaybeAsync<any, any>>
#Parameters
#x
X
#Returns
ResultFor<X[keyof X], { [K in string | number | symbol]: InferSuccess<X[K]> }, InferFailure<X[keyof X]>[]>
#Call Signature
collect<
X>(x):ResultFor<X[number], { [K in string | number | symbol]: InferSuccess<X[K]> },InferFailure<X[number]>[]>
#Type Parameters
#X
X extends ResultMaybeAsync<any, any>[]
#Parameters
#x
X
#Returns
ResultFor<X[number], { [K in string | number | symbol]: InferSuccess<X[K]> }, InferFailure<X[number]>[]>
#Call Signature
collect<
X>(x):ResultFor<X[number], { [K in string | number | symbol]: InferSuccess<X[K]> },InferFailure<X[number]>[]>
#Type Parameters
#X
X extends readonly ResultMaybeAsync<any, any>[]
#Parameters
#x
X
#Returns
ResultFor<X[number], { [K in string | number | symbol]: InferSuccess<X[K]> }, InferFailure<X[number]>[]>
#Call Signature
collect<
X,Fn>(x,fn):ResultFor<ReturnType<Fn>, { [K in string | number | symbol]: InferSuccess<Fn> },InferFailure<Fn>[]>
#Type Parameters
#X
X extends unknown[]
#Fn
Fn extends (value) => ResultMaybeAsync<any, any>
#Parameters
#x
X
#fn
Fn
#Returns
ResultFor<ReturnType<Fn>, { [K in string | number | symbol]: InferSuccess<Fn> }, InferFailure<Fn>[]>
#Call Signature
collect<
X,Fn>(x,fn):ResultFor<ReturnType<Fn>, { [K in string | number | symbol]: InferSuccess<Fn> },InferFailure<Fn>[]>
#Type Parameters
#X
X extends readonly unknown[]
#Fn
Fn extends (value) => ResultMaybeAsync<any, any>
#Parameters
#x
X
#fn
Fn
#Returns
ResultFor<ReturnType<Fn>, { [K in string | number | symbol]: InferSuccess<Fn> }, InferFailure<Fn>[]>
