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

mchains

v1.1.6

Published

0 dependency simple string markov chains

Readme

mchains.js

Simple library to generate string markov chains

install

npm i mchains --save

usage

supports UMD

usage with commonjs:


const Chain = require('mchains');

usage in the browser without any loader

<!-- In the html-->
<script src="node_modules/mchains/mchains.js"></script>
<script src="yourCode.js"></script>

//inside your javascript source
const Chain = window.mchains;

documentation

constructor


new Chain(parameters);

Parameters:

  • Configuration object(optional)
  • Training data(optional)

Configuration object(optional):

order:

Defines how many items are grouped together to form a state, in the case of strings that is either words or characters If you want to use this for word generation a lower order will produce more random-like patterns than higher order values

type:

Determines how the strings are parsed and generated:

  • "character" will make the strings be parsed as sequences of characters
  • "word" will make the strings be parsed as sequences of words (strings delimited by " ", "\n" and "\r" )

Training data:

Optional training data for the chain to process, more information about training data below.

methods

train()

Takes data to train from it generating new states for the chain.

Valid data:

  • A string
  • An array of strings
  • An object with string values in its properties

Examples:


//option 1:
const training = ' just a simple string';

//option 2:
const training = ['first string', 'second string', ['string inside an array', 'etc...']];

//option 3: 
const training = {
    firstSampleData: ['first string', 'second string', ['string inside an array', 'etc...']],
    secondSampleData: {
        whateverKey: ['first string', 'second string', ['string inside an array', 'etc...']],
        anotherKey: 'just a simple string'
    },
    thirdSampleData: 'just a simple string'
}

doesStateExist(String)

returns true if the string matches one of the chain states, false otherwise

configOutput(config)

Takes a configuration object to configure the output of the generate method

Configuration object properties:

  • minLength -> defaults to chain order
  • maxLength -> defaults to chain's order * 2
  • amount -> defaults to 1
  • capitalizeFirst -> defaults to false
  • cropToLength -> defaults to false

getNgrams()

returns an array of strings, representing the unique ngrams found in the provided training data

generate(config)

returns an array of strings generated randomly from the internal states of the chain

  • The config param matches the structure of the parameter of configOutput method

Properties

Note: public properties that are ment to be readonly, they are initialized with the cosntructor:

Chain.order

returns the order of the chain, that is, how many items are grouped together to form each state

let chain = new Chain();
chain.order 

Chain.type

returns the method used to parse strings, if Chain.type is not String this property will not be used by the algorithm

let chain = new Chain();
chain.type 

live examples

The following page uses this algorithm in order to generate some of the names demo page