clonetree 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 +24 -2
- package/index.js +18 -0
- package/package.json +18 -4
package/README.md
CHANGED
|
@@ -1,3 +1,25 @@
|
|
|
1
|
-
#
|
|
1
|
+
# clonetree
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Deep-clone tree-shaped data structures (`{ id, children: [...] }`) without shared references.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install clonetree
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
const { cloneTree } = require('clonetree');
|
|
15
|
+
|
|
16
|
+
const tree = { id: 1, children: [{ id: 2, children: [] }] };
|
|
17
|
+
const copy = cloneTree(tree);
|
|
18
|
+
|
|
19
|
+
copy.children[0].id === 2; // true
|
|
20
|
+
copy.children !== tree.children; // true
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## License
|
|
24
|
+
|
|
25
|
+
MIT
|
package/index.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
function cloneNode(node) {
|
|
4
|
+
if (node == null || typeof node !== 'object') return node;
|
|
5
|
+
const { children, ...rest } = node;
|
|
6
|
+
const copy = { ...rest };
|
|
7
|
+
if (Array.isArray(children)) {
|
|
8
|
+
copy.children = children.map(cloneNode);
|
|
9
|
+
}
|
|
10
|
+
return copy;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function cloneTree(root) {
|
|
14
|
+
return cloneNode(root);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
module.exports = { cloneTree, cloneNode };
|
|
18
|
+
module.exports.default = cloneTree;
|
package/package.json
CHANGED
|
@@ -1,6 +1,20 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "clonetree",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Deep-clone tree-shaped data structures.",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"tree",
|
|
8
|
+
"clone",
|
|
9
|
+
"deep-clone",
|
|
10
|
+
"structuredClone"
|
|
11
|
+
],
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"files": [
|
|
14
|
+
"index.js",
|
|
15
|
+
"README.md"
|
|
16
|
+
],
|
|
17
|
+
"engines": {
|
|
18
|
+
"node": ">=17"
|
|
19
|
+
}
|
|
20
|
+
}
|