Skip to content

Improve generic types #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,41 @@
/**
* @typedef {import('unist').Node} Node
* @typedef {import('unist').Data} Data
*/
/**
* @template {object} [DateLike=Data]
* @typedef {import('unist').Node<DateLike>} Node<DateLike>
* @typedef {import('unist').Parent} Parent
*/

/**
* Function called with a node to produce a new node.
*
* @template {Node<object>} [OutputNode = Node]
* @template {Node<object>} [InputNode = OutputNode]
* @callback MapFunction
* @param {Node} node Current node being processed
* @param {InputNode} node Current node being processed
* @param {number} [index] Index of `node`, or `null`
* @param {Parent} [parent] Parent of `node`, or `null`
* @returns {Node} Node to be used in the new tree. Its children are not used: if the original node has children, those are mapped.
* @param {Parent<InputNode>} [parent] Parent of `node`, or `null`
* @returns {OutputNode} Node to be used in the new tree. Its children are not used: if the original node has children, those are mapped.
*/

/**
* Unist utility to create a new tree by mapping all nodes with the given function.
*
* @param {Node} tree Tree to map
* @param {MapFunction} iteratee Function that returns a new node
* @returns {Node} New mapped tree.
* @template {Node<object>} [OutputNode = Node]
* @template {Node<object>} [InputNode = OutputNode]
* @param {InputNode} tree Tree to map
* @param {MapFunction<OutputNode, InputNode>} iteratee Function that returns a new node
* @returns {OutputNode} New mapped tree.
*/
export function map(tree, iteratee) {
return preorder(tree, null, null)

/**
* @param {Node} node
* @param {InputNode} node
* @param {number} [index]
* @param {Parent} [parent]
* @returns {Node}
* @param {Parent<InputNode>} [parent]
* @returns {OutputNode}
*/
function preorder(node, index, parent) {
var newNode = Object.assign({}, iteratee(node, index, parent))
Expand Down
10 changes: 6 additions & 4 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ test('unist-util-map', function (t) {
t.end()

/**
* @param {Node} node
* @returns {Node}
* @template {Node} NodeLike
* @param {NodeLike} node
* @returns {NodeLike}
Copy link
Member

Choose a reason for hiding this comment

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

this one’s broken? (it apparently already was?)

*/
function changeLeaf(node) {
return node.type === 'leaf'
Expand All @@ -40,8 +41,9 @@ test('unist-util-map', function (t) {
}

/**
* @param {Node} node
* @returns {Node?}
* @template {Node} NodeLike
* @param {NodeLike} node
* @returns {NodeLike?}
*/
function nullLeaf(node) {
return node.type === 'leaf' ? null : node
Expand Down