Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

performant-array-to-tree

Package Overview
Dependencies
Maintainers
1
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

performant-array-to-tree - npm Package Compare versions

Comparing version 1.8.2 to 1.9.0

1

build/arrayToTree.d.ts

@@ -20,2 +20,3 @@ export interface Item {

};
nestedIds: boolean;
}

@@ -22,0 +23,0 @@ /**

9

build/arrayToTree.js

@@ -22,2 +22,3 @@ "use strict";

rootParentIds: { "": true },
nestedIds: true,
};

@@ -46,4 +47,8 @@ /**

var item = items_1[_i];
var itemId = getNestedProperty(item, conf.id);
var parentId = getNestedProperty(item, conf.parentId);
var itemId = conf.nestedIds
? getNestedProperty(item, conf.id)
: item[conf.id];
var parentId = conf.nestedIds
? getNestedProperty(item, conf.parentId)
: item[conf.parentId];
if (conf.rootParentIds[itemId]) {

@@ -50,0 +55,0 @@ throw new Error("The item array contains a node whose parentId both exists in another node and is in " +

@@ -1,1 +0,1 @@

"use strict";var __assign=this&&this.__assign||function(){return(__assign=Object.assign||function(e){for(var r,t=1,n=arguments.length;t<n;t++)for(var a in r=arguments[t])Object.prototype.hasOwnProperty.call(r,a)&&(e[a]=r[a]);return e}).apply(this,arguments)};Object.defineProperty(exports,"__esModule",{value:!0});var defaultConfig={id:"id",parentId:"parentId",dataField:"data",childrenField:"children",throwIfOrphans:!1,rootParentIds:{"":!(exports.arrayToTree=void 0)}};function arrayToTree(e,r){var t;void 0===r&&(r={});for(var n=__assign(__assign({},defaultConfig),r),a=[],o={},i=r.throwIfOrphans?new Set:null,s=0,d=e;s<d.length;s++){var l=d[s],h=getNestedProperty(l,n.id),p=getNestedProperty(l,n.parentId);if(n.rootParentIds[h])throw new Error('The item array contains a node whose parentId both exists in another node and is in `rootParentIds` (`itemId`: "'+h+'", `rootParentIds`: '+Object.keys(n.rootParentIds).map(function(e){return'"'+e+'"'}).join(", ")+").");Object.prototype.hasOwnProperty.call(o,h)||(o[h]=((t={})[n.childrenField]=[],t)),i&&i.delete(h),n.dataField?o[h][n.dataField]=l:o[h]=__assign(__assign({},l),((c={})[n.childrenField]=o[h][n.childrenField],c));var c=o[h];null==p||n.rootParentIds[p]?a.push(c):(Object.prototype.hasOwnProperty.call(o,p)||(o[p]=((h={})[n.childrenField]=[],h),i&&i.add(p)),o[p][n.childrenField].push(c))}if(null!=i&&i.size)throw new Error("The items array contains orphans that point to the following parentIds: ["+Array.from(i)+"]. These parentIds do not exist in the items array. Hint: prevent orphans to result in an error by passing the following option: { throwIfOrphans: false }");return a}function getNestedProperty(e,r){return r.split(".").reduce(function(e,r){return e[r]},e)}exports.arrayToTree=arrayToTree;
"use strict";var __assign=this&&this.__assign||function(){return(__assign=Object.assign||function(e){for(var r,t=1,n=arguments.length;t<n;t++)for(var a in r=arguments[t])Object.prototype.hasOwnProperty.call(r,a)&&(e[a]=r[a]);return e}).apply(this,arguments)};Object.defineProperty(exports,"__esModule",{value:!0});var defaultConfig={id:"id",parentId:"parentId",dataField:"data",childrenField:"children",throwIfOrphans:!1,rootParentIds:{"":!(exports.arrayToTree=void 0)},nestedIds:!0};function arrayToTree(e,r){var t;void 0===r&&(r={});for(var n=__assign(__assign({},defaultConfig),r),a=[],o={},s=r.throwIfOrphans?new Set:null,i=0,d=e;i<d.length;i++){var l=d[i],p=n.nestedIds?getNestedProperty(l,n.id):l[n.id],h=n.nestedIds?getNestedProperty(l,n.parentId):l[n.parentId];if(n.rootParentIds[p])throw new Error('The item array contains a node whose parentId both exists in another node and is in `rootParentIds` (`itemId`: "'+p+'", `rootParentIds`: '+Object.keys(n.rootParentIds).map(function(e){return'"'+e+'"'}).join(", ")+").");Object.prototype.hasOwnProperty.call(o,p)||(o[p]=((t={})[n.childrenField]=[],t)),s&&s.delete(p),n.dataField?o[p][n.dataField]=l:o[p]=__assign(__assign({},l),((c={})[n.childrenField]=o[p][n.childrenField],c));var c=o[p];null==h||n.rootParentIds[h]?a.push(c):(Object.prototype.hasOwnProperty.call(o,h)||(o[h]=((p={})[n.childrenField]=[],p),s&&s.add(h)),o[h][n.childrenField].push(c))}if(null!=s&&s.size)throw new Error("The items array contains orphans that point to the following parentIds: ["+Array.from(s)+"]. These parentIds do not exist in the items array. Hint: prevent orphans to result in an error by passing the following option: { throwIfOrphans: false }");return a}function getNestedProperty(e,r){return r.split(".").reduce(function(e,r){return e[r]},e)}exports.arrayToTree=arrayToTree;

@@ -73,2 +73,36 @@ "use strict";

});
it("should work with nested objects and a custom key with dots if nested properties are disabled", function () {
chai_1.expect(arrayToTree_1.arrayToTree([
{ ".key": "4", "my.parent": null, custom: "abc" },
{ ".key": "31", "my.parent": "4", custom: "12" },
{ ".key": "1941", "my.parent": "418", custom: "de" },
{ ".key": "1", "my.parent": "418", custom: "ZZZz" },
{ ".key": "418", "my.parent": null, custom: "ü" },
], {
id: ".key",
parentId: "my.parent",
childrenField: "nodes",
nestedIds: false,
})).to.deep.equal([
{
data: { ".key": "4", "my.parent": null, custom: "abc" },
nodes: [
{ data: { ".key": "31", "my.parent": "4", custom: "12" }, nodes: [] },
],
},
{
data: { ".key": "418", "my.parent": null, custom: "ü" },
nodes: [
{
data: { ".key": "1941", "my.parent": "418", custom: "de" },
nodes: [],
},
{
data: { ".key": "1", "my.parent": "418", custom: "ZZZz" },
nodes: [],
},
],
},
]);
});
it("should ignore objects if parentId does not exist", function () {

@@ -75,0 +109,0 @@ chai_1.expect(arrayToTree_1.arrayToTree([

{
"name": "performant-array-to-tree",
"version": "1.8.2",
"version": "1.9.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": [

@@ -69,6 +69,7 @@ # Performant array to tree

- `parentId`: key of the parent's id field of the item. Also works with nested properties (e. g. `"nested.parentId"`). Default: `"parentId"`.
- `nestedIds`: option to enable/disable nested ids. Default: `true`.
- `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"`
- `throwIfOrphans`: option to throw an error if the array of items contains one or more items that have no parents in the array. This option has a small runtime penalty, so it's disabled by default. When enabled, the function will throw an error containing the parentIds that were not found in the items array. When disabled, the function will just ignore orphans and not add them to the tree. Default: `false`
- `rootParentIds`: Object with parent ids as keys and `true` as values that should be considered the top or root elements of the tree. This is useful when your tree is a subset of full tree, which means there is no item whose parent id is one of `undefined`, `null` or `''`. The array you pass in will be replace the default value. `undefined` and `null` are always considered to be rootParentIds. For more details, see [#23](https://github.com/philipstanislaus/performant-array-to-tree/issues/23). Default: `{'': true}`
- `rootParentIds`: object with parent ids as keys and `true` as values that should be considered the top or root elements of the tree. This is useful when your tree is a subset of full tree, which means there is no item whose parent id is one of `undefined`, `null` or `''`. The array you pass in will be replace the default value. `undefined` and `null` are always considered to be rootParentIds. For more details, see [#23](https://github.com/philipstanislaus/performant-array-to-tree/issues/23). Default: `{'': true}`

@@ -75,0 +76,0 @@ Example:

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc