ctrl-fx 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.
Files changed (3) hide show
  1. package/LICENSE +184 -0
  2. package/README.md +124 -0
  3. package/package.json +87 -0
package/LICENSE ADDED
@@ -0,0 +1,184 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but not
32
+ limited to compiled object code, generated documentation, and
33
+ conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship made available under the
36
+ License, as indicated by a copyright notice that is included in or
37
+ attached to the work (an example is provided in the Appendix below).
38
+
39
+ "Derivative Works" shall mean any work, whether in Source or Object
40
+ form, that is based on (or derived from) the Work and for which the
41
+ editorial revisions, annotations, elaborations, or other
42
+ transformations represent, as a whole, an original work of authorship.
43
+ For the purposes of this License, Derivative Works shall not include
44
+ works that remain separable from, or merely link (or bind by name) to
45
+ the interfaces of, the Work and interfaces of, the Work and Derivative
46
+ Works thereof.
47
+
48
+ "Contribution" shall mean, as submitted to the Licensor for inclusion
49
+ in the Work by the copyright owner or by an individual or Legal Entity
50
+ authorized to submit on behalf of the copyright owner. For the
51
+ purposes of this definition, "submitted" means any form of electronic,
52
+ verbal, or written communication sent to the Licensor or its
53
+ representatives, including but not limited to communication on
54
+ electronic mailing lists, source code control systems, and issue
55
+ tracking systems that are managed by, or on behalf of, the Licensor
56
+ for the purpose of discussing and improving the Work, but excluding
57
+ communication that is conspicuously marked or designated in writing by
58
+ the copyright owner as "Not a Contribution."
59
+
60
+ "Contributor" shall mean Licensor and any Legal Entity on behalf of
61
+ whom a Contribution has been received by the Licensor and incorporated
62
+ within the Work.
63
+
64
+ 2. Grant of Copyright License. Subject to the terms and conditions of
65
+ this License, each Contributor hereby grants to You a perpetual,
66
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
67
+ copyright license to reproduce, prepare Derivative Works of, publicly
68
+ display, publicly perform, sublicense, and distribute the Work and
69
+ such Derivative Works in Source or Object form.
70
+
71
+ 3. Grant of Patent License. Subject to the terms and conditions of this
72
+ License, each Contributor hereby grants to You a perpetual, worldwide,
73
+ non-exclusive, no-charge, royalty-free, irrevocable (except as stated
74
+ in this section) patent license to make, have made, use, offer to
75
+ sell, sell, import, and otherwise transfer the Work, where such
76
+ license applies only to those patent claims licensable by such
77
+ Contributor that are necessarily infringed by their Contribution(s)
78
+ alone or by the combination of their Contribution(s) with the Work to
79
+ which such Contribution(s) was submitted. If You institute patent
80
+ litigation against any entity (including a cross-claim or counterclaim
81
+ in a lawsuit) alleging that the Work or any Contribution incorporated
82
+ within the Work constitutes direct or contributory patent infringement,
83
+ then any and all patent licenses granted to You under this License for
84
+ that Work shall terminate as of the date such litigation is filed.
85
+
86
+ 4. Redistribution. You may reproduce and distribute copies of the Work
87
+ or Derivative Works thereof in any medium, with or without
88
+ modifications, and in Source or Object form, provided that You meet
89
+ the following conditions:
90
+
91
+ (a) You must give any other recipients of the Work or Derivative Works
92
+ a copy of this License; and
93
+
94
+ (b) You must cause any modified files to carry prominent notices
95
+ stating that You changed the files; and
96
+
97
+ (c) You must retain, in the Source form of any Derivative Works that
98
+ You distribute, all copyright, patent, trademark, and attribution
99
+ notices from the Source form of the Work, excluding those notices
100
+ that do not pertain to any part of the Derivative Works; and
101
+
102
+ (d) If the Work includes a "NOTICE" text file as part of its
103
+ distribution, You must include a readable copy of the attribution
104
+ notices contained within such NOTICE file, in at least one of the
105
+ following places: within a NOTICE text file distributed as part of
106
+ the Derivative Works; within the Source form or documentation, if
107
+ provided along with the Derivative Works; or, within a display
108
+ generated by the Derivative Works, if and wherever such third-party
109
+ notices normally appear. The contents of the NOTICE file are for
110
+ informational purposes only and do not modify the License. You may
111
+ add Your own attribution notices within Derivative Works that You
112
+ distribute, alongside or as an addendum to the NOTICE text from
113
+ the Work, provided that such additional attribution notices cannot
114
+ be construed as modifying the License.
115
+
116
+ You may add Your own license statement for Your modifications and may
117
+ provide additional grant of rights to use, reproduce, modify, prepare
118
+ Derivative Works of, publicly display, publicly perform, sublicense,
119
+ and distribute those modifications and Derivative Works, subject to
120
+ the terms and conditions of this License, or under a different
121
+ license, provided that Your use of the Work complies with the terms
122
+ and conditions of this License.
123
+
124
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
125
+ any Contribution intentionally submitted for inclusion in the Work by
126
+ You to the Licensor shall be under the terms and conditions of this
127
+ License, without any additional terms or conditions. Notwithstanding
128
+ the above, nothing herein shall supersede or modify the terms of any
129
+ separate license agreement you may have executed with Licensor
130
+ regarding such Contributions.
131
+
132
+ 6. Trademarks. This License does not grant permission to use the trade
133
+ names, trademarks, service marks, or product names of the Licensor,
134
+ except as required for reasonable and customary use in describing the
135
+ origin of the Work and reproducing the content of the NOTICE file.
136
+
137
+ 7. Disclaimer of Warranty. Unless required by applicable law or agreed
138
+ to in writing, Licensor provides the Work (and each Contributor
139
+ provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES
140
+ OR CONDITIONS OF ANY KIND, either express or implied, including,
141
+ without limitation, any warranties or conditions of TITLE,
142
+ NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR
143
+ PURPOSE. You are solely responsible for determining the appropriateness
144
+ of using or reproducing the Work and assume any risks associated with
145
+ Your exercise of permissions under this License.
146
+
147
+ 8. Limitation of Liability. In no event and under no legal theory,
148
+ whether in tort (including negligence), contract, or otherwise, unless
149
+ required by applicable law (such as deliberate and grossly negligent
150
+ acts) or agreed to in writing, shall any Contributor be liable to You
151
+ for damages, including any direct, indirect, special, incidental, or
152
+ exemplary damages of any character arising as a result of this License
153
+ or out of the use or inability to use the Work (including but not
154
+ limited to damages for loss of goodwill, work stoppage, computer
155
+ failure or malfunction, or all other commercial damages or losses),
156
+ even if such Contributor has been advised of the possibility of such
157
+ damages.
158
+
159
+ 9. Accepting Warranty or Additional Liability. While redistributing the
160
+ Work or Derivative Works thereof, You may choose to offer, and charge
161
+ a fee for, acceptance of support, warranty, indemnity, or other
162
+ liability obligations and/or rights consistent with this License.
163
+ However, in accepting such obligations, You may offer such obligations
164
+ only on Your own behalf and on Your sole responsibility, not on behalf
165
+ of any other Contributor, and only if You agree to indemnify, defend,
166
+ and hold each Contributor harmless for any liability incurred by, or
167
+ claims asserted against, such Contributor by reason of your accepting
168
+ any such warranty or additional liability.
169
+
170
+ END OF TERMS AND CONDITIONS
171
+
172
+ Copyright 2025 Christopher Carroll
173
+
174
+ Licensed under the Apache License, Version 2.0 (the "License");
175
+ you may not use this file except in compliance with the License.
176
+ You may obtain a copy of the License at
177
+
178
+ http://www.apache.org/licenses/LICENSE-2.0
179
+
180
+ Unless required by applicable law or agreed to in writing, software
181
+ distributed under the License is distributed on an "AS IS" BASIS,
182
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
183
+ See the License for the specific language governing permissions and
184
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,124 @@
1
+ # ctrl-fx
2
+
3
+ Ctrl-fx is a no-dependency, purely functional TypeScript framework for writing
4
+ web applications and web components. Like many front end frameworks, it is
5
+ inspired by [Elm](https://elm-lang.org).
6
+
7
+ All side effects are managed by the framework. Users of the framework specify
8
+ data structures that represent effects. These data structures are associated
9
+ with a DOM element's event types. At runtime, when an event occurs, an
10
+ interpreter reads these data structures and actually performs the effects. A
11
+ different interpreter is used for testing, where the state of the world can be
12
+ specified and inspected by the application author.
13
+
14
+ State is managed by the framework. Unlike Elm, there is no top-level update
15
+ function, updating state is itself an effect.
16
+
17
+ An application is a function that takes state and returns a virtual DOM.
18
+
19
+ ## A very minimal example
20
+
21
+ ```ts
22
+ import { manageApplication } from 'ctrl-fx'
23
+ import { makeDom } from 'ctrl-fx/dom'
24
+ import { makeEffects } from 'ctrl-fx/effects'
25
+
26
+ // This is a very simple example of a stateful application. The application
27
+ // state is of type `number`.
28
+
29
+ // We import the dom elements functions with `makeDom()`, passing it the
30
+ // application state type, and `never` for the Event type, since this is a top
31
+ // level application and won't be firing any events upward in the component
32
+ // tree.
33
+ const { button_, div_, p_ } = makeDom<number, never>()
34
+
35
+ // Same thing for the effects we'll use, we pass the application state type and
36
+ // event type to `makeEffects()` to import versions of the Effects that are
37
+ // typed correctly for this application.
38
+ const { updateState } = makeEffects<number, never>()
39
+
40
+ // The application itself is a function that takes the current count returns a
41
+ // view that displays it, and allows more clicks by attaching the `updateState`
42
+ // effect to the button.
43
+ const application = (count: number) =>
44
+ div_(
45
+ p_(`Click count: ${count}`),
46
+ button_('Click Me').onClick(updateState(count => count + 1)),
47
+ )
48
+
49
+ // The manageApplication() function creates a ComponentManager that will manage
50
+ // application state. Whenever application state changes, the view is
51
+ // re-rendered application the DOM is updated. This function takes our
52
+ // application and the initial state of 0.
53
+ manageApplication(application, 0)
54
+ ```
55
+
56
+ ## Testing
57
+
58
+ One of the main goals of Ctrl-fx is to facilitate comprehensive testing. Because
59
+ side effects are modeled with data structures, application logic can easily be
60
+ tested with a purely functional effect interpreter that is distinct from the
61
+ runtime effect interpreter.
62
+
63
+ Here's a quick example.
64
+
65
+ ```typescript
66
+ import { describe, expect, it } from 'vitest'
67
+ import { makeDom, typeAttr } from 'ctrl-fx/dom'
68
+ import { makeEffects } from 'ctrl-fx/effects'
69
+ import { click, one, testApplication, textInput } from 'ctrl-fx/testing'
70
+
71
+ type State = string
72
+
73
+ // `_` is a fragment helper — it groups children without adding a DOM wrapper
74
+ // element.
75
+ const { _, button_, input, p_ } = makeDom<State, never>()
76
+
77
+ const { alert, getState, updateState } = makeEffects<State, never>()
78
+
79
+ // The application takes the user's name as state and says hello when the
80
+ // button is clicked. The text field updates state on each keystroke.
81
+ const application = (state: State) =>
82
+ _(
83
+ p_('Name'),
84
+ input(typeAttr('text'))
85
+ .prop('value', state)
86
+ .onTextInput(textState => updateState(_prev => textState.value)),
87
+ button_('Say Hello').onClick(
88
+ getState().flatMap(state => alert(`Hello, ${state}`)),
89
+ ),
90
+ )
91
+
92
+ describe('shows alert', () => {
93
+ it('says hello to user', () => {
94
+ // `testApplication` runs the app with a simulated environment.
95
+ // `one('selector')` queries the virtual DOM and asserts exactly one match.
96
+ const app = testApplication(application, '')
97
+ // type "world" into the text field
98
+ .run(textInput(one('input'), 'world'))
99
+ // click the button
100
+ .run(click(one('button')))
101
+
102
+ // `app.data` exposes the simulated environment — window, storage, etc.
103
+ expect(app.data.window.alerts.length).toBe(1)
104
+ expect(app.data.window.alerts[0]).toBe('Hello, world')
105
+ })
106
+ })
107
+ ```
108
+
109
+ ## Installation
110
+
111
+ ```sh
112
+ npm install ctrl-fx
113
+ ```
114
+
115
+ ## Learn more
116
+
117
+ See the [guide](doc/guide.md) (which is currently a work in progress) for a more
118
+ in-depth view of the framework.
119
+
120
+ If you clone this repository, you can run examples:
121
+
122
+ ```
123
+ npm install && npm run examples
124
+ ```
package/package.json ADDED
@@ -0,0 +1,87 @@
1
+ {
2
+ "name": "ctrl-fx",
3
+ "version": "0.0.1",
4
+ "description": "A no-dependency, purely functional TypeScript framework for building web applications",
5
+ "type": "module",
6
+ "license": "Apache-2.0",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/chris24carroll/ctrl-fx.git"
10
+ },
11
+ "keywords": [
12
+ "framework",
13
+ "functional",
14
+ "typescript",
15
+ "virtual-dom",
16
+ "effects",
17
+ "web",
18
+ "spa",
19
+ "frontend",
20
+ "component",
21
+ "state-management"
22
+ ],
23
+ "files": [
24
+ "dist"
25
+ ],
26
+ "scripts": {
27
+ "dev": "vite --host",
28
+ "clean": "rm -rf dist",
29
+ "build": "tsc && vite build --config doc/examples/vite.config.ts doc/examples",
30
+ "build:lib": "tsc -p tsconfig.build.json",
31
+ "preview": "vite preview",
32
+ "test": "vitest run",
33
+ "test:watch": "vitest --watch",
34
+ "test:coverage": "vitest --coverage",
35
+ "deploy": "./tools/archive.sh && ./tools/deploy.sh",
36
+ "format": "prettier --write src",
37
+ "format:check": "prettier --check src",
38
+ "lint": "eslint src",
39
+ "lint:fix": "eslint src --fix",
40
+ "typecheck": "tsc --noEmit",
41
+ "examples": "vite doc/examples --open",
42
+ "docs": "typedoc --entryPoints src/index.ts src/effects.ts src/testing.ts src/dom/index.ts src/dom/components.ts src/db/index.ts src/db/errors.ts src/router.ts src/webcomponent.ts src/utils/result.ts src/utils/lens.ts src/utils/match.ts --tsconfig tsconfig.json",
43
+ "prepublishOnly": "npm run build:lib"
44
+ },
45
+ "exports": {
46
+ ".": {
47
+ "types": "./dist/index.d.ts",
48
+ "import": "./dist/index.js"
49
+ },
50
+ "./db": {
51
+ "types": "./dist/db/index.d.ts",
52
+ "import": "./dist/db/index.js"
53
+ },
54
+ "./dom": {
55
+ "types": "./dist/dom/index.d.ts",
56
+ "import": "./dist/dom/index.js"
57
+ },
58
+ "./effects": {
59
+ "types": "./dist/effects.d.ts",
60
+ "import": "./dist/effects.js"
61
+ },
62
+ "./testing": {
63
+ "types": "./dist/testing.d.ts",
64
+ "import": "./dist/testing.js"
65
+ },
66
+ "./router": {
67
+ "types": "./dist/router.d.ts",
68
+ "import": "./dist/router.js"
69
+ },
70
+ "./webcomponent": {
71
+ "types": "./dist/webcomponent.d.ts",
72
+ "import": "./dist/webcomponent.js"
73
+ }
74
+ },
75
+ "devDependencies": {
76
+ "@eslint/js": "^10.0.1",
77
+ "typedoc": "^0.28.19",
78
+ "eslint": "^10.4.1",
79
+ "highlight.js": "^11.11.1",
80
+ "prettier": "^3.8.3",
81
+ "ts-node": "^10.9.2",
82
+ "tsx": "^4.20.6",
83
+ "typescript": "~5.8.3",
84
+ "typescript-eslint": "^8.60.0",
85
+ "vitest": "^3.2.4"
86
+ }
87
+ }