@elliemae/ds-tree-model 3.49.0-rc.9 → 3.49.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.
@@ -34,57 +34,11 @@ module.exports = __toCommonJS(DSTree_exports);
34
34
  var React = __toESM(require("react"));
35
35
  var import_Node = require("./Node.js");
36
36
  class DSTree {
37
+ root;
38
+ _hash;
39
+ nodes;
40
+ getUniqueId;
37
41
  constructor(item, options) {
38
- this.getRoot = () => this.root;
39
- this.getNode = (id) => this.nodes[id];
40
- this.getNodes = () => this.nodes;
41
- this.walk = (callback) => {
42
- this.root.walk(callback);
43
- };
44
- this.walkParents = (callback) => {
45
- this.root.walkParents(callback);
46
- };
47
- this.findNode = (callback) => this.root.findNode(callback);
48
- this.findAllNodes = (callback) => this.root.findAllNodes(callback);
49
- this.flatten = () => this.root.flatten();
50
- this.addNode = (item, options = {}) => {
51
- const { position, parent, callback } = options;
52
- const parentDefault = parent || this.root;
53
- const positionDefault = position ?? parentDefault.children.length;
54
- const node = new import_Node.Node(item, {
55
- childIndex: positionDefault,
56
- depth: parentDefault.depth + 1,
57
- parent: parentDefault,
58
- tree: this
59
- });
60
- this.getNode(parentDefault.dsId).addNode(node);
61
- this._hash += 1;
62
- callback?.(node, this);
63
- return node;
64
- };
65
- this.removeNode = (id, options = {}) => {
66
- const node = this.getNode(id);
67
- if (!node) throw new Error(`Node with id ${id} not found`);
68
- node.removeNode();
69
- this._hash += 1;
70
- options.callback?.(node, this);
71
- return node;
72
- };
73
- this.moveNode = (id, options = {}) => {
74
- const removedNode = this.removeNode(id);
75
- const node = this.addNode(removedNode.getJson(), options);
76
- this._hash += 1;
77
- return node;
78
- };
79
- this.replaceNode = (id, item, options = {}) => {
80
- const removedNode = this.removeNode(id);
81
- const node = this.addNode(item, { parent: removedNode.parent, position: removedNode.childIndex });
82
- this._hash += 1;
83
- options.callback?.(node, this);
84
- return node;
85
- };
86
- this.getPath = (id) => this.getRoot().getPath(id);
87
- this.getPathIds = (id) => this.getRoot().getPathIds(id);
88
42
  this.getUniqueId = options.getUniqueId;
89
43
  this.root = new import_Node.Node(item, { childIndex: 0, depth: 0, tree: this });
90
44
  this.nodes = {};
@@ -106,5 +60,55 @@ class DSTree {
106
60
  get hash() {
107
61
  return this._hash;
108
62
  }
63
+ getRoot = () => this.root;
64
+ getNode = (id) => this.nodes[id];
65
+ getNodes = () => this.nodes;
66
+ walk = (callback) => {
67
+ this.root.walk(callback);
68
+ };
69
+ walkParents = (callback) => {
70
+ this.root.walkParents(callback);
71
+ };
72
+ findNode = (callback) => this.root.findNode(callback);
73
+ findAllNodes = (callback) => this.root.findAllNodes(callback);
74
+ flatten = () => this.root.flatten();
75
+ addNode = (item, options = {}) => {
76
+ const { position, parent, callback } = options;
77
+ const parentDefault = parent || this.root;
78
+ const positionDefault = position ?? parentDefault.children.length;
79
+ const node = new import_Node.Node(item, {
80
+ childIndex: positionDefault,
81
+ depth: parentDefault.depth + 1,
82
+ parent: parentDefault,
83
+ tree: this
84
+ });
85
+ this.getNode(parentDefault.dsId).addNode(node);
86
+ this._hash += 1;
87
+ callback?.(node, this);
88
+ return node;
89
+ };
90
+ removeNode = (id, options = {}) => {
91
+ const node = this.getNode(id);
92
+ if (!node) throw new Error(`Node with id ${id} not found`);
93
+ node.removeNode();
94
+ this._hash += 1;
95
+ options.callback?.(node, this);
96
+ return node;
97
+ };
98
+ moveNode = (id, options = {}) => {
99
+ const removedNode = this.removeNode(id);
100
+ const node = this.addNode(removedNode.getJson(), options);
101
+ this._hash += 1;
102
+ return node;
103
+ };
104
+ replaceNode = (id, item, options = {}) => {
105
+ const removedNode = this.removeNode(id);
106
+ const node = this.addNode(item, { parent: removedNode.parent, position: removedNode.childIndex });
107
+ this._hash += 1;
108
+ options.callback?.(node, this);
109
+ return node;
110
+ };
111
+ getPath = (id) => this.getRoot().getPath(id);
112
+ getPathIds = (id) => this.getRoot().getPathIds(id);
109
113
  }
110
114
  //# sourceMappingURL=DSTree.js.map
@@ -2,6 +2,6 @@
2
2
  "version": 3,
3
3
  "sources": ["../../src/DSTree.ts", "../../../../../scripts/build/transpile/react-shim.js"],
4
4
  "sourcesContent": ["/* eslint-disable no-underscore-dangle */\nimport type {\n AddOptions,\n AnyObjectWithoutReservedKeys,\n MoveOptions,\n MutateOptions,\n RemoveOptions,\n Item,\n} from './types.js';\nimport { Node } from './Node.js';\n\nexport class DSTree<AppInterface extends AnyObjectWithoutReservedKeys = Record<string, unknown>> {\n private root: Node<AppInterface>;\n\n private _hash: number;\n\n private nodes: Record<string | number, Node<AppInterface>>;\n\n getUniqueId: (item: Item<AppInterface>) => string | number;\n\n constructor(item: Item<AppInterface>, options: { getUniqueId: (item: Item<AppInterface>) => string | number }) {\n this.getUniqueId = options.getUniqueId;\n this.root = new Node<AppInterface>(item, { childIndex: 0, depth: 0, tree: this });\n this.nodes = {};\n this._hash = 0;\n\n // autocalculate a dictionary where dsId are keys and nodes are values\n // and add the tree link to each node\n this.root.walk((node) => {\n this.nodes[node.dsId] = node;\n return true;\n });\n }\n\n addNodeToNodesDictionary(node: Node<AppInterface>): void {\n // check that we have no repeated ids\n if (this.nodes[node.dsId]) {\n throw new Error(`DSTree: repeated dsId ${node.dsId}`);\n }\n\n this.nodes[node.dsId] = node;\n }\n\n removeNodeFromNodesDictionary(node: Node<AppInterface>): void {\n delete this.nodes[node.dsId];\n }\n\n get hash(): number {\n return this._hash;\n }\n\n getRoot = (): Node<AppInterface> => this.root;\n\n getNode = (id: string | number): Node<AppInterface> => this.nodes[id];\n\n getNodes = (): Record<string, Node<AppInterface>> => this.nodes;\n\n walk = (callback: (node: Node<AppInterface>) => boolean): void => {\n this.root.walk(callback);\n };\n\n walkParents = (callback: (node: Node<AppInterface>) => boolean): void => {\n this.root.walkParents(callback);\n };\n\n findNode = (callback: (node: Node<AppInterface>) => boolean): Node<AppInterface> | null =>\n this.root.findNode(callback);\n\n findAllNodes = (callback: (node: Node<AppInterface>) => boolean): Node<AppInterface>[] =>\n this.root.findAllNodes(callback);\n\n flatten = (): Node<AppInterface>[] => this.root.flatten();\n\n addNode = (item: Item<AppInterface>, options: AddOptions<AppInterface> = {}): Node<AppInterface> => {\n const { position, parent, callback } = options;\n const parentDefault = parent || this.root;\n const positionDefault = position ?? parentDefault.children.length;\n\n // create node and add it to the parent\n const node = new Node<AppInterface>(item, {\n childIndex: positionDefault,\n depth: parentDefault.depth + 1,\n parent: parentDefault,\n tree: this,\n });\n this.getNode(parentDefault.dsId).addNode(node);\n\n // change hash\n this._hash += 1;\n\n // invoke the callback if present\n callback?.(node, this);\n\n return node;\n };\n\n removeNode = (id: string | number, options: RemoveOptions<AppInterface> = {}): Node<AppInterface> => {\n const node = this.getNode(id);\n if (!node) throw new Error(`Node with id ${id} not found`);\n\n node.removeNode();\n\n // change hash\n this._hash += 1;\n\n // invoke the callback if present\n options.callback?.(node, this);\n\n return node;\n };\n\n moveNode = (id: string | number, options: MoveOptions<AppInterface> = {}): Node<AppInterface> => {\n const removedNode = this.removeNode(id);\n const node = this.addNode(removedNode.getJson(), options);\n\n this._hash += 1;\n\n return node;\n };\n\n replaceNode = (\n id: string | number,\n item: Item<AppInterface>,\n options: MutateOptions<AppInterface> = {},\n ): Node<AppInterface> => {\n const removedNode = this.removeNode(id);\n const node = this.addNode(item, { parent: removedNode.parent, position: removedNode.childIndex });\n\n this._hash += 1;\n\n // invoke the callback if present\n options.callback?.(node, this);\n\n return node;\n };\n\n getPath = (id: string | number): Node<AppInterface>[] => this.getRoot().getPath(id);\n\n getPathIds = (id: string | number) => this.getRoot().getPathIds(id);\n}\n", "import * as React from 'react';\nexport { React };\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;ACAA,YAAuB;ADSvB,kBAAqB;AAEd,MAAM,OAAoF;AAAA,EAS/F,YAAY,MAA0B,SAAyE;AA+B/G,mBAAU,MAA0B,KAAK;AAEzC,mBAAU,CAAC,OAA4C,KAAK,M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AK,SAAS;AAGd,eAAW,MAAM,IAAI;AAErB,WAAO;AAAA,EACT;AAAA,EAEA,aAAa,CAAC,IAAqB,UAAuC,CAAC,MAA0B;AACnG,UAAM,OAAO,KAAK,QAAQ,EAAE;AAC5B,QAAI,CAAC,KAAM,OAAM,IAAI,MAAM,gBAAgB,EAAE,YAAY;AAEzD,SAAK,WAAW;AAGhB,SAAK,SAAS;AAGd,YAAQ,WAAW,MAAM,IAAI;AAE7B,WAAO;AAAA,EACT;AAAA,EAEA,WAAW,CAAC,IAAqB,UAAqC,CAAC,MAA0B;AAC/F,UAAM,cAAc,KAAK,WAAW,EAAE;AACtC,UAAM,OAAO,KAAK,QAAQ,YAAY,QAAQ,GAAG,OAAO;AAExD,SAAK,SAAS;AAEd,WAAO;AAAA,EACT;AAAA,EAEA,cAAc,CACZ,IACA,MACA,UAAuC,CAAC,MACjB;AACvB,UAAM,cAAc,KAAK,WAAW,EAAE;AACtC,UAAM,OAAO,KAAK,QAAQ,MAAM,EAAE,QAAQ,YAAY,QAAQ,UAAU,YAAY,WAAW,CAAC;AAEhG,SAAK,SAAS;AAGd,YAAQ,WAAW,MAAM,IAAI;AAE7B,WAAO;AAAA,EACT;AAAA,EAEA,UAAU,CAAC,OAA8C,KAAK,QAAQ,EAAE,QAAQ,EAAE;AAAA,EAElF,aAAa,CAAC,OAAwB,KAAK,QAAQ,EAAE,WAAW,EAAE;AACpE;",
6
6
  "names": []
7
7
  }
package/dist/cjs/Node.js CHANGED
@@ -34,100 +34,16 @@ module.exports = __toCommonJS(Node_exports);
34
34
  var React = __toESM(require("react"));
35
35
  var import_lodash = require("lodash");
36
36
  class Node {
37
+ dsId;
38
+ childIndex;
39
+ depth;
40
+ plainItem;
41
+ plainChildren;
42
+ parent;
43
+ children;
44
+ tree;
45
+ _hash;
37
46
  constructor(item, { childIndex, depth, parent, tree }) {
38
- // ===========================================================================
39
- // INTERNAL METHODS
40
- // ===========================================================================
41
- this.fixChildIndexes = () => {
42
- for (let i = 0; i < this.children.length; i += 1) {
43
- this.children[i].childIndex = i;
44
- }
45
- this._hash += 1;
46
- };
47
- this.getCleanItem = (item) => {
48
- const { subitems, ...plainItem } = item;
49
- return (0, import_lodash.cloneDeep)(plainItem);
50
- };
51
- this.walk = (callback) => {
52
- const shouldContinueWalking = callback(this);
53
- if (shouldContinueWalking) {
54
- for (const child of this.children) {
55
- child.walk(callback);
56
- }
57
- }
58
- };
59
- this.walkParents = (callback) => {
60
- const shouldContinueWalking = callback(this);
61
- if (shouldContinueWalking && this.parent) {
62
- this.parent.walkParents(callback);
63
- }
64
- };
65
- this.findNode = (callback) => {
66
- let found = null;
67
- this.walk((node) => {
68
- if (found) return false;
69
- if (callback(node)) found = node;
70
- return !found;
71
- });
72
- return found;
73
- };
74
- this.findAllNodes = (callback) => {
75
- const found = [];
76
- this.walk((node) => {
77
- if (callback(node)) found.push(node);
78
- return true;
79
- });
80
- return found;
81
- };
82
- this.flatten = () => {
83
- const flattened = [];
84
- this.walk((node) => {
85
- flattened.push(node);
86
- return true;
87
- });
88
- return flattened;
89
- };
90
- this.getPath = (id) => {
91
- const path = [];
92
- const node = this.tree.getNode(id);
93
- if (!node) return path;
94
- node.walkParents((parent) => {
95
- path.push(parent);
96
- return parent.dsId !== this.dsId;
97
- });
98
- return path.reverse();
99
- };
100
- this.getPathIds = (id) => this.getPath(id).map((node) => node.dsId);
101
- this.getJson = () => ({
102
- ...this.plainItem,
103
- subitems: this.children?.map?.((child) => child.getJson()) ?? void 0
104
- });
105
- // ===========================================================================
106
- // WRITE METHODS
107
- // ===========================================================================
108
- this.addNode = (node) => {
109
- const position = node.childIndex;
110
- if (position < 0 || position > this.children.length) {
111
- throw new Error(`Invalid position ${position} for parent ${this.dsId} with item ${node.dsId}`);
112
- }
113
- this.children.splice(position, 0, node);
114
- this.fixChildIndexes();
115
- node.walk((child) => {
116
- this.tree.addNodeToNodesDictionary(child);
117
- return true;
118
- });
119
- this._hash += 1;
120
- };
121
- this.removeNode = () => {
122
- if (!this.parent) throw new Error(`Cannot remove root node with id ${this.dsId}`);
123
- this.parent.children.splice(this.childIndex, 1);
124
- this.parent.fixChildIndexes();
125
- this.walk((child) => {
126
- this.tree.removeNodeFromNodesDictionary(child);
127
- return true;
128
- });
129
- this._hash += 1;
130
- };
131
47
  this.dsId = tree.getUniqueId(item);
132
48
  this.childIndex = childIndex;
133
49
  this.depth = depth;
@@ -151,10 +67,103 @@ class Node {
151
67
  this.plainItem = this.getCleanItem(item);
152
68
  }
153
69
  // ===========================================================================
70
+ // INTERNAL METHODS
71
+ // ===========================================================================
72
+ fixChildIndexes = () => {
73
+ for (let i = 0; i < this.children.length; i += 1) {
74
+ this.children[i].childIndex = i;
75
+ }
76
+ this._hash += 1;
77
+ };
78
+ getCleanItem = (item) => {
79
+ const { subitems, ...plainItem } = item;
80
+ return (0, import_lodash.cloneDeep)(plainItem);
81
+ };
82
+ // ===========================================================================
154
83
  // READ METHODS
155
84
  // ===========================================================================
156
85
  get hash() {
157
86
  return this._hash;
158
87
  }
88
+ walk = (callback) => {
89
+ const shouldContinueWalking = callback(this);
90
+ if (shouldContinueWalking) {
91
+ for (const child of this.children) {
92
+ child.walk(callback);
93
+ }
94
+ }
95
+ };
96
+ walkParents = (callback) => {
97
+ const shouldContinueWalking = callback(this);
98
+ if (shouldContinueWalking && this.parent) {
99
+ this.parent.walkParents(callback);
100
+ }
101
+ };
102
+ findNode = (callback) => {
103
+ let found = null;
104
+ this.walk((node) => {
105
+ if (found) return false;
106
+ if (callback(node)) found = node;
107
+ return !found;
108
+ });
109
+ return found;
110
+ };
111
+ findAllNodes = (callback) => {
112
+ const found = [];
113
+ this.walk((node) => {
114
+ if (callback(node)) found.push(node);
115
+ return true;
116
+ });
117
+ return found;
118
+ };
119
+ flatten = () => {
120
+ const flattened = [];
121
+ this.walk((node) => {
122
+ flattened.push(node);
123
+ return true;
124
+ });
125
+ return flattened;
126
+ };
127
+ getPath = (id) => {
128
+ const path = [];
129
+ const node = this.tree.getNode(id);
130
+ if (!node) return path;
131
+ node.walkParents((parent) => {
132
+ path.push(parent);
133
+ return parent.dsId !== this.dsId;
134
+ });
135
+ return path.reverse();
136
+ };
137
+ getPathIds = (id) => this.getPath(id).map((node) => node.dsId);
138
+ getJson = () => ({
139
+ ...this.plainItem,
140
+ subitems: this.children?.map?.((child) => child.getJson()) ?? void 0
141
+ });
142
+ // ===========================================================================
143
+ // WRITE METHODS
144
+ // ===========================================================================
145
+ addNode = (node) => {
146
+ const position = node.childIndex;
147
+ if (position < 0 || position > this.children.length) {
148
+ throw new Error(`Invalid position ${position} for parent ${this.dsId} with item ${node.dsId}`);
149
+ }
150
+ this.children.splice(position, 0, node);
151
+ this.fixChildIndexes();
152
+ node.walk((child) => {
153
+ this.tree.addNodeToNodesDictionary(child);
154
+ return true;
155
+ });
156
+ this._hash += 1;
157
+ };
158
+ removeNode = () => {
159
+ if (!this.parent) throw new Error(`Cannot remove root node with id ${this.dsId}`);
160
+ this.parent.children.splice(this.childIndex, 1);
161
+ this.parent.fixChildIndexes();
162
+ this.walk((child) => {
163
+ this.tree.removeNodeFromNodesDictionary(child);
164
+ return true;
165
+ });
166
+ this._hash += 1;
167
+ };
159
168
  }
160
169
  //# sourceMappingURL=Node.js.map
@@ -2,6 +2,6 @@
2
2
  "version": 3,
3
3
  "sources": ["../../src/Node.ts", "../../../../../scripts/build/transpile/react-shim.js"],
4
4
  "sourcesContent": ["/* eslint-disable no-underscore-dangle */\n/* eslint-disable no-use-before-define */\n/* eslint-disable max-params */\nimport { cloneDeep } from 'lodash';\nimport type { DSTree } from './DSTree.js';\nimport type { NodeConstructorOptions, AnyObjectWithoutReservedKeys, Item } from './types.js';\n\nexport class Node<AppInterface extends AnyObjectWithoutReservedKeys = Record<string, unknown>> {\n dsId: string | number;\n\n childIndex: number;\n\n depth: number;\n\n plainItem: AppInterface;\n\n plainChildren: AppInterface[];\n\n parent: Node<AppInterface> | null;\n\n children: Node<AppInterface>[];\n\n private tree!: DSTree<AppInterface>;\n\n private _hash: number;\n\n constructor(item: Item<AppInterface>, { childIndex, depth, parent, tree }: NodeConstructorOptions<AppInterface>) {\n this.dsId = tree.getUniqueId(item);\n this.childIndex = childIndex;\n this.depth = depth;\n this.parent = parent ?? null;\n this.tree = tree;\n this._hash = 0;\n this.children = [];\n this.plainChildren = [];\n\n // on construction, we add the children to the node\n // this will recursively happen for all children because this triggers `new Node()` recursively\n const subitems = item.subitems ?? [];\n subitems.forEach((subitem, index) => {\n this.plainChildren.push(this.getCleanItem(subitem));\n this.children.push(\n new Node<AppInterface>(subitem, {\n childIndex: index,\n depth: depth + 1,\n parent: this,\n tree,\n }),\n );\n });\n // Save the item without the subitems\n this.plainItem = this.getCleanItem(item);\n }\n\n // ===========================================================================\n // INTERNAL METHODS\n // ===========================================================================\n\n fixChildIndexes = (): void => {\n for (let i = 0; i < this.children.length; i += 1) {\n this.children[i].childIndex = i;\n }\n this._hash += 1;\n };\n\n getCleanItem = (item: Item<AppInterface>): AppInterface => {\n const { subitems, ...plainItem } = item;\n return cloneDeep(plainItem as AppInterface);\n };\n\n // ===========================================================================\n // READ METHODS\n // ===========================================================================\n\n get hash(): number {\n return this._hash;\n }\n\n walk = (callback: (node: Node<AppInterface>) => boolean): void => {\n const shouldContinueWalking = callback(this);\n if (shouldContinueWalking) {\n // eslint-disable-next-line no-restricted-syntax\n for (const child of this.children) {\n child.walk(callback);\n }\n }\n };\n\n walkParents = (callback: (node: Node<AppInterface>) => boolean): void => {\n const shouldContinueWalking = callback(this);\n if (shouldContinueWalking && this.parent) {\n this.parent.walkParents(callback);\n }\n };\n\n findNode = (callback: (node: Node<AppInterface>) => boolean): Node<AppInterface> | null => {\n // find first node using walk\n let found: Node<AppInterface> | null = null;\n this.walk((node) => {\n // If we already found it, don't look anymore\n if (found) return false;\n\n // Otherwise keep looking\n if (callback(node)) found = node;\n return !found;\n });\n\n return found;\n };\n\n findAllNodes = (callback: (node: Node<AppInterface>) => boolean): Node<AppInterface>[] => {\n const found: Node<AppInterface>[] = [];\n this.walk((node) => {\n if (callback(node)) found.push(node);\n return true;\n });\n return found;\n };\n\n flatten = (): Node<AppInterface>[] => {\n const flattened: Node<AppInterface>[] = [];\n this.walk((node) => {\n flattened.push(node);\n return true;\n });\n return flattened;\n };\n\n getPath = (id: string | number): Node<AppInterface>[] => {\n const path: Node<AppInterface>[] = [];\n const node = this.tree.getNode(id);\n if (!node) return path;\n node.walkParents((parent) => {\n path.push(parent);\n return parent.dsId !== this.dsId;\n });\n return path.reverse();\n };\n\n getPathIds = (id: string | number) => this.getPath(id).map((node) => node.dsId);\n\n getJson = (): Item<AppInterface> =>\n ({\n ...this.plainItem,\n subitems: this.children?.map?.((child) => child.getJson()) ?? undefined,\n }) as Item<AppInterface>;\n\n // ===========================================================================\n // WRITE METHODS\n // ===========================================================================\n\n addNode = (node: Node<AppInterface>) => {\n // check that the position is valid for that parent\n const position = node.childIndex;\n if (position < 0 || position > this.children.length) {\n throw new Error(`Invalid position ${position} for parent ${this.dsId} with item ${node.dsId}`);\n }\n\n // add the new node\n this.children.splice(position, 0, node);\n\n // fix the childIndex of the nodes after the new one\n this.fixChildIndexes();\n\n // add subtree of the new node to the node dictionary\n node.walk((child) => {\n this.tree.addNodeToNodesDictionary(child);\n return true;\n });\n\n this._hash += 1;\n };\n\n removeNode = () => {\n // if no parent throw error\n if (!this.parent) throw new Error(`Cannot remove root node with id ${this.dsId}`);\n\n // remove from parent\n this.parent.children.splice(this.childIndex, 1);\n\n // fix the childIndex of the nodes of the parent\n this.parent.fixChildIndexes();\n\n // remove subtree from the node dictionary\n this.walk((child) => {\n this.tree.removeNodeFromNodesDictionary(child);\n return true;\n });\n\n this._hash += 1;\n };\n}\n", "import * as React from 'react';\nexport { React };\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;ACAA,YAAuB;ADGvB,oBAA0B;AAInB,MAAM,KAAkF;AAAA,EAmB7F,YAAY,MAA0B,EAAE,YA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AAC;AAED,SAAK,SAAS;AAAA,EAChB;AACF;",
6
6
  "names": []
7
7
  }
@@ -1,57 +1,11 @@
1
1
  import * as React from "react";
2
2
  import { Node } from "./Node.js";
3
3
  class DSTree {
4
+ root;
5
+ _hash;
6
+ nodes;
7
+ getUniqueId;
4
8
  constructor(item, options) {
5
- this.getRoot = () => this.root;
6
- this.getNode = (id) => this.nodes[id];
7
- this.getNodes = () => this.nodes;
8
- this.walk = (callback) => {
9
- this.root.walk(callback);
10
- };
11
- this.walkParents = (callback) => {
12
- this.root.walkParents(callback);
13
- };
14
- this.findNode = (callback) => this.root.findNode(callback);
15
- this.findAllNodes = (callback) => this.root.findAllNodes(callback);
16
- this.flatten = () => this.root.flatten();
17
- this.addNode = (item, options = {}) => {
18
- const { position, parent, callback } = options;
19
- const parentDefault = parent || this.root;
20
- const positionDefault = position ?? parentDefault.children.length;
21
- const node = new Node(item, {
22
- childIndex: positionDefault,
23
- depth: parentDefault.depth + 1,
24
- parent: parentDefault,
25
- tree: this
26
- });
27
- this.getNode(parentDefault.dsId).addNode(node);
28
- this._hash += 1;
29
- callback?.(node, this);
30
- return node;
31
- };
32
- this.removeNode = (id, options = {}) => {
33
- const node = this.getNode(id);
34
- if (!node) throw new Error(`Node with id ${id} not found`);
35
- node.removeNode();
36
- this._hash += 1;
37
- options.callback?.(node, this);
38
- return node;
39
- };
40
- this.moveNode = (id, options = {}) => {
41
- const removedNode = this.removeNode(id);
42
- const node = this.addNode(removedNode.getJson(), options);
43
- this._hash += 1;
44
- return node;
45
- };
46
- this.replaceNode = (id, item, options = {}) => {
47
- const removedNode = this.removeNode(id);
48
- const node = this.addNode(item, { parent: removedNode.parent, position: removedNode.childIndex });
49
- this._hash += 1;
50
- options.callback?.(node, this);
51
- return node;
52
- };
53
- this.getPath = (id) => this.getRoot().getPath(id);
54
- this.getPathIds = (id) => this.getRoot().getPathIds(id);
55
9
  this.getUniqueId = options.getUniqueId;
56
10
  this.root = new Node(item, { childIndex: 0, depth: 0, tree: this });
57
11
  this.nodes = {};
@@ -73,6 +27,56 @@ class DSTree {
73
27
  get hash() {
74
28
  return this._hash;
75
29
  }
30
+ getRoot = () => this.root;
31
+ getNode = (id) => this.nodes[id];
32
+ getNodes = () => this.nodes;
33
+ walk = (callback) => {
34
+ this.root.walk(callback);
35
+ };
36
+ walkParents = (callback) => {
37
+ this.root.walkParents(callback);
38
+ };
39
+ findNode = (callback) => this.root.findNode(callback);
40
+ findAllNodes = (callback) => this.root.findAllNodes(callback);
41
+ flatten = () => this.root.flatten();
42
+ addNode = (item, options = {}) => {
43
+ const { position, parent, callback } = options;
44
+ const parentDefault = parent || this.root;
45
+ const positionDefault = position ?? parentDefault.children.length;
46
+ const node = new Node(item, {
47
+ childIndex: positionDefault,
48
+ depth: parentDefault.depth + 1,
49
+ parent: parentDefault,
50
+ tree: this
51
+ });
52
+ this.getNode(parentDefault.dsId).addNode(node);
53
+ this._hash += 1;
54
+ callback?.(node, this);
55
+ return node;
56
+ };
57
+ removeNode = (id, options = {}) => {
58
+ const node = this.getNode(id);
59
+ if (!node) throw new Error(`Node with id ${id} not found`);
60
+ node.removeNode();
61
+ this._hash += 1;
62
+ options.callback?.(node, this);
63
+ return node;
64
+ };
65
+ moveNode = (id, options = {}) => {
66
+ const removedNode = this.removeNode(id);
67
+ const node = this.addNode(removedNode.getJson(), options);
68
+ this._hash += 1;
69
+ return node;
70
+ };
71
+ replaceNode = (id, item, options = {}) => {
72
+ const removedNode = this.removeNode(id);
73
+ const node = this.addNode(item, { parent: removedNode.parent, position: removedNode.childIndex });
74
+ this._hash += 1;
75
+ options.callback?.(node, this);
76
+ return node;
77
+ };
78
+ getPath = (id) => this.getRoot().getPath(id);
79
+ getPathIds = (id) => this.getRoot().getPathIds(id);
76
80
  }
77
81
  export {
78
82
  DSTree
@@ -2,6 +2,6 @@
2
2
  "version": 3,
3
3
  "sources": ["../../../../../scripts/build/transpile/react-shim.js", "../../src/DSTree.ts"],
4
4
  "sourcesContent": ["import * as React from 'react';\nexport { React };\n", "/* eslint-disable no-underscore-dangle */\nimport type {\n AddOptions,\n AnyObjectWithoutReservedKeys,\n MoveOptions,\n MutateOptions,\n RemoveOptions,\n Item,\n} from './types.js';\nimport { Node } from './Node.js';\n\nexport class DSTree<AppInterface extends AnyObjectWithoutReservedKeys = Record<string, unknown>> {\n private root: Node<AppInterface>;\n\n private _hash: number;\n\n private nodes: Record<string | number, Node<AppInterface>>;\n\n getUniqueId: (item: Item<AppInterface>) => string | number;\n\n constructor(item: Item<AppInterface>, options: { getUniqueId: (item: Item<AppInterface>) => string | number }) {\n this.getUniqueId = options.getUniqueId;\n this.root = new Node<AppInterface>(item, { childIndex: 0, depth: 0, tree: this });\n this.nodes = {};\n this._hash = 0;\n\n // autocalculate a dictionary where dsId are keys and nodes are values\n // and add the tree link to each node\n this.root.walk((node) => {\n this.nodes[node.dsId] = node;\n return true;\n });\n }\n\n addNodeToNodesDictionary(node: Node<AppInterface>): void {\n // check that we have no repeated ids\n if (this.nodes[node.dsId]) {\n throw new Error(`DSTree: repeated dsId ${node.dsId}`);\n }\n\n this.nodes[node.dsId] = node;\n }\n\n removeNodeFromNodesDictionary(node: Node<AppInterface>): void {\n delete this.nodes[node.dsId];\n }\n\n get hash(): number {\n return this._hash;\n }\n\n getRoot = (): Node<AppInterface> => this.root;\n\n getNode = (id: string | number): Node<AppInterface> => this.nodes[id];\n\n getNodes = (): Record<string, Node<AppInterface>> => this.nodes;\n\n walk = (callback: (node: Node<AppInterface>) => boolean): void => {\n this.root.walk(callback);\n };\n\n walkParents = (callback: (node: Node<AppInterface>) => boolean): void => {\n this.root.walkParents(callback);\n };\n\n findNode = (callback: (node: Node<AppInterface>) => boolean): Node<AppInterface> | null =>\n this.root.findNode(callback);\n\n findAllNodes = (callback: (node: Node<AppInterface>) => boolean): Node<AppInterface>[] =>\n this.root.findAllNodes(callback);\n\n flatten = (): Node<AppInterface>[] => this.root.flatten();\n\n addNode = (item: Item<AppInterface>, options: AddOptions<AppInterface> = {}): Node<AppInterface> => {\n const { position, parent, callback } = options;\n const parentDefault = parent || this.root;\n const positionDefault = position ?? parentDefault.children.length;\n\n // create node and add it to the parent\n const node = new Node<AppInterface>(item, {\n childIndex: positionDefault,\n depth: parentDefault.depth + 1,\n parent: parentDefault,\n tree: this,\n });\n this.getNode(parentDefault.dsId).addNode(node);\n\n // change hash\n this._hash += 1;\n\n // invoke the callback if present\n callback?.(node, this);\n\n return node;\n };\n\n removeNode = (id: string | number, options: RemoveOptions<AppInterface> = {}): Node<AppInterface> => {\n const node = this.getNode(id);\n if (!node) throw new Error(`Node with id ${id} not found`);\n\n node.removeNode();\n\n // change hash\n this._hash += 1;\n\n // invoke the callback if present\n options.callback?.(node, this);\n\n return node;\n };\n\n moveNode = (id: string | number, options: MoveOptions<AppInterface> = {}): Node<AppInterface> => {\n const removedNode = this.removeNode(id);\n const node = this.addNode(removedNode.getJson(), options);\n\n this._hash += 1;\n\n return node;\n };\n\n replaceNode = (\n id: string | number,\n item: Item<AppInterface>,\n options: MutateOptions<AppInterface> = {},\n ): Node<AppInterface> => {\n const removedNode = this.removeNode(id);\n const node = this.addNode(item, { parent: removedNode.parent, position: removedNode.childIndex });\n\n this._hash += 1;\n\n // invoke the callback if present\n options.callback?.(node, this);\n\n return node;\n };\n\n getPath = (id: string | number): Node<AppInterface>[] => this.getRoot().getPath(id);\n\n getPathIds = (id: string | number) => this.getRoot().getPathIds(id);\n}\n"],
5
- "mappings": "AAAA,YAAY,WAAW;ACSvB,SAAS,YAAY;AAEd,MAAM,OAAoF;AAAA,EAS/F,YAAY,MAA0B,SAAyE;AA+B/G,mBAAU,MAA0B,KAAK;AAEzC,mBAAU,CAAC,OAA4C,KAAK,MAAM,EAAE;AAEpE,oBAAW,MAA0C,KAAK;AAE1D,gBAAO,CAAC,aAA0D;AAChE,WAAK,KAAK,KAAK,QAAQ;AAAA,IACzB;AAEA,uBAAc,CAAC,aAA0D;AACvE,WAAK,KAAK,YAAY,QAAQ;AAAA,IACh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AAK,SAAS;AAGd,YAAQ,WAAW,MAAM,IAAI;AAE7B,WAAO;AAAA,EACT;AAAA,EAEA,WAAW,CAAC,IAAqB,UAAqC,CAAC,MAA0B;AAC/F,UAAM,cAAc,KAAK,WAAW,EAAE;AACtC,UAAM,OAAO,KAAK,QAAQ,YAAY,QAAQ,GAAG,OAAO;AAExD,SAAK,SAAS;AAEd,WAAO;AAAA,EACT;AAAA,EAEA,cAAc,CACZ,IACA,MACA,UAAuC,CAAC,MACjB;AACvB,UAAM,cAAc,KAAK,WAAW,EAAE;AACtC,UAAM,OAAO,KAAK,QAAQ,MAAM,EAAE,QAAQ,YAAY,QAAQ,UAAU,YAAY,WAAW,CAAC;AAEhG,SAAK,SAAS;AAGd,YAAQ,WAAW,MAAM,IAAI;AAE7B,WAAO;AAAA,EACT;AAAA,EAEA,UAAU,CAAC,OAA8C,KAAK,QAAQ,EAAE,QAAQ,EAAE;AAAA,EAElF,aAAa,CAAC,OAAwB,KAAK,QAAQ,EAAE,WAAW,EAAE;AACpE;",
6
6
  "names": []
7
7
  }
package/dist/esm/Node.js CHANGED
@@ -1,100 +1,16 @@
1
1
  import * as React from "react";
2
2
  import { cloneDeep } from "lodash";
3
3
  class Node {
4
+ dsId;
5
+ childIndex;
6
+ depth;
7
+ plainItem;
8
+ plainChildren;
9
+ parent;
10
+ children;
11
+ tree;
12
+ _hash;
4
13
  constructor(item, { childIndex, depth, parent, tree }) {
5
- // ===========================================================================
6
- // INTERNAL METHODS
7
- // ===========================================================================
8
- this.fixChildIndexes = () => {
9
- for (let i = 0; i < this.children.length; i += 1) {
10
- this.children[i].childIndex = i;
11
- }
12
- this._hash += 1;
13
- };
14
- this.getCleanItem = (item) => {
15
- const { subitems, ...plainItem } = item;
16
- return cloneDeep(plainItem);
17
- };
18
- this.walk = (callback) => {
19
- const shouldContinueWalking = callback(this);
20
- if (shouldContinueWalking) {
21
- for (const child of this.children) {
22
- child.walk(callback);
23
- }
24
- }
25
- };
26
- this.walkParents = (callback) => {
27
- const shouldContinueWalking = callback(this);
28
- if (shouldContinueWalking && this.parent) {
29
- this.parent.walkParents(callback);
30
- }
31
- };
32
- this.findNode = (callback) => {
33
- let found = null;
34
- this.walk((node) => {
35
- if (found) return false;
36
- if (callback(node)) found = node;
37
- return !found;
38
- });
39
- return found;
40
- };
41
- this.findAllNodes = (callback) => {
42
- const found = [];
43
- this.walk((node) => {
44
- if (callback(node)) found.push(node);
45
- return true;
46
- });
47
- return found;
48
- };
49
- this.flatten = () => {
50
- const flattened = [];
51
- this.walk((node) => {
52
- flattened.push(node);
53
- return true;
54
- });
55
- return flattened;
56
- };
57
- this.getPath = (id) => {
58
- const path = [];
59
- const node = this.tree.getNode(id);
60
- if (!node) return path;
61
- node.walkParents((parent) => {
62
- path.push(parent);
63
- return parent.dsId !== this.dsId;
64
- });
65
- return path.reverse();
66
- };
67
- this.getPathIds = (id) => this.getPath(id).map((node) => node.dsId);
68
- this.getJson = () => ({
69
- ...this.plainItem,
70
- subitems: this.children?.map?.((child) => child.getJson()) ?? void 0
71
- });
72
- // ===========================================================================
73
- // WRITE METHODS
74
- // ===========================================================================
75
- this.addNode = (node) => {
76
- const position = node.childIndex;
77
- if (position < 0 || position > this.children.length) {
78
- throw new Error(`Invalid position ${position} for parent ${this.dsId} with item ${node.dsId}`);
79
- }
80
- this.children.splice(position, 0, node);
81
- this.fixChildIndexes();
82
- node.walk((child) => {
83
- this.tree.addNodeToNodesDictionary(child);
84
- return true;
85
- });
86
- this._hash += 1;
87
- };
88
- this.removeNode = () => {
89
- if (!this.parent) throw new Error(`Cannot remove root node with id ${this.dsId}`);
90
- this.parent.children.splice(this.childIndex, 1);
91
- this.parent.fixChildIndexes();
92
- this.walk((child) => {
93
- this.tree.removeNodeFromNodesDictionary(child);
94
- return true;
95
- });
96
- this._hash += 1;
97
- };
98
14
  this.dsId = tree.getUniqueId(item);
99
15
  this.childIndex = childIndex;
100
16
  this.depth = depth;
@@ -118,11 +34,104 @@ class Node {
118
34
  this.plainItem = this.getCleanItem(item);
119
35
  }
120
36
  // ===========================================================================
37
+ // INTERNAL METHODS
38
+ // ===========================================================================
39
+ fixChildIndexes = () => {
40
+ for (let i = 0; i < this.children.length; i += 1) {
41
+ this.children[i].childIndex = i;
42
+ }
43
+ this._hash += 1;
44
+ };
45
+ getCleanItem = (item) => {
46
+ const { subitems, ...plainItem } = item;
47
+ return cloneDeep(plainItem);
48
+ };
49
+ // ===========================================================================
121
50
  // READ METHODS
122
51
  // ===========================================================================
123
52
  get hash() {
124
53
  return this._hash;
125
54
  }
55
+ walk = (callback) => {
56
+ const shouldContinueWalking = callback(this);
57
+ if (shouldContinueWalking) {
58
+ for (const child of this.children) {
59
+ child.walk(callback);
60
+ }
61
+ }
62
+ };
63
+ walkParents = (callback) => {
64
+ const shouldContinueWalking = callback(this);
65
+ if (shouldContinueWalking && this.parent) {
66
+ this.parent.walkParents(callback);
67
+ }
68
+ };
69
+ findNode = (callback) => {
70
+ let found = null;
71
+ this.walk((node) => {
72
+ if (found) return false;
73
+ if (callback(node)) found = node;
74
+ return !found;
75
+ });
76
+ return found;
77
+ };
78
+ findAllNodes = (callback) => {
79
+ const found = [];
80
+ this.walk((node) => {
81
+ if (callback(node)) found.push(node);
82
+ return true;
83
+ });
84
+ return found;
85
+ };
86
+ flatten = () => {
87
+ const flattened = [];
88
+ this.walk((node) => {
89
+ flattened.push(node);
90
+ return true;
91
+ });
92
+ return flattened;
93
+ };
94
+ getPath = (id) => {
95
+ const path = [];
96
+ const node = this.tree.getNode(id);
97
+ if (!node) return path;
98
+ node.walkParents((parent) => {
99
+ path.push(parent);
100
+ return parent.dsId !== this.dsId;
101
+ });
102
+ return path.reverse();
103
+ };
104
+ getPathIds = (id) => this.getPath(id).map((node) => node.dsId);
105
+ getJson = () => ({
106
+ ...this.plainItem,
107
+ subitems: this.children?.map?.((child) => child.getJson()) ?? void 0
108
+ });
109
+ // ===========================================================================
110
+ // WRITE METHODS
111
+ // ===========================================================================
112
+ addNode = (node) => {
113
+ const position = node.childIndex;
114
+ if (position < 0 || position > this.children.length) {
115
+ throw new Error(`Invalid position ${position} for parent ${this.dsId} with item ${node.dsId}`);
116
+ }
117
+ this.children.splice(position, 0, node);
118
+ this.fixChildIndexes();
119
+ node.walk((child) => {
120
+ this.tree.addNodeToNodesDictionary(child);
121
+ return true;
122
+ });
123
+ this._hash += 1;
124
+ };
125
+ removeNode = () => {
126
+ if (!this.parent) throw new Error(`Cannot remove root node with id ${this.dsId}`);
127
+ this.parent.children.splice(this.childIndex, 1);
128
+ this.parent.fixChildIndexes();
129
+ this.walk((child) => {
130
+ this.tree.removeNodeFromNodesDictionary(child);
131
+ return true;
132
+ });
133
+ this._hash += 1;
134
+ };
126
135
  }
127
136
  export {
128
137
  Node
@@ -2,6 +2,6 @@
2
2
  "version": 3,
3
3
  "sources": ["../../../../../scripts/build/transpile/react-shim.js", "../../src/Node.ts"],
4
4
  "sourcesContent": ["import * as React from 'react';\nexport { React };\n", "/* eslint-disable no-underscore-dangle */\n/* eslint-disable no-use-before-define */\n/* eslint-disable max-params */\nimport { cloneDeep } from 'lodash';\nimport type { DSTree } from './DSTree.js';\nimport type { NodeConstructorOptions, AnyObjectWithoutReservedKeys, Item } from './types.js';\n\nexport class Node<AppInterface extends AnyObjectWithoutReservedKeys = Record<string, unknown>> {\n dsId: string | number;\n\n childIndex: number;\n\n depth: number;\n\n plainItem: AppInterface;\n\n plainChildren: AppInterface[];\n\n parent: Node<AppInterface> | null;\n\n children: Node<AppInterface>[];\n\n private tree!: DSTree<AppInterface>;\n\n private _hash: number;\n\n constructor(item: Item<AppInterface>, { childIndex, depth, parent, tree }: NodeConstructorOptions<AppInterface>) {\n this.dsId = tree.getUniqueId(item);\n this.childIndex = childIndex;\n this.depth = depth;\n this.parent = parent ?? null;\n this.tree = tree;\n this._hash = 0;\n this.children = [];\n this.plainChildren = [];\n\n // on construction, we add the children to the node\n // this will recursively happen for all children because this triggers `new Node()` recursively\n const subitems = item.subitems ?? [];\n subitems.forEach((subitem, index) => {\n this.plainChildren.push(this.getCleanItem(subitem));\n this.children.push(\n new Node<AppInterface>(subitem, {\n childIndex: index,\n depth: depth + 1,\n parent: this,\n tree,\n }),\n );\n });\n // Save the item without the subitems\n this.plainItem = this.getCleanItem(item);\n }\n\n // ===========================================================================\n // INTERNAL METHODS\n // ===========================================================================\n\n fixChildIndexes = (): void => {\n for (let i = 0; i < this.children.length; i += 1) {\n this.children[i].childIndex = i;\n }\n this._hash += 1;\n };\n\n getCleanItem = (item: Item<AppInterface>): AppInterface => {\n const { subitems, ...plainItem } = item;\n return cloneDeep(plainItem as AppInterface);\n };\n\n // ===========================================================================\n // READ METHODS\n // ===========================================================================\n\n get hash(): number {\n return this._hash;\n }\n\n walk = (callback: (node: Node<AppInterface>) => boolean): void => {\n const shouldContinueWalking = callback(this);\n if (shouldContinueWalking) {\n // eslint-disable-next-line no-restricted-syntax\n for (const child of this.children) {\n child.walk(callback);\n }\n }\n };\n\n walkParents = (callback: (node: Node<AppInterface>) => boolean): void => {\n const shouldContinueWalking = callback(this);\n if (shouldContinueWalking && this.parent) {\n this.parent.walkParents(callback);\n }\n };\n\n findNode = (callback: (node: Node<AppInterface>) => boolean): Node<AppInterface> | null => {\n // find first node using walk\n let found: Node<AppInterface> | null = null;\n this.walk((node) => {\n // If we already found it, don't look anymore\n if (found) return false;\n\n // Otherwise keep looking\n if (callback(node)) found = node;\n return !found;\n });\n\n return found;\n };\n\n findAllNodes = (callback: (node: Node<AppInterface>) => boolean): Node<AppInterface>[] => {\n const found: Node<AppInterface>[] = [];\n this.walk((node) => {\n if (callback(node)) found.push(node);\n return true;\n });\n return found;\n };\n\n flatten = (): Node<AppInterface>[] => {\n const flattened: Node<AppInterface>[] = [];\n this.walk((node) => {\n flattened.push(node);\n return true;\n });\n return flattened;\n };\n\n getPath = (id: string | number): Node<AppInterface>[] => {\n const path: Node<AppInterface>[] = [];\n const node = this.tree.getNode(id);\n if (!node) return path;\n node.walkParents((parent) => {\n path.push(parent);\n return parent.dsId !== this.dsId;\n });\n return path.reverse();\n };\n\n getPathIds = (id: string | number) => this.getPath(id).map((node) => node.dsId);\n\n getJson = (): Item<AppInterface> =>\n ({\n ...this.plainItem,\n subitems: this.children?.map?.((child) => child.getJson()) ?? undefined,\n }) as Item<AppInterface>;\n\n // ===========================================================================\n // WRITE METHODS\n // ===========================================================================\n\n addNode = (node: Node<AppInterface>) => {\n // check that the position is valid for that parent\n const position = node.childIndex;\n if (position < 0 || position > this.children.length) {\n throw new Error(`Invalid position ${position} for parent ${this.dsId} with item ${node.dsId}`);\n }\n\n // add the new node\n this.children.splice(position, 0, node);\n\n // fix the childIndex of the nodes after the new one\n this.fixChildIndexes();\n\n // add subtree of the new node to the node dictionary\n node.walk((child) => {\n this.tree.addNodeToNodesDictionary(child);\n return true;\n });\n\n this._hash += 1;\n };\n\n removeNode = () => {\n // if no parent throw error\n if (!this.parent) throw new Error(`Cannot remove root node with id ${this.dsId}`);\n\n // remove from parent\n this.parent.children.splice(this.childIndex, 1);\n\n // fix the childIndex of the nodes of the parent\n this.parent.fixChildIndexes();\n\n // remove subtree from the node dictionary\n this.walk((child) => {\n this.tree.removeNodeFromNodesDictionary(child);\n return true;\n });\n\n this._hash += 1;\n };\n}\n"],
5
- "mappings": "AAAA,YAAY,WAAW;ACGvB,SAAS,iBAAiB;AAInB,MAAM,KAAkF;AAAA,EAmB7F,YAAY,MAA0B,EAAE,YAAY,OAAO,QAAQ,KAAK,GAAyC;AAgCjH;AAAA;AAAA;AAAA,2BAAkB,MAAY;AAC5B,eAAS,IAAI,GAAG,IAAI,KAAK,SAAS,QAAQ,KAAK,GAAG;AAChD,aAAK,SAAS,CAAC,EAAE,aAAa;AAAA,MAChC;AACA,WAAK,SAAS;AAAA,IAChB;AAEA,wBAAe,CAAC,SAA2C;AACzD,YAAM,EAAE,UAAU,GAAG,UAAU,IAAI;AACnC,aAAO,UAAU,SAAyB;AAAA,IAC5C;AAUA,gBAAO,CAAC,aAA0D;AAChE,Y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AC,SAAS,KAAK,IAAI;AAAA,EAE9E,UAAU,OACP;AAAA,IACC,GAAG,KAAK;AAAA,IACR,UAAU,KAAK,UAAU,MAAM,CAAC,UAAU,MAAM,QAAQ,CAAC,KAAK;AAAA,EAChE;AAAA;AAAA;AAAA;AAAA,EAMF,UAAU,CAAC,SAA6B;AAEtC,UAAM,WAAW,KAAK;AACtB,QAAI,WAAW,KAAK,WAAW,KAAK,SAAS,QAAQ;AACnD,YAAM,IAAI,MAAM,oBAAoB,QAAQ,eAAe,KAAK,IAAI,cAAc,KAAK,IAAI,EAAE;AAAA,IAC/F;AAGA,SAAK,SAAS,OAAO,UAAU,GAAG,IAAI;AAGtC,SAAK,gBAAgB;AAGrB,SAAK,KAAK,CAAC,UAAU;AACnB,WAAK,KAAK,yBAAyB,KAAK;AACxC,aAAO;AAAA,IACT,CAAC;AAED,SAAK,SAAS;AAAA,EAChB;AAAA,EAEA,aAAa,MAAM;AAEjB,QAAI,CAAC,KAAK,OAAQ,OAAM,IAAI,MAAM,mCAAmC,KAAK,IAAI,EAAE;AAGhF,SAAK,OAAO,SAAS,OAAO,KAAK,YAAY,CAAC;AAG9C,SAAK,OAAO,gBAAgB;AAG5B,SAAK,KAAK,CAAC,UAAU;AACnB,WAAK,KAAK,8BAA8B,KAAK;AAC7C,aAAO;AAAA,IACT,CAAC;AAED,SAAK,SAAS;AAAA,EAChB;AACF;",
6
6
  "names": []
7
7
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@elliemae/ds-tree-model",
3
- "version": "3.49.0-rc.9",
3
+ "version": "3.49.1",
4
4
  "license": "MIT",
5
5
  "description": "ICE MT - Dimsum - Tree Model",
6
6
  "files": [
@@ -25,8 +25,8 @@
25
25
  "url": "https://git.elliemae.io/platform-ui/dimsum.git"
26
26
  },
27
27
  "engines": {
28
- "pnpm": ">=6",
29
- "node": ">=16"
28
+ "pnpm": ">=9",
29
+ "node": ">=22"
30
30
  },
31
31
  "author": "ICE MT",
32
32
  "jestSonar": {
@@ -40,11 +40,13 @@
40
40
  "typeSafety": true
41
41
  },
42
42
  "dependencies": {
43
- "@elliemae/ds-props-helpers": "3.49.0-rc.9"
43
+ "@elliemae/ds-props-helpers": "3.49.1"
44
44
  },
45
45
  "devDependencies": {
46
46
  "@elliemae/pui-cli": "9.0.0-next.50",
47
- "@elliemae/ds-monorepo-devops": "3.49.0-rc.9"
47
+ "jest": "~29.7.0",
48
+ "jest-cli": "~29.7.0",
49
+ "@elliemae/ds-monorepo-devops": "3.49.1"
48
50
  },
49
51
  "peerDependencies": {
50
52
  "lodash": "^4.17.21",