Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

utility to index property values or computed keys to nodes

License

NotificationsYou must be signed in to change notification settings

syntax-tree/unist-util-index

BuildCoverageDownloadsSizeSponsorsBackersChat

unist utility to create an index from certain nodes.

Contents

What is this?

This utility creates a mutable index data structure, that maps property valuesor computed keys, to nodes.For example, you can use this to index all (footnote) definitions in a tree,or all headings of a certain rank, to later retrieve them without having to walkthe tree each time.

When should I use this?

This is a utility that helps you deal with indexing the tree.It’s pretty small, and you can definitely do it yourself, but this littlewrapper makes it all a bit easier.

Install

This package isESM only.In Node.js (version 16+), install withnpm:

npm install unist-util-index

In Deno withesm.sh:

import{Index}from'https://esm.sh/unist-util-index@4'

In browsers withesm.sh:

<scripttype="module">import{Index}from'https://esm.sh/unist-util-index@4?bundle'</script>

Use

importfsfrom'node:fs/promises'import{fromMarkdown}from'mdast-util-from-markdown'import{toString}from'mdast-util-to-string'import{Index}from'unist-util-index'// Parse and read this repo’s readme:consttree=fromMarkdown(awaitfs.readFile('readme.md'))// Index on heading depth:constindexOnDepth=newIndex('depth',tree,'heading')console.log(indexOnDepth.get(2).map(function(d){returntoString(d)}))// Index on definition identifier:constindexOnIdentifier=newIndex('identifier',tree,'definition')console.log(indexOnIdentifier.get('unist').map(function(node){returnnode.url}))

Yields:

['Contents','What is this?','When should I use this?','Install','Use','API','Types','Compatibility','Related','Contribute','License']['https://github.com/syntax-tree/unist']

API

This package exports the identifierIndex.There is no default export.

Index(prop|keyFunction[, tree[, test]])

Create a mutable index data structure, that maps property values or computedkeys, to nodes.

Iftree is given, the index is initialized with all nodes, optionallyfiltered bytest.

Parameters
  • prop (string)— field to look up in each node to find keys
  • keyFunction (KeyFunction)— function called with each node to calculate keys
  • tree (Node, optional)— tree to index
  • test (Test, optional)—unist-util-is compatible test
Returns

Instance (Index).

Index#get(key)

Get nodes bykey.

Parameters
  • key (unknown)— key to retrieve, can be anything that can be used as a key in aMap
Returns

List of zero or more nodes (Array<Node>).

Index#add(node)

Addnode to the index (if not already present).

Parameters
  • node (Node)— node to index
Returns

Current instance (Index).

Index#remove(node)

Removenode from the index (if present).

Parameters
  • node (Node)— node to remove
Returns

Current instance (Index).

KeyFunction

Function called with every added node to calculate the key to index on(TypeScript type).

Parameters
  • node (Node)— node to calculate a key for
Returns

Key to index on (unknown).

Can be anything that can be used as a key in aMap.

Test

unist-util-is compatible test (TypeScript type).

Types

This package is fully typed withTypeScript.It exports the additional typesKeyFunction andTest.

Compatibility

Projects maintained by the unified collective are compatible with maintainedversions of Node.js.

When we cut a new major release, we drop support for unmaintained versions ofNode.This means we try to keep the current release line,unist-util-index@^4,compatible with Node.js 16.

Related

Contribute

Seecontributing.md insyntax-tree/.github forways to get started.Seesupport.md for ways to get help.

This project has aCode of Conduct.By interacting with this repository, organisation, or community you agree toabide by its terms.

License

MIT © Eugene Sharygin

About

utility to index property values or computed keys to nodes

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Contributors4

  •  
  •  
  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp