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

js-state-stack

v1.0.13

Published

JavaScript State management library

Downloads

3

Readme

js-state-stack

JavaScript State management library

Installation

CLI:

npm install js-state-stack

CDN:

<script src="https://cdn.jsdelivr.com/npm/js-state-stack/state-stack.js"></script>

Overview

js-state-stack is a simple state management library for JavaScript. It is designed to be simple and easy to use. It is also designed to be lightweight and fast.

Usage

Creating a StateStack


const onChange = () => {} // custom onChange function
const onClear = () => {} // custom onClear function
const onReject = () => {} // custom onReject function

const stateStack = new StateStack(onChange, onClear, onReject);

Or

class CustomStateStackClass extends StateStack {
    onChange() {
        // custom onChange function
    }

    onClear() {
        // custom onClear function
    }

    onReject() {
        // custom onReject function
    }
}

const myStateStack = new CustomStateStackClass();

Managing the states

A state can be anything you wish. Every state change, it will fire either onChange() or onReject() which you can use to do whatever you want. Both functions will pass in the current State.content as a parameter. Use this to render the state of whatever you want.

const stateStack = new StateStack(onChange, onClear, onReject);

// all of these fire teh StateStack.onChange() function passing in the current state
stateStack.addState('state1');
stateStack.addState('state2');
stateStack.addState('state3');

stateStack.states; // [State: {content: 'state1'}, State: {content: 'state2'}, State: {content: 'state3'}]
stateStack.currentState; // State: {content: 'state3'}


stateStack.prev(); // 'state2' - fires StateStack.onChange() function
stateStack.prev(); // 'state1' - StateStack.onChange()
stateStack.prev(); // 'state1' (no change) - StateStack.onReject()
stateStack.hasNext; // true
stateStack.hasPrev; // false

stateStack.next(); // 'state2' - StateStack.onChange()
stateStack.next(); // 'state3' - StateStack.onChange()
stateStack.next(); // 'state3' (no change) - StateStack.onReject()
stateStack.hasNext; // false
stateStack.hasPrev; // true

stateStack.first(); // 'state1' - StateStack.onChange()
stateStack.last(); // 'state3' - StateStack.onChange()

stateStack.clear(); // fires StateStack.onClear() function
stateStack.states; // []

Branches

While a StateStack uses an Array of states, a BranchStack uses an object which contains StateStacks. Each have a unique name. This allows you to have multiple states that can be managed independently of each other. Every branch change runs BranchStack.currentBranch.onChange() which you can use to do whatever you want.

const branchStack = new BranchStack(onChange, onClear, onReject);

/* Adding Branches */
branchStack.newBranch('branch1', new StateStack());

branchStack.branches; // { branch1: StateStack }
branchStack.currentBranch; // StateStack (branch1)

branchStack.copyBranch('branch1', 'branch2'); // does not change currentBranch

branchStack.branches; // { branch1: StateStack, branch2: StateStack }
branchStack.currentBranch; // StateStack (branch1)

/* Switching Branches */
branchStack.goToBranch('branch2'); // changes currentBranch to branch2

branchStack.branches; // { branch1: StateStack, branch2: StateStack }
branchStack.currentBranch; // StateStack (branch2)


/* Deleting Branches */
branchStack.deleteBranch('branch1'); // deletes branch1

branchStack.branches; // { branch2: StateStack }
branchStack.currentBranch; // StateStack (branch2)

branchStack.deleteBranch('branch2'); // throws error because branch2 is the currentBranch

/* Renaming Branches */
branchStack.renameBranch('branch2', 'branch3'); // renames branch2 to branch3

branchStack.branches; // { branch3: StateStack }
branchStack.currentBranch; // StateStack (branch3)

/* Merge Branches */
branchStack.newBranch('branch1', new StateStack());
branchStack.newBranch('branch2', new StateStack());

branchStack.branches; // { branch1: StateStack, branch2: StateStack, branch3: StateStack }

branchStack.mergeBranches('branch1', 'branch2', 'branch4'); // merges branch1 and branch2 into branch4