{"id":19777431,"url":"https://github.com/ilib-js/ilib-tree-node","last_synced_at":"2025-04-30T19:32:18.004Z","repository":{"id":48706465,"uuid":"165341807","full_name":"iLib-js/ilib-tree-node","owner":"iLib-js","description":"Utility class that knows how to flatten and reconstruct ","archived":true,"fork":false,"pushed_at":"2024-11-12T12:59:27.000Z","size":50,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-04-05T02:24:38.928Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/iLib-js.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-01-12T03:29:32.000Z","updated_at":"2024-11-21T22:04:29.000Z","dependencies_parsed_at":"2024-06-21T03:06:53.725Z","dependency_job_id":null,"html_url":"https://github.com/iLib-js/ilib-tree-node","commit_stats":{"total_commits":24,"total_committers":3,"mean_commits":8.0,"dds":"0.41666666666666663","last_synced_commit":"c23ac60a9cfe7d8620bc4b5ceae7d291367d2116"},"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iLib-js%2Filib-tree-node","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iLib-js%2Filib-tree-node/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iLib-js%2Filib-tree-node/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iLib-js%2Filib-tree-node/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/iLib-js","download_url":"https://codeload.github.com/iLib-js/ilib-tree-node/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251769464,"owners_count":21640907,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-12T05:24:53.222Z","updated_at":"2025-04-30T19:32:17.996Z","avatar_url":"https://github.com/iLib-js.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n\u003e :warning: **Deprecation Notice** :warning:\n\u003e This repository has been deprecated. Please use the corresponding package from the [iLib-js monorepo](https://github.com/iLib-js/ilib-mono) instead.\n\n# iLib Tree Node\n\nSimple class to build, construct, and deconstruct an arbitrary tree\nstructure.\n\nA node in the tree may have an arbitrary number of children. Each node\nmust have a type property and can optionally have any number of other\nproperties. There will also be a \"children\" property which will contain\nany possible children, as well as a \"use\" and a \"parent\" property, so\nthe properties \"children\" and \"use\" and \"parent\" are reserved.\n\nThere is no tree structure per-se, only nodes. Any node can be considered\nto be the root of a tree. Even a single node is a tree of size 1.\n\n### Building a Tree from Scratch\n\nTo build a tree from scratch, you can use the following methods:\n\n```\nnode.add() - add a child node to the current node\n```\n\nTo create a tree, create a new Node instance and then add it as\na child to another Node instance.\n\nExample of building a tree from scratch:\n\n```\nimport Node from 'ilib-tree-node';\n\nlet parent = new Node({\n    type: 'x'\n});\n\nlet child = new Node({\n    type: 'text',\n    value: \"this is a string\"\n});\n\nparent.add(child);\n\n// add an anonymous node\nparent.add(new Node({\n    type: 'text',\n    value: \"another string\"\n}));\n```\n\n### Deconstructing the Tree to an Array of Nodes\n\nTo deconstruct (flatten) the tree into an array of nodes, simply use:\n\n```\nnode.toArray() - deconstruct the tree into an array of nodes\n```\n\nIf a node has children, the toArray method will add a start node with\nthe \"use\" property set to \"start\", followed\nby the children within that node, followed finally by an end node with\nthe \"use\" property set to \"end\". This happens recursively for all the\nchildren such that the entire tree underneath the node is flattened as\nwell.\n\nThe array can be reconstructed back\ninto a tree using the information in the \"use\" property. Any node that\ndoes not have children will get added to the array as-is.\n\n### Reconstructing the Tree from an Array of Nodes\n\nTo reconstruct a tree from an array of nodes, use the following static method:\n\n```\nNode.fromArray(array) - reconstruct the tree from an array of nodes\n```\n\nThis method will use the \"use\" property to rebuild a tree of varying depths.\n\nYou may add a \"use\" property to any\nnodes in your array, as long as the start and end nodes for any\nnode type are balanced and well-formed. (ie. they are properly nested\nlike XML open and closing tags.). If they are not well-formed, this\nmethod will create a tree with an unexpected structure. Any array\nthat is a result of a toArray call is guaranteed to be well-formed.\n\n### Unist Trees\n\nNote that this type of tree follows the [unist](https://github.com/syntax-tree/unist)\ninterface for a tree, and therefore any of the unist utilities may be\nused to manipulate the tree.\n\nTo convert a regular unist tree into a ilib-tree-node tree, simply\ndo the following:\n\n```\nimport Node from 'ilib-tree-node';\nimport map from 'unist-utils-map';\n\nlet ast = X; // some unist tree, probably from a parser\n\nlet treeNode = map(ast, node =\u003e new Node(node));\n```\n\n## License\n\nCopyright © 2018-2024, JEDLSoft\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n\n    http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n\nSee the License for the specific language governing permissions and\nlimitations under the License.\n\n# Release Notes\n\n## v2.0.0\n\n- convert all unit tests from nodeunit to jest\n- export the es6 code as real es6 modules and old transpiled javascript\n  for older packages to use (breaking change)\n\n## v1.3.0\n\n- implement the ability to add an array of nodes to the children at the\nsame time with the new `addChildren` method\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Filib-js%2Filib-tree-node","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Filib-js%2Filib-tree-node","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Filib-js%2Filib-tree-node/lists"}