Add unist-util-distance to list of utilities#154
Open
gorango wants to merge 2 commits into
Open
Conversation
remcohaszing
approved these changes
Apr 15, 2026
Member
remcohaszing
left a comment
There was a problem hiding this comment.
Thanks!
A few small tips:
- You can use
@importtags in JSDoc to import types. - You don’t need the
NodeLiketype. You can just useNode. - 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 omitunist-util-inspectfrom the example.
Contributor
Author
|
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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Initial checklist
Description of changes
Add
unist-util-distanceto list of utilities