Skip to content

Commit 5ccd618

Browse files
committed
feat(task): add prepend as well
1 parent 4575f40 commit 5ccd618

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

docs/task-instance.md

+23-1
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ type forward = <S2>(value: S2) => Task<E, S2>;
442442

443443
## append
444444

445-
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.
445+
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.
446446

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

465+
## prepend
466+
467+
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.
468+
469+
{% tabs %}
470+
{% tab title="Usage" %}
471+
472+
```typescript
473+
const task: Task<unknown, [number, number]> = Task.of(5).prepend(10);
474+
```
475+
476+
{% endtab %}
477+
478+
{% tab title="Type Definition" %}
479+
480+
```typescript
481+
type prepend = <S2>(this: Task<E, S>, value: S2) => Task<E, [S2, S]>;
482+
```
483+
484+
{% endtab %}
485+
{% endtabs %}
486+
465487
## ap
466488

467489
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.

src/Task/Task.ts

+15
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,21 @@ export class Task<E, S> implements PromiseLike<S> {
130130
return map<E, S, any[]>(a => [a, ...items], this);
131131
}
132132

133+
public prepend<A, B, C, D, E>(
134+
a: A,
135+
b: B,
136+
c: C,
137+
d: D,
138+
e: E
139+
): Task<E, [S, A, B, C, D, E]>;
140+
public prepend<A, B, C, D>(a: A, b: B, c: C, d: D): Task<E, [A, B, C, D, S]>;
141+
public prepend<A, B, C>(a: A, b: B, c: C): Task<E, [A, B, C, S]>;
142+
public prepend<A, B>(a: A, b: B): Task<E, [A, B, S]>;
143+
public prepend<A>(a: A): Task<E, [A, S]>;
144+
public prepend(...items: any[]): Task<E, any[]> {
145+
return map<E, S, any[]>(a => [...items, a], this);
146+
}
147+
133148
public tap(fn: (result: S) => void): Task<E, S> {
134149
return tap(fn, this);
135150
}

src/Task/__tests__/prepend.spec.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { succeed } from "../Task";
2+
3+
describe("prepend", () => {
4+
test("should prepend some number of constants to the current task value ", () => {
5+
const resolve = jest.fn();
6+
const reject = jest.fn();
7+
8+
succeed(5)
9+
.prepend(10, "15", 20)
10+
.fork(reject, resolve);
11+
12+
expect(resolve).toBeCalledWith([10, "15", 20, 5]);
13+
expect(reject).not.toBeCalled();
14+
});
15+
});

0 commit comments

Comments
 (0)