@j0u1/finity 1.0.2 → 1.0.4
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 +3 -1
- package/dist/index.d.ts +11 -0
- package/dist/index.js +25 -0
- package/package.json +14 -3
- package/bun.lock +0 -26
- package/index.test.ts +0 -27
- package/index.ts +0 -27
- package/tsconfig.build.json +0 -14
- package/tsconfig.json +0 -30
package/README.md
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
# @j0u1/finity
|
|
2
2
|
|
|
3
|
+
Lightweight finite state machine (FSM) library for TypeScript.
|
|
4
|
+
|
|
3
5
|
[](https://www.npmjs.com/package/@j0u1/finity)
|
|
4
6
|
[](https://wakatime.com/badge/user/42e0f82b-2688-4a48-85af-5eedd1812f70/project/7cf65f9a-042d-4ed8-ac25-717b8b4ff28b)
|
|
7
|
+
[](https://badge.socket.dev/npm/package/@j0u1/finity)
|
|
5
8
|
|
|
6
|
-
Lightweight finite state machine (FSM) library for TypeScript.
|
|
7
9
|
|
|
8
10
|
## Install
|
|
9
11
|
|
package/dist/index.d.ts
ADDED
|
@@ -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,7 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@j0u1/finity",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"
|
|
3
|
+
"version": "1.0.4",
|
|
4
|
+
"description": " Lightweight finite state machine library for TypeScript",
|
|
5
|
+
"author": "j0u1",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"fsm",
|
|
8
|
+
"state-machine",
|
|
9
|
+
"finite-state-machine",
|
|
10
|
+
"fsm-library"
|
|
11
|
+
],
|
|
12
|
+
"files": [
|
|
13
|
+
"dist"
|
|
14
|
+
],
|
|
15
|
+
"module": "./dist/index.js",
|
|
5
16
|
"main": "./dist/index.js",
|
|
6
17
|
"type": "module",
|
|
7
18
|
"scripts": {
|
|
@@ -13,7 +24,7 @@
|
|
|
13
24
|
},
|
|
14
25
|
"license": "MIT",
|
|
15
26
|
"devDependencies": {
|
|
16
|
-
"@types/bun": "
|
|
27
|
+
"@types/bun": "^1.3.14"
|
|
17
28
|
},
|
|
18
29
|
"peerDependencies": {
|
|
19
30
|
"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
|
-
canChangeTo(state: string) {
|
|
24
|
-
return this.transitions[this.current] === state
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
}
|
package/tsconfig.build.json
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"lib": ["ESNext"],
|
|
4
|
-
"target": "ESNext",
|
|
5
|
-
"module": "ESNext",
|
|
6
|
-
"moduleResolution": "bundler",
|
|
7
|
-
"declaration": true,
|
|
8
|
-
"emitDeclarationOnly": true,
|
|
9
|
-
"outDir": "./dist",
|
|
10
|
-
"strict": true,
|
|
11
|
-
"skipLibCheck": true
|
|
12
|
-
},
|
|
13
|
-
"include": ["index.ts"]
|
|
14
|
-
}
|
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
|
-
}
|