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

@malipetek/straightforward-state

v1.0.5

Published

Straightforward State, with conditional watching at one level deep

Downloads

16

Readme

Straightforward State

Percentage of issues still open Average time to resolve an issue

GitHub stars

Npm package total downloads Npm package total downloads

Npm package total downloads

Npm package total downloads

A state object that is conditionally watchable on first depth only.

Play

On codesandbox:

Edit magical-wilbur-f247um

On Stackblitz:

Edit on Stackblitz

Install:

npm i -s @malipetek/straigtforward-state

or

yarn add @malipetek/straigtforward-state

import or require

import state from "@malipetek/straigtforward-state";

or

const state = require("@malipetek/straigtforward-state");

Alternatively you can get instances like this:

import { newState } from "@malipetek/straigtforward-state";
const state1 = newState();
const state2 = newState();

Initialize

state.set = { things: [1,2,3], numberOfThings: 6, aLotOfThings: false };

or

state.set({ things: [1,2,3], numberOfThings: 3, aLotOfThings: false });

Access

Access values as usual

state.things // [1,2,3]
state.numberOfThings // 3

Set

Set the state properties as usual. When set watchers will be run, mutations does not trigger however, like pushing to an array value.

state.things = [...state.things, 4];

Watch

Watch a member

state.watch.things = (val, was) => {
  val // [1,2,3,4]
  state.things // [1,2,3,4]

  state.numberOfThings = state.things.length;
};

state.watch.numberOfThings((val, was) => {
  val // 4
  was // 3
});

Watch All

state.watch.all = (changedKey, val, was) => {
  changedKey // 'things', 'numberOfThings'
  state[changedKey] // [1], 1
  val // [1], 1
  was // [1,2,3], 3
};

state.things = [1];
state.numberOfThings = 1;

Conditional watch

state.when.things.watch.numberOfThings(callback);
// or
const newThings = [1,2];
state.when.things.is(newThings).watch.numberOfThings = (val, was) => {
  val // === newThings
};
state.things = newThings;

and, or Operators

state.when.numberOfThings.or.aLotOfThings.watch.things((val, was) => {

});

state.when.numberOfThings.and.aLotOfThings.watch.things = (val, was) => {
  // fires once
};

state.things = [1,2,3,4];

state.aLotOfThings = true;

state.things = [1,2,3,4];

is operator

Is operator can take a value or a function with 1 param that is the state member.

state.when.numberOfThings.is(v => v.length > 6).and.aLotOfThings.is(false).watch.things((things) => {
  state.aLotOfThings = true;
});

state.things = [1,2,3,4,5,6,7,8];

more and less operators

more and less operators only can be used after is fn call followed by a or.

state.when.numberOfThings.is(6).or.more.watch.things(callback);

👉 Submit feedback

👉 Submit issues