diff --git a/typescript/option/README.md b/typescript/option/README.md index 7c24f01..734f94d 100644 --- a/typescript/option/README.md +++ b/typescript/option/README.md @@ -14,9 +14,7 @@ npm install @adrielus/option ## Usage -Curently there are no docs, but all functions have the same type definition as the ones from [fsharp](https://msdn.microsoft.com/en-us/visualfsharpdocs/conceptual/core.option-module-%5Bfsharp%5D) (except the ones from my package are not curreied) - -There are also a few original helpers (match, flat and withDefault), but I'ts pretty easy to guess what those do +For detailed usage read [the docs](docs/main.md) # Contributing diff --git a/typescript/option/docs/main.md b/typescript/option/docs/main.md new file mode 100644 index 0000000..3e0d141 --- /dev/null +++ b/typescript/option/docs/main.md @@ -0,0 +1,190 @@ +# Documentation + +## Table of contents: + +### General: + +- [Option](#Option) +- [Some](#Some) +- [None](#None) + +### Helpers: + +- [bind](#Bind) +- [count](#Count) +- [exists](#Exists) +- [filter](#Filter) +- [fold](#Fold) +- [foldback](#Foldback) +- [forall](#Forall) + +# General + +## Option + +Data type holding an optional value (can be either None or Some(x)) + +### Signature + +```ts +type Option = Internals.SomeClass | Internals.NoneClass +``` + +## None + +Value holding nothing + +### Signature + +```ts +const None: Internals.NoneClass +``` + +## Some + +Creates an Option instance holding a value + +### Signature + +```ts +const Some: (v: T) => Internals.SomeClass +``` + +### Usage + +```ts +import { Some } from '@adrielus/option' + +Some(x) // Some(x) +``` + +# Helpers + +## Bind + +Invokes a function on an optional value that itself yields an option. + +### Signature + +```ts +const bind: (binder: Mapper>, option: Option) => Option +``` + +### Usage + +```ts +import { Some, None, bind } from '@adrielus/option' + +const half = (x: number) => (x % 2 ? None : Some(x / 2)) + +bind(half, Some(14)) // Some(7) +bind(half, Some(13)) // None +bind(half, None) // None +``` + +## Count + +Returns a zero if the option is None, a one otherwise. + +### Signature: + +```ts +const count: (option: Option) => number +``` + +### Usage + +```ts +import { Some, None, count } from '@adrielus/option' + +count(Some(x)) // 1 +count(None) // 0 +``` + +## Exists + +Returns false if the option is None, otherwise it returns the result of applying the predicate to the option value. + +### Signature + +```ts +const exists: (predicate: Mapper, option: Option) => boolean +``` + +### Usage + +```ts +import { Some, None, exists } from '@adrielus/option' + +exists(() => true, None) // false +exists(() => true, Some(x)) // true +exists(() => false, Some(x)) // false +``` + +## Filter + +Invokes a function on an optional value that itself yields an option. + +### Signature: + +```ts +const filter: (predicate: Mapper, option: Option) => NoneClass +``` + +### Usage + +```ts +import { Some, None, filter } from '@adrielus/option' + +filter(() => true, None) // None +filter(() => true, Some(x)) // Some(x) +filter(() => false, Some(x)) // None +``` + +## Fold + +A function to update the state data when given a value from an option. + +### Signature + +```ts +const fold: (folder: Folder, initial: U, option: Option) => U +``` + +### Usage + +```ts +import { Some, None, fold } from '@adrielus/option' + +const add = (a: number, b: number) => a + b + +fold(add, x, None) // x +fold(add, x, Some(y)) // x + y +``` + +## Foldback + +A function to update the state data when given a value from an option. + +### Signature + +```ts +const foldback: ( + folder: BackFolder, + option: Option, + initial: U +) => U +``` + +### Usage + +```ts +import { Some, None, foldback } from '@adrielus/option' + +const add = (a: number, b: number) => a + b + +foldback(add, None, x) // x +foldback(add, Some(y), x) // x + y +``` + +**_This is still work in progress, right now only covering about 60% of the library. Contributions are welcome_**