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 🙏

© 2026 – Pkg Stats / Ryan Hefner

state-machine-graph-lib

v0.1.1

Published

A state machine to model business processes

Downloads

36

Readme

state-machine-graph-lib

npm version

Una librería simple y ligera para modelar máquinas de estado dirigidas a través de grafos, con control de permisos en las transiciones. Ideal para gestionar flujos de trabajo, aprobaciones y procesos empresariales.


🚀 Instalación

Instalá la versión estable desde npm:

npm install [email protected]

También podés usar Yarn:

yarn add [email protected]

🧩 Conceptos básicos

El paquete utiliza una estructura de grafo dirigida (StateMachineGraph) que contiene:

  • nodes: Estados definidos por id y label.
  • edges: Transiciones entre nodos con permisos necesarios.
interface StateMachineGraph {
  nodes: { id: string; label: string; }[];
  edges: { source: string; target: string; permissions: string[] }[];
}

📌 Uso

import { StateMachine, StateMachineGraph } from 'state-machine-graph-lib';

// 1. Definí tu grafo
const graph: StateMachineGraph = {
  nodes: [
    { id: 'pendiente', label: 'Pendiente' },
    { id: 'aprobado', label: 'Aprobado' },
    { id: 'rechazado', label: 'Rechazado' }
  ],
  edges: [
    { source: 'pendiente', target: 'aprobado', permissions: ['can_approve'] },
    { source: 'pendiente', target: 'rechazado', permissions: ['can_reject'] }
  ]
};

// 2. Creá una instancia de StateMachine
const sm = new StateMachine();

// 3. Consultá los estados disponibles
const userPerms = ['can_approve'];
const next = sm.getNextAvailableStates('pendiente', userPerms, graph);
console.log(next); // ["aprobado"]

// 4. Validá una transición específica
const transition = sm.validTransition(userPerms, 'pendiente', 'aprobado', graph);
/*
{
  state: "aprobado",
  nextAvailableStates: []  // futuros estados posibles desde "aprobado"
}
*/
console.log(transition);

📚 API rápida

| Método | Descripción | |--------------------------------------|-----------------------------------------------------------------------------| | getNextAvailableStates(state, permissions, graph): string[] | Retorna una lista de estados destino válidos desde state, según permissions. | | getNextAvailableStatesByStates(permissions, statesArray, graph): string[][] | Devuelve los estados alcanzables para cada estado en statesArray. | | validTransition(permissions, fromState, toState, graph): { state: string, nextAvailableStates: string[] } | Comprueba si un estado es alcanzable, devolviendo el nuevo estado y siguientes posibles estados, o vacío si la transición no es válida. |


✅ ¿Por qué usar esta librería?

  • Fácil de usar: Interfaz simple para definir estados y permisos.
  • 🔐 Control basado en roles: Las transiciones sólo se permiten si los permisos coinciden.
  • 🧠 Ideal para procesos empresariales: Maneja workflows de aprobación, revisión, etc.
  • 📦 Sin dependencias extra: Usa solo @dagrejs/graphlib bajo el capó.

🧪 Ejemplo avanzado

const graph: StateMachineGraph = {
  nodes: [
    { id: 'draft', label: 'Borrador' },
    { id: 'review', label: 'En revisión' },
    { id: 'published', label: 'Publicado' }
  ],
  edges: [
    { source: 'draft', target: 'review', permissions: ['editor'] },
    { source: 'review', target: 'published', permissions: ['publisher'] },
    { source: 'review', target: 'draft', permissions: ['editor'] }
  ]
};

const sm = new StateMachine();
const perms = ['editor'];
console.log(sm.getNextAvailableStates('draft', perms, graph)); // → ["review"]

const perms2 = ['publisher'];
console.log(sm.validTransition(perms2, 'review', 'published', graph));
// → { state: 'published', nextAvailableStates: [] }

📜 Licencia

MIT


Disfrutá usando state-machine-graph-lib para tus flujos de estado. Si querés apoyo o tenés sugerencias, abrí un issue en GitHub.