Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/razshare/svelte-unsafe


https://github.com/razshare/svelte-unsafe

Last synced: about 1 month ago
JSON representation

Awesome Lists containing this project

README

        

# Svelte Unsafe

Manage errors through conditionals.

Install with

```sh
npm i -D svelte-unsafe
```

Use `ok()` to create a successful result

```js
import { ok } from 'svelte-unsafe'
/**
* @returns {import("./types").Unsafe}
*/
function greeting(){
return ok("hello world")
}
```

Or `error()` to create errors

```js
/**
* @param {string} name
* @returns {import("./types").Unsafe}
*/
function greet(name){
if(name.length < 2){
return error('Name must be at least 2 characters.')
}
return ok('hello world')
}
```

Then manage your errors through conditionals

```js
const greetAttempt = greet('A')
if(greetAttempt.error){
console.error(greetAttempt.error)
} else {
console.log(greetAttempt.value)
}
```