Unstore
Safely get the value of a store. If the value is not a store, it will be returned as it.
๐ฌ Usage
<script>
import {readable, writable} from "svelte/store"
import {unstore} from "@sveu/shared"
const num = readable(0)
const str = readable("Hello")
const list = [1,2, "Hi"]
</script>
{unstore(num)}
<br/>
{unstore(str)}
<br/>
{unstore(list)}
๐ฉโ๐ปAPI
๐ป Arguments
Name | Description | Type | Required |
---|---|---|---|
value | A plain value or a store | MaybeStore<T> | Yes |
โฉ๏ธ Returns
A value.
๐งช Playground
Source Code ๐
Source Code
import { get } from "svelte/store"
import { is_readable } from "../utils"
import type { MaybeStore } from "../utils"
/**
* Safely get the value of a store, or return the value if it's not a store.
*
* @param value - The value to unstore.
*
* @returns The value of the store, or the value if it's not a store.
*/
export function unstore<T>(value: MaybeStore<T>): T {
return is_readable(value) ? get(value) : value
}
Last update: 2023-03-07
Authors: Mohamed-Kaizen