cleanup...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2019-05-07 19:09:05 +03:00
parent 3e264b88c1
commit 99eba83adf

View File

@ -845,7 +845,9 @@ var BaseBrowserPrototype = {
// XXX passing both index directly and context containing index // XXX passing both index directly and context containing index
// (context.index) feels excessive... // (context.index) feels excessive...
// ...if this can produce errors we need to simplify... // ...if this can produce errors we need to simplify...
// XXX add docs about maintaining context to implement/extend walkers... // XXX add docs:
// - maintaining context to implement/extend walkers...
// - correctly stopping recursive calls (call root stop(..))
// XXX can this be simpler??? // XXX can this be simpler???
walk2: function(func, recursion, walkable, options){ walk2: function(func, recursion, walkable, options){
var that = this var that = this
@ -876,13 +878,10 @@ var BaseBrowserPrototype = {
: null : null
options = args.shift() || {} options = args.shift() || {}
// build context... // get/build context...
var context = args.shift() var context = args.shift()
var path = context instanceof Array ?
context
: ((context && context.path) || [])
context = context instanceof Array ? context = context instanceof Array ?
{path: path} { path: context }
: (context || {}) : (context || {})
context.root = context.root || this context.root = context.root || this
context.index = context.index || 0 context.index = context.index || 0
@ -908,20 +907,18 @@ var BaseBrowserPrototype = {
return walk( return walk(
function(state, node, next, stop){ function(state, node, next, stop){
// stop all instances... // keep only the root stop(..) -> stop the entire call tree...
//stop = context.stop = stop stop = context.stop = context.stop || stop
// skip non-iterable items... // skip non-iterable items...
if(!iterateNonIterable && node.noniterable){ if(!iterateNonIterable && node.noniterable){
return state return state }
}
var nested = false var nested = false
var doNested = function(list){ var doNested = function(list){
// this can be called only once... // this can be called only once -> return cached results...
if(nested !== false){ if(nested !== false){
return nested return nested }
}
// normalize... // normalize...
list = (list === true || list == null) ? list = (list === true || list == null) ?
@ -939,11 +936,11 @@ var BaseBrowserPrototype = {
[formArgs] [formArgs]
: []), : []),
walkable, walkable,
options, options, context) }
context) }
return (list === false ? return (list === false ?
[] []
// handle arrays internally...
: list instanceof Array ? : list instanceof Array ?
// NOTE: this gets the path and i from context... // NOTE: this gets the path and i from context...
next('do', state, next('do', state,
@ -964,26 +961,22 @@ var BaseBrowserPrototype = {
list, context.index, p, list, context.index, p,
options, context, options, context,
func, useWalk) || [])) func, useWalk) || []))
// .walk2(..)
: useWalk()) : useWalk())
// normalize and merge to state...
.run(function(){ .run(function(){
// normalize...
nested = this instanceof Array ? nested = this instanceof Array ?
this this
: [this] : [this]
// merge recursion results into states... // merge...
// NOTE: since we pass on the context we do
// not really need to update the index
// here...
!(list === false || list instanceof Array) !(list === false || list instanceof Array)
&& state.splice(state.length, 0, ...nested) && state.splice(state.length, 0, ...nested)
return nested return nested
}) }) }
}
// prepare context... // prepare context...
var id = node.id || node.value var id = node.id || node.value
path = context.path = context.path || path var path = context.path = context.path || []
var [inline, p, children] = var [inline, p, children] =
// inline... // inline...
isWalkable(node) ? isWalkable(node) ?
@ -991,7 +984,7 @@ var BaseBrowserPrototype = {
// nested... // nested...
: (!skipNested && isWalkable(node.children)) ? : (!skipNested && isWalkable(node.children)) ?
[false, [false,
// prepare context for nested items... // update context for nested items...
path.push(id) path.push(id)
&& path, && path,
node.children] node.children]
@ -1002,9 +995,9 @@ var BaseBrowserPrototype = {
reverse == 'flat' reverse == 'flat'
&& children && children
&& doNested() && doNested()
// element... // do element...
state.splice(state.length, 0, state.splice(state.length, 0,
...[ func ? ...( func ?
(func.call(that, (func.call(that,
...(inline ? ...(inline ?
[null, context.index] [null, context.index]
@ -1013,8 +1006,11 @@ var BaseBrowserPrototype = {
doNested, doNested,
stop, stop,
children) || []) children) || [])
: [node] ]) : [node] ))
// normal order -> do children... // normal order -> do children...
// NOTE: doNested(..) is executed only once so in reverse
// the call will have no effect, but we need to
// update the context...
children children
&& (doNested(), && (doNested(),
// restore path context... // restore path context...
@ -1022,23 +1018,13 @@ var BaseBrowserPrototype = {
return state return state
}, },
// normalize the root result...
function(state, mode){
// if we stopped, thread the stop up...
mode == 'stopped'
&& context.root !== that
&& context.stop instanceof Function
&& context.stop(state)
// normalize the result...
return (context.root === that
&& state instanceof Array) ?
state.flat()
: state },
[], [],
// input items...
...(reverse ? ...(reverse ?
this.items.slice().reverse() this.items
: this.items)) .slice()
}, .reverse()
: this.items)) },
// Text render... // Text render...