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

@solid-primitives/destructure

v0.1.17

Published

Primitives for destructuring reactive objects – like props or stores – or signals of them into a separate accessors updated individually.

Downloads

636

Readme

@solid-primitives/destructure

turborepo size version stage

Primitive for destructuring reactive objects – like props or stores – or signals of them into a separate accessors updated individually.

Installation

npm install @solid-primitives/destructure
# or
yarn add @solid-primitives/destructure

destructure

Spreads an reactive object (store or props) or a reactive object signal into a tuple/map of signals for each object key.

Import

import { destructure } from "@solid-primitives/destructure";

How to use it

destructure is an reactive primitive, hence needs to be used under an reactive root. Pass an reactive object or a signal as it's first argument, and configure it's behavior via options:

  • memo - wraps accessors in createMemo, making each property update independently. (enabled by default for signal source)
  • lazy - property accessors are created on key read. enable if you want to only a subset of source properties, or use properties initially missing
  • deep - destructure nested objects
// spread tuples
const [first, second, third] = destructure(() => [1, 2, 3]);
first(); // => 1
second(); // => 2
third(); // => 3

// spread objects
const { name, age } = destructure({ name: "John", age: 36 });
name(); // => "John"
age(); // => 36

Spread component props

const ListItem: Component<{ title: string; label: string; highlight: boolean }> = props => {
  const { title, label, highlight } = destructure(props);
  return <h2>{title()}</h2>;
};

Caching keys (memo)

By default keys of an accessor source are cached and keys of an object source are not. (reactive objet like stores and props should be fine-grained anyway) But caching can be controlled by specifying a memo property in options.

const [store, setStore] = createStore({
  user: {
    name: "John",
    lastName: "Brown",
    age: 25,
  },
});
// disable memo (for accessors is enabled by default)
const { name, lastName, age } = destructure(() => store.user, { memo: false });

Destructuring nested objects

The deep flag causes the all the nested objects to be destructured into signals as well.

const [store, setStore] = createStore({
  user: {
    name: "Johnny",
    lastName: "Bravo",
    age: 19,
  },
});

const {
  user: { name, lastName, age },
} = destructure(store, { deep: true });

name(); // => "Johnny"

Lazy vs Eager Evaluation

By default, destructure loops over all the available keys eagerly - on init. Lazy evaluation can be enabled via lazy flag.

Accessing keys initially missing

Lazy access allows for getting hold of accessors to keys that are not in the object yet, while still being reactive, and updating when that key will be added.

const [user, setUser] = createSignal({
  name: "John",
  lastName: "Brown",
});
const { age } = destructure(user, { lazy: true });
age(); // => undefined (not an error though)

setUser(p => ({
  ...p,
  age: 34,
}));
age(); // => 34
Caching only used properties

When caching is enabled, eager evaluation will create memos for each key, regardless of if it was accessed or not. As lazy waits for your read, the memos are created only for accessed keys.

const [user, setUser] = createSignal({
  name: "John",
  lastName: "Brown",
  age: 25,
});
// only the `age` key will be cached
const { age } = destructure(user, { lazy: true });
Spreading limitations

No {...obj} or [...list] is possible with lazy, all the keys have to accessed directly.

const obj = destructure(user, { lazy: true });
// this won't work:
const newObj = { ...obj };

Only initially available indexes can be accessed while using this syntax: [a, b] = list

// DO NOT do this:
const [a, b, c] = destructure([1, 2], { lazy: true });
a(); // => 1
b(); // => 2
c(); // error (c will be undefined)

// Do this instead:
const list = destructure([1, 2], { lazy: true });
const [a, b] = list;
const c = list[2];
a(); // => 1
b(); // => 2
c(); // => undefined (no error)

Acknowledgements

  • This package was initially based on the implementation of spread and destructure in solid-use.

Changelog

See CHANGELOG.md