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

dfa-machine

v1.0.2

Published

A simple implementation of a dynamic finite state machine represinting an instance of a DFA (deterministic finite automata)

Downloads

3

Readme

dfa-machine

A simple implementation of a dynamic finite state machine represinting an instance of a DFA (deterministic finite automata)

Installation

Install dfa-machine using npm with the command $ npm install dfa-machine --save

Abstract

Any deterministic finite automata is defined by five tuples (Q,Σ,q0,δ,F) where:

  • Q: The set of all states
  • Σ (sigma): The set of all accepted inputs
  • q0: The initial or starting state
  • δ (delta): The transition function δ: Q X Σ ⟶ Q
  • F: The set of final/accepted states

Illustration

dfa-machine uses the same concept and implements it in javascript as follows: The constructor of the DFA accpets two arguments:

  1. sigma: an array of all accepted inputs The current version supports inputs of the types String, Number, and Boolean only
  2. machine object: an object describing initial state, final state(s), all states, and the trnsition functions/course for each state.
    • The key initial must be present and have the value of a string represinting the name/key of the initial state which will be provided in the states object.
    • The key final must be present and have the value of an array describing the states which will be considered accepted as a final states for a given string to be validated.
    • The key states must be present and have the value of an object describing each state as a key (including but not limited to the initial and final states).
      • states key must have the value of an object such as {on: {input1: 'next-state', input2: 'next-state', ...etc}} which will describe the transition function for each state while reading all inputs to be applied.

Usage

const DFA = require('dfa-machine');

In this example we will create a dfa on Σ = {0,1} that accepts strings of length greater than 0 with an even number of 1s which described in the below figure.

dfa-example

Our states:

  • q0: The initial state. It represents either an empty string or a string with no 1s in it (non-final).
  • q1: Represents an odd number of 1s (non-final).
  • q2: Represents an even number of 1s (final).

With that in mind we will setup our DFA instance as follows:

const sigma = [0, 1];

const machineObj = {
  initial: 'q0', // `q0` is the key-name of the initial state provided in `states` object
  final: ['q2'], // `['q2']` is the key-name(s) of the states for the machine to consider final/acceptable to end with (which must be present in `states` key below).
  states: {
    q0: { on: { 0: 'q0', 1: 'q1' } }, // the `on` object will describe the course for the transition function to follow as when recieving input 0 while the state is q0 the next state will be q0
    q1: { on: { 0: 'q1', 1: 'q2' } },
    q2: { on: { 0: 'q2', 1: 'q1' } },
  },
};

const dfa = new DFA(sigma, machineObj);

dfa.validate('1101101001').status; // Valid

// OR

dfa.validate('1101101001');
dfa.status; // Valid

Examples

See more examples here: https://github.com/ahmedisam99/dfa-machine/tree/master/examples

License

MIT