minor fixes + experimenting with search....

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2019-04-27 17:02:36 +03:00
parent 8c7f20a946
commit e1235b2dd1

View File

@ -586,6 +586,7 @@ var BaseBrowserPrototype = {
// //
// //
// XXX can we add support for partial walks, i.e. start/end ranges??? // XXX can we add support for partial walks, i.e. start/end ranges???
// XXX can this support breadth first walking???
// XXX revise protocol... // XXX revise protocol...
walk: function(func, options){ walk: function(func, options){
var that = this var that = this
@ -855,7 +856,11 @@ var BaseBrowserPrototype = {
func = args[0] instanceof Function ? func = args[0] instanceof Function ?
args.shift() args.shift()
: undefined : undefined
var options = args.pop() || {} options = args[args.length-1] || {}
options = !(typeof(options) == typeof(123)
|| options instanceof Array) ?
args.pop()
: {}
options = !options.defaultReverse ? options = !options.defaultReverse ?
Object.assign({}, Object.assign({},
options, options,
@ -884,49 +889,67 @@ var BaseBrowserPrototype = {
// XXX EXPERIMENTAL... // XXX EXPERIMENTAL...
// XXX make this a bit smarter and accept an index or a path... // XXX make this a bit smarter and accept an index or a path...
// XXX can this replace .get(..) // XXX can this replace .get(..)
search: function(func, options){ search: function(pattern, options){
var that = this var that = this
// XXX can we avoid this in every client???
var args = [...arguments] var args = [...arguments]
func = args[0] instanceof Function ? pattern = args.shift()
args.shift() options = args[args.length-1] || {}
: undefined options = !(typeof(options) == typeof(123)
var options = args.pop() || {} || options instanceof Array) ?
args.pop()
: {}
// XXX better name... // XXX better name???
var allMatching = options.allMatching var allMatching = options.allMatching
var func = (
// predicate...
pattern instanceof Function ?
pattern
// path...
: pattern instanceof Array ?
function(elem, i, path){
return path.length > 0
&& pattern.length == path.length
&& (pattern[path.length-1] == '*'
|| pattern[path.length-1] == path[path.length-1]) }
// index...
: function(elem, i, path){
return i == pattern } )
var Stop = new Error('Stop search exception...') var Stop = new Error('Stop search exception...')
var res = [] var res = []
try { try {
this.walk( res = this.walk(
function(i, path, elem, doNested){ function(i, path, elem, doNested){
// check the element and if it matches then break the search... // XXX returning path and/or i might be a good idea...
func.call(that, elem, i, path, that) ? // predicate...
// XXX returning path and i might be a good idea... res = func.call(that, elem, i, path, that) ?
res.push(elem) [elem]
: null : []
if(res.length > 0 && !allMatching){ if(res.length > 0 && !allMatching){
throw Stop throw Stop
} }
return [] return res
}, },
// XXX for path tests use this to narrow down the options... // XXX for path tests use this to narrow down the options...
function(_, i, path, sublist, options){ function(_, i, path, sublist, options){
// NOTE: this needs to call the actual func that the user // skip mismatching paths...
// gave us and not the constructed function that we // XXX this does not do the right thing...
// pass to .walk(..) above... // dialog_1.search(['B', '*'], {allMatching: true})
return sublist.search(func, i, path, options) || [] }, if(pattern instanceof Array
&& pattern[path.length-1] != '*'
&& pattern[path.length-1] != path[path.length-1]){
return []
}
return sublist.search(pattern, i, path, options) || [] },
...args, ...args,
options) options)
// nothing found...
return res
} catch(e){ } catch(e){
// we got a result... // we got a result...
if(e === Stop){ if(e === Stop){
@ -935,6 +958,10 @@ var BaseBrowserPrototype = {
// error... // error...
throw e throw e
} }
return allMatching ?
res
: res[0]
}, },