performant-array-to-tree
Advanced tools
Comparing version 1.5.0 to 1.6.0
@@ -26,3 +26,3 @@ "use strict"; | ||
if (config === void 0) { config = {}; } | ||
var conf = __assign({}, defaultConfig, config); | ||
var conf = __assign(__assign({}, defaultConfig), config); | ||
// the resulting unflattened tree | ||
@@ -50,6 +50,6 @@ var rootItems = []; | ||
else { | ||
lookup[itemId] = __assign({}, item, (_b = {}, _b[conf.childrenField] = lookup[itemId][conf.childrenField], _b)); | ||
lookup[itemId] = __assign(__assign({}, item), (_b = {}, _b[conf.childrenField] = lookup[itemId][conf.childrenField], _b)); | ||
} | ||
var TreeItem = lookup[itemId]; | ||
if (parentId === null || parentId === undefined) { | ||
if (parentId === null || parentId === undefined || parentId === '') { | ||
// is a root item | ||
@@ -56,0 +56,0 @@ rootItems.push(TreeItem); |
@@ -1,1 +0,1 @@ | ||
"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; | ||
"use strict";var __assign=this&&this.__assign||function(){return(__assign=Object.assign||function(e){for(var r,a=1,i=arguments.length;a<i;a++)for(var t in r=arguments[a])Object.prototype.hasOwnProperty.call(r,t)&&(e[t]=r[t]);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,i,t;void 0===r&&(r={});for(var n=__assign(__assign({},defaultConfig),r),d=[],l={},s=0,o=e;s<o.length;s++){var c=o[s],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(__assign({},c),((i={})[n.childrenField]=l[p][n.childrenField],i));var _=l[p];null==h||""===h?d.push(_):(Object.prototype.hasOwnProperty.call(l,h)||(l[h]=((t={})[n.childrenField]=[],t)),l[h][n.childrenField].push(_))}return d}exports.arrayToTree=arrayToTree; |
@@ -177,2 +177,24 @@ "use strict"; | ||
}); | ||
it('should treat objects with empty string as parentId as root objects', function () { | ||
chai_1.expect(arrayToTree_1.arrayToTree([ | ||
{ id: '4', parentId: '', custom: 'abc' }, | ||
{ id: '31', parentId: '4', custom: '12' }, | ||
{ id: '1941', parentId: '418', custom: 'de' }, | ||
{ id: '1', parentId: '418', custom: 'ZZZz' }, | ||
{ id: '418', parentId: '', custom: 'ü' }, | ||
{ id: '1313', parentId: '13', custom: 'Not existing' }, | ||
])).to.deep.equal([ | ||
{ | ||
data: { id: '4', parentId: '', custom: 'abc' }, children: [ | ||
{ data: { id: '31', parentId: '4', custom: '12' }, children: [] }, | ||
], | ||
}, | ||
{ | ||
data: { id: '418', parentId: '', custom: 'ü' }, children: [ | ||
{ data: { id: '1941', parentId: '418', custom: 'de' }, children: [] }, | ||
{ data: { id: '1', parentId: '418', custom: 'ZZZz' }, children: [] }, | ||
], | ||
}, | ||
]); | ||
}); | ||
it('should work with empty inputs', function () { | ||
@@ -179,0 +201,0 @@ chai_1.expect(arrayToTree_1.arrayToTree([])).to.deep.equal([]); |
{ | ||
"name": "performant-array-to-tree", | ||
"version": "1.5.0", | ||
"version": "1.6.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.", | ||
@@ -37,14 +37,14 @@ "keywords": [ | ||
"devDependencies": { | ||
"@types/chai": "^4.0.4", | ||
"@types/chai": "^4.2.7", | ||
"@types/mocha": "^5.2.7", | ||
"chai": "^4.1.2", | ||
"coveralls": "^3.0.0", | ||
"coveralls": "^3.0.9", | ||
"istanbul": "^0.4.5", | ||
"mocha": "^6.1.4", | ||
"mocha": "^7.0.0", | ||
"mocha-lcov-reporter": "^1.3.0", | ||
"remap-istanbul": "^0.13.0", | ||
"tslint": "^5.2.0", | ||
"tslint-config-standard": "^8.0.1", | ||
"typescript": "^3.5.2", | ||
"uglify-js": "^3.1.1" | ||
"tslint": "^5.20.1", | ||
"tslint-config-standard": "^9.0.0", | ||
"typescript": "^3.7.5", | ||
"uglify-js": "^3.7.5" | ||
}, | ||
@@ -56,3 +56,3 @@ "scripts": { | ||
"build": "rm -rf build && tsc && npm run uglify", | ||
"lint": "rm -rf build && tslint --type-check --project tsconfig.json './src/**/*.ts'", | ||
"lint": "rm -rf build && tslint --project tsconfig.json './src/**/*.ts'", | ||
"test-spec": "mocha build/**/*.spec.js -R spec --bail", | ||
@@ -59,0 +59,0 @@ "test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- build/**/*.spec.js -R spec --bail", |
@@ -10,3 +10,4 @@ { | ||
"declaration": true, | ||
"strictNullChecks": true | ||
"strictNullChecks": true, | ||
"lib": ["ES6"] | ||
}, | ||
@@ -13,0 +14,0 @@ "exclude": [ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
39518
308