unist-util-map
Advanced tools
Comparing version 3.0.1 to 3.1.0
/** | ||
* @typedef {import('unist').Parent} Parent | ||
* @typedef {import('unist').Position} Position | ||
* @typedef {import('unist').Node} Node | ||
* @typedef {Record<string, unknown> & {type: string, position?: Position|undefined}} NodeLike | ||
*/ | ||
/** | ||
* Function called with a node to produce a new node. | ||
* | ||
* @callback MapFunction | ||
* @param {NodeLike|Node} node Current node being processed | ||
* @param {number} [index] Index of `node`, or `null` | ||
* @param {Parent} [parent] Parent of `node`, or `null` | ||
* @returns {NodeLike|Node} 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 {NodeLike|Node} tree Tree to map | ||
* @param {MapFunction} iteratee Function that returns a new node | ||
* @returns {NodeLike|Node} New mapped tree. | ||
* @template {Node} Tree | ||
* @param {Tree} tree Tree to map | ||
* @param {import('./complex-types').MapFunction<Tree>} iteratee Function that returns a new node | ||
* @returns {Tree} New mapped tree. | ||
*/ | ||
export function map( | ||
tree: NodeLike | Node, | ||
iteratee: MapFunction | ||
): NodeLike | Node | ||
export type Parent = import('unist').Parent | ||
export type Position = import('unist').Position | ||
export function map<Tree extends import('unist').Node<import('unist').Data>>( | ||
tree: Tree, | ||
iteratee: import('./complex-types').MapFunction<Tree> | ||
): Tree | ||
export type Node = import('unist').Node | ||
export type NodeLike = Record<string, unknown> & { | ||
type: string | ||
position?: Position | undefined | ||
} | ||
/** | ||
* Function called with a node to produce a new node. | ||
*/ | ||
export type MapFunction = ( | ||
node: NodeLike | Node, | ||
index?: number, | ||
parent?: Parent | ||
) => NodeLike | Node |
31
index.js
/** | ||
* @typedef {import('unist').Parent} Parent | ||
* @typedef {import('unist').Position} Position | ||
* @typedef {import('unist').Node} Node | ||
* @typedef {Record<string, unknown> & {type: string, position?: Position|undefined}} NodeLike | ||
*/ | ||
/** | ||
* Function called with a node to produce a new node. | ||
* | ||
* @callback MapFunction | ||
* @param {NodeLike|Node} node Current node being processed | ||
* @param {number} [index] Index of `node`, or `null` | ||
* @param {Parent} [parent] Parent of `node`, or `null` | ||
* @returns {NodeLike|Node} 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 {NodeLike|Node} tree Tree to map | ||
* @param {MapFunction} iteratee Function that returns a new node | ||
* @returns {NodeLike|Node} New mapped tree. | ||
* @template {Node} Tree | ||
* @param {Tree} tree Tree to map | ||
* @param {import('./complex-types').MapFunction<Tree>} iteratee Function that returns a new node | ||
* @returns {Tree} New mapped tree. | ||
*/ | ||
export function map(tree, iteratee) { | ||
// @ts-expect-error Looks like a children. | ||
return preorder(tree, null, null) | ||
/** | ||
* @param {NodeLike|Node} node | ||
* @param {number} [index] | ||
* @param {Parent} [parent] | ||
* @returns {Node} | ||
*/ | ||
/** @type {import('./complex-types').MapFunction<Tree>} */ | ||
function preorder(node, index, parent) { | ||
@@ -40,6 +24,5 @@ var newNode = Object.assign({}, iteratee(node, index, parent)) | ||
newNode.children = node.children.map(function ( | ||
/** @type {Node} */ child, | ||
/** @type {import('./complex-types').InclusiveDescendant<Tree>} */ child, | ||
/** @type {number} */ index | ||
) { | ||
// @ts-expect-error Looks like a parent. | ||
return preorder(child, index, node) | ||
@@ -46,0 +29,0 @@ }) |
{ | ||
"name": "unist-util-map", | ||
"version": "3.0.1", | ||
"version": "3.1.0", | ||
"description": "unist utility to create a new tree by mapping all nodes", | ||
@@ -55,3 +55,3 @@ "license": "MIT", | ||
"prepack": "npm run build && npm run format", | ||
"build": "rimraf \"*.d.ts\" && tsc && type-coverage", | ||
"build": "rimraf \"{index,test}.d.ts\" && tsc && type-coverage", | ||
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix", | ||
@@ -58,0 +58,0 @@ "test-api": "node test.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9364
45