8000 Refactor to move implementation to `lib/` · syntax-tree/unist-util-map@8eca708 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8eca708

Browse files
committed
Refactor to move implementation to lib/
1 parent 75347a5 commit 8eca708

File tree

5 files changed

+50
-44
lines changed

5 files changed

+50
-44
lines changed

index.js

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,6 @@
11
/**
2-
* @typedef {import('unist').Node} Node
2+
* @template {import('unist').Node} [Kind=import('unist').Node]
3+
* @typedef {import('./lib/index.js').MapFunction<Kind>} MapFunction
34
*/
45

5-
/**
6-
* @template {Node} [Tree=Node]
7-
* @typedef {import('./complex-types.js').MapFunction<Tree>} MapFunction
8-
* Function called with a node, its index, and its parent to produce a new
9-
* node.
10-
*/
11-
12-
/**
13-
* Create a new tree by mapping all nodes with the given function.
14-
*
15-
* @template {Node} Tree
16-
* Type of input tree.
17-
* @param {Tree} tree
18-
* Tree to map.
19-
* @param {MapFunction<Tree>} mapFunction
20-
* Function called with a node, its index, and its parent to produce a new
21-
* node.
22-
* @returns {Tree}
23-
* New mapped tree.
24-
*/
25-
export function map(tree, mapFunction) {
26-
// @ts-expect-error Looks like a children.
27-
return preorder(tree, null, null)
28-
29-
/** @type {import('./complex-types.js').MapFunction<Tree>} */
30-
function preorder(node, index, parent) {
31-
var newNode = Object.assign({}, mapFunction(node, index, parent))
32-
33-
if ('children' in node) {
34-
// @ts-expect-error Looks like a parent.
35-
newNode.children = node.children.map(function (
36-
/** @type {import('./complex-types.js').InclusiveDescendant<Tree>} */ child,
37-
/** @type {number} */ index
38-
) {
39-
return preorder(child, index, node)
40-
})
41-
}
42-
43-
return newNode
44-
}
45-
}
6+
export {map} from './lib/index.js'
File renamed without changes.

lib/index.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* @typedef {import('unist').Node} Node
3+
*/
4+
5+
/**
6+
* @template {Node} [Tree=Node]
7+
* @typedef {import('./complex-types.js').MapFunction<Tree>} MapFunction
8+
* Function called with a node, its index, and its parent to produce a new
9+
* node.
10+
*/
11+
12+
/**
13+
* Create a new tree by mapping all nodes with the given function.
14+
*
15+
* @template {Node} Tree
16+
* Type of input tree.
17+
* @param {Tree} tree
18+
* Tree to map.
19+
* @param {MapFunction<Tree>} mapFunction
20+
* Function called with a node, its index, and its parent to produce a new
21+
* node.
22+
* @returns {Tree}
23+
* New mapped tree.
24+
*/
25+
export function map(tree, mapFunction) {
26+
// @ts-expect-error Looks like a children.
27+
return preorder(tree, null, null)
28+
29+
/** @type {import('./complex-types.js').MapFunction<Tree>} */
30+
function preorder(node, index, parent) {
31+
var newNode = Object.assign({}, mapFunction(node, index, parent))
32+
33+
if ('children' in node) {
34+
// @ts-expect-error Looks like a parent.
35+
newNode.children = node.children.map(function (
36+
/** @type {import('./complex-types.js').InclusiveDescendant<Tree>} */ child,
37+
/** @type {number} */ index
38+
) {
39+
return preorder(child, index, node)
40+
})
41+
}
42+
43+
return newNode
44+
}
45+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"main": "index.js",
3232
"types": "index.d.ts",
3333
"files": [
34-
"complex-types.d.ts",
34+
"lib/",
3535
"index.d.ts",
3636
"index.js"
3737
],

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"include": ["**/*.js", "complex-types.d.ts"],
2+
"include": ["**/*.js", "lib/complex-types.d.ts"],
33
"exclude": ["coverage/", "node_modules/"],
44
"compilerOptions": {
55
"checkJs": true,

0 commit comments

Comments
 (0)
0