{"id":25167,"library":"dfa","title":"dfa","description":"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.","status":"maintenance","version":"1.2.0","language":"javascript","source_language":"en","source_url":"ssh://git@github.com/devongovett/dfa","tags":["javascript","state","machine","compiler"],"install":[{"cmd":"npm install dfa","lang":"bash","label":"npm"},{"cmd":"yarn add dfa","lang":"bash","label":"yarn"},{"cmd":"pnpm add dfa","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Default export from 'dfa/compile', not a named export from 'dfa'.","wrong":"import { compile } from 'dfa'","symbol":"compile","correct":"import compile from 'dfa/compile'"},{"note":"ESM only; CommonJS require may fail if the package is not properly transpiled.","wrong":"const dfa = require('dfa')","symbol":"default","correct":"import dfa from 'dfa'"},{"note":"In CommonJS, the default export is under .default due to ESM compatibility.","wrong":"const compile = require('dfa/compile')","symbol":"compile (CJS)","correct":"const compile = require('dfa/compile').default"},{"note":"dfa is the default export of 'dfa' package, which exposes a compile method.","wrong":"import { dfa } from 'dfa'","symbol":"dfa","correct":"import dfa from 'dfa'; dfa.compile(...)"}],"quickstart":{"code":"import compile from 'dfa/compile';\nimport fs from 'fs';\n\nconst machineCode = `\nX = 0;\nL = 1;\nV = 2;\nT = 3;\nLV = 4;\nLVT = 5;\nM = 6;\ndecomposed = L V T?;\npartial = LV T?;\ncomposed = LVT;\nmain = (decomposed | partial | composed) M?;\n`;\n\nconst stateMachine = compile(machineCode);\n\n// Input as array of symbols (as per your symbol mapping)\nconst input = [0, 1, 2, 3, 0, 4, 6];\nfor (const [start, end] of stateMachine.match(input)) {\n  console.log(`Match from ${start} to ${end}`);\n}","lang":"typescript","description":"Shows how to define a state machine DSL, compile it, and find matches in an array of symbols."},"warnings":[{"fix":"Use import syntax or set up your environment to handle ESM packages. If you must use require(), use require('dfa/compile').default.","message":"The package uses ESM-only imports. CommonJS require() will not work without additional configuration (e.g., using .default).","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Consider using an alternative library or forking the repository if updates are needed.","message":"The package has not been updated since 2017 and the author has not responded to issues. No security fixes or new features are expected.","severity":"deprecated","affected_versions":"all"},{"fix":"Read the file contents using fs.readFileSync or similar, then pass the string to compile.","message":"The compile function expects a string containing the DSL, not a file path. Passing a file path will result in a parse error.","severity":"gotcha","affected_versions":"all"},{"fix":"Adjust your logic: the match covers indices from start to end-1 inclusive.","message":"The match() method returns an iterator of [start, end] arrays, but end is exclusive. Users often expect inclusive end indices.","severity":"gotcha","affected_versions":"all"},{"fix":"Map your input values to integers before using match().","message":"Symbols must be integers; non-integer values (e.g., strings) are not supported and will cause unexpected behavior.","severity":"gotcha","affected_versions":"all"},{"fix":"If upgrading from <1.2.0, update import statements from require('dfa/compile') to import compile from 'dfa/compile'.","message":"In version 1.2.0, the package changed from CommonJS to ESM. Older versions used CJS and had different import behavior.","severity":"breaking","affected_versions":"1.2.0"}],"env_vars":null,"last_verified":"2026-05-01T00:00:00.000Z","next_check":"2026-07-30T00:00:00.000Z","problems":[{"fix":"Use import syntax or set type: 'module' in package.json, or use dynamic import().","cause":"Using require() on an ESM-only module without a transpiler.","error":"SyntaxError: Unexpected token 'export'"},{"fix":"Use the correct import: import compile from 'dfa/compile'.","cause":"Using the default export incorrectly: e.g., import { compile } from 'dfa' instead of import compile from 'dfa/compile'.","error":"TypeError: compile is not a function"},{"fix":"Ensure your bundler (webpack, rollup, etc.) supports ESM and that the package is installed correctly.","cause":"The package is not recognized; possibly missing a build step or using an older bundler.","error":"Error: Failed to load module: dfa/compile"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}