-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
50 lines (45 loc) · 1.02 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
* @typedef {import('unist').Parent} Parent
* @typedef {import('unist').Node} Node
*
* @callback Modifier
* @param {Node} node
* @param {number} index
* @param {Parent} parent
* @returns {number|void}
*
* @callback Modify
* @param {Parent} node
* @returns {void}
*/
import {arrayIterate} from 'array-iterate'
/**
* Turn `callback` into a child-modifier accepting a parent.
* See `array-iterate` for more info.
*
* @param {Modifier} callback
* @returns {Modify}
*/
export function modifyChildren(callback) {
return iterator
/**
* @param {Parent} parent
* @returns {void}
*/
function iterator(parent) {
if (!parent || !parent.children) {
throw new Error('Missing children in `parent` for `modifier`')
}
arrayIterate(parent.children, iteratee, parent)
}
/**
* Pass the context as the third argument to `callback`.
*
* @this {Parent}
* @param {Node} node
* @param {number} index
*/
function iteratee(node, index) {
return callback(node, index, this)
}
}