Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Result.tapEither/Async #19

Merged
merged 5 commits into from
May 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Unit } from './unit.js';
import {
Action,
ActionOfT,
AsyncAction,
AsyncActionOfT,
ErrorHandler,
FunctionOfT,
Expand Down Expand Up @@ -850,6 +851,26 @@ export class Result<TValue = Unit, TError = string> {
return this;
}

/**
* Executes the action on both success and failure.
* @param action a function
* @returns the current Result
*/
tapEither(action: Action): Result<TValue, TError> {
action();

return this;
}

/**
* Executes the asynchronous action on both success and failure.
* @param action a function
* @returns the current Result wrapped in a ResultAsync
*/
tapEitherAsync(action: AsyncAction): ResultAsync<TValue, TError> {
return ResultAsync.from<TValue, TError>(action().then(() => this));
}

/**
* Maps a successful Result's value to a new value,
* or a failed Result's error to a new value
Expand Down
27 changes: 27 additions & 0 deletions src/resultAsync.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Result } from './result.js';
import { Unit } from './unit.js';
import {
Action,
ActionOfT,
AsyncAction,
AsyncActionOfT,
Expand Down Expand Up @@ -558,6 +559,32 @@ export class ResultAsync<TValue = Unit, TError = string> {
);
}

/**
* Executes the given async action if the ResultAsync is successful or failed
* @param action an async function
* @returns the current ResultAsync wrapping the inner Result
*/
tapEither(asyncAction: AsyncAction): ResultAsync<TValue, TError>;
/**
* Executes the given action if the ResultAsync is successful or failed
* @param action a function
* @returns the current ResultAsync wrapping the inner Result
*/
tapEither(action: Action): ResultAsync<TValue, TError>;
tapEither(action: Action | AsyncAction): ResultAsync<TValue, TError> {
return new ResultAsync(
seangwright marked this conversation as resolved.
Show resolved Hide resolved
this.value.then(async (originalResult) => {
const actionResult = action();

if (actionResult instanceof Promise) {
await actionResult;
}

return originalResult;
})
);
}

/**
*
* @param matcher
Expand Down
23 changes: 23 additions & 0 deletions test/result/tapEither.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Result } from '@/src/result';

describe('Result', () => {
describe('tapEither', () => {
test('will execute the given action if the Result is successful', () => {
let wasCalled = false;
const sut = Result.success(1);

sut.tapEither(() => (wasCalled = true));

expect(wasCalled).toBe(true);
});

test('will execute the given action if the Result is a failure', () => {
let wasCalled = false;
const sut = Result.failure<number>('error');

sut.tapEither(() => (wasCalled = true));

expect(wasCalled).toBe(true);
});
});
});
35 changes: 35 additions & 0 deletions test/result/tapEitherAsync.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Result } from '@/src/result';

describe('Result', () => {
describe('tapEitherAsync', () => {
describe('promise', () => {
test('will execute the asynchronous action if the Result is successful', async () => {
let wasCalled = false;
const sut = Result.success(1);

const asyncAction = () => {
wasCalled = true;
return Promise.resolve();
};

await sut.tapEitherAsync(asyncAction).toPromise();

expect(wasCalled).toBe(true);
});

test('will execute the asynchronous action if the Result is a failure', async () => {
let wasCalled = false;
const sut = Result.failure<number>('error');

const asyncAction = () => {
wasCalled = true;
return Promise.resolve();
};

await sut.tapEitherAsync(asyncAction).toPromise();

expect(wasCalled).toBe(true);
});
});
});
});
53 changes: 53 additions & 0 deletions test/resultAsync/tapEither.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { ResultAsync } from '@/src/resultAsync';

describe('ResultAsync', () => {
describe('tapEither', () => {
test('executes the action with a failed ResultAsync', async () => {
let wasCalled = false;
const sut = ResultAsync.failure<number>('error');

const result = await sut
.tapEither(() => {
wasCalled = true;
})
.toPromise();

expect(result).toFailWith('error');
expect(wasCalled).toBe(true);
});

test('executes the action with a successful ResultAsync', async () => {
const value = 1;
let wasCalled = false;

const sut = ResultAsync.success(value);

const result = await sut
.tapEither(() => {
wasCalled = true;
})
.toPromise();

expect(result).toSucceedWith(1);
expect(wasCalled).toBe(true);
});

test('executes the async action with a successful ResultAsync', async () => {
const value = 1;
let wasCalled = false;

const sut = ResultAsync.success(value);

const result = await sut
.tapEither(() => {
wasCalled = true;

return Promise.resolve();
})
.toPromise();

expect(result).toSucceedWith(1);
expect(wasCalled).toBe(true);
});
});
});