{"id":18486863,"url":"https://github.com/joaonuno/tree-model-js","last_synced_at":"2025-10-22T15:42:17.025Z","repository":{"id":8925314,"uuid":"10654522","full_name":"joaonuno/tree-model-js","owner":"joaonuno","description":"Manipulate and traverse tree-like structures in javascript.","archived":false,"fork":false,"pushed_at":"2023-09-25T21:24:55.000Z","size":589,"stargazers_count":963,"open_issues_count":26,"forks_count":118,"subscribers_count":23,"default_branch":"master","last_synced_at":"2024-10-14T07:04:17.211Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://jnuno.com/tree-model-js","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/joaonuno.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}},"created_at":"2013-06-12T23:16:09.000Z","updated_at":"2024-09-19T03:47:07.000Z","dependencies_parsed_at":"2024-01-14T04:07:55.179Z","dependency_job_id":null,"html_url":"https://github.com/joaonuno/tree-model-js","commit_stats":{"total_commits":86,"total_committers":11,"mean_commits":7.818181818181818,"dds":0.2906976744186046,"last_synced_commit":"1ac126a99528b13d9a0aa4e519cddab41ac8838b"},"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joaonuno%2Ftree-model-js","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joaonuno%2Ftree-model-js/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joaonuno%2Ftree-model-js/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joaonuno%2Ftree-model-js/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/joaonuno","download_url":"https://codeload.github.com/joaonuno/tree-model-js/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247922537,"owners_count":21018815,"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-06T12:49:54.174Z","updated_at":"2025-10-22T15:42:11.980Z","avatar_url":"https://github.com/joaonuno.png","language":"JavaScript","readme":"# TreeModel\n\nManipulate and traverse tree-like structures in javascript.\n\nFor download and demos, please [visit TreeModel website](http://jnuno.com/tree-model-js).\n\n[![Build Status](https://travis-ci.org/joaonuno/tree-model-js.svg)](https://travis-ci.org/joaonuno/tree-model-js)\n[![Coverage Status](https://coveralls.io/repos/github/joaonuno/tree-model-js/badge.svg?branch=master)](https://coveralls.io/github/joaonuno/tree-model-js?branch=master)\n\n## Installation\n\n### Node\n\nTreeModel is available as an npm module so you can install it with `npm install tree-model` and use it in your script:\n\n```js\nvar TreeModel = require('tree-model'),\n    tree = new TreeModel(),\n    root = tree.parse({name: 'a', children: [{name: 'b'}]});\n```\n\n#### TypeScript\nType definitions are already bundled with the package, which should just work with npm install.\n\nYou can maually find the definition files in the `types` folder.\n\n### Browser\n\n[Visit TreeModel website](http://jnuno.com/tree-model-js) to download browser-ready bundles.\n\n## Questions?\n\nIf you have any doubt using this library please post a question on [stackoverflow](http://stackoverflow.com/questions/ask?tags=treemodel) tagged with `treemodel`.\n\n## API Reference\n\n### Create a new TreeModel\n\nCreate a new TreeModel with the given options.\n\n```js\nvar tree = new TreeModel(options)\n```\n\nValid properties for the options object are:\n\n* `childrenPropertyName` - The name for the children array property. Default is `children`;\n* `modelComparatorFn` - A comparator function to sort the children when parsing the model and adding children. The default order policy is to keep the parsed order and append new children. The comparator function receives the model for two nodes just like the [Array.sort](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort) function. The provided sort algorithm is **stable**.\n\n### Parse the hierarchy object\n\nParse the given user defined model and return the root Node object.\n\n```js\nNode tree.parse(model)\n```\n\n### Is Root?\n\nReturn `true` if this Node is the root, `false` otherwise.\n\n```js\nBoolean node.isRoot()\n```\n\n### Has Children?\n\nReturn `true` if this Node has one or more children, `false` otherwise.\n\n```js\nBoolean node.hasChildren()\n```\n\n### Add a child\n\nAdd the given node as child of this one. Return the child Node.\n\n```js\nNode parentNode.addChild(childNode)\n```\n\n### Add a child at a given index\n\nAdd the given node as child of this one at the given index. Return the child Node.\n\n```js\nNode parentNode.addChildAtIndex(childNode, index)\n```\n\n### Set the index of a node among its siblings\n\nSets the index of the node among its siblings to the given value. Return the node itself.\n\n```js\nNode node.setIndex(index)\n```\n\n### Get the index of a node among its siblings\n\nGets the index of the node relative to its siblings. Return the index value.\n\n```js\nInt node.getIndex()\n```\n\n### Get the node path\n\nGet the array of Nodes representing the path from the root to this Node (inclusive).\n\n```js\nArray\u003cNode\u003e node.getPath()\n```\n\n### Delete a node from the tree\n\nDrop the subtree starting at this node. Returns the node itself, which is now a root node.\n\n```js\nNode node.drop()\n```\n\n*Warning* - Dropping a node while walking the tree is not supported. You must first collect the nodes to drop using one of the traversal functions and then drop them. Example:\n\n```js\nroot.all( /* predicate */ ).forEach(function (node) {\n  node.drop();\n});\n```\n\n### Find a node\n\nStarting from this node, find the first Node that matches the predicate and return it. The **predicate** is a function wich receives the visited Node and returns `true` if the Node should be picked and `false` otherwise.\n\n```js\nNode node.first(predicate)\n```\n\n### Find all nodes\n\nStarting from this node, find all Nodes that match the predicate and return these.\n\n```js\nArray\u003cNode\u003e node.all(predicate)\n```\n\n### Walk the tree\n\nStarting from this node, traverse the subtree calling the action for each visited node. The action is a function which receives the visited Node as argument. The traversal can be halted by returning `false` from the action.\n\n```js\nnode.walk([options], action, [context])\n```\n\n**Note** - `first`, `all` and `walk` can optionally receive as first argument an object with traversal options. Currently the only supported option is the traversal `strategy` which can be any of the following:\n\n* `{strategy: 'pre'}` - Depth-first pre-order *[default]*;\n* `{strategy: 'post'}` - Depth-first post-order;\n* `{strategy: 'breadth'}` - Breadth-first.\n\nThese functions can also take, as the last parameter, the *context* on which the action will be called.\n\n## Contributing\n\n### Setup\n\nFork this repository and run `npm install` on the project root folder to make sure you have all project dependencies installed.\n\n### Code Linting\n\nRun `npm run lint`\n\nThis will check both source and tests for code correctness and style compliance.\n\n### Running Tests\n\nRun `npm test`\n\n### Type definitions\n\nTo modify the type definitions, look inside the `types` folder. \n`index.d.ts` contains the definition and `tree-model-tests.ts` contains type tests.\n\nTo verify changes:\n\nRun `npm run dtslint`.\n","funding_links":[],"categories":["JavaScript","Data Structure"],"sub_categories":["Torrent"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoaonuno%2Ftree-model-js","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjoaonuno%2Ftree-model-js","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoaonuno%2Ftree-model-js/lists"}