@jckj/tree-util 0.0.0-stage → 1.1.12

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 hzqhxy
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 @@
1
- # Temporary Holding Version
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.
1
+ ### 第一次提交
@@ -0,0 +1,144 @@
1
+ // src/util/tree/index.ts
2
+ function e(e, t) {
3
+ let n = {
4
+ idKey: "id",
5
+ parentKey: "parent_id",
6
+ childrenKey: "children",
7
+ rootId: "0",
8
+ isTree: !1,
9
+ ...t
10
+ };
11
+ if (!Array.isArray(e)) throw TypeError("Expected an array");
12
+ let r = {}, i = [];
13
+ for (let t = 0; t < e.length; t++) {
14
+ let i = e[t], a = i[n.idKey];
15
+ r[a] = { ...i };
16
+ }
17
+ for (let t = 0; t < e.length; t++) {
18
+ let a = e[t], o = a[n.idKey], s = a[n.parentKey] || n.rootId, c = r[o], l = r[s];
19
+ l ? (l[n.childrenKey] || (l[n.childrenKey] = []), l[n.childrenKey].push(c)) : i.push(c);
20
+ }
21
+ return i;
22
+ }
23
+ function t(e, t) {
24
+ let n = {
25
+ idKey: "id",
26
+ parentKey: "parent_id",
27
+ childrenKey: "children",
28
+ rootId: "0",
29
+ isTree: !0,
30
+ ...t
31
+ }, r = [], i = [];
32
+ for (let t = e.length - 1; t >= 0; t--) i.push([e[t], n.rootId]);
33
+ for (; i.length > 0;) {
34
+ let [e, t] = i.pop(), a = e[n.childrenKey], { [n.childrenKey]: o, ...s } = e;
35
+ if (s.hasOwnProperty(n.parentKey) || (s[n.parentKey] = t), r.push(s), Array.isArray(a) && a.length > 0) {
36
+ let e = s[n.idKey];
37
+ for (let t = a.length - 1; t >= 0; t--) i.push([a[t], e]);
38
+ }
39
+ }
40
+ return r;
41
+ }
42
+ function n(e, t, n) {
43
+ let r = {
44
+ childrenKey: "children",
45
+ ...n
46
+ }, i = [...e];
47
+ for (; i.length > 0;) {
48
+ let n = i.pop();
49
+ t(n, e);
50
+ let a = n[r.childrenKey];
51
+ if (Array.isArray(a) && a.length > 0) for (let e = a.length - 1; e >= 0; e--) i.push(a[e]);
52
+ }
53
+ }
54
+ function r(e, t, n) {
55
+ let r = {
56
+ idKey: "id",
57
+ childrenKey: "children",
58
+ ...n
59
+ }, i = [...e];
60
+ for (; i.length > 0;) {
61
+ let e = i.pop();
62
+ if (e[r.idKey] === t) return e;
63
+ let n = e[r.childrenKey];
64
+ if (Array.isArray(n) && n.length > 0) for (let e = n.length - 1; e >= 0; e--) i.push(n[e]);
65
+ }
66
+ }
67
+ function i(e, n, i) {
68
+ let a = {
69
+ idKey: "id",
70
+ childrenKey: "children",
71
+ ...i
72
+ }, o = r(e, n, a);
73
+ if (!o) return [];
74
+ let s = o[a.childrenKey];
75
+ return Array.isArray(s) && s.length > 0 ? t(s, a) : [];
76
+ }
77
+ function a(e, n, r) {
78
+ let i = {
79
+ idKey: "id",
80
+ parentKey: "parent_id",
81
+ childrenKey: "children",
82
+ isTree: !0,
83
+ ...r
84
+ }, a = i.isTree ? t(e, i) : e, o = /* @__PURE__ */ new Map();
85
+ a.forEach((e) => o.set(e[i.idKey], e));
86
+ let s = [], c = o.get(n);
87
+ for (; c;) {
88
+ s.push(c);
89
+ let e = c[i.parentKey];
90
+ c = e ? o.get(e) : void 0;
91
+ }
92
+ return s;
93
+ }
94
+ function o(e, t, n) {
95
+ let r = {
96
+ childrenKey: "children",
97
+ ...n
98
+ }, i = (e) => {
99
+ let n = [];
100
+ for (let a of e) {
101
+ let e = a[r.childrenKey], o = Array.isArray(e) ? i(e) : [];
102
+ if (t(a) || o.length > 0) {
103
+ let e = { ...a };
104
+ o.length > 0 && (e[r.childrenKey] = o), n.push(e);
105
+ }
106
+ }
107
+ return n;
108
+ };
109
+ return i(e);
110
+ }
111
+ function s(e, t, n) {
112
+ let r = {
113
+ childrenKey: "children",
114
+ ...n
115
+ }, i = (e) => e.map((e) => {
116
+ let n = e[r.childrenKey], a = t(e);
117
+ return Array.isArray(n) && n.length > 0 && (a[r.childrenKey] = i(n)), a;
118
+ });
119
+ return i(e);
120
+ }
121
+ function c(e, t, n, r) {
122
+ let i = {
123
+ idKey: "id",
124
+ childrenKey: "children",
125
+ ...r
126
+ }, a = (e) => {
127
+ let r = !1, o = e.map((e) => {
128
+ if (e[i.idKey] === t) return r = !0, n(e);
129
+ let o = e[i.childrenKey];
130
+ if (Array.isArray(o) && o.length > 0) {
131
+ let t = a(o);
132
+ if (t !== o) {
133
+ r = !0;
134
+ let n = { ...e };
135
+ return n[i.childrenKey] = t, n;
136
+ }
137
+ }
138
+ return e;
139
+ });
140
+ return r ? o : e;
141
+ };
142
+ return a(e);
143
+ }
144
+ export { e as arrayToTree, o as filterTree, n as forEachNode, i as getChildrenToList, r as getNodeById, a as getParentNodes, s as mapTree, t as treeToArray, c as updateNodeById };
@@ -0,0 +1 @@
1
+ (function(e,t){typeof exports==`object`&&typeof module<`u`?t(exports):typeof define==`function`&&define.amd?define([`exports`],t):(e=typeof globalThis<`u`?globalThis:e||self,t(e.treeUtil={}))})(this,function(e){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});function t(e,t){let n={idKey:`id`,parentKey:`parent_id`,childrenKey:`children`,rootId:`0`,isTree:!1,...t};if(!Array.isArray(e))throw TypeError(`Expected an array`);let r={},i=[];for(let t=0;t<e.length;t++){let i=e[t],a=i[n.idKey];r[a]={...i}}for(let t=0;t<e.length;t++){let a=e[t],o=a[n.idKey],s=a[n.parentKey]||n.rootId,c=r[o],l=r[s];l?(l[n.childrenKey]||(l[n.childrenKey]=[]),l[n.childrenKey].push(c)):i.push(c)}return i}function n(e,t){let n={idKey:`id`,parentKey:`parent_id`,childrenKey:`children`,rootId:`0`,isTree:!0,...t},r=[],i=[];for(let t=e.length-1;t>=0;t--)i.push([e[t],n.rootId]);for(;i.length>0;){let[e,t]=i.pop(),a=e[n.childrenKey],{[n.childrenKey]:o,...s}=e;if(s.hasOwnProperty(n.parentKey)||(s[n.parentKey]=t),r.push(s),Array.isArray(a)&&a.length>0){let e=s[n.idKey];for(let t=a.length-1;t>=0;t--)i.push([a[t],e])}}return r}function r(e,t,n){let r={childrenKey:`children`,...n},i=[...e];for(;i.length>0;){let n=i.pop();t(n,e);let a=n[r.childrenKey];if(Array.isArray(a)&&a.length>0)for(let e=a.length-1;e>=0;e--)i.push(a[e])}}function i(e,t,n){let r={idKey:`id`,childrenKey:`children`,...n},i=[...e];for(;i.length>0;){let e=i.pop();if(e[r.idKey]===t)return e;let n=e[r.childrenKey];if(Array.isArray(n)&&n.length>0)for(let e=n.length-1;e>=0;e--)i.push(n[e])}}function a(e,t,r){let a={idKey:`id`,childrenKey:`children`,...r},o=i(e,t,a);if(!o)return[];let s=o[a.childrenKey];return Array.isArray(s)&&s.length>0?n(s,a):[]}function o(e,t,r){let i={idKey:`id`,parentKey:`parent_id`,childrenKey:`children`,isTree:!0,...r},a=i.isTree?n(e,i):e,o=new Map;a.forEach(e=>o.set(e[i.idKey],e));let s=[],c=o.get(t);for(;c;){s.push(c);let e=c[i.parentKey];c=e?o.get(e):void 0}return s}function s(e,t,n){let r={childrenKey:`children`,...n},i=e=>{let n=[];for(let a of e){let e=a[r.childrenKey],o=Array.isArray(e)?i(e):[];if(t(a)||o.length>0){let e={...a};o.length>0&&(e[r.childrenKey]=o),n.push(e)}}return n};return i(e)}function c(e,t,n){let r={childrenKey:`children`,...n},i=e=>e.map(e=>{let n=e[r.childrenKey],a=t(e);return Array.isArray(n)&&n.length>0&&(a[r.childrenKey]=i(n)),a});return i(e)}function l(e,t,n,r){let i={idKey:`id`,childrenKey:`children`,...r},a=e=>{let r=!1,o=e.map(e=>{if(e[i.idKey]===t)return r=!0,n(e);let o=e[i.childrenKey];if(Array.isArray(o)&&o.length>0){let t=a(o);if(t!==o){r=!0;let n={...e};return n[i.childrenKey]=t,n}}return e});return r?o:e};return a(e)}e.arrayToTree=t,e.filterTree=s,e.forEachNode=r,e.getChildrenToList=a,e.getNodeById=i,e.getParentNodes=o,e.mapTree=c,e.treeToArray=n,e.updateNodeById=l});
package/package.json CHANGED
@@ -1,6 +1,45 @@
1
1
  {
2
2
  "name": "@jckj/tree-util",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "1.1.12",
4
+ "publishConfig": {
5
+ "access": "public",
6
+ "registry": "https://registry.npmjs.org/"
7
+ },
8
+ "type": "module",
9
+ "files": [
10
+ "dist",
11
+ "types.d.ts",
12
+ "README.md",
13
+ "LICENSE"
14
+ ],
15
+ "main": "dist/treeUtil.umd.js",
16
+ "module": "dist/treeUtil.js",
17
+ "types": "types.d.ts",
18
+ "exports": {
19
+ "types": "./types.d.ts",
20
+ "import": "./dist/treeUtil.js",
21
+ "require": "./dist/treeUtil.umd.js"
22
+ },
23
+ "description": "",
24
+ "repository": {
25
+ "type": "git",
26
+ "url": "https://gitee.com/hzqhxy/tree-util"
27
+ },
28
+ "keywords": [
29
+ "tree",
30
+ "util",
31
+ "tree-util",
32
+ "tree-util-ts"
33
+ ],
34
+ "author": "chuz1 <jckjsjz@163.com>",
35
+ "license": "MIT",
36
+ "scripts": {
37
+ "dev": "vite",
38
+ "build": "tsc && vite build",
39
+ "preview": "vite preview"
40
+ },
41
+ "devDependencies": {
42
+ "typescript": "~7.0.2",
43
+ "vite": "^8.3.6"
44
+ }
45
+ }
package/types.d.ts ADDED
@@ -0,0 +1,22 @@
1
+ declare module "tree-util-jc" {
2
+ /**
3
+ * #### 树形结构操作配置选项
4
+ * @template T 原始数据项的类型
5
+ */
6
+ export interface TreeOptions<T> {
7
+ /** 节点的唯一标识属性名,默认 'id' */
8
+ idKey?: keyof T;
9
+ /** 指向父节点的属性名,默认 'parent_id' */
10
+ parentKey?: keyof T;
11
+ /** 存放子节点的属性名,默认 'children' */
12
+ childrenKey?: string;
13
+ /** 根节点的父节点标识值,默认 '0' */
14
+ rootId?: string | number;
15
+ /** 传入的 data 是否已经是树形结构 (用于 getParentNodes 判断) */
16
+ isTree?: boolean;
17
+ }
18
+ export function arrayToTree<T extends object>(
19
+ arr: T[],
20
+ options?: TreeOptions<T>,
21
+ ): T[];
22
+ }