{"id":29125782,"url":"https://github.com/syntax-tree/unist-util-index","last_synced_at":"2025-06-29T22:03:44.250Z","repository":{"id":57386417,"uuid":"50314755","full_name":"syntax-tree/unist-util-index","owner":"syntax-tree","description":"utility to index property values or computed keys to nodes","archived":false,"fork":false,"pushed_at":"2023-07-07T10:33:06.000Z","size":119,"stargazers_count":9,"open_issues_count":0,"forks_count":1,"subscribers_count":9,"default_branch":"main","last_synced_at":"2025-05-22T10:57:43.696Z","etag":null,"topics":["index","map","syntax-tree","unist","unist-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":"2016-01-25T00:02:55.000Z","updated_at":"2024-11-09T21:28:05.000Z","dependencies_parsed_at":"2024-06-19T00:00:00.613Z","dependency_job_id":"24ca81d4-25c4-493b-a40d-62910d235d04","html_url":"https://github.com/syntax-tree/unist-util-index","commit_stats":{"total_commits":93,"total_committers":4,"mean_commits":23.25,"dds":"0.15053763440860213","last_synced_commit":"174848ee02b92a524c10967868e2a980650a70bd"},"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"purl":"pkg:github/syntax-tree/unist-util-index","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Funist-util-index","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Funist-util-index/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Funist-util-index/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Funist-util-index/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/syntax-tree","download_url":"https://codeload.github.com/syntax-tree/unist-util-index/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Funist-util-index/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261865557,"owners_count":23221974,"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":["index","map","syntax-tree","unist","unist-util"],"created_at":"2025-06-29T22:03:42.449Z","updated_at":"2025-06-29T22:03:44.222Z","avatar_url":"https://github.com/syntax-tree.png","language":"JavaScript","readme":"# unist-util-index\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 create an index from certain nodes.\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    *   [`Index(prop|keyFunction[, tree[, test]])`](#indexpropkeyfunction-tree-test)\n    *   [`KeyFunction`](#keyfunction)\n    *   [`Test`](#test)\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 mutable index data structure, that maps property values\nor computed keys, to nodes.\nFor example, you can use this to index all (footnote) definitions in a tree,\nor all headings of a certain rank, to later retrieve them without having to walk\nthe tree each time.\n\n## When should I use this?\n\nThis is a utility that helps you deal with indexing the tree.\nIt’s pretty small, and you can definitely do it yourself, but this little\nwrapper makes it all a bit easier.\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-index\n```\n\nIn Deno with [`esm.sh`][esmsh]:\n\n```js\nimport {Index} from 'https://esm.sh/unist-util-index@4'\n```\n\nIn browsers with [`esm.sh`][esmsh]:\n\n```html\n\u003cscript type=\"module\"\u003e\n  import {Index} from 'https://esm.sh/unist-util-index@4?bundle'\n\u003c/script\u003e\n```\n\n## Use\n\n```js\nimport fs from 'node:fs/promises'\nimport {fromMarkdown} from 'mdast-util-from-markdown'\nimport {toString} from 'mdast-util-to-string'\nimport {Index} from 'unist-util-index'\n\n// Parse and read this repo’s readme:\nconst tree = fromMarkdown(await fs.readFile('readme.md'))\n\n// Index on heading depth:\nconst indexOnDepth = new Index('depth', tree, 'heading')\n\nconsole.log(\n  indexOnDepth.get(2).map(function (d) {\n    return toString(d)\n  })\n)\n\n// Index on definition identifier:\nconst indexOnIdentifier = new Index('identifier', tree, 'definition')\n\nconsole.log(\n  indexOnIdentifier.get('unist').map(function (node) {\n    return node.url\n  })\n)\n```\n\nYields:\n\n```js\n[\n  'Contents',\n  'What is this?',\n  'When should I use this?',\n  'Install',\n  'Use',\n  'API',\n  'Types',\n  'Compatibility',\n  'Related',\n  'Contribute',\n  'License'\n]\n[ 'https://github.com/syntax-tree/unist' ]\n```\n\n## API\n\nThis package exports the identifier [`Index`][index].\nThere is no default export.\n\n### `Index(prop|keyFunction[, tree[, test]])`\n\nCreate a mutable index data structure, that maps property values or computed\nkeys, to nodes.\n\nIf `tree` is given, the index is initialized with all nodes, optionally\nfiltered by `test`.\n\n###### Parameters\n\n*   `prop` (`string`)\n    — field to look up in each node to find keys\n*   `keyFunction` ([`KeyFunction`][keyfunction])\n    — function called with each node to calculate keys\n*   `tree` ([`Node`][node], optional)\n    — tree to index\n*   `test` ([`Test`][test], optional)\n    — `unist-util-is` compatible test\n\n###### Returns\n\nInstance (`Index`).\n\n#### `Index#get(key)`\n\nGet nodes by `key`.\n\n###### Parameters\n\n*   `key` (`unknown`)\n    — key to retrieve, can be anything that can be used as a key in a\n    [`Map`][map]\n\n###### Returns\n\nList of zero or more nodes ([`Array\u003cNode\u003e`][node]).\n\n#### `Index#add(node)`\n\nAdd `node` to the index (if not already present).\n\n###### Parameters\n\n*   `node` ([`Node`][node])\n    — node to index\n\n###### Returns\n\nCurrent instance (`Index`).\n\n#### `Index#remove(node)`\n\nRemove `node` from the index (if present).\n\n###### Parameters\n\n*   `node` ([`Node`][node])\n    — node to remove\n\n###### Returns\n\nCurrent instance (`Index`).\n\n### `KeyFunction`\n\nFunction called with every added node to calculate the key to index on\n(TypeScript type).\n\n###### Parameters\n\n*   `node` ([`Node`][node])\n    — node to calculate a key for\n\n###### Returns\n\nKey to index on (`unknown`).\n\nCan be anything that can be used as a key in a [`Map`][map].\n\n### `Test`\n\n[`unist-util-is`][unist-util-is] compatible test (TypeScript type).\n\n## Types\n\nThis package is fully typed with [TypeScript][].\nIt exports the additional types [`KeyFunction`][keyfunction] and\n[`Test`][test].\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-index@^4`,\ncompatible with Node.js 16.\n\n## Related\n\n*   [`unist-util-is`](https://github.com/syntax-tree/unist-util-is)\n    — utility to check if a node passes a test\n*   [`unist-util-visit`](https://github.com/syntax-tree/unist-util-visit)\n    — utility to recursively walk over nodes\n*   [`unist-util-select`](https://github.com/syntax-tree/unist-util-select)\n    — select nodes with CSS-like selectors\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-index/workflows/main/badge.svg\n\n[build]: https://github.com/syntax-tree/unist-util-index/actions\n\n[coverage-badge]: https://img.shields.io/codecov/c/github/syntax-tree/unist-util-index.svg\n\n[coverage]: https://codecov.io/github/syntax-tree/unist-util-index\n\n[downloads-badge]: https://img.shields.io/npm/dm/unist-util-index.svg\n\n[downloads]: https://www.npmjs.com/package/unist-util-index\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-index\n\n[size]: https://bundlejs.com/?q=unist-util-index\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[map]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Map\n\n[unist-util-is]: https://github.com/syntax-tree/unist-util-is\n\n[index]: #indexpropkeyfunction-tree-test\n\n[keyfunction]: #keyfunction\n\n[test]: #test\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-index","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsyntax-tree%2Funist-util-index","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsyntax-tree%2Funist-util-index/lists"}