neverthrow
neverthrow copied to clipboard
feat: Add a function that combines multiple results and returns an object
Hi @supermacro ,
First of all, I would like to thank you for creating such a wonderful library. I have been using neverthrow in a production environment for about a year and I am very satisfied with it.
In this PR, I am adding a function that combines multiple Results, passed as an object, into a single Result. This can be very useful for functional programming as shown below:
const createTaskId: (id: string) => Result<TaskId, Error>;
const createTaskContent: (content: string) => Result<TaskContent, Error>;
const task = Result.struct({
id: createTaskId('id'),
content: createTaskContent('content'),
});
To do the same thing with the current API, you need to use map as shown below. This requires you to remember which data is in which position in the array, which is not very convenient.
const createTaskId: (id: string) => Result<TaskId, Error>;
const createTaskContent: (content: string) => Result<TaskContent, Error>;
const values = Result.combineWithAllErrors([
createTaskId('id'),
createTaskContent('content'),
]);
const task = values.map(([id, content]) => ({ id, content }));