@j0u1/finity 1.0.1 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # @j0u1/finity
2
2
 
3
3
  [![npm](https://img.shields.io/npm/v/@j0u1/finity)](https://www.npmjs.com/package/@j0u1/finity)
4
+ [![wakatime](https://wakatime.com/badge/user/42e0f82b-2688-4a48-85af-5eedd1812f70/project/7cf65f9a-042d-4ed8-ac25-717b8b4ff28b.svg)](https://wakatime.com/badge/user/42e0f82b-2688-4a48-85af-5eedd1812f70/project/7cf65f9a-042d-4ed8-ac25-717b8b4ff28b)
4
5
 
5
6
  Lightweight finite state machine (FSM) library for TypeScript.
6
7
 
@@ -29,8 +30,8 @@ const traffic = createMachine({
29
30
  traffic.current // "red"
30
31
  traffic.next() // "yellow"
31
32
  traffic.current // "yellow"
32
- traffic.can("green") // true
33
- traffic.can("red") // false
33
+ traffic.canChangeTo("green") // true
34
+ traffic.canChangeTo("red") // false
34
35
  ```
35
36
 
36
37
  ## API
@@ -48,4 +49,4 @@ Returns an object with:
48
49
 
49
50
  - **`current`** — current state
50
51
  - **`next()`** — transitions to the next state, returns new state. Throws if no transition exists.
51
- - **`can(state)`** — returns `true` if transition to given state is possible from current state
52
+ - **`canChangeTo(state)`** — returns `true` if transition to given state is possible from current state
@@ -0,0 +1,11 @@
1
+ interface ConfigType {
2
+ initial: string;
3
+ transitions: Record<string, string>;
4
+ }
5
+ export declare function createMachine(config: ConfigType): {
6
+ current: string;
7
+ transitions: Record<string, string>;
8
+ next(): string;
9
+ canChangeTo(state: string): boolean;
10
+ };
11
+ export {};
package/dist/index.js ADDED
@@ -0,0 +1,25 @@
1
+ // index.ts
2
+ class FinityError extends Error {
3
+ constructor(message) {
4
+ super(message);
5
+ this.name = "FinityError";
6
+ }
7
+ }
8
+ function createMachine(config) {
9
+ return {
10
+ current: config.initial,
11
+ transitions: config.transitions,
12
+ next() {
13
+ const nextState = this.transitions[this.current];
14
+ if (!nextState)
15
+ throw new FinityError(`No transition from "${this.current}"`);
16
+ return this.current = nextState;
17
+ },
18
+ canChangeTo(state) {
19
+ return this.transitions[this.current] === state;
20
+ }
21
+ };
22
+ }
23
+ export {
24
+ createMachine
25
+ };
package/package.json CHANGED
@@ -1,11 +1,20 @@
1
1
  {
2
2
  "name": "@j0u1/finity",
3
- "version": "1.0.1",
4
- "module": "index.ts",
3
+ "version": "1.0.3",
4
+ "description": " Lightweight finite state machine library for TypeScript",
5
+ "author": "j0u1",
6
+ "keywords": [
7
+ "fsm",
8
+ "state-machine"
9
+ ],
10
+ "files": [
11
+ "dist"
12
+ ],
13
+ "module": "./dist/index.js",
5
14
  "main": "./dist/index.js",
6
15
  "type": "module",
7
16
  "scripts": {
8
- "build": "bun build ./index.ts --outdir ./dist --format esm --packages external"
17
+ "build": "bun build ./index.ts --outdir ./dist --format esm --packages external && tsc -p tsconfig.build.json"
9
18
  },
10
19
  "repository": {
11
20
  "type": "git",
@@ -13,7 +22,7 @@
13
22
  },
14
23
  "license": "MIT",
15
24
  "devDependencies": {
16
- "@types/bun": "latest"
25
+ "@types/bun": "^1.3.14"
17
26
  },
18
27
  "peerDependencies": {
19
28
  "typescript": "^5"
package/bun.lock DELETED
@@ -1,26 +0,0 @@
1
- {
2
- "lockfileVersion": 1,
3
- "configVersion": 1,
4
- "workspaces": {
5
- "": {
6
- "name": "finity",
7
- "devDependencies": {
8
- "@types/bun": "latest",
9
- },
10
- "peerDependencies": {
11
- "typescript": "^5",
12
- },
13
- },
14
- },
15
- "packages": {
16
- "@types/bun": ["@types/bun@1.3.14", "", { "dependencies": { "bun-types": "1.3.14" } }, "sha512-h1hFqFVcvAvD9j9K7ZW7vd82aSA+rTdznZa+5bwvCwqSB1jmmfLcbIWhOLx1/+boy/xmjgCs/OMUL8hRJSmnPw=="],
17
-
18
- "@types/node": ["@types/node@25.8.0", "", { "dependencies": { "undici-types": ">=7.24.0 <7.24.7" } }, "sha512-TCFSk8IZh+iLX1xtksoBVtdmgL+1IX0fC9BeU4QqFSuNdN/K+HUlhqOzEmSYYpZUVsLYcPqc9KX+60iDuninSQ=="],
19
-
20
- "bun-types": ["bun-types@1.3.14", "", { "dependencies": { "@types/node": "*" } }, "sha512-4N0ig0fEomHt5R0KCFWjovxow98rIoRwKolrYdCcknNwMekCXRnWEUvgu5soYV8QXtVsrUD8B95MBOZGPvr6KQ=="],
21
-
22
- "typescript": ["typescript@5.9.3", "", { "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" } }, "sha512-jl1vZzPDinLr9eUt3J/t7V6FgNEw9QjvBPdysz9KfQDD41fQrC2Y4vKQdiaUpFT4bXlb1RHhLpp8wtm6M5TgSw=="],
23
-
24
- "undici-types": ["undici-types@7.24.6", "", {}, "sha512-WRNW+sJgj5OBN4/0JpHFqtqzhpbnV0GuB+OozA9gCL7a993SmU+1JBZCzLNxYsbMfIeDL+lTsphD5jN5N+n0zg=="],
25
- }
26
- }
package/index.test.ts DELETED
@@ -1,27 +0,0 @@
1
- import { test, expect } from "bun:test"
2
- import { createMachine } from "./index"
3
-
4
- test("initial state", () => {
5
- const m = createMachine({ initial: 'red', transitions: { red: 'yellow' } })
6
- expect(m.current).toBe('red')
7
- })
8
-
9
- test("next() changes state", () => {
10
- const m = createMachine({ initial: 'red', transitions: { red: 'yellow' } })
11
- expect(m.next()).toBe('yellow')
12
- })
13
-
14
- test("can() returns true if transition is possible", () => {
15
- const m = createMachine({ initial: 'red', transitions: { red: 'yellow' } })
16
- expect(m.can('yellow')).toBe(true)
17
- })
18
-
19
- test("can() returns false if transition is not possible", () => {
20
- const m = createMachine({ initial: 'red', transitions: { red: 'yellow' } })
21
- expect(m.can('green')).toBe(false)
22
- })
23
-
24
- test("next() throws if no transition exists", () => {
25
- const m = createMachine({ initial: 'red', transitions: {} })
26
- expect(() => m.next()).toThrow('No transition from "red"')
27
- })
package/index.ts DELETED
@@ -1,27 +0,0 @@
1
- interface ConfigType {
2
- initial: string,
3
- transitions: Record<string, string>
4
- }
5
-
6
- class FinityError extends Error {
7
- constructor(message: string) {
8
- super(message)
9
- this.name = 'FinityError'
10
- }
11
- }
12
-
13
- export function createMachine(config: ConfigType) {
14
- return {
15
- current: config.initial,
16
- transitions: config.transitions,
17
-
18
- next() {
19
- const nextState = this.transitions[this.current]
20
- if (!nextState) throw new FinityError(`No transition from "${this.current}"`)
21
- return this.current = nextState
22
- },
23
- can(state: string) {
24
- return this.transitions[this.current] === state
25
- }
26
- }
27
- }
package/tsconfig.json DELETED
@@ -1,30 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- // Environment setup & latest features
4
- "lib": ["ESNext"],
5
- "target": "ESNext",
6
- "module": "Preserve",
7
- "moduleDetection": "force",
8
- "jsx": "react-jsx",
9
- "allowJs": true,
10
- "types": ["bun"],
11
-
12
- // Bundler mode
13
- "moduleResolution": "bundler",
14
- "allowImportingTsExtensions": true,
15
- "verbatimModuleSyntax": true,
16
- "noEmit": true,
17
-
18
- // Best practices
19
- "strict": true,
20
- "skipLibCheck": true,
21
- "noFallthroughCasesInSwitch": true,
22
- "noUncheckedIndexedAccess": true,
23
- "noImplicitOverride": true,
24
-
25
- // Some stricter flags (disabled by default)
26
- "noUnusedLocals": false,
27
- "noUnusedParameters": false,
28
- "noPropertyAccessFromIndexSignature": false
29
- }
30
- }