{"id":13716591,"url":"https://github.com/syntax-tree/unist-util-map","last_synced_at":"2025-07-12T07:38:07.603Z","repository":{"id":5944675,"uuid":"54302159","full_name":"syntax-tree/unist-util-map","owner":"syntax-tree","description":"utility to create a new tree by mapping all nodes","archived":false,"fork":false,"pushed_at":"2023-07-07T14:44:37.000Z","size":123,"stargazers_count":35,"open_issues_count":0,"forks_count":4,"subscribers_count":9,"default_branch":"main","last_synced_at":"2024-10-29T15:39:30.627Z","etag":null,"topics":["map","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":{"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},"funding":{"github":"unifiedjs","open_collective":"unified"}},"created_at":"2016-03-20T05:27:06.000Z","updated_at":"2024-04-28T15:16:07.000Z","dependencies_parsed_at":"2024-06-18T14:05:26.272Z","dependency_job_id":"dffd391f-2221-4ffe-8e54-f30e6548f526","html_url":"https://github.com/syntax-tree/unist-util-map","commit_stats":{"total_commits":90,"total_committers":5,"mean_commits":18.0,"dds":0.1333333333333333,"last_synced_commit":"3ec6dfa25f1784cf94ddc53fc0767679bd9e1520"},"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Funist-util-map","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Funist-util-map/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Funist-util-map/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Funist-util-map/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/syntax-tree","download_url":"https://codeload.github.com/syntax-tree/unist-util-map/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224567641,"owners_count":17332829,"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":["map","syntax-tree","unist","unist-util","util"],"created_at":"2024-08-03T00:01:12.183Z","updated_at":"2024-11-14T04:31:54.279Z","avatar_url":"https://github.com/syntax-tree.png","language":"JavaScript","readme":"# unist-util-map\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 a new tree by mapping all nodes with a given\nfunction.\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    *   [`map(tree, mapFunction)`](#maptree-mapfunction)\n*   [Types](#types)\n*   [Compatibility](#compatibility)\n*   [Related](#related)\n*   [Contribute](#contribute)\n*   [License](#license)\n\n## What is this?\n\nThis is a small utility that helps you make new trees.\n\n## When should I use this?\n\nYou can use this utility to create a new tree by mapping all nodes with a given\nfunction.\nCreating new trees like this can lead to performance problems, as it creates\nnew objects for every node.\nWhen dealing with potentially large trees, and relatively few changes, use\n[`unist-util-visit`][unist-util-visit] (or\n[`unist-util-visit-parents`][unist-util-visit-parents]) instead.\n\nTo remove certain nodes, you can also walk the tree with `unist-util-visit`, or\nuse [`unist-util-filter`][unist-util-filter] (clones the tree instead of\nmutating) or [`unist-util-remove`][unist-util-remove] (mutates).\nTo create trees, use [`unist-builder`][unist-builder].\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-map\n```\n\nIn Deno with [`esm.sh`][esmsh]:\n\n```js\nimport {map} from 'https://esm.sh/unist-util-map@4'\n```\n\nIn browsers with [`esm.sh`][esmsh]:\n\n```html\n\u003cscript type=\"module\"\u003e\n  import {map} from 'https://esm.sh/unist-util-map@4?bundle'\n\u003c/script\u003e\n```\n\n## Use\n\n```js\nimport {u} from 'unist-builder'\nimport {map} from 'unist-util-map'\n\nconst tree = u('tree', [\n  u('leaf', 'leaf 1'),\n  u('node', [u('leaf', 'leaf 2')]),\n  u('void'),\n  u('leaf', 'leaf 3')\n])\n\nconst next = map(tree, function (node) {\n  return node.type === 'leaf'\n    ? Object.assign({}, node, {value: 'CHANGED'})\n    : node\n})\n\nconsole.dir(next, {depth: undefined})\n```\n\nYields:\n\n```js\n{\n  type: 'tree',\n  children: [\n    {type: 'leaf', value: 'CHANGED'},\n    {type: 'node', children: [{type: 'leaf', value: 'CHANGED'}]},\n    {type: 'void'},\n    {type: 'leaf', value: 'CHANGED'}\n  ]\n}\n```\n\n\u003e 👉 **Note**: `next` is a changed clone and `tree` is not mutated.\n\n## API\n\nThis package exports the identifier [`map`][api-map].\nThere is no default export.\n\n### `map(tree, mapFunction)`\n\nCreate a new tree by mapping all nodes with the given function.\n\n###### Parameters\n\n*   `tree` ([`Node`][node])\n    — tree to map\n*   `mapFunction` ([`MapFunction`][api-mapfunction])\n    — function called with a node, its index, and its parent to produce a new\n    node\n\n###### Returns\n\nNew mapped tree ([`Node`][node]).\n\n#### `MapFunction`\n\nFunction called with a node, its index, and its parent to produce a new\nnode (TypeScript type).\n\n###### Parameters\n\n*   `node` ([`Node`][node])\n    — node to map\n*   `index` (`number` or `undefined`)\n    — index of `node` in `parent` (if any)\n*   `parent` ([`Node`][node] or `undefined`)\n    — parent of `node`\n\n###### Returns\n\nNew mapped node ([`Node`][node]).\n\nThe children on the returned node are not used.\nIf the original node has children, those are mapped instead.\n\n## Types\n\nThis package is fully typed with [TypeScript][].\nIt exports the additional type [`MapFunction`][api-mapfunction].\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-map@^4`,\ncompatible with Node.js 16.\n\n## Related\n\n*   [`unist-util-filter`](https://github.com/syntax-tree/unist-util-filter)\n    — create a new tree with all nodes that pass the given function\n*   [`unist-util-flatmap`](https://gitlab.com/staltz/unist-util-flatmap)\n    — create a new tree by expanding a node into many\n*   [`unist-util-remove`](https://github.com/syntax-tree/unist-util-remove)\n    — remove nodes from trees\n*   [`unist-util-select`](https://github.com/syntax-tree/unist-util-select)\n    — select nodes with CSS-like selectors\n*   [`unist-util-visit`](https://github.com/syntax-tree/unist-util-visit)\n    — walk trees\n*   [`unist-builder`](https://github.com/syntax-tree/unist-builder)\n    — create trees\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, organization, or community you agree to\nabide by its terms.\n\n## License\n\n[MIT][license] © [azu][author]\n\n\u003c!-- Definitions --\u003e\n\n[build-badge]: https://github.com/syntax-tree/unist-util-map/workflows/main/badge.svg\n\n[build]: https://github.com/syntax-tree/unist-util-map/actions\n\n[coverage-badge]: https://img.shields.io/codecov/c/github/syntax-tree/unist-util-map.svg\n\n[coverage]: https://codecov.io/github/syntax-tree/unist-util-map\n\n[downloads-badge]: https://img.shields.io/npm/dm/unist-util-map.svg\n\n[downloads]: https://www.npmjs.com/package/unist-util-map\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-map\n\n[size]: https://bundlejs.com/?q=unist-util-map\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[author]: https://efcl.info\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]: https://github.com/syntax-tree/unist-util-visit\n\n[unist-util-visit-parents]: https://github.com/syntax-tree/unist-util-visit-parents\n\n[unist-util-filter]: https://github.com/syntax-tree/unist-util-filter\n\n[unist-util-remove]: https://github.com/syntax-tree/unist-util-remove\n\n[unist-builder]: https://github.com/syntax-tree/unist-builder\n\n[api-map]: #maptree-mapfunction\n\n[api-mapfunction]: #mapfunction\n","funding_links":["https://github.com/sponsors/unifiedjs","https://opencollective.com/unified"],"categories":["unist utilities"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsyntax-tree%2Funist-util-map","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsyntax-tree%2Funist-util-map","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsyntax-tree%2Funist-util-map/lists"}