fixes and tweaks...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2019-04-28 02:38:54 +03:00
parent b8f20d5d7a
commit ec2101bf4a

View File

@ -611,7 +611,14 @@ var BaseBrowserPrototype = {
args.shift() args.shift()
: [] : []
path = path instanceof Array ? path : [path] path = path instanceof Array ? path : [path]
var options = args.pop() || {} options = args.pop() || {}
// set call context...
options = !options.root ?
Object.assign({},
options,
{root: this})
: options
// options... // options...
options = Object.assign(Object.create(this.options || {}), options) options = Object.assign(Object.create(this.options || {}), options)
@ -696,9 +703,14 @@ var BaseBrowserPrototype = {
var res = this instanceof Array ? var res = this instanceof Array ?
this this
: [this] : [this]
// XXX BUG: this depends on the length
// of the result and not on the number
// of func calls...
// ...i.e. should depend on input
// and not on output...
i += res.length i += res.length
nested = res nested = res.flat()
return res return nested
}) })
} }
@ -745,9 +757,15 @@ var BaseBrowserPrototype = {
[] []
: doNested(sublist)) ) : doNested(sublist)) )
}) })
.flat() } // XXX this here loses the length information we need in doNested(..)
// to calc indexes...
//.flat()
}
return walk(i, path, this.items) var res = walk(i, path, this.items)
return options.root === this ?
res.flat()
: res
}, },
@ -853,7 +871,8 @@ var BaseBrowserPrototype = {
// .map(func, i, path[, options]) // .map(func, i, path[, options])
// these set the "base" path and index passed to func... // these set the "base" path and index passed to func...
var args = [...arguments] var args = [...arguments]
func = args[0] instanceof Function ? func = (args[0] instanceof Function
|| args[0] === undefined) ?
args.shift() args.shift()
: undefined : undefined
options = args[args.length-1] || {} options = args[args.length-1] || {}
@ -867,6 +886,7 @@ var BaseBrowserPrototype = {
{ defaultReverse: 'flat' }) { defaultReverse: 'flat' })
: options : options
return this.walk( return this.walk(
function(i, path, elem, doNested){ function(i, path, elem, doNested){
return elem != null ? return elem != null ?
@ -905,7 +925,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() || {})
: {} : {}
// normalize the test predicate... // normalize the test predicate...
@ -917,6 +937,7 @@ var BaseBrowserPrototype = {
// XXX BUG: this for some reason matches ['B', '*'] to ['nested', 'moo'] // 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){
console.log('%%%', path, pattern)
return path.length > 0 return path.length > 0
&& pattern.length == path.length && pattern.length == path.length
&& (pattern[path.length-1] == '*' && (pattern[path.length-1] == '*'
@ -928,27 +949,23 @@ var BaseBrowserPrototype = {
&& i == pattern } ) && i == pattern } )
var Stop = new Error('Stop iteration...') return this.walk(
var res function(i, path, elem, doNested){
console.log('---', i, path)
try { if(elem && func.call(this, elem, i, path)){
this
.map(function(elem, i, path){
if(func.call(this, elem, i, path)){
res = elem res = elem
throw Stop //throw Stop
return [elem]
} }
}, options) return []
},
} catch(err){ function(_, i, path, sublist, options){
if(err === Stop){ // XXX skip paths...
return res // XXX
} return sublist.search(pattern, i, path, options)
},
throw err ...args,
} options)
return res
}, },