{"id":13339504,"url":"https://github.com/syntax-tree/unist-util-parents","last_synced_at":"2025-06-29T22:03:42.233Z","repository":{"id":4364422,"uuid":"42546784","full_name":"syntax-tree/unist-util-parents","owner":"syntax-tree","description":"unist utility to add references to parents on nodes in a tree","archived":false,"fork":false,"pushed_at":"2023-07-07T10:43:49.000Z","size":96,"stargazers_count":5,"open_issues_count":0,"forks_count":2,"subscribers_count":9,"default_branch":"main","last_synced_at":"2025-06-03T23:09:16.920Z","etag":null,"topics":["parent","syntax-tree","unist","unist-util","util"],"latest_commit_sha":null,"homepage":"https://unifiedjs.com","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/syntax-tree.png","metadata":{"funding":{"github":"unifiedjs","open_collective":"unified"},"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":"2015-09-15T21:10:34.000Z","updated_at":"2024-03-29T13:24:53.000Z","dependencies_parsed_at":"2024-06-18T21:34:24.073Z","dependency_job_id":"676cdfde-a659-48cb-964f-9b4bb79d9e10","html_url":"https://github.com/syntax-tree/unist-util-parents","commit_stats":{"total_commits":74,"total_committers":3,"mean_commits":"24.666666666666668","dds":"0.20270270270270274","last_synced_commit":"a8ca793859b70a45bc29a7ee954bd9950391b2e4"},"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"purl":"pkg:github/syntax-tree/unist-util-parents","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Funist-util-parents","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Funist-util-parents/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Funist-util-parents/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Funist-util-parents/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/syntax-tree","download_url":"https://codeload.github.com/syntax-tree/unist-util-parents/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Funist-util-parents/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260576438,"owners_count":23030586,"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":["parent","syntax-tree","unist","unist-util","util"],"created_at":"2024-07-29T19:20:04.292Z","updated_at":"2025-06-29T22:03:42.184Z","avatar_url":"https://github.com/syntax-tree.png","language":"JavaScript","readme":"# unist-util-parents\n\n[![Build][build-badge]][build]\n[![Coverage][coverage-badge]][coverage]\n[![Downloads][downloads-badge]][downloads]\n[![Size][size-badge]][size]\n[![Sponsors][sponsors-badge]][collective]\n[![Backers][backers-badge]][collective]\n[![Chat][chat-badge]][chat]\n\n[unist][] utility to add references to parents on nodes in a tree.\n\n## Contents\n\n*   [What is this?](#what-is-this)\n*   [When should I use this?](#when-should-i-use-this)\n*   [Install](#install)\n*   [Use](#use)\n*   [API](#api)\n    *   [`parents(tree)`](#parentstree)\n    *   [`Proxy`](#proxy)\n*   [Types](#types)\n*   [Compatibility](#compatibility)\n*   [Related](#related)\n*   [Contribute](#contribute)\n*   [License](#license)\n\n## What is this?\n\nThis utility creates a proxy of the tree that acts like the original tree upon\nreading, but each proxied node has a reference to its parent node.\n\n## When should I use this?\n\nThis package can be very useful for problems where it is needed to figure out\nwhat a nodes ancestors are, because unist itself is a non-cyclical data\nstructure, and thus does not provide that information.\nOn the other hand, this info on ancestors can also be gathered when walking the\ntree with [`unist-util-visit-parents`][unist-util-visit-parents].\n\n## Install\n\nThis package is [ESM only][esm].\nIn Node.js (version 16+), install with [npm][]:\n\n```sh\nnpm install unist-util-parents\n```\n\nIn Deno with [`esm.sh`][esmsh]:\n\n```js\nimport {parent} from 'https://esm.sh/unist-util-parents@3'\n```\n\nIn browsers with [`esm.sh`][esmsh]:\n\n```html\n\u003cscript type=\"module\"\u003e\n  import {parent} from 'https://esm.sh/unist-util-parents@3?bundle'\n\u003c/script\u003e\n```\n\n## Use\n\n```js\nimport {u} from 'unist-builder'\nimport {parents} from 'unist-util-parents'\n\nconst tree = u('root', [\n  u('leaf', 'leaf 1'),\n  u('node', [\n    u('leaf', 'leaf 2'),\n    u('void'),\n    u('node', [\n      u('leaf', 'leaf 3'),\n      u('node', [u('leaf', 'leaf 4')]),\n      u('void'),\n      u('leaf', 'leaf 5')\n    ])\n  ])\n])\n\nconst wrapped = parents(tree)\n\n// Leaf 4\nconst node = wrapped.children[1].children[2].children[1].children[0]\n\nconst chain = []\nwhile (node) {\n  chain.push(node.type)\n  node = node.parent\n}\n\nconsole.log(chain.reverse())\n```\n\nYields:\n\n```js\n['root', 'node', 'node', 'node', 'leaf']\n```\n\n## API\n\nThis package exports the identifier [`parents`][api-parents].\nThere is no default export.\n\n### `parents(tree)`\n\nCreate a proxy of `tree` that acts like the original tree upon reading, but\neach proxied node has a reference to its parent node.\n\n###### Notes\n\nThe returned proxy imposes two additional fields on all of its nodes:\n\n*   `parent` — parent link (or `undefined` for the root)\n*   `node` — link to the original node\n\nThese new fields are not enumerable and the original tree is *not changed*.\nThis means you can use `JSON.stringify` on the wrapped tree and it’s the same.\n\n`wrapped.children` returns array of wrapped child nodes, so that any recursive\nalgorithm will work on a wrapped tree just as well.\n\nTo write changes to the tree, use `.node` to access the original tree.\n\n###### Parameters\n\n*   `tree` ([`Node`][node])\n    — tree to proxy\n\n###### Returns\n\nProxy of `tree` ([`Proxy`][api-proxy]).\n\n### `Proxy`\n\nA proxy of a [`Node`][node] that adds two additional fields:\n\n*   `parent` — parent link (or `undefined` for the root)\n*   `node` — link to the original node\n\n## Types\n\nThis package is fully typed with [TypeScript][].\nIt exports the additional type [`Proxy`][api-proxy].\n\n## Compatibility\n\nProjects maintained by the unified collective are compatible with maintained\nversions of Node.js.\n\nWhen we cut a new major release, we drop support for unmaintained versions of\nNode.\nThis means we try to keep the current release line, `unist-util-parents@^3`,\ncompatible with Node.js 16.\n\n## Related\n\n*   [`unist-util-visit-parents`][unist-util-visit-parents]\n    — walk the tree with ancestral information\n\n## Contribute\n\nSee [`contributing.md`][contributing] in [`syntax-tree/.github`][health] for\nways to get started.\nSee [`support.md`][support] for ways to get help.\n\nThis project has a [code of conduct][coc].\nBy interacting with this repository, organisation, or community you agree to\nabide by its terms.\n\n## License\n\n[MIT][license] © Eugene Sharygin\n\n\u003c!-- Definitions --\u003e\n\n[build-badge]: https://github.com/syntax-tree/unist-util-parents/workflows/main/badge.svg\n\n[build]: https://github.com/syntax-tree/unist-util-parents/actions\n\n[coverage-badge]: https://img.shields.io/codecov/c/github/syntax-tree/unist-util-parents.svg\n\n[coverage]: https://codecov.io/github/syntax-tree/unist-util-parents\n\n[downloads-badge]: https://img.shields.io/npm/dm/unist-util-parents.svg\n\n[downloads]: https://www.npmjs.com/package/unist-util-parents\n\n[size-badge]: https://img.shields.io/badge/dynamic/json?label=minzipped%20size\u0026query=$.size.compressedSize\u0026url=https://deno.bundlejs.com/?q=unist-util-parents\n\n[size]: https://bundlejs.com/?q=unist-util-parents\n\n[sponsors-badge]: https://opencollective.com/unified/sponsors/badge.svg\n\n[backers-badge]: https://opencollective.com/unified/backers/badge.svg\n\n[collective]: https://opencollective.com/unified\n\n[chat-badge]: https://img.shields.io/badge/chat-discussions-success.svg\n\n[chat]: https://github.com/syntax-tree/unist/discussions\n\n[npm]: https://docs.npmjs.com/cli/install\n\n[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c\n\n[esmsh]: https://esm.sh\n\n[typescript]: https://www.typescriptlang.org\n\n[license]: license\n\n[health]: https://github.com/syntax-tree/.github\n\n[contributing]: https://github.com/syntax-tree/.github/blob/main/contributing.md\n\n[support]: https://github.com/syntax-tree/.github/blob/main/support.md\n\n[coc]: https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md\n\n[unist]: https://github.com/syntax-tree/unist\n\n[node]: https://github.com/syntax-tree/unist#node\n\n[unist-util-visit-parents]: https://github.com/syntax-tree/unist-util-visit-parents\n\n[api-parents]: #parentstree\n\n[api-proxy]: #proxy\n","funding_links":["https://github.com/sponsors/unifiedjs","https://opencollective.com/unified"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsyntax-tree%2Funist-util-parents","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsyntax-tree%2Funist-util-parents","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsyntax-tree%2Funist-util-parents/lists"}