Skip to content

Commit

Permalink
feat: adds flattern
Browse files Browse the repository at this point in the history
  • Loading branch information
OctoD committed Jul 16, 2019
1 parent f94bddf commit ad5e392
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Heavily inspired by rust [std::option](https://doc.rust-lang.org/std/option/inde
- [andThen](#andThen)
- [expect](#expect)
- [filter](#filter)
- [flattern](#flattern)
- [isNone](#isNone)
- [isSome](#isSome)
- [map](#map)
Expand Down Expand Up @@ -123,6 +124,14 @@ Option(1).filter(a => a > 0).isSome() // true
Option(1).filter(a => a > 10).isSome() // false
```

#### flattern

Converts from `Option<Option<T>>` to `Option<T>`

```ts
Option(Some(100)).flattern() // Some(100)
```

#### isNone

Returns if has not a value
Expand Down
18 changes: 18 additions & 0 deletions src/Option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,24 @@ class OptionLike<T> {
return predicateresult ? this : (None() as any);
}

/**
* Converts from `Option<Option<T>>` to `Option<T>`
*
* ```ts
* Option(Some(100)).flattern() // Some(100)
* ```
*
* @returns {OptionLike<T>}
* @memberof OptionLike
*/
public flattern(): OptionLike<T> {
if (this.value instanceof OptionLike) {
return this.value;
}

return this;
}

/**
* Returns if has not a value
*
Expand Down
4 changes: 4 additions & 0 deletions src/__tests__/Option.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,8 @@ describe(`Option`, () => {
expect(Some(100).transpose()).toStrictEqual(Ok(Some(100)));
expect(None().transpose()).toStrictEqual(Ok(None()));
});

it("Converts from `Option<Option<T>>` to `Option<T>`", () => {
expect(Option(Some(100)).flattern()).toStrictEqual(Some(100));
});
});

0 comments on commit ad5e392

Please sign in to comment.