bcherny / Tsoption
Correct, easy to use Option type for TypeScript. π¦
Stars: β 53
Programming Languages
typescript
32286 projects
scala
5932 projects
Projects that are alternatives of or similar to Tsoption
Language Ext
C# functional language extensions - a base class library for functional programming
Stars: β 3,964 (+7379.25%)
Mutual labels: monad, functional-programming
Kotlin Result
A multiplatform Result monad for modelling success or failure operations.
Stars: β 369 (+596.23%)
Mutual labels: monad, functional-programming
Fun Task
Abstraction for managing asynchronous code in JS
Stars: β 363 (+584.91%)
Mutual labels: monad, functional-programming
Monio
Async-capable IO monad for JS
Stars: β 311 (+486.79%)
Mutual labels: monad, functional-programming
Static Land
Specification for common algebraic structures in JavaScript based on Fantasy Land
Stars: β 699 (+1218.87%)
Mutual labels: monad, functional-programming
Magic In Ten Mins
εειιζ³η»δΉ
Stars: β 327 (+516.98%)
Mutual labels: monad, functional-programming
Purefun
Functional Programming library for Java
Stars: β 37 (-30.19%)
Mutual labels: monad, functional-programming
Functional Examples
Examples with Functional JavaScript, following Professor Frisby's course
Stars: β 179 (+237.74%)
Mutual labels: monad, functional-programming
Learn Fp
learn-by-doing course/tutorial for functional programming on scala
Stars: β 548 (+933.96%)
Mutual labels: monad, functional-programming
Bow
πΉ Bow is a cross-platform library for Typed Functional Programming in Swift
Stars: β 538 (+915.09%)
Mutual labels: monad, functional-programming
Cats Mtl
cats transformer type classes.
Stars: β 238 (+349.06%)
Mutual labels: monad, functional-programming
Fp Core.rs
A library for functional programming in Rust
Stars: β 772 (+1356.6%)
Mutual labels: monad, functional-programming
Funcadelic.js
Functional programming and category theory for everyday JavaScript development
Stars: β 183 (+245.28%)
Mutual labels: monad, functional-programming
Scriptum
A fool's scriptum on functional programming
Stars: β 346 (+552.83%)
Mutual labels: monad, functional-programming
Fluture
π¦ Fantasy Land compliant (monadic) alternative to Promises
Stars: β 2,249 (+4143.4%)
Mutual labels: monad, functional-programming
Fp Resources
Functional programming great resources
Stars: β 369 (+596.23%)
Mutual labels: monad, functional-programming
Fpgo
Monad, Functional Programming features for Golang
Stars: β 165 (+211.32%)
Mutual labels: monad, functional-programming
Mostly Adequate Guide Chinese
ε½ζ°εΌηΌη¨ζεδΈζη
Stars: β 2,093 (+3849.06%)
Mutual labels: monad, functional-programming
Dry Monads
Useful, common monads in idiomatic Ruby
Stars: β 453 (+754.72%)
Mutual labels: monad, functional-programming
Lambda
Functional patterns for Java
Stars: β 737 (+1290.57%)
Mutual labels: monad, functional-programming
tsoption
Correct, easy to use Option type for TypeScript. Like Scala options; see the introductory blog post.
Installation
# Using Yarn:
yarn add tsoption
# Or, using NPM:
npm install tsoption --save
Usage
Note: You can use JavaScript instead of TypeScript, but it's not as fun.
let a = Option.of(3) // Some<number>(3)
.flatMap(_ => Some.of(5)) // Some<number>(5)
.map(_ => 'a') // Some<string>('a')
.orElse(Option.of('b')) // Some<string>('a') (non-string type gives a compile error)
.getOrElse('c') // 'a'
let b = Option.of('a') // Some<string>('a')
.flatMap(_ => new None) // None<string>() (flatMap can map to any type)
.orElse(Option.of('b')) // Some<string>('b') (non-string type gives a compile error)
.get() // 'b'
API
Note: The types of each of the expressions below are known at compile time.
// Create an option
Option.of(3) // Some(3)
Option.of('abc') // Some('abc')
Option.of(null) // None (for convenience)
Option.of(undefined) // None (for convenience)
Some.of(3) // Some(3)
Some.of(null) // Some(null)
new Some([1, 2, 3]) // Some([1, 2, 3])
new None // None
// #flatMap
Some.of(3).flatMap(_ => Some.of(_ * 2)) // Some(6)
None.of().flatMap(() => Some.of(2)) // None
// #get
Some.of(3).get() // 3
None.of().get() // COMPILE ERROR! Can't call get() on None
// #getOrElse
Some.of(1).getOrElse(2) // 1
None.of().getOrElse(2) // 2
// #isEmpty
Some.of(2).isEmpty() // false (known at compile time too!)
None.of().isEmpty() // true (known at compile time too!)
// #map
Some.of(2).map(_ => _ * 2) // Some(4)
None.of().map(() => 2) // None (known at compile time too!)
// #nonEmpty
Some.of(2).nonEmpty() // true (known at compile time too!)
None.of().nonEmpty() // false (known at compile time too!)
// #orElse
Some.of(2).orElse(Option.of(3)) // Some(2)
None.of().orElse(Option.of(3)) // Some(3)
// #toString
Some.of(2).toString() // "Some(2)"
None.of().toString() // "None"
Fantasyland
TSOption is Fantasyland-compliant. It implements:
- [x] Applicative
- [x] Apply
- [x] Chain
- [x] Functor
- [x] Monad
Fantasyland-Compatible API
import * as fl from 'fantasy-land'
const {ap, chain, map, of} = fl
// Create an option
Option[of](3) // Some(3)
Option[of]('abc') // Some('abc')
Option[of](null) // None
Option[of](undefined) // None
// #chain
Option[of](3)[chain](_ => Option[of](_ * 2)) // Some(6)
Option[of](null)[chain](() => Option[of](2)) // None (known at compile time too!)
// #map
Option[of](2)[map](_ => _ * 2) // Some(4)
Option[of](null)[map](() => 2) // None (known at compile time too!)
// #ap
Option[of](2)[ap](Option[of]((_: number) => _ * 2)) // Some(4)
Option[of](null)[ap](Option[of](() => 2)) // None (known at compile time too!)
Tests
npm test
License
MIT
Note that the project description data, including the texts, logos, images, and/or trademarks,
for each open source project belongs to its rightful owner.
If you wish to add or remove any projects, please contact us at [email protected].