Registry / serialization / dfa
library1.2.0jsnpmunverified

A JavaScript library that compiles a regular-expression-like syntax into fast deterministic finite automata (DFA). Version 1.2.0 is the latest as of 2025, with no active development in recent years. It is designed for pattern matching against non-string sequences (e.g., arrays of symbols) and provides a portable state machine representation. Supports common regex operators like alternation, concatenation, repetition, and tagging. Files are written in a custom CoffeeScript-like DSL and compiled to efficient match tables.

npm install dfa
INSTALL
IMPORT
SIG · DFA
D
dfa
serializationjavascriptv1.2.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

compile
import compile from 'dfa/compile'
import { compile } from 'dfa'
Default export from 'dfa/compile', not a named export from 'dfa'.
default
import dfa from 'dfa'
const dfa = require('dfa')
ESM only; CommonJS require may fail if the package is not properly transpiled.
compile (CJS)
const compile = require('dfa/compile').default
const compile = require('dfa/compile')
In CommonJS, the default export is under .default due to ESM compatibility.
dfa
import dfa from 'dfa'; dfa.compile(...)
import { dfa } from 'dfa'
dfa is the default export of 'dfa' package, which exposes a compile method.

Shows how to define a state machine DSL, compile it, and find matches in an array of symbols.

import compile from 'dfa/compile'; import fs from 'fs'; const machineCode = ` X = 0; L = 1; V = 2; T = 3; LV = 4; LVT = 5; M = 6; decomposed = L V T?; partial = LV T?; composed = LVT; main = (decomposed | partial | composed) M?; `; const stateMachine = compile(machineCode); // Input as array of symbols (as per your symbol mapping) const input = [0, 1, 2, 3, 0, 4, 6]; for (const [start, end] of stateMachine.match(input)) { console.log(`Match from ${start} to ${end}`); }
Debug
Known issues
gotchaThe package uses ESM-only imports. CommonJS require() will not work without additional configuration (e.g., using .default).
fix
Use import syntax or set up your environment to handle ESM packages. If you must use require(), use require('dfa/compile').default.
affects: >=1.0.0
deprecatedThe package has not been updated since 2017 and the author has not responded to issues. No security fixes or new features are expected.
fix
Consider using an alternative library or forking the repository if updates are needed.
affects: all
gotchaThe compile function expects a string containing the DSL, not a file path. Passing a file path will result in a parse error.
fix
Read the file contents using fs.readFileSync or similar, then pass the string to compile.
affects: all
gotchaThe match() method returns an iterator of [start, end] arrays, but end is exclusive. Users often expect inclusive end indices.
fix
Adjust your logic: the match covers indices from start to end-1 inclusive.
affects: all
gotchaSymbols must be integers; non-integer values (e.g., strings) are not supported and will cause unexpected behavior.
fix
Map your input values to integers before using match().
affects: all
breakingIn version 1.2.0, the package changed from CommonJS to ESM. Older versions used CJS and had different import behavior.
fix
If upgrading from <1.2.0, update import statements from require('dfa/compile') to import compile from 'dfa/compile'.
affects: 1.2.0
Errors
Common errors & fixes
SyntaxError: Unexpected token 'export'
Using require() on an ESM-only module without a transpiler.
fix
Use import syntax or set type: 'module' in package.json, or use dynamic import().
TypeError: compile is not a function
Using the default export incorrectly: e.g., import { compile } from 'dfa' instead of import compile from 'dfa/compile'.
fix
Use the correct import: import compile from 'dfa/compile'.
Error: Failed to load module: dfa/compile
The package is not recognized; possibly missing a build step or using an older bundler.
fix
Ensure your bundler (webpack, rollup, etc.) supports ESM and that the package is installed correctly.
Upgrade
Version history
1.2.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
11 hits · last 30 days
node
10
Resources
dfa — npm install dfa · libregistry