lume-js 0.0.0 → 0.2.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Sathvik C
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,51 @@
1
+ # lume-js
2
+
3
+ Minimal reactive state + DOM binding with **zero runtime** overhead.
4
+ Inspired by Go-style simplicity.
5
+
6
+ ## Philosophy
7
+ - ⚡ Zero runtime, direct DOM updates (no VDOM, no diffing).
8
+ - 🔑 Simple `state()` and `bindDom()` — that's it.
9
+ - 🧩 Explicit nesting (`state({ user: state({ name: "Alice" }) })`).
10
+ - ✨ Works anywhere — plain JS, or alongside other frameworks.
11
+
12
+ ## Install
13
+
14
+ ```bash
15
+ npm install lume-js
16
+ ```
17
+
18
+ ## Example
19
+
20
+ **main.js**
21
+ ```js
22
+ import { state, bindDom } from "lume-js";
23
+
24
+ const store = state({
25
+ count: 0,
26
+ user: state({ name: "Alice" })
27
+ });
28
+
29
+ bindDom(document.body, store);
30
+
31
+ document.getElementById("inc").addEventListener("click", () => {
32
+ store.count++;
33
+ });
34
+
35
+ document.getElementById("changeName").addEventListener("click", () => {
36
+ store.user.name = store.user.name === "Alice" ? "Bob" : "Alice";
37
+ });
38
+ ```
39
+
40
+ **index.html**
41
+ ```html
42
+ <p>Count: <span data-bind="count"></span></p>
43
+ <p>User Name: <span data-bind="user.name"></span></p>
44
+
45
+ <button id="inc">Increment</button>
46
+ <button id="changeName">Change Name</button>
47
+ ```
48
+
49
+ ## Status
50
+
51
+ 🚧 Early alpha (`0.1.0`). API may change. Feedback welcome!
package/package.json CHANGED
@@ -1,12 +1,16 @@
1
1
  {
2
2
  "name": "lume-js",
3
- "version": "0.0.0",
4
- "description": "",
5
- "main": "index.js",
3
+ "version": "0.2.0",
4
+ "main": "src/index.js",
5
+ "type": "module",
6
6
  "scripts": {
7
- "test": "echo \"Error: no test specified\" && exit 1"
7
+ "dev": "vite examples",
8
+ "build": "echo 'No build step yet, zero-runtime already!'"
8
9
  },
9
- "keywords": [],
10
- "author": "",
11
- "license": "ISC"
10
+ "files": [
11
+ "src"
12
+ ],
13
+ "devDependencies": {
14
+ "vite": "^7.1.9"
15
+ }
12
16
  }
@@ -0,0 +1,50 @@
1
+ /**
2
+ * Lume-JS Zero-runtime DOM binding
3
+ *
4
+ * Binds reactive state to DOM elements using [data-bind].
5
+ * Supports two-way binding for INPUT/TEXTAREA.
6
+ *
7
+ * Usage:
8
+ * import { bindDom } from "lume-js";
9
+ * bindDom(document.body, store);
10
+ *
11
+ * HTML:
12
+ * <span data-bind="count"></span>
13
+ * <input data-bind="user.name">
14
+ */
15
+
16
+ import { resolvePath } from "./utils.js";
17
+
18
+ /**
19
+ * Zero-runtime DOM binding for a reactive store
20
+ *
21
+ * @param {HTMLElement} root - root element to scan for [data-bind]
22
+ * @param {object} store - reactive state object
23
+ */
24
+ export function bindDom(root, store) {
25
+ const nodes = root.querySelectorAll("[data-bind]");
26
+
27
+ nodes.forEach(el => {
28
+ const pathArr = el.getAttribute("data-bind").split(".");
29
+ const lastKey = pathArr.pop();
30
+
31
+ let target;
32
+ try {
33
+ target = resolvePath(store, pathArr); // must be wrapped with state() if nested
34
+ } catch (err) {
35
+ console.warn(`Skipping binding for ${el}: ${err.message}`);
36
+ return;
37
+ }
38
+
39
+ // Subscribe once
40
+ target.$subscribe(lastKey, val => {
41
+ if (el.tagName === "INPUT" || el.tagName === "TEXTAREA") el.value = val;
42
+ else el.textContent = val;
43
+ });
44
+
45
+ // 2-way binding for inputs
46
+ if (el.tagName === "INPUT" || el.tagName === "TEXTAREA") {
47
+ el.addEventListener("input", e => target[lastKey] = e.target.value);
48
+ }
49
+ });
50
+ }
@@ -0,0 +1,57 @@
1
+ /**
2
+ * Lume-JS Reactive State Core
3
+ *
4
+ * Provides minimal, zero-runtime reactive state.
5
+ *
6
+ * Features:
7
+ * - Lightweight and Go-style
8
+ * - Explicit nested states
9
+ * - $subscribe for listening to key changes
10
+ *
11
+ * Usage:
12
+ * import { state } from "lume-js";
13
+ * const store = state({ count: 0 });
14
+ * store.$subscribe("count", val => console.log(val));
15
+ */
16
+
17
+
18
+ /**
19
+ * Creates a reactive state object.
20
+ *
21
+ * @param {Object} obj - Initial state object
22
+ * @returns {Proxy} Reactive proxy with $subscribe method
23
+ */
24
+ export function state(obj) {
25
+ const listeners = {};
26
+
27
+ // Notify subscribers of a key
28
+ function notify(key, val) {
29
+ if (listeners[key]) listeners[key].forEach(fn => fn(val));
30
+ }
31
+
32
+ const proxy = new Proxy(obj, {
33
+ get(target, key) {
34
+ return target[key];
35
+ },
36
+ set(target, key, value) {
37
+ target[key] = value;
38
+ notify(key, value);
39
+ return true;
40
+ }
41
+ });
42
+
43
+ /**
44
+ * Subscribe to changes for a specific key.
45
+ * Calls the callback immediately with the current value.
46
+ *
47
+ * @param {string} key
48
+ * @param {function} fn
49
+ */
50
+ proxy.$subscribe = (key, fn) => {
51
+ if (!listeners[key]) listeners[key] = [];
52
+ listeners[key].push(fn);
53
+ fn(proxy[key]); // initialize
54
+ };
55
+
56
+ return proxy;
57
+ }
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Resolve a nested path in an object.
3
+ * Example: path "user.name" returns obj.user.name
4
+ *
5
+ * @param {object} obj - The root object
6
+ * @param {string[]} pathArr - Array of keys
7
+ * @returns {object} Last object in the path
8
+ */
9
+ export function resolvePath(obj, pathArr) {
10
+ return pathArr.reduce((acc, key) => {
11
+ if (!acc) throw new Error(`Invalid path: ${pathArr.join(".")}`);
12
+ return acc[key];
13
+ }, obj);
14
+ }
package/src/index.js ADDED
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Lume-JS Library Core API
3
+ *
4
+ * Exposes:
5
+ * - state(): create reactive state
6
+ * - bindDom(): zero-runtime DOM binding
7
+ *
8
+ * Usage:
9
+ * import { state, bindDom } from "lume-js";
10
+ */
11
+
12
+ export { state } from "./core/state.js";
13
+ export { bindDom } from "./core/bindDom.js";
package/index.js DELETED
@@ -1 +0,0 @@
1
- console.log("hello world!");