{"id":26455123,"url":"https://github.com/generouslabs/unist-util-reduce","last_synced_at":"2025-03-18T20:30:09.291Z","repository":{"id":43992071,"uuid":"240761633","full_name":"GenerousLabs/unist-util-reduce","owner":"GenerousLabs","description":null,"archived":false,"fork":false,"pushed_at":"2022-02-13T01:30:14.000Z","size":652,"stargazers_count":3,"open_issues_count":9,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-08-10T05:17:58.199Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","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/GenerousLabs.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}},"created_at":"2020-02-15T17:52:23.000Z","updated_at":"2024-04-14T09:16:33.000Z","dependencies_parsed_at":"2022-09-26T20:20:30.203Z","dependency_job_id":null,"html_url":"https://github.com/GenerousLabs/unist-util-reduce","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GenerousLabs%2Funist-util-reduce","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GenerousLabs%2Funist-util-reduce/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GenerousLabs%2Funist-util-reduce/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GenerousLabs%2Funist-util-reduce/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/GenerousLabs","download_url":"https://codeload.github.com/GenerousLabs/unist-util-reduce/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244301036,"owners_count":20430853,"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":"2025-03-18T20:30:08.723Z","updated_at":"2025-03-18T20:30:09.269Z","avatar_url":"https://github.com/GenerousLabs.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# unist-util-reduce\n\n[![Chat][chat-badge]][chat]\n\n[**unist**][unist] utility to recursively reduce a tree\n\n## Install\n\n[npm][]:\n\n```bash\nnpm install unist-util-reduce\n```\n\n## Usage\n\n```js\nconst u = require(\"unist-builder\");\nconst reduce = require(\"unist-util-reduce\");\nconst assert = require(\"assert\");\n\nconst tree = u(\"tree\", [\n  u(\"leaf\", \"1\"),\n  u(\"node\", [u(\"leaf\", \"2\")]),\n  u(\"void\"),\n  u(\"leaf\", \"3\")\n]);\n\nconst newTree = reduce(tree, function(node) {\n  if (node.value === \"2\") {\n    const duplicateNode = { ...node, value: \"two\" };\n    return [duplicateNode, node];\n  }\n  return node;\n});\n\nconst expected = u(\"tree\", [\n  u(\"leaf\", \"1\"),\n  u(\"node\", [u(\"leaf\", \"two\"), u(\"leaf\", \"2\")]),\n  u(\"void\"),\n  u(\"leaf\", \"3\")\n]);\n\nassert.deepEqual(newTree, expected);\n```\n\nNOTE: `leaf` with `value` `2` is visited before it's parent `node`. By the\ntime your `transform` function is invoked, it's `children` will already have\nbeen passed through `transform`.\n\n## API\n\n### `reduce(tree, transform)`\n\n- `tree` - A node of type `Parent`\n- `transform` - A function with the signature:\n  - `(node: Node, path: number[], root: Paretn) =\u003e Node | Node[]`\n    - `node` - The `Node` in the tree to be transformed\n    - `path` - The path to reach this node in each level of the tree\n    - `root` - The `Parent` root node\n    - Returns - What you return is passed to `.concat()` on the `children`\n      array of the node's parent.\n\n## Related\n\n- [`unist-util-filter`](https://github.com/eush77/unist-util-filter)\n  — Create a new tree with all nodes that pass a test\n- [`unist-util-map`](https://github.com/syntax-tree/unist-util-map)\n  — Create a new tree with all nodes mapped by a given function\n- [`unist-util-remove`](https://github.com/eush77/unist-util-remove)\n  — Remove nodes from a tree that pass a test\n- [`unist-util-select`](https://github.com/eush77/unist-util-select)\n  — Select nodes with CSS-like selectors\n\n## License\n\n[MIT][license] © [Callum Macdonald / GeneroUS Labs](https://github.com/GenerousLabs/unist-util-reduce)\n\n\u003c!-- Definition --\u003e\n\n[chat-badge]: https://img.shields.io/badge/chat-spectrum-7b16ff.svg\n[chat]: https://spectrum.chat/unified/syntax-tree\n[npm]: https://docs.npmjs.com/cli/install\n[license]: licensea\n[unist]: https://github.com/syntax-tree/unist\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgenerouslabs%2Funist-util-reduce","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgenerouslabs%2Funist-util-reduce","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgenerouslabs%2Funist-util-reduce/lists"}