.walk2(..) mostly done...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2019-08-23 06:24:17 +03:00
parent ac89db2c83
commit 2fc4cffa85

View File

@ -1926,16 +1926,46 @@ var BaseBrowserPrototype = {
// func(elem, i, path, next(..), stop()) // func(elem, i, path, next(..), stop())
// -> res??? // -> res???
// //
//
// NOTE: when options.reverse is set to true, func(..) for a parent
// item is still called BEFORE it is called for the children but
// its return value is placed after, i.e. the func(..) call order
// is different to the result order.
//
//
// options format:
// {
// reverse: <bool>,
//
// ...
// }
//
//
// XXX add sections support...
walk2: function(func, options){ walk2: function(func, options){
var that = this var that = this
var [func, options={}, context={}] = [...arguments]
// options...
var handleReverse = function(lst){
return options.reverse ?
lst.slice().reverse()
: lst }
// stopping mechanics...
var res var res
var Stop = new Error('walk2(..): Stop walk.') var Stop = context.stop ?
var stop = function(res){ throw stop } null
: new Error('walk2(..): Stop.')
var stop = context.stop =
context.stop
|| function(r){
res = r
throw Stop }
try { try {
var map var map
return this.items return handleReverse(this.items)
.map(map = function(elem){ .map(map = function(elem){
// XXX // XXX
var i = 0 var i = 0
@ -1950,24 +1980,38 @@ var BaseBrowserPrototype = {
children = elems == null ? children = elems == null ?
[] []
: elems } : elems }
// handle item...
// XXX should func(..) be called before .children is handled
// in reverse mode???
var item =
[(elem instanceof Array || elem instanceof BaseBrowser) ?
// skip inlined block items...
[]
// XXX revise return value semantics...
// should false be treated the same as null and undefined???
: func.call(that, elem, i, path, next, stop) || []]
.flat()
return [ return [
// item... // item...
// XXX should we call func(..) on inlined sections??? ...!options.reverse ?
// i.e. when elem instanceof Array || elem instanceof BaseBrowser ??? item
// ...should this be an option??? : [],
...[(elem instanceof Array || elem instanceof BaseBrowser) ?
[]
// XXX should that be root call context or the local call context???
: func.call(that, elem, i, path, next, stop) || []].flat(),
// children... // children...
...children instanceof Array ? ...children instanceof Array ?
children handleReverse(children)
.map(map) .map(map)
.flat() .flat()
: children instanceof BaseBrowser ? : children instanceof BaseBrowser ?
// XXX add support for other handlers (???)
children children
.walk2(func, options) .walk2(func, options, context)
: [] ] }) : [],
// item (in reverse)...
...options.reverse ?
item
: [], ] })
.flat() .flat()
// handle Stop and errors... // handle Stop and errors...