{"id":18675003,"url":"https://github.com/fsvieira/fsalib","last_synced_at":"2026-05-01T06:33:18.410Z","repository":{"id":57242399,"uuid":"150270935","full_name":"fsvieira/fsalib","owner":"fsvieira","description":"Simple finite state automaton, with some advanced operations.","archived":false,"fork":false,"pushed_at":"2018-11-10T22:54:08.000Z","size":92,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-10-03T22:10:17.652Z","etag":null,"topics":["automata","automaton","clean","determinism","fsa","intersection","json","minimization","subtract","union"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fsvieira.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-09-25T13:37:09.000Z","updated_at":"2018-11-10T22:54:10.000Z","dependencies_parsed_at":"2022-09-08T02:26:08.051Z","dependency_job_id":null,"html_url":"https://github.com/fsvieira/fsalib","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/fsvieira/fsalib","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fsvieira%2Ffsalib","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fsvieira%2Ffsalib/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fsvieira%2Ffsalib/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fsvieira%2Ffsalib/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fsvieira","download_url":"https://codeload.github.com/fsvieira/fsalib/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fsvieira%2Ffsalib/sbom","scorecard":{"id":412894,"data":{"date":"2025-08-11","repo":{"name":"github.com/fsvieira/fsalib","commit":"5e199f90cdbacdace6c949cb62e24016cc7d3904"},"scorecard":{"version":"v5.2.1-40-gf6ed084d","commit":"f6ed084d17c9236477efd66e5b258b9d4cc7b389"},"score":3,"checks":[{"name":"Packaging","score":-1,"reason":"packaging workflow not detected","details":["Warn: no GitHub/GitLab publishing workflow detected."],"documentation":{"short":"Determines if the project is published as a package that others can easily download, install, easily update, and uninstall.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#packaging"}},{"name":"Maintained","score":0,"reason":"0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project is \"actively maintained\".","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#maintained"}},{"name":"Pinned-Dependencies","score":-1,"reason":"no dependencies found","details":null,"documentation":{"short":"Determines if the project has declared and pinned the dependencies of its build process.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#pinned-dependencies"}},{"name":"Dangerous-Workflow","score":-1,"reason":"no workflows found","details":null,"documentation":{"short":"Determines if the project's GitHub Action workflows avoid dangerous patterns.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#dangerous-workflow"}},{"name":"Token-Permissions","score":-1,"reason":"No tokens found","details":null,"documentation":{"short":"Determines if the project's workflows follow the principle of least privilege.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#token-permissions"}},{"name":"SAST","score":0,"reason":"no SAST tool detected","details":["Warn: no pull requests merged into dev branch"],"documentation":{"short":"Determines if the project uses static code analysis.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#sast"}},{"name":"Binary-Artifacts","score":10,"reason":"no binaries found in the repo","details":null,"documentation":{"short":"Determines if the project has generated executable (binary) artifacts in the source repository.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#binary-artifacts"}},{"name":"Code-Review","score":0,"reason":"Found 0/26 approved changesets -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project requires human code review before pull requests (aka merge requests) are merged.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#code-review"}},{"name":"CII-Best-Practices","score":0,"reason":"no effort to earn an OpenSSF best practices badge detected","details":null,"documentation":{"short":"Determines if the project has an OpenSSF (formerly CII) Best Practices Badge.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#cii-best-practices"}},{"name":"Security-Policy","score":0,"reason":"security policy file not detected","details":["Warn: no security policy file detected","Warn: no security file to analyze","Warn: no security file to analyze","Warn: no security file to analyze"],"documentation":{"short":"Determines if the project has published a security policy.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#security-policy"}},{"name":"Vulnerabilities","score":10,"reason":"0 existing vulnerabilities detected","details":null,"documentation":{"short":"Determines if the project has open, known unfixed vulnerabilities.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#vulnerabilities"}},{"name":"Fuzzing","score":0,"reason":"project is not fuzzed","details":["Warn: no fuzzer integrations found"],"documentation":{"short":"Determines if the project uses fuzzing.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#fuzzing"}},{"name":"License","score":10,"reason":"license file detected","details":["Info: project has a license file: LICENSE:0","Info: FSF or OSI recognized license: MIT License: LICENSE:0"],"documentation":{"short":"Determines if the project has defined a license.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#license"}},{"name":"Signed-Releases","score":-1,"reason":"no releases found","details":null,"documentation":{"short":"Determines if the project cryptographically signs release artifacts.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#signed-releases"}},{"name":"Branch-Protection","score":0,"reason":"branch protection not enabled on development/release branches","details":["Warn: branch protection not enabled for branch 'master'"],"documentation":{"short":"Determines if the default and release branches are protected with GitHub's branch protection settings.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#branch-protection"}}]},"last_synced_at":"2025-08-18T23:09:14.999Z","repository_id":57242399,"created_at":"2025-08-18T23:09:15.000Z","updated_at":"2025-08-18T23:09:15.000Z"},"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32487459,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-30T13:12:12.517Z","status":"online","status_checked_at":"2026-05-01T02:00:05.856Z","response_time":64,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["automata","automaton","clean","determinism","fsa","intersection","json","minimization","subtract","union"],"created_at":"2024-11-07T09:21:39.346Z","updated_at":"2026-05-01T06:33:18.394Z","avatar_url":"https://github.com/fsvieira.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Finite State Automaton Library\n\nIts a very simple fSA lib, with some advanced operations like:\n\n * Clean dead and unreachable states,\n * Determinism,\n * Minimization,\n * Union,\n * Intersection,\n * Subtraction,\n * Negation,\n * walk methods (delta, hasfinals, filterFinals, walk, ...),\n * toDot,\n * fromJSON,\n * toJSON\n\nAll operations except clean and fromJSON are non-destructive, this means that the \nresult is a new FSA and origin FSA is unchanged.\n\n# Install  \n\n`npm install fsalib --save`\n\n# Use\n\n```javascript\n    const FSA = require(\"fsalib\");\n\n    const abc = new FSA();\n    const def = new FSA();\n\n    {\n        const s = abc.getStart();\n        const s1 = abc.newState();\n        const s2 = abc.newState();\n        const s3 = abc.newState();\n\n        abc.setFinal(s3);\n\n        abc.transition(s, 'a', s1);\n        abc.transition(s1, 'b', s2);\n        abc.transition(s2, 'c', s3);\n    }\n\n    {\n        const s = def.getStart();\n        const s1 = def.newState();\n        const s2 = def.newState();\n        const s3 = def.newState();\n\n        def.setFinal(s3);\n\n        def.transition(s, 'd', s1);\n        def.transition(s1, 'e', s2);\n        def.transition(s2, 'f', s3);\n    }\n\n    // clean a,\n    abc.clean();\n\n    // create a deterministic FSA from abc,\n    const det = abc.deterministic();\n\n    // create new minimized FSA from abc.\n    const min = fa.minimize();\n\n    // union of both FSA,\n    // result (un) will acept all words that are acepted by abc or def.\n    const un = abc.union(def);\n\n    // intersect both FSA,\n    // result (inter) will acept all words acepted by both FSA (abc and def).\n    const inter = abc.intersect(def);\n    \n    // subtract abc - def,\n    // result (sub) FSA will acept all words acepted by abc but not the ones acepted by def.\n    const sub = abc.subtract(def);\n\n    // negation,\n    // will acepted all words  \n    // it will create a FSA aceptiong all possible words\n    // generated by abc alphabet except the words acepted by abc.\n    const neg = abc.negation();\n\n    // print dot (graphviz graph version)\n    // handy for debug.\n    console.log(abc.toDot());\n\n    // serialize to json,\n    // return a json description of the abc FSA.\n    console.log(JSON.stringify(abc.toJSON()))\n\n    // deserialize from json,\n    // create a copy of abc.\n    const f = FSA.fromJSON(abc.toJSON());\n```\n\n## More toDot() options\n\nThe method toDot can receive one object argument to override how symbol and states are converted to strings.\n\nExample: \n\n```\n    const a = new FA();\n\n    const s = a.getStart();\n    const s1 = a.newState();\n    const s2 = a.newState();\n    const s3 = a.newState();\n    const s4 = a.newState();\n\n    a.setFinal(s2);\n    a.setFinal(s4);\n\n    a.transition(s, 'a', s1);\n    a.transition(s1, 'b', s2);\n\n    a.transition(s, 'a', s3);\n    a.transition(s3, 'c', s4);\n\n    const ad = a.deterministic();\n\n    // Create toString functions,\n\n    /* toStringState:\n        The function receives a state as argument.\n\n        We are goint to rewrite start state as \"START\", all other states will be \n        prefixed with \"S_\".\n     */ \n    const toStringState = (state =\u003e ad.start===state?\"START\":\"S_\" + state);\n\n    /* toStringSymbol:\n        The function receives as arguments:\n            * the transition from state,\n            * the transition symbol,\n            * the transition to state.\n\n        we are going to atach to each symbol-\u003eto the information of \n        the minium level of to state.        \n    */\n    const toStringSymbol = ((from, symbol, to) =\u003e {\n        let level = 0;\n        do {\n            level++;\n        } while (!ad.positionStates(level).has(to));\n\n        return `${symbol} [level=${level}]`;\n    });\n\n    // now we call toDot with both functions (or with just one function),\n    console.log(\n        ad.toDot(\n            // if a function one or the two functions are not provided then it will use the default.\n            {\n                toStringState,\n                toStringSymbol\n            }\n        )\n    );\n\n    /*\n        This should print something like this:\n\n        digraph G {\n            rankdir=LR;\n            size=\"8,5\"\n            node [shape = doublecircle]; S_3 S_4;\n            node [shape = circle];\n            START -\u003e S_2 [label = \"a [level=1]\"]\n            S_2 -\u003e S_3 [label = \"b [level=2]\"]\n            S_2 -\u003e S_4 [label = \"c [level=2]\"]\n        }\n\n        You can check it with this website: https://dreampuf.github.io/GraphvizOnline/\n    */\n\n```\n\n\n# Walk Methods\n\n## delta(froms, symbol)\n\n * froms, a set of states,\n * symbol, the transition symbol.\n\nExample:\n```javascript\n    const FSA = require(\"fsalib\");\n    \n    // create a abc FSA\n    const abc = new FSA();\n\n    const s = abc.getStart();\n    const s1 = abc.newState();\n    const s2 = abc.newState();\n    const s3 = abc.newState();\n\n    abc.setFinal(s3);\n\n    abc.transition(s, 'a', s1);\n    abc.transition(s1, 'b', s2);\n    abc.transition(s2, 'c', s3);\n\n    // make first transtion with delta,\n    // start from abc start state.\n    const start = abc.delta(new Set([abc.getStart()]), 'a'),\n    \n    // we can also encapsulate calls\n    const froms = abc.delta(\n        abc.delta(\n            start,\n            'b'\n        ),\n        'c'\n    );\n\n    // after getting the froms results, we can get \n    // the finals using filterFinals.\n    const finals = abc.filterFinals(froms);\n    \n    // or if only want to know that word is accepted we can do this:\n    const accepted = abc.hasFinal(froms);\n\n    // print all abc path resulting states, in this case its the final state s3\n    console.log([...froms].join(\", \"));\n    \n    // print all abc path final states, in this case s3\n    console.log([...finals].join(\", \"));\n    \n    // print if word abc was accepted, in this case yes (true).\n    console.log(accepted);\n```\n\n## walk(symbol, ...symbol)\n\n The walk method is a curry function, so it can receive an arbitrary number of symbols, it can \n also be called by steps.\n \n Return: the walk function return another function, with associeted attributes finals (Set) and states (Set).\n Words are accepted if at the end of word, states and finals are not empty.\n \n \n Examples (consider abc as FSA accepting word abc):\n ```javascript \n    \n    const s = abc.walk('a', 'b', 'c');\n    console.log([...s.finals]);\n    console.log([...s.states]);\n  ```     \n  \n```javascript \n    \n    const s = abc.walk('a')('b', 'c');\n    console.log([...s.finals]);\n    console.log([...s.states]);\n```     \n  \n  ```javascript \n    \n    const step1 = abc.walk('a');\n    console.log([...step1.finals]); // finals is empty\n    console.log([...step1.states]); // but states is not, \n    \n    const s = step1('a', 'b');\n    console.log([...s.finals]); // finals is not empty\n    console.log([...s.states]); // states is not empty\n```\n  \n```javascript \n    const steps = abc.walk(); // walk can be called with no symbols,\n    // but returning function must always be called with at least one symbol.\n    const s = steps('a')('b')('c');\n    console.log([...s.finals]); // finals is not empty\n    console.log([...s.states]); // states is not empty\n```\n  \n```javascript \n    const accepted = abc.walk(...\"abc\".split(\"\")).finals.size \u003e 0;\n    const rejected = abc.walk(...\"abcd\".split(\"\")).finals.size === 0;\n    \n    console.log(\"abc is accepted: \" + accepted?\"yes\":\"no\");\n    console.log(\"abcd is accepted: \" + !rejected?\"yes\":\"no\");\n```\n\n## positionStates (position)\n\n    It will get the states on given position, where position is the walking depth \n    of the FA, starting from start.\n\n    Returns a Set of states, if there is no states at the given position a empty Set \n    is returned.\n\n    Example:\n```javascript\n    const FSA = require(\"fsalib\");\n\n    const abc = new FSA();\n\n    const s = abc.getStart();\n    const s1 = abc.newState();\n    const s2 = abc.newState();\n    const s3 = abc.newState();\n\n    abc.setFinal(s3);\n\n    abc.transition(s, 'a', s1);\n    abc.transition(s1, 'b', s2);\n    abc.transition(s1, 'c', s3);\n    \n    const start = abc.positionStates(0);\n    /**\n     * the states at position 0 is the start state,\n     * we walk 0 symbols from start.\n     */\n    console.log([...start].join(\", \")); // output: s\n\n    const depthOne = abc.positionStates(1);\n    /**\n     * the states at position 1, in this case are the same as\n     * walk(\"a\"), it will return s1 state.\n     */\n    console.log([...depthOne].join(\", \")); // output: s1\n\n    /**\n     * the states at position 2, in this case are the same as\n     * walk(\"a\")(\"b\") UNION walk(\"a\")(\"c\"), it will return s2 and s3 state.\n     */\n    const depthTwo = abc.positionStates(2);\n    console.log([...depthTwo].join(\", \")); // output: s2, s3\n```\n\n\n\n\n# FA Fields\n\nIf walk mehods are not engough you can access the FA fields directly.\nHere is a example taken from the toJSON function:\n\n```javascript\n    const FSA = require(\"fsalib\");\n\n    const fa = new FSA();\n    const transitions = [];\n\n    // this.transitions is a Map where key is a from state\n    // and value is another Map with key as symbol and values \n    // a Set of destination states (tos).\n    for (let [from, symbols] of fa.transitions) {\n        const ss = [];\n        // symbols is a Map with key symbols and value a Set of to states.\n        for (let [symbol, tos] of symbols) {\n            // In case FA is deterministic the tos is Set with only one element.\n            ss.push([symbol, [...tos]]);\n        }\n\n        transitions.push([from, ss]);\n    }\n\n    return {\n        // Final states,\n        finals: [...fa.finals],\n        \n        // all states, including final states,\n        states: [...fa.states],\n\n        // the start state,\n        start: this.start,\n\n        // Symbols the FA alphabet,\n        symbols: [...fa.symbols],\n\n        // FA transitions,\n        transitions,\n\n        // its the state id counter,\n        // everytime a state is created we return the current ids value and \n        // increment it, for next round, messing up the ids is not a good ideia. \n        ids: fa.ids\n    };\n```\n\n# More Examples\n\nThere are lot more examples/test here: https://github.com/fsvieira/fsalib/blob/master/src/fsa.test.js\n\n# Use Cases \n\n    The use cases of Finite State Automata are very large well known and document\n    on many sources. \n\n    Some examples can be, regular expressions, pattern analyses/recognition, \n    application/game state machines ...\n\n    With a little creativity we can found some use for a FSA, on almost any application.\n\n\n\n    \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffsvieira%2Ffsalib","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffsvieira%2Ffsalib","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffsvieira%2Ffsalib/lists"}