Skip to content

Commit 291738a

Browse files
committed
Refactor to move implementation to lib/
1 parent 71d21e1 commit 291738a

File tree

3 files changed

+70
-58
lines changed

3 files changed

+70
-58
lines changed

index.js

+5-58
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,11 @@
11
/**
2-
* @typedef {import('unist').Parent} UnistParent
3-
* @typedef {import('unist').Node} UnistNode
2+
* @template {import('unist').Parent} Kind
3+
* @typedef {import('./lib/index.js').Modifier<Kind>} Modifier
44
*/
55

66
/**
7-
* @template {UnistParent} Parent
8-
* @callback Modifier
9-
* Callback called for each `child` in `parent` later given to `modify`.
10-
* @param {Parent['children'][number]} node
11-
* Child of `parent`.
12-
* @param {number} index
13-
* Position of `child` in `parent`.
14-
* @param {Parent} parent
15-
* Parent node.
16-
* @returns {number|void}
17-
* Position to move to next.
7+
* @template {import('unist').Parent} Kind
8+
* @typedef {import('./lib/index.js').Modify<Kind>} Modify
189
*/
1910

20-
/**
21-
* @template {UnistParent} Parent
22-
* @callback Modify
23-
* Modify children of `parent`.
24-
* @param {Parent} node
25-
* Parent node.
26-
* @returns {void}
27-
* Nothing.
28-
*/
29-
30-
import {arrayIterate} from 'array-iterate'
31-
32-
/**
33-
* Wrap `modifier` to be called for each child in the nodes later given to
34-
* `modify`.
35-
*
36-
* @template {UnistParent} Parent
37-
* @param {Modifier<Parent>} modifier
38-
* Callback called for each `child` in `parent` later given to `modify`.
39-
* @returns {Modify<Parent>}
40-
* Modify children of `parent`.
41-
*/
42-
export function modifyChildren(modifier) {
43-
return modify
44-
45-
/** @type {Modify<UnistParent>} */
46-
function modify(parent) {
47-
if (!parent || !parent.children) {
48-
throw new Error('Missing children in `parent` for `modifier`')
49-
}
50-
51-
arrayIterate(parent.children, iteratee, parent)
52-
}
53-
54-
/**
55-
* Pass the context as the third argument to `modifier`.
56-
*
57-
* @this {Parent}
58-
* @param {UnistNode} node
59-
* @param {number} index
60-
*/
61-
function iteratee(node, index) {
62-
return modifier(node, index, this)
63-
}
64-
}
11+
export {modifyChildren} from './lib/index.js'

lib/index.js

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* @typedef {import('unist').Parent} UnistParent
3+
* @typedef {import('unist').Node} UnistNode
4+
*/
5+
6+
/**
7+
* @template {UnistParent} Parent
8+
* @callback Modifier
9+
* Callback called for each `child` in `parent` later given to `modify`.
10+
* @param {Parent['children'][number]} node
11+
* Child of `parent`.
12+
* @param {number} index
13+
* Position of `child` in `parent`.
14+
* @param {Parent} parent
15+
* Parent node.
16+
* @returns {number|void}
17+
* Position to move to next.
18+
*/
19+
20+
/**
21+
* @template {UnistParent} Parent
22+
* @callback Modify
23+
* Modify children of `parent`.
24+
* @param {Parent} node
25+
* Parent node.
26+
* @returns {void}
27+
* Nothing.
28+
*/
29+
30+
import {arrayIterate} from 'array-iterate'
31+
32+
/**
33+
* Wrap `modifier` to be called for each child in the nodes later given to
34+
* `modify`.
35+
*
36+
* @template {UnistParent} Parent
37+
* @param {Modifier<Parent>} modifier
38+
* Callback called for each `child` in `parent` later given to `modify`.
39+
* @returns {Modify<Parent>}
40+
* Modify children of `parent`.
41+
*/
42+
export function modifyChildren(modifier) {
43+
return modify
44+
45+
/** @type {Modify<UnistParent>} */
46+
function modify(parent) {
47+
if (!parent || !parent.children) {
48+
throw new Error('Missing children in `parent` for `modifier`')
49+
}
50+
51+
arrayIterate(parent.children, iteratee, parent)
52+
}
53+
54+
/**
55+
* Pass the context as the third argument to `modifier`.
56+
*
57+
* @this {Parent}
58+
* @param {UnistNode} node
59+
* @param {number} index
60+
*/
61+
function iteratee(node, index) {
62+
return modifier(node, index, this)
63+
}
64+
}

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"main": "index.js",
3030
"types": "index.d.ts",
3131
"files": [
32+
"lib/",
3233
"index.d.ts",
3334
"index.js"
3435
],

0 commit comments

Comments
 (0)