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

altered.js

v0.8.6

Published

Reversible state changes for Node & Browser

Downloads

4

Readme

Altered.js

TL;DR: Reversible state changes, Javascript version.

This module is a port of my similarly named Python module, Altered States. It's purpose is reversible state changes aimed at e.g. testing. The 'Altered.js' name is mostly to distinguish from it's Python counterpart, from here on I'll refer to it as 'Altered States' too, and mean the Javascript version.

Altered States is mostly meant to handle setup/tear down of test fixtures. For the Javascript version, that means that the after the environment is modified, either:

1. On-shot modifications with a callback

You modify your world, and a provide a callback to be called after the modifications are applied. When the callback returns, the modifications will be restored (Node.js CLI repl commands shown):

> var state = require('./altered').state
undefined
> var object = {a: 1}
undefined
> state(object, {a: 2}, function() { console.log(object) })
{ a: 2 }        // output of `console.log` inside the callback
{ a: 1 }        // original is also returned

For Altered State to be able to restore the state you will need to let Altered States perform your modifications via a difference object ("diff object" below). In the example above, the property a gets overwritten with a new value. A diff object can also add properties:

> state(object, {b: 2}, function() { console.log(object) })
{ a: 1, b: 2 }  // propery `b` added
{ a: 1 }        // property `b` no longer present after restore

Setting a property to undefined will "forget" (perform temporary removal of) that propery:

> state(object, {a: undefined}, function() { console.log(object) })
{}              // property `a` not present
{ a: 1 }        // restored

(Note: I'm still unsure if undefined really is the best marker for "forgetting" a property)

2. Two-step modification

As an alternative, you can also set up a modification and have a restore function returned to you that you can call yourself at a later time. You do this via the change function instead:

> // Assumes a new session
> var change = require('./altered').change
undefined
> var object = {a: 1}
undefined
> var restore = change(object, {a: 2}); // returns a function
undefined
> object
{ a: 2 }
> restore()
undefined
> object
{ a: 1 }  // now restored

The same alteration rules as in section 1 is used when change is called instead of state.

Modifying the code

The script is actually very short, hence there are no subdirectories in the project, so modifying it yourself should be simple. Classic make in combination with npm is used to build. A simple make test should be enought to set up a newly cloned directory for development. If it isn't please open an issue and I'll look into it.