1.0.1 • Published 2 years ago

@bruju/automata-composer v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

Automata Composer

This package lets users build finite state automata by composing automatas with the operators chain (concatenation), or (|), maybe (?), star (*), plus (+) and inverse (reverse all transitions).

Transitions are strings of any length and minimization is automatically applied when the automata is built.

How to use

Recognize a word

import { unit } from "@bruju/automata-composer"; 
const automata = unit("hamster").build();
console.log(automata.test(["hamster"])); // true
console.log(automata.test(["ham", "ster"])); // false

Recognize a known sequence

import { chain, unit } from "@bruju/automata-composer"; 
const helloWorld = chain(unit("Hello"), unit("world")).build();

console.log(helloWorld.test(["Hello", "world"])); // true

A more complex pattern

import { chain, unit, or, plus, maybe } from "@bruju/automata-composer"; 

const theAnimals = chain(
  or(unit("Save"), unit("Kill")),
  unit("the"),
  unit("animals"),
  plus(unit("!")),
  maybe(unit("(This is a GDQ reference)"))
).build();

// Both true
console.log(theAnimals.test(["Save", "the", "animals", "!", "!", "!"]));
console.log(theAnimals.test([
  "Kill", "the", "animals", "!", "(This is a GDQ reference)"
]));

Theory

Theory is partly inspired by :

License

Licensed under the MIT Licence by Julian Bruyat