Skip to content

Commit

Permalink
adding documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Inalegwu committed Sep 16, 2024
1 parent 09f5603 commit 86716a6
Showing 1 changed file with 87 additions and 3 deletions.
90 changes: 87 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ neverthrow under the hood



### Examples
## Examples

### Synchronous Functions
```ts
import {makeSafeFunc} from "make-safe-func";

Expand All @@ -23,7 +24,90 @@ function unsafeDivide(a:number,b:number){
const safeDivide=makeSafeFunc(unsafeDivide);

safeDivide(1/2).match(console.log,console.error);
const result=safeDivide._unsafeUnwrap();
```

### Asynchronous Functions
```ts
import {makeSafeFunc} from "make-safe-func";

function unsafeFetch(url:string){
const result=await fetch(url);

return result
}

const safeFetch=makeSafeFunc(unsafeFetch);

safeFetch("https://google.com").match(console.log,console.error);
```


### Matching on Result
```ts
import {makeSafeFunc} from "make-safe-func";

function unsafeFetch(url:string){
const result=await fetch(url);

return result
}

const safeFetch=makeSafeFunc(unsafeFetch);

safeFetch("https://google.com").match(console.log,console.error);
```

### Unwrapping Result with .unwrapOr
```ts
import {makeSafeFunc} from "make-safe-func";

function unsafeFetch(url:string){
const result=await fetch(url);

return result
}

const safeFetch=makeSafeFunc(unsafeFetch);

console.log(result);
// used to return default values in the event
// of a failure
safeFetch("https://google.com").unwrapOr("failed");
```

### Mapping over result
```ts
import {makeSafeFunc} from "make-safe-func";

function unsafeDivide(a:number,b:number){
if(b===0) throw new Error("Divide by Zero Error");
return a/b
}

const safeDivide=makeSafeFunc(unsafeDivide);

const increment=(v:number)=>v+1;

// perform a transformation on the
// result and receive a Result of the Transformation
const res=safeDivide(1/2).map(increment)
```


### .andThen
```ts
import {makeSafeFunc} from "make-safe-func";

function unsafeDivide(a:number,b:number){
if(b===0) throw new Error("Divide by Zero Error");
return a/b
}

const safeDivide=makeSafeFunc(unsafeDivide);

const increment=(v:number)=>v+1;

// perform a transformation on the
// result that might fail. You must return a new Result
// Value
const res=safeDivide(1/2).andThen()
```

0 comments on commit 86716a6

Please sign in to comment.