tweaking...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2019-04-27 20:59:56 +03:00
parent e1235b2dd1
commit b8f20d5d7a

View File

@ -696,7 +696,7 @@ var BaseBrowserPrototype = {
var res = this instanceof Array ? var res = this instanceof Array ?
this this
: [this] : [this]
i += this.length i += res.length
nested = res nested = res
return res return res
}) })
@ -859,7 +859,7 @@ var BaseBrowserPrototype = {
options = args[args.length-1] || {} options = args[args.length-1] || {}
options = !(typeof(options) == typeof(123) options = !(typeof(options) == typeof(123)
|| options instanceof Array) ? || options instanceof Array) ?
args.pop() (args.pop() || {})
: {} : {}
options = !options.defaultReverse ? options = !options.defaultReverse ?
Object.assign({}, Object.assign({},
@ -887,6 +887,14 @@ var BaseBrowserPrototype = {
// XXX EXPERIMENTAL... // XXX EXPERIMENTAL...
//
// .search(index[, options])
// .search(path[, options])
// .search(func[, options])
// -> item
// -> undefined
//
//
// 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(pattern, options){ search: function(pattern, options){
@ -900,14 +908,13 @@ var BaseBrowserPrototype = {
args.pop() args.pop()
: {} : {}
// XXX better name??? // normalize the test predicate...
var allMatching = options.allMatching
var func = ( var func = (
// predicate... // predicate...
pattern instanceof Function ? pattern instanceof Function ?
pattern pattern
// path... // path...
// XXX BUG: this for some reason matches ['B', '*'] to ['nested', 'moo']
: pattern instanceof Array ? : pattern instanceof Array ?
function(elem, i, path){ function(elem, i, path){
return path.length > 0 return path.length > 0
@ -916,52 +923,32 @@ var BaseBrowserPrototype = {
|| pattern[path.length-1] == path[path.length-1]) } || pattern[path.length-1] == path[path.length-1]) }
// index... // index...
: function(elem, i, path){ : function(elem, i, path){
return i == pattern } ) return elem
&& path.length > 0
&& i == pattern } )
var Stop = new Error('Stop search exception...')
var res = [] var Stop = new Error('Stop iteration...')
var res
try { try {
res = this.walk( this
function(i, path, elem, doNested){ .map(function(elem, i, path){
// XXX returning path and/or i might be a good idea... if(func.call(this, elem, i, path)){
// predicate... res = elem
res = func.call(that, elem, i, path, that) ?
[elem]
: []
if(res.length > 0 && !allMatching){
throw Stop throw Stop
} }
}, options)
return res } catch(err){
}, if(err === Stop){
// XXX for path tests use this to narrow down the options... return res
function(_, i, path, sublist, options){
// skip mismatching paths...
// XXX this does not do the right thing...
// dialog_1.search(['B', '*'], {allMatching: true})
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,
options)
} catch(e){
// we got a result...
if(e === Stop){
return res[0]
} }
// error...
throw e throw err
} }
return allMatching ? return res
res
: res[0]
}, },