{"_id":"r-array-to-tree","_rev":"1-b7f587c7b1256b3cc658cd27d1aadcd3","name":"r-array-to-tree","dist-tags":{"latest":"1.0.0"},"versions":{"1.0.0":{"name":"r-array-to-tree","version":"1.0.0","description":"array to tree","module":"dist/esm/index.js","main":"dist/cjs/index.js","types":"types/index.d.ts","scripts":{"build":"npm run build:esm && npm run build:cjs","build:esm":"tsc -d --declarationDir types --module es2015 --target es5 --outDir dist/esm","build:cjs":"tsc --module commonjs --target es5 --outDir dist/cjs","test:unit":"jest","prepubish":"npm run test:unit && npm run build"},"keywords":["tree"],"author":{"name":"dawenci","email":"hi@dawenci.me"},"license":"MIT","repository":{"type":"git","url":"git+https://github.com/dawenci/r-array-to-tree.git"},"devDependencies":{"@types/jest":"^25.1.4","jest":"^25.1.0","ts-jest":"^25.2.1","typescript":"^3.8.3"},"gitHead":"bf25859a5c1c38049d95ac5e26f4037c88e1d16f","bugs":{"url":"https://github.com/dawenci/r-array-to-tree/issues"},"homepage":"https://github.com/dawenci/r-array-to-tree#readme","_id":"r-array-to-tree@1.0.0","_nodeVersion":"12.13.0","_npmVersion":"6.12.0","dist":{"integrity":"sha512-2FB08nhLkYiX0OHjR7iz5qqmx6lX4OxO8cev5bIUwiXEFbd8GXwQeEsTWs5RKAnMSicMpv1zyCTXSQR5o0jWSw==","shasum":"90c2f73578b777b3c9e5fccb754e8c053e561ef0","tarball":"https://registry.npmjs.org/r-array-to-tree/-/r-array-to-tree-1.0.0.tgz","fileCount":6,"unpackedSize":21977,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJebD0RCRA9TVsSAnZWagAA9MgP+QAnVlMIMPuxRavo2aTm\n8QtyYuefF+VswibnTUiSdvxNkQaFoHysqE7AphPoquXPURBtG5ZNyaS5CR0V\nnURXUYUUPFQH0gzds+p9kcfHR6iLxjNspZFPtoeWmZa8x7n9AbmIOv9tScKj\nkXxs/np3ZN3yq2sgTtr+BS77k4WqRkeV2Ah3LVxnucQTs/oEYn41EWN8Ill5\nnJqOMxfU1CJdu7p6s/dIqC6UMleL5V1n59Eia/t7Nt+bdFxHZNcO9JUexqhO\nj0EWPX3DJLjTiFxOz2GEsIiJAacgTd/+VEvNfzdfwO+6LY2jU0AIoREoxUd4\nl2mN2k/MPaWONIoTnnETU9NSjz9yZjjI7P3iHlWC1iFjtJ1XqHPqMksGz4FR\nGjLElxwacbH05ysdN5xbc58u+fKFn7Ca+P0pQY43lA5HoAEA1+5+EGR0dllx\n6NSNBe4lAsvTP6D+/HNCHc1lC2mTMiyTWMyM34+aNNg/AF8El3S/r07SG91H\n46JxKuCsNMNK4pa3j07MNNFwgikAn0R7UsItLsDOruEGJ1GR1oRJE9AO231v\nMt/bC2KvTm5PJsamJEPUf1dRzeUh+jW3r5YERMylduxQzY5l3xA2V2OH2+Ng\n1WS3sR/KO9AUuseGgjXYCepKDRCWFFXCtGaFhahB/zIdyjlyjF6g7GFgjc/Z\nBVpd\r\n=hr23\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIFYKeGjYVLWWiGFQiCiaepHWzLjYCPRMSMhxu3iAdQhuAiEAnFPzflxVhYCQ/jBDAA9M2ujDMQoxiDNp2NIyaHIwFtE="}]},"maintainers":[{"name":"dawenci","email":"dawenci@gmail.com"}],"_npmUser":{"name":"dawenci","email":"dawenci@gmail.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/r-array-to-tree_1.0.0_1584151824850_0.23113507461531113"},"_hasShrinkwrap":false}},"time":{"created":"2020-03-14T02:10:24.850Z","1.0.0":"2020-03-14T02:10:25.001Z","modified":"2022-05-14T00:04:36.472Z"},"maintainers":[{"name":"dawenci","email":"dawenci@gmail.com"}],"description":"array to tree","homepage":"https://github.com/dawenci/r-array-to-tree#readme","keywords":["tree"],"repository":{"type":"git","url":"git+https://github.com/dawenci/r-array-to-tree.git"},"author":{"name":"dawenci","email":"hi@dawenci.me"},"bugs":{"url":"https://github.com/dawenci/r-array-to-tree/issues"},"license":"MIT","readme":"# r-array-to-tree\n\n## Installation\n\n```\nnpm install r-array-to-tree --save\n```\n\n## Usage\n\n```typescript\nimport { arrayToTree } from 'r-array-to-tree'\n\nconst array = [\n  { id: 1, parentId: null },\n  { id: 2, parentId: 1 },\n]\n\nconst tree = arrayToTree(array)\n// tree:\n// [\n//   {\n//     data: {\n//       id: 1,\n//       parentId: null\n//     },\n//     children: [\n//       {\n//         data: { id: 2, parentId: 1 }\n//       }\n//     ]\n//   }\n// ]\n```\n\n## options:\n\n```typescript\n{\n  idField?: string\n  parentIdField?: string\n  childrenField?: string\n  parentField?: string | boolean\n  dataField?: string | boolean\n  orphansAsRoot?: boolean\n  isRoot?: (item: any) => boolean\n  hasChild?: (item: any) => boolean\n}\n```\n\n### options.idField\n\nThe default value is `'id'`\n\n```typescript\nconst array = [\n  { itemId: 1, parentId: null },\n  { itemId: 2, parentId: 1 },\n]\nconst options = { idField: 'itemId' }\nconst tree = arrayToTree(array, options)\n\n// tree:\n// [\n//   {\n//     data: { itemId: 1, parentId: null },\n//     children: [\n//       {\n//         data: { itemId: 2, parentId: 1 }\n//       },\n//     ]\n//   }\n// ]\n```\n\n### options.parentIdField\n\nThe default value is `'parentId'`.\n\n```typescript\nconst array = [\n  { id: 1, pid: null },\n  { id: 2, pid: 1 },\n]\nconst options = { parentIdField: 'pid' }\nconst tree = arrayToTree(array, options)\n\n// tree:\n// [\n//   {\n//     data: { id: 1, pid: null },\n//     children: [\n//       {\n//         data: { id: 2, pid: 1 }\n//       },\n//     ]\n//   }\n// ]\n```\n\n### options.childrenField\n\nThe default value is `'children'`.\n\n```typescript\nconst array = [\n  { id: 1, parentId: null },\n  { id: 2, parentId: 1 },\n]\nconst options = { childrenField: 'nodes' }\nconst tree = arrayToTree(array, options)\n\n// tree:\n// [\n//   {\n//     data: { id: 1, parentId: null },\n//     nodes: [\n//       {\n//         data: { id: 2, parentId: 1 }\n//       },\n//     ]\n//   }\n// ]\n```\n\n### options.parentField\n\nThe default value is `undefined`\n\n```typescript\nconst array = [\n  { id: 1, parentId: null },\n  { id: 2, parentId: 1 },\n]\n\nconst options = { parentField: true }\n// OR { parentField: 'parent' }\nconst tree = arrayToTree(array, options)\n\n// tree:\n// [\n//   {\n//     data: { id: 1, parentId: null },\n//     children: [\n//       {\n//         data: { id: 2, parentId: 1 },\n//         parent: <Point_to_parent>\n//       },\n//     ]\n//   }\n// ]\n```\n\n### options.dataField\n\nThe default value is `'data'`\n\n```typescript\nconst array = [\n  { id: 1, parentId: null },\n  { id: 2, parentId: 1 },\n]\nconst options = { dataField: 'value' }\nconst tree = arrayToTree(array, options)\n\n// tree:\n// [\n//   {\n//     value: { id: 1, parentId: null },\n//     children: [\n//       {\n//         value: { id: 2, parentId: 1 },\n//       },\n//     ]\n//   }\n// ]\n\nconst array2 = [\n  { id: 1, parentId: null },\n  { id: 2, parentId: 1 },\n]\nconst options2 = { dataField: false }\nconst tree2 = arrayToTree(array, options)\n\n// tree2:\n// [\n//   {\n//     id: 1,\n//     parentId: null,\n//     children: [\n//       {\n//         id: 2,\n//         parentId: 1\n//       }\n//     ]\n//   }\n// ]\n\n```\n> Warn:\n>\n> If `dataField` is `false` or `''` (empty string), the `arrayToTree` function mutates array.\n\n\n### options.orphansAsRoot\n\nThe default value is `false`\n\n```typescript\n\nconst array = [\n  { id: 1, parentId: null },\n  { id: 2, parentId: 3 },\n]\nconst tree1 = arrayToTree(array)\nconst tree2 = arrayToTree(array, { orphansAsRoot: true })\n\n// tree1:\n// [\n//   {\n//     data: { id: 1, parentId: null }\n//   }\n// ]\n\n// tree2:\n// [\n//   {\n//     data: { id: 1, parentId: null }\n//   },\n//   {\n//     data: { id: 2, parentId: 3 }\n//   },\n// ]\n```\n\n### options.isRoot\n\nThe default value is `(item) => item[options.parentIdField] == null`\n\n```typescript\nconst array = [\n  { id: 1, parentId: 0 },\n  { id: 2, parentId: 1 },\n]\nconst options = {\n  isRoot: item => item.parentId === 0\n}\nconst tree = arrayToTree(array, options)\n\n// tree:\n// [\n//   {\n//     data: { id: 1, parentId: 0 },\n//     children: [\n//       {\n//         data: { id: 2, parentId: 1 },\n//       },\n//     ]\n//   }\n// ]\n```\n\n### options.hasChild\n\nThe default value is `undefined`\n\n```typescript\nconst array = [\n  { id: 1, parentId: null, childCount: 1 },\n  { id: 2, parentId: 1, childCount: 0 },\n]\nconst options = {\n  hasChild: (item) => item.childCount > 0\n}\nconst tree = arrayToTree(array, options)\n\n// tree:\n// [\n//   {\n//     data: { id: 1, parentId: null, childCount: 1 },\n//     children: [\n//       {\n//         data: { id: 2, parentId: 1, childCount: 0 },\n//       },\n//     ]\n//   }\n// ]\n```\n","readmeFilename":"README.md"}