Skip to content

Commit

Permalink
Add when function.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kapelianovych committed Aug 13, 2021
1 parent 06c33df commit 41b7566
Show file tree
Hide file tree
Showing 7 changed files with 284 additions and 192 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
# [0.30.2] - 2021-08-01
# [0.31.0] - 2021-08-13

### Added

- `when` function.

## [0.30.2] - 2021-08-01

### Fixed

Expand Down
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,30 @@ const deepFrozenObject /*: DeepReadonly<{ hello: () => void }> */ = freeze(
);
```
### when
```ts
interface ConditionalFunction<A extends ReadonlyArray<any>, R> {
(onTrue: (...values: A) => R): (...values: A) => Option<R>;
(onTrue: (...values: A) => R, onFalse: (...values: A) => R): (
...values: A
) => R;
}

function when<A extends ReadonlyArray<any>, R>(
condition: (...values: A) => boolean,
): ConditionalFunction<A, R>;
```
Replaces conditional flow (ternary operator and `if`/`else`).
```ts
const multiplyIf = when((num: number) => num > 10)((num) => num * 3, identity);

const result /*: number */ = multiplyIf(9); // Will be returned as is.
const result2 /*: number */ = multiplyIf(11); // Will be multiplied.
```
### wrap
```typescript
Expand Down

0 comments on commit 41b7566

Please sign in to comment.