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 🙏

© 2024 – Pkg Stats / Ryan Hefner

svue

v1.5.3

Published

Svue is a small (~2kb) library bringing Vue-style reactive stores with data and computed properties into Svelte.

Downloads

36

Readme

svue

Svue is a small (~2kb) library bringing Vue-style reactive stores with data and computed properties into Svelte.

The concise format leverages Svelte’s built-in store capabilities, allowing you to create structures like:

export const sv = new Svue({
  data() {
    return {
      x: 2,
      y: 3
    }
  },
  computed: {
    z(x, y) {
      return x + y;
    },
  }
});

You can then bring these simple reactive stores into your Svelte components:

<script>
  import { sv } from "./example.js";
</script>

<div>x: {$sv.x}</div>
<div>y: {$sv.y}</div>
<div>z (x + y): {$sv.z}</div>

<div>
  <button on:click={() => ($sv.x += 1)}>Increment X</button>
  <button on:click={() => ($sv.y += 1)}>Increment Y</button>
</div>

You can also mess around with the Svue store in plain JavaScript:

sv.x = 10;
sv.y = 15;
console.log(sv.z); // logs 25
sv.y = 0;
console.log(sv.z); // logs 10

Installation

Install simply with:

npm install svue

Then in a JavaScript file you can create a new store with:

import { Svue } from 'svue';

export const store = new Svue({
  data() {
    return {
      ...
    }
  },
  computed: {
    ...
  }

And use the store in your Svelte component, using the traditional $-prefix:

<script>
  import {store} from './store.js';
</script>

<div>$store.x</div>

Example

Clone this repository and run:

npm install
npm run dev

Browse to the files src/App.svelte and src/example.js in this repository. Navigate your browser to port 8080 and open up the console. Following the code should give an example of how Svue works in practice.

Guide

Svue leverages Svelte’s writable and derived wrappers from svelte/store, providing a more convenient syntax for declaring complex webs of dependencies.

You initialize a Svue object with a JavaScript object that currently supports two properties, data() and computed.

The data() property is a function that returns the initial data for the Svue store. We treat data() as a function so that objects/arrays are returned fresh each time.

const car = new Svue({
  data() {
    return {
      make: "Nissan",
      model: "Rogue",
      year: 2015,
    }
  }
});

The computed property is an object that is used to return derived values based on the store’s properties. Each field of computed is a function that returns a value based on parameters.

For example, we can extend our Svue store in the previous example to auto-calculate a car’s name based on its make and model.

const car = new Svue({
  data() {
    return {
      make: "Nissan",
      model: "Rogue",
      year: 2015,
    }
  },
  computed: {
    name(make, model) {
      return `${make} ${model}`;
    }
  },
});

Here, name(make, model) { ... } means we are creating a new derived property on the Svue store called name that depends on the make and model properties. Any time the make or model change, the name will auto-update.

Computed properties can also depend on each other. Let’s extend our example to have a nameWithYear field that depends on name and year:

const car = new Svue({
  data() {
    return {
      make: "Nissan",
      model: "Rogue",
      year: 2015,
    }
  },
  computed: {
    nameWithYear(name, year) {
      return `${name} (${year})`;
    },
    name(make, model) {
      return `${make} ${model}`;
    }
  },
});

Notice how the order of computed properties does not matter, even though nameWithYear depends on name which comes after it.

Under the hood, this is all implemented as writable (data) and derived (computed) objects from svelte/store. Getter and setter methods are instantiated dynamically allowing simple JavaScript interaction with Svue stores. The entire Svue store has a subscription function per the Svelte store contract, allowing it to be used in any Svelte component prefixed with $ (see examples above).

License

MIT