{"id":17242797,"url":"https://github.com/imcuttle/visit-tree","last_synced_at":"2025-04-14T03:25:43.994Z","repository":{"id":34079926,"uuid":"168941161","full_name":"imcuttle/visit-tree","owner":"imcuttle","description":"Visit tree by pre or post DFS","archived":false,"fork":false,"pushed_at":"2023-02-28T07:52:19.000Z","size":845,"stargazers_count":5,"open_issues_count":20,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-23T02:34:06.538Z","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":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/imcuttle.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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-02-03T12:09:08.000Z","updated_at":"2024-02-27T08:24:26.000Z","dependencies_parsed_at":"2024-06-19T09:58:15.816Z","dependency_job_id":null,"html_url":"https://github.com/imcuttle/visit-tree","commit_stats":{"total_commits":28,"total_committers":4,"mean_commits":7.0,"dds":0.3214285714285714,"last_synced_commit":"ad67363af8e5fdbd505d963a4227c0cfaecde0c2"},"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imcuttle%2Fvisit-tree","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imcuttle%2Fvisit-tree/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imcuttle%2Fvisit-tree/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imcuttle%2Fvisit-tree/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/imcuttle","download_url":"https://codeload.github.com/imcuttle/visit-tree/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248814677,"owners_count":21165801,"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-15T06:14:05.511Z","updated_at":"2025-04-14T03:25:43.975Z","avatar_url":"https://github.com/imcuttle.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# @moyuyc/visit-tree\n\n[![Build status](https://img.shields.io/travis/imcuttle/visit-tree/master.svg?style=flat-square)](https://travis-ci.org/imcuttle/visit-tree)\n[![Test coverage](https://img.shields.io/codecov/c/github/imcuttle/visit-tree.svg?style=flat-square)](https://codecov.io/github/imcuttle/visit-tree?branch=master)\n[![NPM version](https://img.shields.io/npm/v/@moyuyc/visit-tree.svg?style=flat-square)](https://www.npmjs.com/package/@moyuyc/visit-tree)\n[![NPM Downloads](https://img.shields.io/npm/dm/@moyuyc/visit-tree.svg?style=flat-square\u0026maxAge=43200)](https://www.npmjs.com/package/@moyuyc/visit-tree)\n[![Prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://prettier.io/)\n[![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg?style=flat-square)](https://conventionalcommits.org)\n\n\u003e Visit tree by pre or post DFS\n\n## Installation\n\n```bash\nnpm install @moyuyc/visit-tree\n# or use yarn\nyarn add @moyuyc/visit-tree\n```\n\n## Usage\n\n```javascript\nconst visitTree = require('@moyuyc/visit-tree')\n\n// or sync\nconst { sync } = require('@moyuyc/visit-tree')\n\nawait visitTree(\n  {\n    value: 'root',\n    children: [\n      {\n        value: 'a'\n      }\n    ]\n  },\n  async (node, ctx) =\u003e {},\n  {}\n)\n```\n\n## API\n\n### **`visitTree(tree, preWalk: (node, ctx: Context) =\u003e void, options: Options): Promise\u003cvoid\u003e`**\n\n### **`visitTree(tree, preWalk: (node, ctx: Context) =\u003e void, postWalk: (node, ctx: Context) =\u003e void, options: Options): Promise\u003cvoid\u003e`**\n\n### Options\n\n#### `path`\n\nAssign children's path.\n\n- **Type:** `string`\n- **Default:** `children`\n\n#### `state`\n\n- **Type:** any\n- **Default:** `null`\n\n### Context\n\n#### `state`\n\nIt's same reference to `options.state`.\n\n#### `node`\n\nThe current node.\n\n#### `children`\n\nThe current node's children.\n\n#### `index`\n\nGet the index of the current node.\n\n#### `depth`\n\nGet the depth of the current node. The depth is the number of ancestors the current node has.\n\n#### `parent`\n\nGet the parent of the current node.\n\n#### `skip`\n\nSkip current node, children won't be visited.\n\n```javascript\nwalk(rootNode, (node, ctx) =\u003e {\n  if (node.name === 'skip') {\n    return ctx.skip()\n  }\n})\n```\n\n#### `break`\n\nStop traversal now.\n\n```javascript\nwalk(rootNode, (node, ctx) =\u003e {\n  if (node.name === 'stop') {\n    return ctx.break()\n  }\n})\n```\n\n#### `replace`\n\n```javascript\nwalk(rootNode, (node, ctx) =\u003e {\n  if (node.name === 'replace-me') {\n    return ctx.replace({ name: 'new-me' })\n  }\n})\n```\n\n#### `remove`\n\n```javascript\nwalk(rootNode, (node, ctx) =\u003e {\n  if (node.name === 'remove-me') {\n    return ctx.remove()\n  }\n})\n```\n\n#### `insert`\n\n```javascript\nwalk(rootNode, (node, ctx) =\u003e {\n  if (node.name === 'insert-me') {\n    return ctx.insert({ name: 'abc' }, { name: '' })\n  }\n})\n```\n\n#### `paths`\n\nGet the paths(index list) of the current node.\n\n#### `parents`\n\nGet the parents(backward) of the current node.\n\n#### `parentCtx`\n\nGet the context of the current node's parent.\n\n## Contributing\n\n- Fork it!\n- Create your new branch:  \n  `git checkout -b feature-new` or `git checkout -b fix-which-bug`\n- Start your magic work now\n- Make sure npm test passes\n- Commit your changes:  \n  `git commit -am 'feat: some description (close #123)'` or `git commit -am 'fix: some description (fix #123)'`\n- Push to the branch: `git push`\n- Submit a pull request :)\n\n## Authors\n\nThis library is written and maintained by imcuttle, \u003ca href=\"mailto:moyuyc95@gmail.com\"\u003emoyuyc95@gmail.com\u003c/a\u003e.\n\n## License\n\nMIT - [imcuttle](https://github.com/imcuttle) 🐟\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fimcuttle%2Fvisit-tree","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fimcuttle%2Fvisit-tree","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fimcuttle%2Fvisit-tree/lists"}