npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@madkarma/svelte-storable

v1.0.5

Published

A Svelte store that syncs with localStorage

Readme

@madkarma/svelte-storable

A lightweight Svelte store that automatically persists its value to localStorage. Perfect for maintaining state across browser sessions with minimal setup.

Features

  • 🔄 Auto-persistence: Automatically syncs store values with localStorage
  • 🎯 Type-safe: Full TypeScript support with generic types
  • 🛠️ Customizable: Custom serialization/deserialization functions
  • 🧹 Clean API: Simple, intuitive interface extending Svelte's writable store

📦 Installation

npm install @madkarma/svelte-storable
bun add @madkarma/svelte-storable
pnpm add @madkarma/svelte-storable
yarn add @madkarma/svelte-storable

🚀 Basic Usage

import storable from '@madkarma/svelte-storable';

// Create a persisted store
const count = storable('count', 0);

// Use it like a regular Svelte store
count.set(5);
count.update((n) => n + 1);

// The value is automatically saved to localStorage
// and restored on page reload

In your Svelte component:

<script>
	import storable from '@madkarma/svelte-storable';

	const count = storable('count', 0);
</script>

<h1>Count: {$count}</h1>
<button on:click={() => count.update((n) => n + 1)}> Increment </button>

📖 API

storable(key, initial, options?)

Creates a persistent Svelte store.

Parameters

  • key (string): The localStorage key to store the value under
  • initial (T): The initial value of the store
  • options (optional): Configuration object
    • serialize ((value: T) => string): Custom serialization function (defaults to JSON.stringify)
    • deserialize ((value: string) => T): Custom deserialization function (defaults to JSON.parse)
    • saveInitial (boolean): Whether to save the initial value to localStorage if no stored value exists (defaults to true)

Returns

A store object with the following methods:

  • subscribe(callback): Subscribe to store changes (standard Svelte store method)
  • set(value): Set a new value
  • update(updater): Update the value using an updater function
  • reset(): Reset the store to its initial value
  • remove(reset?): Remove the value from localStorage
    • reset (boolean): Whether to also reset the store value to initial (defaults to true)

💡 Examples

Basic Counter

import storable from '@madkarma/svelte-storable';

const count = storable('count', 0);

count.update((n) => n + 1); // Automatically saved to localStorage

Custom Serialization (Date Example)

const lastVisit = storable('lastVisit', new Date(), {
	serialize: (date) => date.toISOString(),
	deserialize: (str) => new Date(str)
});

Don't Save Initial Value

Useful when you only want to persist values that the user has explicitly set:

const preferences = storable('prefs', { theme: 'dark' }, { saveInitial: false });

// Initial value won't be saved to localStorage until user changes it

User Preferences

type UserPreferences = {
	theme: 'light' | 'dark';
	language: string;
	notifications: boolean;
};

const preferences = storable<UserPreferences>('user-prefs', {
	theme: 'dark',
	language: 'en',
	notifications: true
});

Reset to Initial Value

const settings = storable('settings', { volume: 50 });

settings.set({ volume: 80 });
settings.reset(); // Back to { volume: 50 }

Remove from Storage

const token = storable('auth-token', null);

// Remove from localStorage and reset to initial value
token.remove();

// Remove from localStorage but keep current value in store
token.remove(false);

📘 TypeScript

Full TypeScript support is included. The store is generic and will infer types from your initial value:

// Type is inferred as Writable<number>
const count = storable('count', 0);

// Type is inferred as Writable<string>
const name = storable('name', 'John');

// Explicit type annotation
const data = storable<{ id: number; name: string }>('data', {
	id: 1,
	name: 'Example'
});

✅ Requirements

  • Svelte 5.0.0 or higher

📄 License

MIT

🤝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.