Skip to content

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