{"id":23078251,"url":"https://github.com/basementuniverse/simple-state-machine","last_synced_at":"2025-04-03T12:43:54.892Z","repository":{"id":77330387,"uuid":"460910320","full_name":"basementuniverse/simple-state-machine","owner":"basementuniverse","description":"Define finite state machines and validate state transitions","archived":false,"fork":false,"pushed_at":"2023-10-25T12:22:26.000Z","size":80,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-04-26T04:03:06.820Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","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/basementuniverse.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,"governance":null}},"created_at":"2022-02-18T15:43:45.000Z","updated_at":"2022-02-18T15:56:30.000Z","dependencies_parsed_at":"2023-04-10T22:08:09.076Z","dependency_job_id":null,"html_url":"https://github.com/basementuniverse/simple-state-machine","commit_stats":{"total_commits":1,"total_committers":1,"mean_commits":1.0,"dds":0.0,"last_synced_commit":"c599db881ec8b51c30b887a3e568fff677db4133"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/basementuniverse%2Fsimple-state-machine","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/basementuniverse%2Fsimple-state-machine/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/basementuniverse%2Fsimple-state-machine/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/basementuniverse%2Fsimple-state-machine/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/basementuniverse","download_url":"https://codeload.github.com/basementuniverse/simple-state-machine/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247005397,"owners_count":20868019,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":[],"created_at":"2024-12-16T10:52:42.016Z","updated_at":"2025-04-03T12:43:54.855Z","avatar_url":"https://github.com/basementuniverse.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Simple State Machine\n\nDefine finite state machines and validate state transitions.\n\nThis library doesn't currently provide state-change events or methods for transitioning between states; all it does is provide a format for defining state machines, and a method for checking that a given transition is valid.\n\n## Installation\n\n```\nnpm install @basementuniverse/simple-state-machine\n```\n\n## Usage\n\n```typescript\nimport {\n  StateMachine,\n  validateTransition,\n} from '@basementuniverse/simple-state-machine';\n\n// Define a finite state machine\nconst myStateMachine: StateMachine = {\n  open: [\n    'closedAndUnlocked',\n  ],\n  closedAndUnlocked: [\n    'open',\n    'closedAndLocked',\n  ],\n  closedAndLocked: [\n    'closedAndUnlocked',\n  ],\n};\n\n// Let's validate some transitions...\nlet result: boolean = false;\n\n// Test #1\nresult = validateTransition(\n  myStateMachine,\n  'open',\n  'closedAndUnlocked'\n);\n\n// result will be true (this is a valid transition)\nconsole.assert(result);\n\n\n// Test #2\nresult = validateTransition(\n  myStateMachine,\n  'closedAndLocked',\n  'open'\n);\n\n// result will be false (this is not a valid transition)\nconsole.assert(!result);\n\n\n// Test #3\nresult = validateTransition(\n  myStateMachine,\n  'open',\n  'closedAndLocked',\n  {\n    throwErrors: true,\n  }\n);\n// this will throw an error with the message:\n// \"Invalid state transition: unable to transition from 'open' to 'closedAndLocked'.\"\n\n// Test #4\nresult = validateTransition(\n  myStateMachine,\n  'open',\n  'open',\n  {\n    allowSelfTransition: true,\n  }\n);\n\n// result will be true (this is a valid transition even though it's not explicitly defined)\nconsole.assert(result);\n```\n\n## Options\n\nThe `validateTransition` function accepts an optional `options` object as its fourth argument. The following options are available:\n\n| Option | Type | Default | Description |\n| --- | --- | --- | --- |\n| `allowSelfTransition` | `boolean` | `true` | If `true`, a transition from a state to itself will be considered valid. |\n| `throwErrors` | `boolean` | `false` | If `true`, an error will be thrown if the transition is invalid. |\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbasementuniverse%2Fsimple-state-machine","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbasementuniverse%2Fsimple-state-machine","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbasementuniverse%2Fsimple-state-machine/lists"}