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 🙏

© 2025 – Pkg Stats / Ryan Hefner

compiled-markov

v1.0.2

Published

A markov generator, with support for chain compilation

Readme

Compiled Markov

A Markov generator module for JS with support for compilation.

Installation

This package is available through npm as compiled-markov.

Here is an example of how to use it:

const Markov = require("compiled-markov")
const fs = require("fs")

// Read our source file
fs.readFile("./source.txt", "utf8", (error, data) => runProgram(data))

const runProgram = (source) => {
  const mark = new Markov()

  // Give the generator the source
  mark.initFromText(source)

  // Let's get some markov sentences! Make them start with 'fear'.
  console.log(mark.generateSentences(3, "fear"))

  // Give us the compiled JSON and write it to a file which we can
  // read later in our production product
  const compiled = mark.getCompiled()
  fs.writeFile("./compiled.txt", compiled, "utf8", (error) => console.log(error))
}

Pretty simple, huh?

In other JS environments, such as web platforms like React, you can pass a string to initFromText, or work out your own way of fetching and opening a text file. It can be a bit more difficult, since fs isn't supported in Node when it's run for browser applications.

wells-source.txt is provided as an example source file, taken from Project Gutenburg. This work (The War of the Worlds, H.G. Wells, 1897) is public domain - in the UK, at least.

Why 'compiled'?

You only have to feed in a source text once for this to work. All the data needed to generate sentences will then be in a compiled string that bears little resemblence to the original text. This means, you can run this locally to read from a source, and then only track and generate sentences from the compiled JSON.

This can be useful if you want to use copyrighted text to generate the Markov chain. Since it isn't the original text when written in this form, it's legal, probably.

DISCLAIMER: no responsibility can be accepted by any contributors to this programme for any legal issues, including but not limited to copyright issues, that result through the use of this programme.

Usage

Initialisation

initFromText(text): [no return value]

  • text: string, the source text string

The generator will generate a new markov chain from the source text provided.

initFromCompiled(compiled): [no return value]

  • compiled: string or Object, the compiled JSON

The generator will try to generate a new markov chain from the compiled JSON passed to it.

Compilation

getCompiled(): string

Returns the compiled JSON that can be passed back into initFromCompiled.

Generation

generateSentences(count, seed): string

  • count: int, the number of sentences to generate
  • seed: string (optional, default: undefined), the word to seed the sentence from

Randomly generate count sentences. seed can be used to choose how to start off generation - it will be the first word of the generated sentences. seed is case sensitive.

Contributing

Please, fork and make a pull request. Or, just make an issue. My JS is less than elegant, and I'm always interested to learn new and improved ways of doing things.