Skip to content

Add unist-util-distance to list of utilities#154

Open
gorango wants to merge 2 commits into
syntax-tree:mainfrom
gorango:main
Open

Add unist-util-distance to list of utilities#154
gorango wants to merge 2 commits into
syntax-tree:mainfrom
gorango:main

Conversation

@gorango
Copy link
Copy Markdown
Contributor

@gorango gorango commented Apr 11, 2026

Initial checklist

  • I read the support docs
  • I read the contributing guide
  • I agree to follow the code of conduct
  • I searched issues and discussions and couldn’t find anything or linked relevant results below
  • I made sure the docs are up to date
  • I included tests (or that’s not needed)

Description of changes

Add unist-util-distance to list of utilities

@github-actions github-actions Bot added 👋 phase/new Post is being triaged automatically 🤞 phase/open Post is being triaged manually and removed 👋 phase/new Post is being triaged automatically labels Apr 11, 2026
Copy link
Copy Markdown
Member

@remcohaszing remcohaszing left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

A few small tips:

  • You can use @import tags in JSDoc to import types.
  • You don’t need the NodeLike type. You can just use Node.
  • I believe the readme example doesn’t work. You search for nodes by reference. But you pass 2 new objects into findDistance().
  • Since findDistance() returns a number, I think you might as well omit unist-util-inspect from the example.

@gorango
Copy link
Copy Markdown
Contributor Author

gorango commented Apr 23, 2026

Thank you for that feedback! I have implemented all of your recommendations:

diff --git a/index.d.ts b/index.d.ts
index 7935948..dea2d71 100644
--- a/index.d.ts
+++ b/index.d.ts
@@ -1,13 +1,11 @@
-import type { Parent, Node, Literal } from 'unist'
-
-type NodeLike = Node | Parent | Literal
+import type { Parent, Node } from 'unist'
 
 /**
  * Find distance between two nodes
  *
  * @param {Parent} tree - Root node
- * @param {NodeLike} nodeA - First node
- * @param {NodeLike} nodeB - Second node
+ * @param {Node} nodeA - First node
+ * @param {Node} nodeB - Second node
  * @returns {number} - Distance between nodes
  */
-export function findDistance(tree: Parent, nodeA: NodeLike, nodeB: NodeLike): number
+export function findDistance(tree: Parent, nodeA: Node, nodeB: Node): number
diff --git a/index.js b/index.js
index 611240e..774b511 100644
--- a/index.js
+++ b/index.js
@@ -1,21 +1,15 @@
 /**
- * @typedef { import('unist').Node } Node
- * @typedef { import('unist').Parent } Parent
- * @typedef { import('unist').Literal } Literal
+ * @import { Node, Parent } from 'unist'
  */
 
 import { visitParents } from 'unist-util-visit-parents'
 
-/**
- * @typedef { Node | Parent | Literal } NodeLike
- */
-
 /**
  * Find distance between two nodes
  *
  * @param {Parent} tree - Root node
- * @param {NodeLike} nodeA - First node
- * @param {NodeLike} nodeB - Second node
+ * @param {Node} nodeA - First node
+ * @param {Node} nodeB - Second node
  * @returns {number} - Distance between nodes
  */
 export function findDistance(tree, nodeA, nodeB) {

... and the readme example now works:

diff --git a/readme.md b/readme.md
index 4e92625..47ad7d0 100644
--- a/readme.md
+++ b/readme.md
@@ -16,7 +16,7 @@ npm install unist-util-distance
 
 ```js
 import { u } from 'unist-builder'
-import { inspect } from 'unist-util-inspect'
+import { find } from 'unist-util-find'
 import { findDistance } from 'unist-util-distance'
 
 const tree =
@@ -38,10 +38,10 @@ const tree =
     u('leaf', 'leaf 4')
   ])
 
-const nodeA = { value: 'leaf 0' }
-const nodeB = { value: 'leaf 1' }
+const nodeA = find(tree, { value: 'leaf 0' })
+const nodeB = find(tree, { value: 'leaf 1' })
 
-console.log(inspect(findDistance(tree, nodeA, nodeB)))
+console.log(findDistance(tree, nodeA, nodeB))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🤞 phase/open Post is being triaged manually

Development

Successfully merging this pull request may close these issues.

2 participants