{"id":15679593,"url":"https://github.com/nylen/easy-tree","last_synced_at":"2025-05-07T10:04:41.090Z","repository":{"id":24533451,"uuid":"27939816","full_name":"nylen/easy-tree","owner":"nylen","description":"Simple Node.js library to manipulate tree data structures.","archived":false,"fork":false,"pushed_at":"2014-12-16T19:35:56.000Z","size":152,"stargazers_count":12,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-25T23:51:13.410Z","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":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nylen.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-12-12T21:59:26.000Z","updated_at":"2021-11-04T10:27:28.000Z","dependencies_parsed_at":"2022-08-31T14:10:45.302Z","dependency_job_id":null,"html_url":"https://github.com/nylen/easy-tree","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nylen%2Feasy-tree","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nylen%2Feasy-tree/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nylen%2Feasy-tree/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nylen%2Feasy-tree/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nylen","download_url":"https://codeload.github.com/nylen/easy-tree/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242945524,"owners_count":20210762,"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-10-03T16:33:25.758Z","updated_at":"2025-03-10T23:33:37.296Z","avatar_url":"https://github.com/nylen.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# easy-tree\n\n[![Build status](https://img.shields.io/travis/nylen/easy-tree.svg?style=flat)](https://travis-ci.org/nylen/easy-tree)\n[![npm package](http://img.shields.io/npm/v/easy-tree.svg?style=flat)](https://www.npmjs.org/package/easy-tree)\n\nThis simple Node.js module provides an easy way to manipulate tree data\nstructures.\n\n## Usage\n\nRequire the module and create a `Tree` object:\n\n```js\nvar Tree = require('easy-tree');\n\n// Create an empty tree\nvar tree = new Tree();\n\n// Create a tree from an arbitrary data object\nvar tree = new Tree({\n    a : 1,\n    b : 2\n});\n\n// Create a tree with several children from an array\nvar tree = new Tree([\n    { a : 1 },\n    { b : 2 }\n]);\n\n// Create a multi-level tree using the `children` property\nvar tree = new Tree({\n    a : 1,\n    b : 2,\n    children : [\n        { c : 3 },\n        { d : 4 }\n    ]\n});\n\n// If you're not a fan of `new`\nvar tree = Tree();\n```\n\nA `Tree` object represents a tree node.  Tree nodes must be JavaScript objects.\nThey can have any data attributes that do not conflict with `Tree` object\nmethod names, and one or more children, which are stored in the `children`\narray.\n\nYou can use `tree.children[i]` and `tree.children.length` to retrieve and count\nchildren, but **do not manipulate the `children` array directly!** Instead, use\nthe following methods to safely and correctly perform operations on the tree:\n\n## Methods\n\nMost methods take a `path` argument which is an array of 0-based indices that\npoint to a tree node.  Omit the `path` argument or use `[]` to perform the\noperation on the current node, but this is not valid for all operations:  a\ntree node cannot remove itself, or insert a node before or after itself,\nbecause it doesn't know its own parent.\n\nMany methods take a `child` argument, which can be either a `Tree` instance or\na plain object which will be converted to a `Tree` instance.\n\n### tree.get([path])\n\nReturns the subtree at `path` (or the tree itself, if `path` is `[]` or\nomitted).\n\n### tree.prepend([path], child)\n\nInsert `child` as the first child of the node given by `path`.\n\nReturns the new number of children of the modified node.\n\n### tree.insertBefore(path, child)\n\nInsert `child` before the node given by `path`.\n\n`path` cannot be omitted and must contain at least 1 element.\n\nReturns the new number of children of the modified node.\n\n### tree.append([path], child)\n\nInsert `child` as the last child of the node given by `path`.\n\nReturns the new number of children of the modified node.\n\n### tree.insertAfter(path, child)\n\nInsert `child` after the node given by `path`.\n\n`path` cannot be omitted and must contain at least 1 element.\n\nReturns the new number of children of the modified node.\n\n### tree.remove(path)\n\nRemoves the node specified by `path`, and **inserts each of its children where\nthe removed node used to be**.\n\n`path` cannot be omitted and must contain at least 1 element.\n\nReturns the removed node (without any children).\n\n### tree.prune(path)\n\nRemoves the entire subtree beginning with the node specified by `path`.\n\n`path` cannot be omitted and must contain at least 1 element.\n\nReturns the removed node and all its children.\n\n### tree.keys([path])\n\nReturns an array of all data attributes in the node specified by `path` (same\nas `Object.keys()` but excludes the `children` property).\n\n### tree.walk([path], [cb])\n\nFor the node specified by `path` and any child and descendant nodes, calls `cb`\n**synchronously** with parameters `path, node`.\n\nReturns the total number of nodes visited.\n\nYou can omit the `cb` parameter to just count the number of nodes in a tree.\n\nIf `cb` returns `false` for a given node, the `walk` function will not descend\nto that node's children and they will not be included in the count of nodes\nvisited.\n\n## Other Notes\n\n`Tree` objects also contain the following private methods, so you cannot use\nany of these names as data attributes:\n\n- `_doAtPath`\n- `_makeTree`\n- `_throwPathError`\n- `_walk`\n\nSaving tree data to JSON and restoring it later works just fine:\n\n```js\nvar myTree = new Tree({ ... });\n\nvar savedData = JSON.stringify(myTree);\n\nmyTree = new Tree(JSON.parse(savedData));\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnylen%2Feasy-tree","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnylen%2Feasy-tree","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnylen%2Feasy-tree/lists"}