Thanks to visit codestin.com
Credit goes to github.com

Skip to content

gorango/unist-util-distance

Repository files navigation

unist-util-distance

Coverage Types Size

Unist utility for finding the distance between nodes. Useful for working with remark, rehype and retext.

Install

npm install unist-util-distance

Use

import { u } from 'unist-builder'
import { find } from 'unist-util-find'
import { findDistance } from 'unist-util-distance'

const tree =
  u('root', [
    u('node', [
      u('leaf', 'leaf 0')
    ]),
    u('node', [
      u('node', [
        u('leaf', 'leaf 1'),
      ]),
      u('node', [
        u('leaf', 'leaf 2'),
        u('node', [
          u('leaf', 'leaf 3'),
        ])
      ])
    ]),
    u('leaf', 'leaf 4')
  ])

const nodeA = find(tree, { value: 'leaf 0' })
const nodeB = find(tree, { value: 'leaf 1' })

console.log(findDistance(tree, nodeA, nodeB))

Yields:

5

API

findDistance(tree, nodeA, nodeB)

Return the distance between two nodes.

  • tree (Parent) - Unist tree to search
  • nodeA (Node) - First node
  • nodeB (Node) - Second node

Returns a number representing the distance (number of edges) between the nodes. Returns 0 if nodes are the same or one is an ancestor of the other.

Test

Run npm test to run tests.

Run npm run coverage to produce a test coverage report.

License

MIT © Goran Spasojevic

About

Unist utility for finding the distance between nodes

Topics

Resources

License

Stars

Watchers

Forks

Contributors