Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/razshare/svelte-unsafe
https://github.com/razshare/svelte-unsafe
Last synced: about 1 month ago
JSON representation
- Host: GitHub
- URL: https://github.com/razshare/svelte-unsafe
- Owner: razshare
- License: mit
- Created: 2024-04-01T04:05:52.000Z (9 months ago)
- Default Branch: main
- Last Pushed: 2024-04-07T01:08:33.000Z (9 months ago)
- Last Synced: 2024-05-28T13:28:35.195Z (7 months ago)
- Language: JavaScript
- Size: 108 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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)
}
```