mirror of
https://github.com/flynx/ImageGrid.git
synced 2025-10-29 18:30:09 +00:00
expanded .search(..), still not final...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
0e21955601
commit
5f740adc2a
@ -917,11 +917,11 @@ var BaseBrowserPrototype = {
|
|||||||
// -> undefined
|
// -> undefined
|
||||||
//
|
//
|
||||||
//
|
//
|
||||||
// XXX make this a bit smarter and accept an index or a path...
|
|
||||||
// XXX can this replace .get(..)
|
// XXX can this replace .get(..)
|
||||||
|
// XXX do we actually need to stop this as soon as we find something,
|
||||||
|
// i.e. options.firstOnly???
|
||||||
search: function(pattern, options){
|
search: function(pattern, options){
|
||||||
var that = this
|
var that = this
|
||||||
|
|
||||||
var args = [...arguments]
|
var args = [...arguments]
|
||||||
pattern = args.shift()
|
pattern = args.shift()
|
||||||
options = args[args.length-1] || {}
|
options = args[args.length-1] || {}
|
||||||
@ -930,48 +930,86 @@ var BaseBrowserPrototype = {
|
|||||||
(args.pop() || {})
|
(args.pop() || {})
|
||||||
: {}
|
: {}
|
||||||
|
|
||||||
// normalize the test predicate...
|
// handle pattern keywords...
|
||||||
// XXX add support for regex...
|
pattern = pattern == 'first' ?
|
||||||
|
0
|
||||||
|
: pattern == 'last' ?
|
||||||
|
-1
|
||||||
|
: pattern
|
||||||
|
|
||||||
|
// normalize negative index...
|
||||||
|
if(typeof(pattern) == typeof(123) && pattern < 0){
|
||||||
|
pattern = -pattern - 1
|
||||||
|
options.reverse = 'flat'
|
||||||
|
}
|
||||||
|
|
||||||
|
// normalize/build the test predicate...
|
||||||
var func = (
|
var func = (
|
||||||
// predicate...
|
// predicate...
|
||||||
pattern instanceof Function ?
|
pattern instanceof Function ?
|
||||||
pattern
|
pattern
|
||||||
|
// regexp...
|
||||||
|
: pattern instanceof RegExp ?
|
||||||
|
function(elem, i, path){
|
||||||
|
return pattern.test(elem.value)
|
||||||
|
|| pattern.test('/'+ path.join('/')) }
|
||||||
// path...
|
// path...
|
||||||
// XXX add support for regex and glob...
|
|
||||||
: pattern instanceof Array ?
|
: pattern instanceof Array ?
|
||||||
function(elem, i, path){
|
function(elem, i, path){
|
||||||
return path.length > 0
|
return path.length > 0
|
||||||
&& pattern.length == path.length
|
&& pattern.length == path.length
|
||||||
&& !pattern.reduce(function(res, e, i){
|
&& !pattern
|
||||||
return res || !(e == '*' || e == path[i]) }, false) }
|
// XXX add support for '**' ???
|
||||||
|
.reduce(function(res, e, i){
|
||||||
|
return res || !(
|
||||||
|
e == '*'
|
||||||
|
|| (e instanceof RegExp
|
||||||
|
&& e.test(path[i]))
|
||||||
|
|| e == path[i]) }, false) }
|
||||||
// index...
|
// index...
|
||||||
: function(elem, i, path){
|
: typeof(pattern) == typeof(123) ?
|
||||||
|
function(elem, i, path){
|
||||||
return elem
|
return elem
|
||||||
&& path.length > 0
|
&& path.length > 0
|
||||||
&& i == pattern } )
|
&& i == pattern }
|
||||||
|
// object query...
|
||||||
|
: function(elem){
|
||||||
|
return Object.entries(pattern)
|
||||||
|
.reduce(function(res, [key, pattern]){
|
||||||
|
return res
|
||||||
|
&& (elem[key] == pattern
|
||||||
|
// bool...
|
||||||
|
|| ((pattern === true || pattern === false)
|
||||||
|
&& pattern === !!elem[key])
|
||||||
|
// predicate...
|
||||||
|
|| (pattern instanceof Function
|
||||||
|
&& pattern.call(that, elem[key]))
|
||||||
|
// regexp...
|
||||||
|
|| (pattern instanceof RegExp
|
||||||
|
&& pattern.test(elem[key]))
|
||||||
|
// type...
|
||||||
|
// XXX problem, we can't distinguish this
|
||||||
|
// and a predicate...
|
||||||
|
// ...so for now use:
|
||||||
|
// .search(v => v instanceof Array)
|
||||||
|
//|| (typeof(pattern) == typeof({})
|
||||||
|
// && pattern instanceof Function
|
||||||
|
// && elem[key] instanceof pattern)
|
||||||
|
) }, true) } )
|
||||||
|
|
||||||
return this.walk(
|
return this.walk(
|
||||||
function(i, path, elem, doNested){
|
function(i, path, elem, doNested){
|
||||||
|
// match...
|
||||||
|
// XXX should this use that???
|
||||||
if(elem && func.call(this, elem, i, path)){
|
if(elem && func.call(this, elem, i, path)){
|
||||||
// XXX is this the right output format???
|
return [[
|
||||||
return [[elem, i, path]]
|
elem,
|
||||||
|
i,
|
||||||
|
path,
|
||||||
|
]]
|
||||||
}
|
}
|
||||||
return []
|
return []
|
||||||
},
|
},
|
||||||
/* XXX
|
|
||||||
function(_, i, path, sublist, options){
|
|
||||||
// XXX does not work yet...
|
|
||||||
//// skip paths that do not match...
|
|
||||||
//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)
|
options)
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user