@j0u1/finity 1.0.0 → 1.0.2

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
package/index.test.ts CHANGED
@@ -1,9 +1,27 @@
1
1
  import { test, expect } from "bun:test"
2
2
  import { createMachine } from "./index"
3
3
 
4
- test("начальное состояние", () => {
4
+ test("initial state", () => {
5
5
  const m = createMachine({ initial: 'red', transitions: { red: 'yellow' } })
6
6
  expect(m.current).toBe('red')
7
+ })
8
+
9
+ test("next() changes state", () => {
10
+ const m = createMachine({ initial: 'red', transitions: { red: 'yellow' } })
7
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' } })
8
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"')
9
27
  })
package/index.ts CHANGED
@@ -3,6 +3,13 @@ interface ConfigType {
3
3
  transitions: Record<string, string>
4
4
  }
5
5
 
6
+ class FinityError extends Error {
7
+ constructor(message: string) {
8
+ super(message)
9
+ this.name = 'FinityError'
10
+ }
11
+ }
12
+
6
13
  export function createMachine(config: ConfigType) {
7
14
  return {
8
15
  current: config.initial,
@@ -10,10 +17,10 @@ export function createMachine(config: ConfigType) {
10
17
 
11
18
  next() {
12
19
  const nextState = this.transitions[this.current]
13
- if (!nextState) throw new Error(`No transition from "${this.current}"`)
20
+ if (!nextState) throw new FinityError(`No transition from "${this.current}"`)
14
21
  return this.current = nextState
15
22
  },
16
- can(state: string) {
23
+ canChangeTo(state: string) {
17
24
  return this.transitions[this.current] === state
18
25
  }
19
26
  }
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@j0u1/finity",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "module": "index.ts",
5
5
  "main": "./dist/index.js",
6
6
  "type": "module",
7
7
  "scripts": {
8
- "build": "bun build ./index.ts --outdir ./dist --format esm --packages external"
8
+ "build": "bun build ./index.ts --outdir ./dist --format esm --packages external && tsc -p tsconfig.build.json"
9
9
  },
10
10
  "repository": {
11
11
  "type": "git",
@@ -0,0 +1,14 @@
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
+ }