Socket
Socket
Sign inDemoInstall

performant-array-to-tree

Package Overview
Dependencies
0
Maintainers
1
Versions
24
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.9.1 to 1.9.2

10

build/arrayToTree.js

@@ -54,5 +54,5 @@ "use strict";

throw new Error("The item array contains a node whose parentId both exists in another node and is in " +
("`rootParentIds` (`itemId`: \"" + itemId + "\", `rootParentIds`: " + Object.keys(conf.rootParentIds)
.map(function (r) { return "\"" + r + "\""; })
.join(", ") + ")."));
"`rootParentIds` (`itemId`: \"".concat(itemId, "\", `rootParentIds`: ").concat(Object.keys(conf.rootParentIds)
.map(function (r) { return "\"".concat(r, "\""); })
.join(", "), ")."));
}

@@ -99,3 +99,3 @@ // look whether item already exists in the lookup table

throw new Error("The items array contains orphans that point to the following parentIds: " +
("[" + Array.from(orphanIds) + "]. These parentIds do not exist in the items array. Hint: prevent orphans to result ") +
"[".concat(Array.from(orphanIds), "]. These parentIds do not exist in the items array. Hint: prevent orphans to result ") +
"in an error by passing the following option: { throwIfOrphans: false }");

@@ -114,4 +114,4 @@ }

function getNestedProperty(item, nestedProperty) {
return nestedProperty.split(".").reduce(function (o, i) { return o[i]; }, item);
return nestedProperty.split(".").reduce(function (o, i) { return o && o[i]; }, item);
}
//# sourceMappingURL=arrayToTree.js.map

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

@@ -7,3 +7,3 @@ "use strict";

it("should work with nested objects", function () {
chai_1.expect(arrayToTree_1.arrayToTree([
(0, chai_1.expect)((0, arrayToTree_1.arrayToTree)([
{ id: "4", parentId: null, custom: "abc" },

@@ -31,3 +31,3 @@ { id: "31", parentId: "4", custom: "12" },

it("should work with integer keys", function () {
chai_1.expect(arrayToTree_1.arrayToTree([
(0, chai_1.expect)((0, arrayToTree_1.arrayToTree)([
{ id: 4, parentId: null, custom: "abc" },

@@ -54,4 +54,17 @@ { id: 31, parentId: 4, custom: "12" },

});
it("should work with integer parentId 0", function () {
(0, chai_1.expect)((0, arrayToTree_1.arrayToTree)([
{ id: 0, parentId: null, custom: "abc" },
{ id: 31, parentId: 0, custom: "12" },
])).to.deep.equal([
{
data: { id: 0, parentId: null, custom: "abc" },
children: [
{ data: { id: 31, parentId: 0, custom: "12" }, children: [] },
],
},
]);
});
it("should work with nested objects and custom keys", function () {
chai_1.expect(arrayToTree_1.arrayToTree([
(0, chai_1.expect)((0, arrayToTree_1.arrayToTree)([
{ num: "4", ref: null, custom: "abc" },

@@ -77,3 +90,3 @@ { num: "31", ref: "4", custom: "12" },

it("should work with nested objects and a custom key with dots if nested properties are disabled", function () {
chai_1.expect(arrayToTree_1.arrayToTree([
(0, chai_1.expect)((0, arrayToTree_1.arrayToTree)([
{ ".key": "4", "my.parent": null, custom: "abc" },

@@ -112,3 +125,3 @@ { ".key": "31", "my.parent": "4", custom: "12" },

it("should ignore objects if parentId does not exist", function () {
chai_1.expect(arrayToTree_1.arrayToTree([
(0, chai_1.expect)((0, arrayToTree_1.arrayToTree)([
{ id: "4", parentId: null, custom: "abc" },

@@ -137,3 +150,3 @@ { id: "31", parentId: "4", custom: "12" },

it("should work with nested objects with dataField set to null", function () {
chai_1.expect(arrayToTree_1.arrayToTree([
(0, chai_1.expect)((0, arrayToTree_1.arrayToTree)([
{ id: "4", parentId: null, custom: "abc" },

@@ -163,3 +176,3 @@ { id: "31", parentId: "4", custom: "12" },

it("should work with nested objects and custom keys with dataField set to null", function () {
chai_1.expect(arrayToTree_1.arrayToTree([
(0, chai_1.expect)((0, arrayToTree_1.arrayToTree)([
{ num: "4", ref: null, custom: "abc" },

@@ -189,3 +202,3 @@ { num: "31", ref: "4", custom: "12" },

it("should ignore objects if parentId does not exist with dataField set to null", function () {
chai_1.expect(arrayToTree_1.arrayToTree([
(0, chai_1.expect)((0, arrayToTree_1.arrayToTree)([
{ id: "4", parentId: null, custom: "abc" },

@@ -216,3 +229,3 @@ { id: "31", parentId: "4", custom: "12" },

it("should treat objects with missing parentId as root objects", function () {
chai_1.expect(arrayToTree_1.arrayToTree([
(0, chai_1.expect)((0, arrayToTree_1.arrayToTree)([
{ id: "4", custom: "abc" },

@@ -241,3 +254,3 @@ { id: "31", parentId: "4", custom: "12" },

it("should treat objects with empty string as parentId as root objects", function () {
chai_1.expect(arrayToTree_1.arrayToTree([
(0, chai_1.expect)((0, arrayToTree_1.arrayToTree)([
{ id: "4", parentId: "", custom: "abc" },

@@ -266,3 +279,3 @@ { id: "31", parentId: "4", custom: "12" },

it("should treat objects with non-zero length string as parentId as root objects if these parent ids are in rootParentIds", function () {
chai_1.expect(arrayToTree_1.arrayToTree([
(0, chai_1.expect)((0, arrayToTree_1.arrayToTree)([
{ id: "4", parentId: "orphan1", custom: "abc" },

@@ -293,3 +306,3 @@ { id: "31", parentId: "4", custom: "12" },

it("should not throw if orphans exist but throwIfOrphans is false", function () {
chai_1.expect(arrayToTree_1.arrayToTree([
(0, chai_1.expect)((0, arrayToTree_1.arrayToTree)([
{ id: "4", parentId: null, custom: "abc" },

@@ -308,4 +321,4 @@ { id: "31", parentId: "4", custom: "12" },

it("should throw if orphans exist and throwIfOrphans is true", function () {
chai_1.expect(function () {
return arrayToTree_1.arrayToTree([
(0, chai_1.expect)(function () {
return (0, arrayToTree_1.arrayToTree)([
{ id: "4", parentId: null, custom: "abc" },

@@ -322,3 +335,3 @@ { id: "31", parentId: "4", custom: "12" },

it("should not throw if no orphans exist and throwIfOrphans is true, but the order is different (see #18)", function () {
chai_1.expect(arrayToTree_1.arrayToTree([
(0, chai_1.expect)((0, arrayToTree_1.arrayToTree)([
{ id: "2", parentId: "root", foo: "bar" },

@@ -346,4 +359,4 @@ { id: "1-1", parentId: "1", foo: "bar" },

it("should throw if orphans exist and throwIfOrphans is true and rootParentIds don't contain orphan parentId", function () {
chai_1.expect(function () {
return arrayToTree_1.arrayToTree([
(0, chai_1.expect)(function () {
return (0, arrayToTree_1.arrayToTree)([
{ id: "4", parentId: null, custom: "abc" },

@@ -363,4 +376,4 @@ { id: "31", parentId: "4", custom: "12" },

it("should throw if a node has parentId that both exists in another node and is in rootParentIds", function () {
chai_1.expect(function () {
return arrayToTree_1.arrayToTree([
(0, chai_1.expect)(function () {
return (0, arrayToTree_1.arrayToTree)([
{ id: "fakeOrphan", parentId: null },

@@ -378,3 +391,3 @@ { id: "aaa", parentId: "fakeOrphan" },

it("should replace default rootParentIds by the provided value", function () {
chai_1.expect(arrayToTree_1.arrayToTree([
(0, chai_1.expect)((0, arrayToTree_1.arrayToTree)([
{ id: "4", parentId: "", custom: "abc" },

@@ -390,6 +403,6 @@ { id: "31", parentId: "4", custom: "12" },

it("should work with empty inputs", function () {
chai_1.expect(arrayToTree_1.arrayToTree([])).to.deep.equal([]);
(0, chai_1.expect)((0, arrayToTree_1.arrayToTree)([])).to.deep.equal([]);
});
it("should work with nested objects and nested id and parentId properties", function () {
chai_1.expect(arrayToTree_1.arrayToTree([
(0, chai_1.expect)((0, arrayToTree_1.arrayToTree)([
{ nested: { id: "1", parentId: null, custom: "1" } },

@@ -428,3 +441,3 @@ { nested: { id: "1.1", parentId: "1", custom: "1.1" } },

it("should work with nested id property", function () {
chai_1.expect(arrayToTree_1.arrayToTree([
(0, chai_1.expect)((0, arrayToTree_1.arrayToTree)([
{ one: { id: "1" }, parentId: null, custom: "1" },

@@ -445,3 +458,3 @@ { one: { id: "1.1" }, parentId: "1", custom: "1.1" },

it("should work with nested parentId property", function () {
chai_1.expect(arrayToTree_1.arrayToTree([
(0, chai_1.expect)((0, arrayToTree_1.arrayToTree)([
{ id: "1", two: { parentId: null }, custom: "1" },

@@ -462,3 +475,3 @@ { id: "1.1", two: { parentId: "1" }, custom: "1.1" },

it("should work with nested id and parentId properties", function () {
chai_1.expect(arrayToTree_1.arrayToTree([
(0, chai_1.expect)((0, arrayToTree_1.arrayToTree)([
{ one: { id: "1" }, two: { parentId: null }, custom: "1" },

@@ -478,3 +491,35 @@ { one: { id: "1.1" }, two: { parentId: "1" }, custom: "1.1" },

});
it("should work with nested id and parentId properties if the parent is null", function () {
(0, chai_1.expect)((0, arrayToTree_1.arrayToTree)([
{ one: { id: "1" }, two: null, custom: "1" },
{ one: { id: "1.1" }, two: { parentId: "1" }, custom: "1.1" },
], { id: "one.id", parentId: "two.parentId" })).to.deep.equal([
{
data: { one: { id: "1" }, two: null, custom: "1" },
children: [
{
data: { one: { id: "1.1" }, two: { parentId: "1" }, custom: "1.1" },
children: [],
},
],
},
]);
});
it("should work with nested id and parentId properties if the parent is undefined", function () {
(0, chai_1.expect)((0, arrayToTree_1.arrayToTree)([
{ one: { id: "1" }, custom: "1" },
{ one: { id: "1.1" }, two: { parentId: "1" }, custom: "1.1" },
], { id: "one.id", parentId: "two.parentId" })).to.deep.equal([
{
data: { one: { id: "1" }, custom: "1" },
children: [
{
data: { one: { id: "1.1" }, two: { parentId: "1" }, custom: "1.1" },
children: [],
},
],
},
]);
});
});
//# sourceMappingURL=arrayToTree.spec.js.map
{
"name": "performant-array-to-tree",
"version": "1.9.1",
"version": "1.9.2",
"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.",

@@ -38,9 +38,9 @@ "keywords": [

"@types/chai": "^4.2.7",
"@types/mocha": "^8.2.0",
"@types/mocha": "^9.1.0",
"chai": "^4.1.2",
"codecov": "^3.8.1",
"mocha": "^8.2.1",
"mocha": "^9.2.0",
"nyc": "^15.1.0",
"prettier": "^2.2.1",
"ts-mocha": "^8.0.0",
"ts-mocha": "^9.0.2",
"tslint": "^6.1.3",

@@ -47,0 +47,0 @@ "tslint-config-prettier": "^1.18.0",

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc