qidian-shared 1.0.18 → 1.0.19

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.
@@ -7986,6 +7986,38 @@ function easyDownload(res, name) {
7986
7986
  }, 1e3);
7987
7987
  }
7988
7988
  }
7989
+ function buildTree(data, id = "id", parentId = "parentId", childrenField = "children") {
7990
+ const childrenListMap = {};
7991
+ const nodeIds = {};
7992
+ const tree = [];
7993
+ for (const d of data) {
7994
+ const pid = d[parentId];
7995
+ if (!childrenListMap[pid]) {
7996
+ childrenListMap[pid] = [];
7997
+ }
7998
+ nodeIds[d[id]] = d;
7999
+ childrenListMap[pid].push(d);
8000
+ }
8001
+ for (const d of data) {
8002
+ const pid = d[parentId];
8003
+ if (!nodeIds[pid]) {
8004
+ tree.push(d);
8005
+ }
8006
+ }
8007
+ for (const t of tree) {
8008
+ adaptToChildrenList(t);
8009
+ }
8010
+ function adaptToChildrenList(o) {
8011
+ const childNodes = childrenListMap[o[id]];
8012
+ if (childNodes) {
8013
+ o[childrenField] = childNodes;
8014
+ for (const c of childNodes) {
8015
+ adaptToChildrenList(c);
8016
+ }
8017
+ }
8018
+ }
8019
+ return tree;
8020
+ }
7989
8021
  var BI_RM = "0123456789abcdefghijklmnopqrstuvwxyz";
7990
8022
  function int2char(n) {
7991
8023
  return BI_RM.charAt(n);
@@ -12380,6 +12412,7 @@ function useTimer(type = "timeout") {
12380
12412
  };
12381
12413
  }
12382
12414
  exports.MD5 = MD5;
12415
+ exports.buildTree = buildTree;
12383
12416
  exports.createEnum = createEnum;
12384
12417
  exports.decrypt = decrypt;
12385
12418
  exports.decryptBase64 = decryptBase64;