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

rigmarole

v1.0.5

Published

Handles undo and redo of a view model.

Downloads

10

Readme

rigmarole

A simple library that leverages the utility of Seamless-Immutable to handle immutable states as updates are pushed to the history stack.

Usage Overview

npm install rigmarole

ES Module

import rigmarole from 'rigmarole';

CommonJS

const rigmarole = require('rigmarole');

Browser

<script src="rigmarole.js"></script>

Example Usage

// Initialize with a maximum stack size of 100
const stateStack = new rigmarole(100);

stateStack.set('user', {});
stateStack.set('user.firstName', 'Anon');
stateStack.set('user.lastName', 'Ymous');

stateStack.set('user', { firstName: 'Test', lastName: 'McTester'});

stateStack.get('user.firstName'); // returns 'Test'
stateStack.get('user.lastName'); // returns 'McTester'

stateStack.undo();

stateStack.get('user.firstName'); // now returns 'Anon'
stateStack.get('user.lastName'); // now returns 'Ymous'

stateStack.redo();

stateStack.get('user.firstName'); // returns 'Test' again
stateStack.get('user.lastName'); // returns 'McTester' again

API

set

Simple helper function for setting a value at a known key path. If the state is already dirty, this and following replaces will not add to the stack. If not, the replace will set it to dirty, and add to the stack.

Returns the path that was affected as a string.

stateStack.set('foo.bar', true);
stateStack.set(['foo', 'bar'], false);

replace

Simple helper function for setting a value at a known key path. If the state is already dirty, this and following replaces will not add to the stack. If not, the replace will set it to dirty, and add to the stack.

Returns the path that was affected as a string.

Usage for this might be for updating the state with data that you don't want to save to the stack.

const stateStack = new rigmarole();
stateStack.getIndex(); // returns -1 after being newly initialized
stateStack.replace('foo.bar', true);
stateStack.getIndex(); // returns 0
stateStack.replace(['foo', 'bar'], false);
stateStack.replace(['foo', 'bar'], true);
stateStack.getIndex(); // returns 0

delete

Deletes a value at a known key path.

stateStack.replace('foo.bar', true);
stateStack.get('foo.bar'); // returns true
stateStack.delete('foo.bar');
stateStack.get('foo.bar'); // returns undefined

get

Retrieves a value at a known key path.

stateStack.set('a', { b: 3 });
const a = stateStack.get('a');
a.b = 4; // should print an error similar to "TypeError: Cannot assign to read only property 'b' of object '#<Object>'"
console.log(a.b) // prints 3

getMutable

Retrieves a value at a known key path as a mutable object.

stateStack.set('a', { b: 3 });
const a = stateStack.getMutable('a');
a.b = 4; // should not print an error
console.log(a.b) // prints 4

undo

Decrements the history index by 1.

Returns the paths that was affected as an array of strings.

stateStack.set('user', {});
stateStack.set('user', { firstName: 'Test', lastName: 'McTester'});
stateStack.replace('user.firstName', 'Anon');
stateStack.set('user.lastName', 'Ymous');

stateStack.undo(); // Returns ['user.firstName', 'user.lastName']

stateStack.get('user.firstName'); // returns 'Test'
stateStack.get('user.lastName'); // returns 'McTester'

redo

Increments the history index by 1.

Returns the paths that was affected as an array of strings.

stateStack.set('user', { firstName: 'Test', lastName: 'McTester'});
stateStack.replace('user.firstName', 'Anon');
stateStack.set('user.lastName', 'Ymous');

stateStack.undo();
stateStack.redo(); // returns ['user.firstName', 'user.lastName']

stateStack.get('user.firstName'); // returns 'Anon'
stateStack.get('user.lastName'); // returns 'Ymous'

ingest

Takes in an object and creates a new complete state from it.

stateStack.ingest({
    a: 2,
    b: 3,
    c: {
        test: 'test'
    }
});

stateStack.get('b.c.test') // returns 'test'

reset

Clears and resets the stack back to the point of initialization.

getIndex

Returns the index of the current state within the history stack.

hasUndo

Returns whether or not a previous state exists within the history stack.

hasRedo

Returns whether or not a next state exists within the history stack.

operation

Wrapper function for accessing the Immutable object and manipulating it while handling the history stack.

Returns the path that was affected as a string.

Only use this if you want to update the state manually.

stateStack.operation('test.path', (state) => ({
    ...state,
    some: { sort: { of: { update: true }}}
}))

mutateOperation

Like the operation method, but instead replaces the current state without affecting the rest of the history stack.

Returns the path that was affected as a string.

Only use this if you want to update the state manually.

stateStack.mutateOperation('test.path', (state) => ({
    ...state,
    some: { sort: { of: { update: true }}}
}))

setDirty

Sets the _isDirty boolean to true which will prevent any changes from pushing new states into the stack.

clearDirty

Sets the _isDirty boolean to false which will allow any changes to push new states into the stack.

To build:

npm run build

To test:

npm test