@lp-labs/tree-selection 0.0.0-stage → 0.1.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) 2026 Libin Prasanth
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 CHANGED
@@ -1,3 +1,102 @@
1
- # Temporary Holding Version
1
+ # @lp-labs/tree-selection
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Dynamic hierarchical leaf selection with derived tri-state and disabled-subtree contracts.
4
+
5
+ Standalone original implementation with zero runtime, peer or sibling dependencies.
6
+ Library ID 440. ES2022, strict TypeScript, NodeNext ESM and CommonJS with declarations.
7
+
8
+ ## Install
9
+
10
+ ```bash
11
+ npm install @lp-labs/tree-selection
12
+ ```
13
+
14
+ CommonJS: `const { createTreeSelection } = require('@lp-labs/tree-selection')`.
15
+
16
+ ## Quickstart
17
+
18
+ ```js
19
+ import { createTreeSelection } from '@lp-labs/tree-selection';
20
+
21
+ const tree = createTreeSelection([
22
+ { id: 'root' },
23
+ { id: 'a', parent: 'root' },
24
+ { id: 'b', parent: 'root' },
25
+ ]);
26
+ tree.toggle('a');
27
+ if (tree.state('root') !== 'mixed') throw new Error('Tri-state failed');
28
+ tree.toggle('root');
29
+ if (tree.selected().join(',') !== 'a,b') throw new Error('Cascade failed');
30
+ ```
31
+
32
+ ## API, customization and contracts
33
+
34
+ `createTreeSelection(nodes)` accepts a forest of unique nonempty IDs and optional parent
35
+ IDs. Input order is not required to be topological. Missing parents and cycles are rejected.
36
+ Limits: 10000 nodes and depth 128 including the root.
37
+
38
+ Only enabled terminal leaves are stored as checked IDs. A disabled node excludes its entire
39
+ subtree, even descendants not explicitly disabled. Aggregation ignores those excluded leaves.
40
+ A node with zero eligible leaves has state 'disabled'; otherwise zero selected = 'unchecked',
41
+ all selected = 'checked', partial = 'mixed'. A parent containing only disabled children
42
+ does not become an independently selectable leaf.
43
+ `toggle(id, selected?)` applies to eligible descendant leaves. Omitted selected fills an
44
+ unchecked/mixed subtree and clears a checked subtree; explicit true/false sets all leaves.
45
+ Toggling a disabled subtree is a no-op. `state(id)` returns the derived state.
46
+ `selected()` returns a detached array of checked leaf IDs in node input order, not branch IDs.
47
+
48
+ `replace(nodes)` validates and builds indexes atomically. Surviving eligible leaf IDs retain
49
+ selection; removed, disabled or newly nonterminal IDs lose it. Newly inserted/re-enabled
50
+ leaves start unchecked: a previously checked branch does not implicitly select new children.
51
+ Inputs and outputs are detached. Unknown IDs, missing parents, cycles and duplicates throw
52
+ TypeError; node/depth limits throw RangeError.
53
+
54
+ Building/replacement O(N * depth), memory O(N * depth) for descendant indexes;
55
+ toggle/state O(eligible descendants), selected O(N). The depth cap bounds this explicitly.
56
+ This is an original hierarchical model separate from selection-model's flat selection and
57
+ range semantics; neither package is imported or used as a renamed implementation.
58
+
59
+ ## Complete public declarations
60
+
61
+ ```ts
62
+ export interface TreeNode {
63
+ id: string;
64
+ parent?: string;
65
+ disabled?: boolean;
66
+ }
67
+ export type CheckState = 'checked' | 'mixed' | 'unchecked' | 'disabled';
68
+ export interface TreeSelection {
69
+ toggle(id: string, selected?: boolean): void;
70
+ state(id: string): CheckState;
71
+ selected(): string[];
72
+ replace(nodes: readonly TreeNode[]): void;
73
+ }
74
+ /** Only enabled terminal leaves are stored; a disabled ancestor excludes its subtree. */
75
+ export declare function createTreeSelection(initial: readonly TreeNode[]): TreeSelection;
76
+ ```
77
+
78
+ ## Scope and performance
79
+
80
+ Parent-linked forest selection, not flat range selection, a tree renderer, expansion model or DOM accessibility layer.
81
+
82
+ Complexities and bounds above assume ordinary JavaScript number arithmetic and Map/Set
83
+ average constant-time lookup. No runtime or peer dependencies, automatic browser adapters,
84
+ global registries or hidden services are included. Importing the module has no browser effects.
85
+
86
+ ## Development
87
+
88
+ ```bash
89
+ npm ci --offline --no-audit --no-fund
90
+ npm run build
91
+ npm run typecheck
92
+ npm test
93
+ npm run format:check
94
+ ```
95
+
96
+ ## Support
97
+
98
+ If this library is useful to you, consider supporting its maintenance with a [donation via PayPal](https://www.paypal.me/LibinPrasanth).
99
+
100
+ ## Author
101
+
102
+ Created by [Libin Prasanth](https://www.linkedin.com/in/libinprasanth/). Connect on LinkedIn for updates and discussion.
@@ -0,0 +1,14 @@
1
+ export interface TreeNode {
2
+ id: string;
3
+ parent?: string;
4
+ disabled?: boolean;
5
+ }
6
+ export type CheckState = 'checked' | 'mixed' | 'unchecked' | 'disabled';
7
+ export interface TreeSelection {
8
+ toggle(id: string, selected?: boolean): void;
9
+ state(id: string): CheckState;
10
+ selected(): string[];
11
+ replace(nodes: readonly TreeNode[]): void;
12
+ }
13
+ /** Only enabled terminal leaves are stored; a disabled ancestor excludes its subtree. */
14
+ export declare function createTreeSelection(initial: readonly TreeNode[]): TreeSelection;
@@ -0,0 +1,89 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createTreeSelection = createTreeSelection;
4
+ /** Only enabled terminal leaves are stored; a disabled ancestor excludes its subtree. */
5
+ function createTreeSelection(initial) {
6
+ let nodes = new Map();
7
+ let leaves = new Map();
8
+ let checked = new Set();
9
+ function replace(next) {
10
+ if (next.length > 10000)
11
+ throw new RangeError('Tree exceeds 10000 nodes');
12
+ const map = new Map();
13
+ for (const n of next) {
14
+ if (typeof n.id !== 'string' ||
15
+ !n.id ||
16
+ map.has(n.id) ||
17
+ (n.parent !== undefined && typeof n.parent !== 'string') ||
18
+ (n.disabled !== undefined && typeof n.disabled !== 'boolean'))
19
+ throw new TypeError('Invalid node ID');
20
+ map.set(n.id, { ...n });
21
+ }
22
+ const parents = new Set();
23
+ for (const n of map.values()) {
24
+ if (n.parent !== undefined) {
25
+ if (!map.has(n.parent))
26
+ throw new TypeError('Unknown parent');
27
+ parents.add(n.parent);
28
+ }
29
+ const seen = new Set();
30
+ let current = n;
31
+ while (current) {
32
+ if (seen.has(current.id))
33
+ throw new TypeError('Tree cycle');
34
+ seen.add(current.id);
35
+ if (seen.size > 128)
36
+ throw new RangeError('Tree depth exceeds 128');
37
+ current = current.parent === undefined ? undefined : map.get(current.parent);
38
+ }
39
+ }
40
+ const index = new Map([...map.keys()].map((id) => [id, []]));
41
+ for (const n of map.values()) {
42
+ if (parents.has(n.id))
43
+ continue;
44
+ const chain = [];
45
+ let current = n;
46
+ while (current) {
47
+ chain.push(current);
48
+ current = current.parent === undefined ? undefined : map.get(current.parent);
49
+ }
50
+ if (chain.some((node) => node.disabled))
51
+ continue;
52
+ for (const node of chain)
53
+ index.get(node.id).push(n.id);
54
+ }
55
+ nodes = map;
56
+ leaves = index;
57
+ checked = new Set([...checked].filter((id) => index.get(id)?.includes(id)));
58
+ }
59
+ function eligible(id) {
60
+ if (!nodes.has(id))
61
+ throw new TypeError('Unknown node');
62
+ return leaves.get(id);
63
+ }
64
+ function state(id) {
65
+ const descendants = eligible(id);
66
+ if (!descendants.length)
67
+ return 'disabled';
68
+ const count = descendants.filter((leaf) => checked.has(leaf)).length;
69
+ return count === 0 ? 'unchecked' : count === descendants.length ? 'checked' : 'mixed';
70
+ }
71
+ replace(initial);
72
+ return {
73
+ replace,
74
+ state,
75
+ toggle(id, selected) {
76
+ if (selected !== undefined && typeof selected !== 'boolean')
77
+ throw new TypeError('Invalid selected state');
78
+ const descendants = eligible(id);
79
+ const value = selected ?? state(id) !== 'checked';
80
+ for (const leaf of descendants) {
81
+ if (value)
82
+ checked.add(leaf);
83
+ else
84
+ checked.delete(leaf);
85
+ }
86
+ },
87
+ selected: () => [...nodes.keys()].filter((id) => checked.has(id)),
88
+ };
89
+ }
@@ -0,0 +1 @@
1
+ {"type":"commonjs"}
@@ -0,0 +1,14 @@
1
+ export interface TreeNode {
2
+ id: string;
3
+ parent?: string;
4
+ disabled?: boolean;
5
+ }
6
+ export type CheckState = 'checked' | 'mixed' | 'unchecked' | 'disabled';
7
+ export interface TreeSelection {
8
+ toggle(id: string, selected?: boolean): void;
9
+ state(id: string): CheckState;
10
+ selected(): string[];
11
+ replace(nodes: readonly TreeNode[]): void;
12
+ }
13
+ /** Only enabled terminal leaves are stored; a disabled ancestor excludes its subtree. */
14
+ export declare function createTreeSelection(initial: readonly TreeNode[]): TreeSelection;
@@ -0,0 +1,86 @@
1
+ /** Only enabled terminal leaves are stored; a disabled ancestor excludes its subtree. */
2
+ export function createTreeSelection(initial) {
3
+ let nodes = new Map();
4
+ let leaves = new Map();
5
+ let checked = new Set();
6
+ function replace(next) {
7
+ if (next.length > 10000)
8
+ throw new RangeError('Tree exceeds 10000 nodes');
9
+ const map = new Map();
10
+ for (const n of next) {
11
+ if (typeof n.id !== 'string' ||
12
+ !n.id ||
13
+ map.has(n.id) ||
14
+ (n.parent !== undefined && typeof n.parent !== 'string') ||
15
+ (n.disabled !== undefined && typeof n.disabled !== 'boolean'))
16
+ throw new TypeError('Invalid node ID');
17
+ map.set(n.id, { ...n });
18
+ }
19
+ const parents = new Set();
20
+ for (const n of map.values()) {
21
+ if (n.parent !== undefined) {
22
+ if (!map.has(n.parent))
23
+ throw new TypeError('Unknown parent');
24
+ parents.add(n.parent);
25
+ }
26
+ const seen = new Set();
27
+ let current = n;
28
+ while (current) {
29
+ if (seen.has(current.id))
30
+ throw new TypeError('Tree cycle');
31
+ seen.add(current.id);
32
+ if (seen.size > 128)
33
+ throw new RangeError('Tree depth exceeds 128');
34
+ current = current.parent === undefined ? undefined : map.get(current.parent);
35
+ }
36
+ }
37
+ const index = new Map([...map.keys()].map((id) => [id, []]));
38
+ for (const n of map.values()) {
39
+ if (parents.has(n.id))
40
+ continue;
41
+ const chain = [];
42
+ let current = n;
43
+ while (current) {
44
+ chain.push(current);
45
+ current = current.parent === undefined ? undefined : map.get(current.parent);
46
+ }
47
+ if (chain.some((node) => node.disabled))
48
+ continue;
49
+ for (const node of chain)
50
+ index.get(node.id).push(n.id);
51
+ }
52
+ nodes = map;
53
+ leaves = index;
54
+ checked = new Set([...checked].filter((id) => index.get(id)?.includes(id)));
55
+ }
56
+ function eligible(id) {
57
+ if (!nodes.has(id))
58
+ throw new TypeError('Unknown node');
59
+ return leaves.get(id);
60
+ }
61
+ function state(id) {
62
+ const descendants = eligible(id);
63
+ if (!descendants.length)
64
+ return 'disabled';
65
+ const count = descendants.filter((leaf) => checked.has(leaf)).length;
66
+ return count === 0 ? 'unchecked' : count === descendants.length ? 'checked' : 'mixed';
67
+ }
68
+ replace(initial);
69
+ return {
70
+ replace,
71
+ state,
72
+ toggle(id, selected) {
73
+ if (selected !== undefined && typeof selected !== 'boolean')
74
+ throw new TypeError('Invalid selected state');
75
+ const descendants = eligible(id);
76
+ const value = selected ?? state(id) !== 'checked';
77
+ for (const leaf of descendants) {
78
+ if (value)
79
+ checked.add(leaf);
80
+ else
81
+ checked.delete(leaf);
82
+ }
83
+ },
84
+ selected: () => [...nodes.keys()].filter((id) => checked.has(id)),
85
+ };
86
+ }
package/package.json CHANGED
@@ -1,6 +1,63 @@
1
1
  {
2
2
  "name": "@lp-labs/tree-selection",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "Dynamic hierarchical leaf selection with derived tri-state and disabled-subtree contracts.",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "main": "./dist/cjs/index.js",
8
+ "module": "./dist/esm/index.js",
9
+ "types": "./dist/esm/index.d.ts",
10
+ "exports": {
11
+ ".": {
12
+ "import": {
13
+ "types": "./dist/esm/index.d.ts",
14
+ "default": "./dist/esm/index.js"
15
+ },
16
+ "require": {
17
+ "types": "./dist/cjs/index.d.ts",
18
+ "default": "./dist/cjs/index.js"
19
+ }
20
+ },
21
+ "./package.json": "./package.json"
22
+ },
23
+ "files": [
24
+ "dist",
25
+ "README.md",
26
+ "LICENSE"
27
+ ],
28
+ "sideEffects": false,
29
+ "engines": {
30
+ "node": ">=18"
31
+ },
32
+ "scripts": {
33
+ "build": "node scripts/build.mjs",
34
+ "typecheck": "npm run build && node scripts/check-types.mjs",
35
+ "test": "npm run build && node --test tests/*.test.mjs",
36
+ "format": "prettier --write src scripts tests *.json README.md",
37
+ "format:check": "prettier --check src scripts tests *.json README.md",
38
+ "prepack": "npm run build"
39
+ },
40
+ "devDependencies": {
41
+ "typescript": "^5.9.3",
42
+ "prettier": "^3.6.2"
43
+ },
44
+ "prettier": {
45
+ "singleQuote": true,
46
+ "semi": true,
47
+ "trailingComma": "all",
48
+ "printWidth": 100,
49
+ "endOfLine": "lf"
50
+ },
51
+ "publishConfig": {
52
+ "access": "public",
53
+ "registry": "https://registry.npmjs.org/"
54
+ },
55
+ "author": {
56
+ "name": "Libin Prasanth",
57
+ "url": "https://www.linkedin.com/in/libinprasanth/"
58
+ },
59
+ "funding": {
60
+ "type": "paypal",
61
+ "url": "https://www.paypal.me/LibinPrasanth"
62
+ }
63
+ }