Skip to content

Commit

Permalink
Fix to add MapFunction type back
Browse files Browse the repository at this point in the history
Closes GH-6.
Closes GH-7.

Reviewed-by: Christian Murphy <christian.murphy.42@gmail.com>
Reviewed-by: Remco Haszing <remcohaszing@gmail.com>
Reviewed-by: Titus Wormer <tituswormer@gmail.com>

Co-authored-by: Titus Wormer <tituswormer@gmail.com>
  • Loading branch information
JounQin and wooorm authored Sep 28, 2022
1 parent 90e8d93 commit 5b1c636
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
9 changes: 8 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,21 @@
* @typedef {import('unist').Node} Node
*/

/**
* @template {Node} [Tree=Node]
* @typedef {import('./complex-types.js').MapFunction<Tree>} MapFunction
* Function called with a node, its index, and its parent to produce a new
* node.
*/

/**
* Create a new tree by mapping all nodes with the given function.
*
* @template {Node} Tree
* Type of input tree.
* @param {Tree} tree
* Tree to map.
* @param {import('./complex-types').MapFunction<Tree>} mapFunction
* @param {MapFunction<Tree>} mapFunction
* Function called with a node, its index, and its parent to produce a new
* node.
* @returns {Tree}
Expand Down
3 changes: 1 addition & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,7 @@ if the original node has children, those are mapped instead.
## Types

This package is fully typed with [TypeScript][].
It exports a type `MapFunction<Tree extends Node = Node>` from
`unist-util-map/complex-types.d.ts` to properly type `MapFunction`s.
It exports a type `MapFunction<Tree extends Node = Node>` to properly type `MapFunction`s.

## Compatibility

Expand Down
16 changes: 16 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ test('unist-util-map', function (t) {
'should work when passing an empty object'
)

/** @type {Root} */
const tree = u('root', [u('node', [u('leaf', '1')]), u('leaf', '2')])

t.deepEqual(
map(tree, asIs),
tree,
'should support an explicitly typed `MapFunction`'
)

t.end()

/**
Expand All @@ -69,4 +78,11 @@ test('unist-util-map', function (t) {
function addValue() {
return {value: 'test'}
}

/**
* @type {import('./index.js').MapFunction<Root>}
*/
function asIs(node) {
return node
}
})

0 comments on commit 5b1c636

Please sign in to comment.