Files
30-seconds-of-code/node_modules/unist-util-modify-children/index.js
2019-08-20 15:52:05 +02:00

36 lines
811 B
JavaScript

'use strict'
var iterate = require('array-iterate')
module.exports = modifierFactory
// Turn `callback` into a child-modifier accepting a parent. See
// `array-iterate` for more info.
function modifierFactory(callback) {
return iteratorFactory(wrapperFactory(callback))
}
// Turn `callback` into a `iterator' accepting a parent.
function iteratorFactory(callback) {
return iterator
function iterator(parent) {
var children = parent && parent.children
if (!children) {
throw new Error('Missing children in `parent` for `modifier`')
}
return iterate(children, callback, parent)
}
}
// Pass the context as the third argument to `callback`.
function wrapperFactory(callback) {
return wrapper
function wrapper(value, index) {
return callback(value, index, this)
}
}