Skip to content

Commit

Permalink
feat(task): add prepend as well
Browse files Browse the repository at this point in the history
  • Loading branch information
tdreyno committed Feb 26, 2020
1 parent 4575f40 commit 5ccd618
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
24 changes: 23 additions & 1 deletion docs/task-instance.md
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ type forward = <S2>(value: S2) => Task<E, S2>;

## append

Given a successful Task, join it with an additional value. Useful for threading a value along with a task. Like `zip`, but when one of the values is not a Task.
Given a successful Task, join it before an additional value. Useful for threading a value along with a task. Like `zip`, but when one of the values is not a Task.

{% tabs %}
{% tab title="Usage" %}
Expand All @@ -462,6 +462,28 @@ type append = <S2>(this: Task<E, S>, value: S2) => Task<E, [S, S2]>;
{% endtab %}
{% endtabs %}

## prepend

Given a successful Task, join it after an additional value. Useful for threading a value along with a task. Like `zip`, but when one of the values is not a Task.

{% tabs %}
{% tab title="Usage" %}

```typescript
const task: Task<unknown, [number, number]> = Task.of(5).prepend(10);
```

{% endtab %}

{% tab title="Type Definition" %}

```typescript
type prepend = <S2>(this: Task<E, S>, value: S2) => Task<E, [S2, S]>;
```

{% endtab %}
{% endtabs %}

## ap

The applicative. If you know what that means, you'll be excited. If not, it is fine. This is a low level tool that helps build more complex features.
Expand Down
15 changes: 15 additions & 0 deletions src/Task/Task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,21 @@ export class Task<E, S> implements PromiseLike<S> {
return map<E, S, any[]>(a => [a, ...items], this);
}

public prepend<A, B, C, D, E>(
a: A,
b: B,
c: C,
d: D,
e: E
): Task<E, [S, A, B, C, D, E]>;
public prepend<A, B, C, D>(a: A, b: B, c: C, d: D): Task<E, [A, B, C, D, S]>;
public prepend<A, B, C>(a: A, b: B, c: C): Task<E, [A, B, C, S]>;
public prepend<A, B>(a: A, b: B): Task<E, [A, B, S]>;
public prepend<A>(a: A): Task<E, [A, S]>;
public prepend(...items: any[]): Task<E, any[]> {
return map<E, S, any[]>(a => [...items, a], this);
}

public tap(fn: (result: S) => void): Task<E, S> {
return tap(fn, this);
}
Expand Down
15 changes: 15 additions & 0 deletions src/Task/__tests__/prepend.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { succeed } from "../Task";

describe("prepend", () => {
test("should prepend some number of constants to the current task value ", () => {
const resolve = jest.fn();
const reject = jest.fn();

succeed(5)
.prepend(10, "15", 20)
.fork(reject, resolve);

expect(resolve).toBeCalledWith([10, "15", 20, 5]);
expect(reject).not.toBeCalled();
});
});

0 comments on commit 5ccd618

Please sign in to comment.