@bemedev/app-bemedev-plugins 0.0.0-stage → 0.0.1
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 +55 -2
- package/lib/index.d.ts +3 -0
- package/lib/index.js +1 -0
- package/lib/plugin.d.ts +29 -0
- package/lib/plugin.d.ts.map +1 -0
- package/lib/plugin.js +28 -0
- package/lib/plugin.js.map +1 -0
- package/lib/swap.d.ts +38 -0
- package/lib/swap.d.ts.map +1 -0
- package/lib/swap.js +31 -0
- package/lib/swap.js.map +1 -0
- package/package.json +93 -3
package/README.md
CHANGED
|
@@ -1,3 +1,56 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @bemedev/app-bemedev-plugins
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Optional utilities for [`@bemedev/app`](../../app), delivered as
|
|
4
|
+
[options plugins](../../options). They are not part of the default helpers: opt in
|
|
5
|
+
explicitly with `use`.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pnpm add @bemedev/app-bemedev-plugins
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## `swap`
|
|
14
|
+
|
|
15
|
+
`swap` remaps the parameters of a standard function into machine-selector arguments
|
|
16
|
+
using decomposed paths. It works with actions, `assign`, guards, delays, and any
|
|
17
|
+
other location accepting a selector.
|
|
18
|
+
|
|
19
|
+
### As a plugin
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
import { createOptions } from '@bemedev/app-options';
|
|
23
|
+
import { swapPlugin } from '@bemedev/app-bemedev-plugins';
|
|
24
|
+
|
|
25
|
+
const options = createOptions(contract).use(swapPlugin)(({ swap, assign }) => ({
|
|
26
|
+
actions: {
|
|
27
|
+
setInput: assign(
|
|
28
|
+
'context.input',
|
|
29
|
+
swap((value: string) => value.trim(), 'WRITE')({ '[0]': '[0].payload.value' }),
|
|
30
|
+
),
|
|
31
|
+
},
|
|
32
|
+
guards: {
|
|
33
|
+
isAdult: swap((age: number) => age >= 18)({ '[0]': '[0].context.user.age' }),
|
|
34
|
+
},
|
|
35
|
+
}));
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
On a machine, attach the plugin through its types:
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
import { createMachine } from '@bemedev/app';
|
|
42
|
+
import { swapPlugin } from '@bemedev/app-bemedev-plugins';
|
|
43
|
+
|
|
44
|
+
const machine = createMachine(
|
|
45
|
+
config,
|
|
46
|
+
{ plugins: [swapPlugin] },
|
|
47
|
+
).provideOptions(({ swap }) => ({ ... }));
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### As a standalone function
|
|
51
|
+
|
|
52
|
+
```ts
|
|
53
|
+
import { swap } from '@bemedev/app-bemedev-plugins';
|
|
54
|
+
|
|
55
|
+
const readAge = swap((age: number) => age)({ '[0]': '[0].context.age' });
|
|
56
|
+
```
|
package/lib/index.d.ts
ADDED
package/lib/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { plugin } from "./plugin.js";
|
package/lib/plugin.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { Swap_F } from "./swap.js";
|
|
2
|
+
/**
|
|
3
|
+
* Options plugin adding the `swap` helper to the options surface.
|
|
4
|
+
*
|
|
5
|
+
* The helper left the default helpers, so it is opted in through the plugin
|
|
6
|
+
* system, either with `createOptions(contract).use(swapPlugin)` or with the
|
|
7
|
+
* `plugins` entry of the machine types.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* import { createOptions } from '@bemedev/app-options';
|
|
12
|
+
* import { swapPlugin } from '@bemedev/app-bemedev-plugins';
|
|
13
|
+
*
|
|
14
|
+
* const options = createOptions(contract).use(swapPlugin)(({ swap }) => ({
|
|
15
|
+
* actions: {
|
|
16
|
+
* increment: swap((value: number) => value + 1)({
|
|
17
|
+
* '[0]': '[0].context',
|
|
18
|
+
* }),
|
|
19
|
+
* },
|
|
20
|
+
* }));
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export declare const plugin: {
|
|
24
|
+
swap: import("@bemedev/app-plugins").UtilityPair<(fn: any, event?: any) => (types: any) => import("@bemedev/app-contract").FnR<import("@bemedev/app-contract").Contract, any>, (fn: any, event?: any) => (types: any) => import("@bemedev/app-contract").FnR<import("@bemedev/app-contract").Contract, any>>;
|
|
25
|
+
};
|
|
26
|
+
/** Type of the plugin returned by {@linkcode plugin}. */
|
|
27
|
+
export type Plugin = typeof plugin;
|
|
28
|
+
export type { Swap_F };
|
|
29
|
+
//# sourceMappingURL=plugin.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugin.d.ts","names":[],"sources":["../src/plugin.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;qBAyBa;;;;YAGD,gBAAgB"}
|
package/lib/plugin.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { swap } from "./swap.js";
|
|
2
|
+
import { createUtilities, same } from "@bemedev/app-plugins";
|
|
3
|
+
// src/plugin.ts
|
|
4
|
+
/**
|
|
5
|
+
* Options plugin adding the `swap` helper to the options surface.
|
|
6
|
+
*
|
|
7
|
+
* The helper left the default helpers, so it is opted in through the plugin
|
|
8
|
+
* system, either with `createOptions(contract).use(swapPlugin)` or with the
|
|
9
|
+
* `plugins` entry of the machine types.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* import { createOptions } from '@bemedev/app-options';
|
|
14
|
+
* import { swapPlugin } from '@bemedev/app-bemedev-plugins';
|
|
15
|
+
*
|
|
16
|
+
* const options = createOptions(contract).use(swapPlugin)(({ swap }) => ({
|
|
17
|
+
* actions: {
|
|
18
|
+
* increment: swap((value: number) => value + 1)({
|
|
19
|
+
* '[0]': '[0].context',
|
|
20
|
+
* }),
|
|
21
|
+
* },
|
|
22
|
+
* }));
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
const plugin = createUtilities({}, () => ({ swap: same(swap) }));
|
|
26
|
+
export { plugin };
|
|
27
|
+
|
|
28
|
+
//# sourceMappingURL=plugin.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugin.js","names":[],"sources":["../src/plugin.ts"],"sourcesContent":["import { createUtilities, same } from '@bemedev/app-plugins';\nimport { swap } from './swap';\nexport type { Swap_F } from './swap';\n\n/**\n * Options plugin adding the `swap` helper to the options surface.\n *\n * The helper left the default helpers, so it is opted in through the plugin\n * system, either with `createOptions(contract).use(swapPlugin)` or with the\n * `plugins` entry of the machine types.\n *\n * @example\n * ```ts\n * import { createOptions } from '@bemedev/app-options';\n * import { swapPlugin } from '@bemedev/app-bemedev-plugins';\n *\n * const options = createOptions(contract).use(swapPlugin)(({ swap }) => ({\n * actions: {\n * increment: swap((value: number) => value + 1)({\n * '[0]': '[0].context',\n * }),\n * },\n * }));\n * ```\n */\nexport const plugin = createUtilities({}, () => ({ swap: same(swap) }));\n\n/** Type of the plugin returned by {@linkcode plugin}. */\nexport type Plugin = typeof plugin;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAyBA,MAAa,SAAS,gBAAgB,CAAC,UAAU,EAAE,MAAM,KAAK,IAAI,EAAE,EAAE"}
|
package/lib/swap.d.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { Contract, FnR } from "@bemedev/app-contract";
|
|
2
|
+
/**
|
|
3
|
+
* Signature of the {@linkcode swap} helper.
|
|
4
|
+
*
|
|
5
|
+
* The mapping keys are passed as decomposed paths, so the returned function
|
|
6
|
+
* stays a standard machine selector of type {@linkcode FnR}.
|
|
7
|
+
*
|
|
8
|
+
* @remarks
|
|
9
|
+
* The plugin surface is not parameterized by the consumer contract, so the
|
|
10
|
+
* mapping keys are untyped. Annotate a value with `Swap<MyContract>` to keep
|
|
11
|
+
* the returned selector typed against a specific contract.
|
|
12
|
+
*
|
|
13
|
+
* @template | {@linkcode Contract} `Ct` - Contract of the machine.
|
|
14
|
+
*/
|
|
15
|
+
export type Swap_F<Ct extends Contract = Contract> = <F extends (...args: any[]) => any, Ev extends string = string>(fn: F, event?: Ev) => (types: any) => FnR<Ct, ReturnType<F>>;
|
|
16
|
+
/**
|
|
17
|
+
* Remaps the arguments of a function from decomposed state paths.
|
|
18
|
+
*
|
|
19
|
+
* Unlike the former default helper, this function does not need the machine
|
|
20
|
+
* contract: an event name scopes the swap to that event, and any other event
|
|
21
|
+
* falls back to the no-op value.
|
|
22
|
+
*
|
|
23
|
+
* @param fn - Function to swap.
|
|
24
|
+
* @param event - Optional event name scoping the swap.
|
|
25
|
+
*
|
|
26
|
+
* @returns A function mapping decomposed paths to the swapped function.
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```ts
|
|
30
|
+
* import { swap } from '@bemedev/app-bemedev-plugins';
|
|
31
|
+
*
|
|
32
|
+
* const inc = swap((value: number) => value + 1)({
|
|
33
|
+
* '[0]': '[0].context',
|
|
34
|
+
* });
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export declare const swap: Swap_F;
|
|
38
|
+
//# sourceMappingURL=swap.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"swap.d.ts","names":[],"sources":["../src/swap.ts"],"mappings":";;;;;;;;;;;;;;YAiBY,OAAO,WAAW,WAAW,aACvC,cAAc,qBACd,4BAEA,IAAI,GACJ,QAAQ,QACJ,eAAe,IAAI,IAAI,WAAW;;;;;;;;;;;;;;;;;;;;;;qBAuB3B,MAAM"}
|
package/lib/swap.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { swap as swap$1 } from "@bemedev/function-swap";
|
|
2
|
+
import { reduceFnMap } from "@bemedev/app-plugins/internal";
|
|
3
|
+
// src/swap.ts
|
|
4
|
+
/**
|
|
5
|
+
* Remaps the arguments of a function from decomposed state paths.
|
|
6
|
+
*
|
|
7
|
+
* Unlike the former default helper, this function does not need the machine
|
|
8
|
+
* contract: an event name scopes the swap to that event, and any other event
|
|
9
|
+
* falls back to the no-op value.
|
|
10
|
+
*
|
|
11
|
+
* @param fn - Function to swap.
|
|
12
|
+
* @param event - Optional event name scoping the swap.
|
|
13
|
+
*
|
|
14
|
+
* @returns A function mapping decomposed paths to the swapped function.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* import { swap } from '@bemedev/app-bemedev-plugins';
|
|
19
|
+
*
|
|
20
|
+
* const inc = swap((value: number) => value + 1)({
|
|
21
|
+
* '[0]': '[0].context',
|
|
22
|
+
* });
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
const swap = (fn, event) => (types) => {
|
|
26
|
+
const swapped = swap$1(fn).constraint()(types);
|
|
27
|
+
return event ? reduceFnMap({ [event]: swapped }, event) : swapped;
|
|
28
|
+
};
|
|
29
|
+
export { swap };
|
|
30
|
+
|
|
31
|
+
//# sourceMappingURL=swap.js.map
|
package/lib/swap.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"swap.js","names":["_swap"],"sources":["../src/swap.ts"],"sourcesContent":["import type { Contract, FnR } from '@bemedev/app-contract';\nimport { swap as _swap } from '@bemedev/function-swap';\nimport { reduceFnMap } from '@bemedev/app-plugins/internal';\n\n/**\n * Signature of the {@linkcode swap} helper.\n *\n * The mapping keys are passed as decomposed paths, so the returned function\n * stays a standard machine selector of type {@linkcode FnR}.\n *\n * @remarks\n * The plugin surface is not parameterized by the consumer contract, so the\n * mapping keys are untyped. Annotate a value with `Swap<MyContract>` to keep\n * the returned selector typed against a specific contract.\n *\n * @template | {@linkcode Contract} `Ct` - Contract of the machine.\n */\nexport type Swap_F<Ct extends Contract = Contract> = <\n F extends (...args: any[]) => any,\n Ev extends string = string,\n>(\n fn: F,\n event?: Ev,\n) => (types: any) => FnR<Ct, ReturnType<F>>;\n\n/**\n * Remaps the arguments of a function from decomposed state paths.\n *\n * Unlike the former default helper, this function does not need the machine\n * contract: an event name scopes the swap to that event, and any other event\n * falls back to the no-op value.\n *\n * @param fn - Function to swap.\n * @param event - Optional event name scoping the swap.\n *\n * @returns A function mapping decomposed paths to the swapped function.\n *\n * @example\n * ```ts\n * import { swap } from '@bemedev/app-bemedev-plugins';\n *\n * const inc = swap((value: number) => value + 1)({\n * '[0]': '[0].context',\n * });\n * ```\n */\nexport const swap: Swap_F = (fn, event) => types => {\n const swapped = _swap(fn).constraint()(types);\n return event ? reduceFnMap({ [event]: swapped }, event) : swapped;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AA8CA,MAAa,QAAgB,IAAI,WAAU,UAAS;CAClD,MAAM,UAAUA,OAAM,EAAE,CAAC,CAAC,WAAW,CAAC,CAAC,KAAK;CAC5C,OAAO,QAAQ,YAAY,GAAG,QAAQ,QAAQ,GAAG,KAAK,IAAI;AAC5D"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,96 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bemedev/app-bemedev-plugins",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Optional composable utilities for @bemedev/app, delivered as options plugins",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"state machines",
|
|
7
|
+
"utilities",
|
|
8
|
+
"plugins",
|
|
9
|
+
"typescript"
|
|
10
|
+
],
|
|
11
|
+
"homepage": "https://github.com/chlbri/app/blob/main/packages/plugins/bemedev/README.md",
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"author": {
|
|
14
|
+
"name": "chlbri",
|
|
15
|
+
"email": "bri_lvi@icloud.com",
|
|
16
|
+
"url": "https://bemedev.vercel.app"
|
|
17
|
+
},
|
|
18
|
+
"maintainers": [
|
|
19
|
+
{
|
|
20
|
+
"email": "bri_lvi@icloud.com",
|
|
21
|
+
"name": "chlbri",
|
|
22
|
+
"url": "https://bemedev.vercel.app"
|
|
23
|
+
}
|
|
24
|
+
],
|
|
25
|
+
"contributors": [
|
|
26
|
+
{
|
|
27
|
+
"email": "bri_lvi@icloud.com",
|
|
28
|
+
"name": "chlbri",
|
|
29
|
+
"url": "https://bemedev.vercel.app"
|
|
30
|
+
}
|
|
31
|
+
],
|
|
32
|
+
"repository": {
|
|
33
|
+
"type": "git",
|
|
34
|
+
"url": "https://github.com/chlbri/app"
|
|
35
|
+
},
|
|
36
|
+
"files": [
|
|
37
|
+
"lib"
|
|
38
|
+
],
|
|
39
|
+
"type": "module",
|
|
40
|
+
"sideEffects": false,
|
|
41
|
+
"main": "lib/index.js",
|
|
42
|
+
"module": "lib/index.js",
|
|
43
|
+
"types": "lib/index.d.ts",
|
|
44
|
+
"exports": {
|
|
45
|
+
".": {
|
|
46
|
+
"import": "./lib/index.js",
|
|
47
|
+
"require": "./lib/index.cjs",
|
|
48
|
+
"types": "./lib/index.d.ts"
|
|
49
|
+
},
|
|
50
|
+
"./swap": {
|
|
51
|
+
"import": "./lib/swap.js",
|
|
52
|
+
"require": "./lib/swap.cjs",
|
|
53
|
+
"types": "./lib/swap.d.ts"
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
"publishConfig": {
|
|
57
|
+
"access": "public"
|
|
58
|
+
},
|
|
59
|
+
"dependencies": {
|
|
60
|
+
"@bemedev/app-plugins": "^0.0.1",
|
|
61
|
+
"@bemedev/function-swap": "^0.2.5"
|
|
62
|
+
},
|
|
63
|
+
"peerDependencies": {
|
|
64
|
+
"@bemedev/app-utils-bemedev": "^0.2.0",
|
|
65
|
+
"@bemedev/typings": "^1.5.0",
|
|
66
|
+
"@bemedev/app-contract": "^3.1.1",
|
|
67
|
+
"@bemedev/decompose": "^2.3.0"
|
|
68
|
+
},
|
|
69
|
+
"devDependencies": {
|
|
70
|
+
"@bemedev/dev-utils": "^1.6.2",
|
|
71
|
+
"@size-limit/file": "^14.3.0",
|
|
72
|
+
"@types/node": "^26.6.6",
|
|
73
|
+
"@vitest/coverage-v8": "5.1.1",
|
|
74
|
+
"pretty-quick": "^4.2.2",
|
|
75
|
+
"rimraf": "^6.1.4",
|
|
76
|
+
"size-limit": "^14.3.0",
|
|
77
|
+
"tslib": "^2.9.1",
|
|
78
|
+
"tsx": "^4.23.15",
|
|
79
|
+
"typescript": "7.0.2",
|
|
80
|
+
"vite-plus": "1.8.0"
|
|
81
|
+
},
|
|
82
|
+
"engines": {
|
|
83
|
+
"node": ">=24"
|
|
84
|
+
},
|
|
85
|
+
"scripts": {
|
|
86
|
+
"p-q": "pretty-quick",
|
|
87
|
+
"posttest": "vp fmt",
|
|
88
|
+
"pretest": "vp run pack",
|
|
89
|
+
"rm:lib": "vp dlx rimraf ./lib",
|
|
90
|
+
"publish:first": "vp run pretest && pnpm publish --access public --no-git-checks",
|
|
91
|
+
"test:coverage": "vp test run",
|
|
92
|
+
"test": "vp run test:coverage --no-coverage",
|
|
93
|
+
"test:only": "vp run test",
|
|
94
|
+
"test:watch": "vp test"
|
|
95
|
+
}
|
|
6
96
|
}
|