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

@belchior/time-traveler

v1.4.0

Published

TimeTraveler is a library to management of state. It becomes useful in scenarios that is necessary compose object (state) based on a sequence of steps (timeline).

Downloads

18

Readme

Coverage Status License MIT

TimeTraveler

TimeTraveler is a library to management of state. It becomes useful in scenarios that is necessary compose object (state) based on a sequence of steps (timeline).

Think in on behavior of your browser when you navigate between pages, it makes available to you go back and forward or jump to an specific page if you press and hold the button of Prev or Next. This concept of time traveling is the major feature of TimeTraveler but you can do more.

Demo: https://belchior.github.io/time-traveler/

Installation

Run the command to install

npm i @belchior/time-traveler

Import

// Default / TypeScript
import TimeTraveler from '@belchior/time-traveler';

// Node ECMAScript module
import TimeTraveler from '@belchior/time-traveler/lib/mjs';

// Node CommonJS
const TimeTraveler = require('@belchior/time-traveler/lib/cjs');

API

Class methods

constructor(timeline, initialState)

timeline required - A list of functions that will be used to produce new states

initialState optional - If passed will be the first state produced by TimeTraveler, otherwise the initialState will be produced by the execution of the first function into timeline

Returns a new instance of TimeTraveler

Usage

const timeline = [/* ... */];
const tt = new TimeTraveler(timeline);

create(timeline, initialState)

recommended Alias to constructor method. The lines above are equivalents

const tt = TimeTraveler.create(timeline);
const tt = new TimeTraveler(timeline);

Instance methods

add(fn) - return this

fn required - Function that will produce state

Adds fn at the end of the timeline

Usage

const timeline = [/* ... */];
const finalStep = state => {/* ... */};
const tt = TimeTraveler.create(timeline);
const tt.add(finalStep);

getState() - return current state

Return the current state produced by timeline

Usage

const timeline = [/* ... */];
const tt = TimeTraveler.create(timeline);
const currentState = tt.getState(); // the initial state

goTo(fn) - return this

fn required - Function belonging to timeline

Set the current state traveling the timeline in both direction or jumping to some point in "time". In case the received function have already been called the state will be taken from the cache, otherwise will be produced calling the received fn.

Note that the sequence matters, if goTo is used in a way that don't follow the timeline the methods next() and prev() will try to follow the timeline created by goTo and then get possibles states from the timeline. Try the demo to get more insides.

Usage

const initialState = () => {/* ... */}
const stepA = () => {/* ... */}
const timeline = [initialState, stepA];
const tt = TimeTraveler.create(timeline);

tt.goTo(stepA); // the current state is stepA
tt.goTo(initialState); // the current state is initialState

next() - return this

Set the current state traveling the timeline to the future.

Usage

const initialState = () => {/* ... */}
const stepA = () => {/* ... */}
const timeline = [initialState, stepA];
const tt = TimeTraveler.create(timeline);

tt.next(); // the current state is stepA

prev() - return this

Set the current state traveling the timeline to the past.

Usage

const initialState = () => {/* ... */}
const stepA = () => {/* ... */}
const timeline = [initialState, stepA];
const tt = TimeTraveler.create(timeline);

tt.next(); // the current state is stepA
tt.prev(); // the current state is initialState

reset() - return this

Clear the states produced by goTo(fn) and next(). Define the current state as the same produced by initialState

Usage

const initialState = () => {/* ... */}
const stepA = () => {/* ... */}
const stepB = () => {/* ... */}
const timeline = [initialState, stepA, stepB];
const tt = TimeTraveler.create(timeline);

tt.next().next(); // the current state is stepB
tt.reset(); // the stepA and stepB was removed and the current state is initialState

How to use

import TimeTraveler from '@belchior/time-traveler';

const initialState = () => ({ stepA: '', stepB: '', });
const stepA = state => ({ ...state, stepA: 'done', });
const stepB = state => ({ ...state, stepB: 'done', });
const timeline = [ initialState, stepA, stepB, ];

const tt = TimeTraveler.create(timeline);

console.log('initialState:', tt.getState());
tt.next();             console.log('next (stepA)', tt.getState());
tt.next();             console.log('next (stepB)', tt.getState());
tt.prev();             console.log('prev (stepA)', tt.getState());
tt.goTo(initialState); console.log('goTo (initialState):', tt.getState());
tt.goTo(stepB);        console.log('goTo (stepB):', tt.getState());