performant-array-to-tree
Advanced tools
Comparing version 1.4.0 to 1.5.0
@@ -9,4 +9,3 @@ export interface Item { | ||
parentId?: string | number | null; | ||
[key: string]: Item | any; | ||
children: TreeItem[]; | ||
[key: string]: Item | TreeItem[] | any; | ||
} | ||
@@ -17,2 +16,3 @@ export interface Config { | ||
dataField: string | null; | ||
childrenField: string; | ||
} | ||
@@ -19,0 +19,0 @@ /** |
@@ -18,2 +18,3 @@ "use strict"; | ||
dataField: 'data', | ||
childrenField: 'children', | ||
}; | ||
@@ -24,2 +25,3 @@ /** | ||
function arrayToTree(items, config) { | ||
var _a, _b, _c; | ||
if (config === void 0) { config = {}; } | ||
@@ -42,3 +44,3 @@ var conf = __assign({}, defaultConfig, config); | ||
// item is not yet there, so add a preliminary item (its data will be added later) | ||
lookup[itemId] = { children: [] }; | ||
lookup[itemId] = (_a = {}, _a[conf.childrenField] = [], _a); | ||
} | ||
@@ -50,3 +52,3 @@ // add the current item's data to the item in the lookup table | ||
else { | ||
lookup[itemId] = __assign({}, item, { children: lookup[itemId].children }); | ||
lookup[itemId] = __assign({}, item, (_b = {}, _b[conf.childrenField] = lookup[itemId][conf.childrenField], _b)); | ||
} | ||
@@ -63,6 +65,6 @@ var TreeItem = lookup[itemId]; | ||
// parent is not yet there, so add a preliminary parent (its data will be added later) | ||
lookup[parentId] = { children: [] }; | ||
lookup[parentId] = (_c = {}, _c[conf.childrenField] = [], _c); | ||
} | ||
// add the current item to the parent | ||
lookup[parentId].children.push(TreeItem); | ||
lookup[parentId][conf.childrenField].push(TreeItem); | ||
} | ||
@@ -69,0 +71,0 @@ } |
@@ -1,1 +0,1 @@ | ||
"use strict";var __assign=this&&this.__assign||function(){return(__assign=Object.assign||function(r){for(var e,a=1,t=arguments.length;a<t;a++)for(var n in e=arguments[a])Object.prototype.hasOwnProperty.call(e,n)&&(r[n]=e[n]);return r}).apply(this,arguments)};Object.defineProperty(exports,"__esModule",{value:!0});var defaultConfig={id:"id",parentId:"parentId",dataField:"data"};function arrayToTree(r,e){void 0===e&&(e={});for(var a=__assign({},defaultConfig,e),t=[],n={},i=0,o=r;i<o.length;i++){var s=o[i],d=s[a.id],l=s[a.parentId];Object.prototype.hasOwnProperty.call(n,d)||(n[d]={children:[]}),a.dataField?n[d][a.dataField]=s:n[d]=__assign({},s,{children:n[d].children});var p=n[d];null==l?t.push(p):(Object.prototype.hasOwnProperty.call(n,l)||(n[l]={children:[]}),n[l].children.push(p))}return t}exports.arrayToTree=arrayToTree; | ||
"use strict";var __assign=this&&this.__assign||function(){return(__assign=Object.assign||function(e){for(var r,a=1,t=arguments.length;a<t;a++)for(var i in r=arguments[a])Object.prototype.hasOwnProperty.call(r,i)&&(e[i]=r[i]);return e}).apply(this,arguments)};Object.defineProperty(exports,"__esModule",{value:!0});var defaultConfig={id:"id",parentId:"parentId",dataField:"data",childrenField:"children"};function arrayToTree(e,r){var a,t,i;void 0===r&&(r={});for(var n=__assign({},defaultConfig,r),d=[],l={},o=0,s=e;o<s.length;o++){var c=s[o],p=c[n.id],h=c[n.parentId];Object.prototype.hasOwnProperty.call(l,p)||(l[p]=((a={})[n.childrenField]=[],a)),n.dataField?l[p][n.dataField]=c:l[p]=__assign({},c,((t={})[n.childrenField]=l[p][n.childrenField],t));var u=l[p];null==h?d.push(u):(Object.prototype.hasOwnProperty.call(l,h)||(l[h]=((i={})[n.childrenField]=[],i)),l[h][n.childrenField].push(u))}return d}exports.arrayToTree=arrayToTree; |
@@ -55,12 +55,12 @@ "use strict"; | ||
{ num: '418', ref: null, custom: 'ü' }, | ||
]), { id: 'num', parentId: 'ref' })).to.deep.equal([ | ||
]), { id: 'num', parentId: 'ref', childrenField: 'nodes' })).to.deep.equal([ | ||
{ | ||
data: { num: '4', ref: null, custom: 'abc' }, children: [ | ||
{ data: { num: '31', ref: '4', custom: '12' }, children: [] }, | ||
data: { num: '4', ref: null, custom: 'abc' }, nodes: [ | ||
{ data: { num: '31', ref: '4', custom: '12' }, nodes: [] }, | ||
], | ||
}, | ||
{ | ||
data: { num: '418', ref: null, custom: 'ü' }, children: [ | ||
{ data: { num: '1941', ref: '418', custom: 'de' }, children: [] }, | ||
{ data: { num: '1', ref: '418', custom: 'ZZZz' }, children: [] }, | ||
data: { num: '418', ref: null, custom: 'ü' }, nodes: [ | ||
{ data: { num: '1941', ref: '418', custom: 'de' }, nodes: [] }, | ||
{ data: { num: '1', ref: '418', custom: 'ZZZz' }, nodes: [] }, | ||
], | ||
@@ -67,0 +67,0 @@ }, |
{ | ||
"name": "performant-array-to-tree", | ||
"version": "1.4.0", | ||
"version": "1.5.0", | ||
"description": "Converts an array of items with ids and parent ids to a nested tree in a performant `O(n)` way. Runs in browsers and node.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -64,2 +64,3 @@ # Performant array to tree | ||
- `parentId`: key of the parent's id field of the item. Default: `"parentId"` | ||
- `childrenField`: key which will contain all child nodes of the parent node. Default: `"children"` | ||
- `dataField`: key which will contain all properties/data of the original items. Set to null if you don't want a container. Default: `"data"` | ||
@@ -76,3 +77,3 @@ | ||
{ num: '418', ref: null, custom: 'ü'}, | ||
], { id: 'num', parentId: 'ref' }) | ||
], { id: 'num', parentId: 'ref', childrenField: 'nodes' }) | ||
``` | ||
@@ -84,8 +85,8 @@ | ||
[ | ||
{ data: { num: '4', ref: null, custom: 'abc' }, children: [ | ||
{ data: { num: '31', ref: '4', custom: '12' }, children: [] }, | ||
{ data: { num: '4', ref: null, custom: 'abc' }, nodes: [ | ||
{ data: { num: '31', ref: '4', custom: '12' }, nodes: [] }, | ||
] }, | ||
{ data: { num: '418', ref: null, custom: 'ü'}, children: [ | ||
{ data: { num: '1941', ref: '418', custom: 'de' }, children: [] }, | ||
{ data: { num: '1', ref: '418', custom: 'ZZZz' }, children: [] }, | ||
{ data: { num: '418', ref: null, custom: 'ü'}, nodes: [ | ||
{ data: { num: '1941', ref: '418', custom: 'de' }, nodes: [] }, | ||
{ data: { num: '1', ref: '418', custom: 'ZZZz' }, nodes: [] }, | ||
] }, | ||
@@ -92,0 +93,0 @@ ] |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
37238
285
128