36 lines
811 B
JavaScript
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)
|
|
}
|
|
}
|