added .indexOf(..) and .pathOf(..), some tweaking and refactoring...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2019-03-20 01:50:48 +03:00
parent 7e4d976b2a
commit cc90252c3f

View File

@ -925,15 +925,15 @@ var BaseBrowserPrototype = {
}, },
// //
// .find(id) // .find(id[, options])
// .find(index) // .find(index[, options])
// .find(path) // .find(path[, options])
// .find(query) // .find(func[, options])
// -> list // -> list
// //
// XXX support browser.js query syntax...
// XXX add '**' patterns... // XXX add '**' patterns...
// XXX add support for path item patterns -- 'a*/*b' // XXX should this return item paths???
// ...one way to do this is to return an object instead of a list...
find: function(query, options){ find: function(query, options){
query = typeof(query) == typeof('str') ? query = typeof(query) == typeof('str') ?
query.split(/[\\\/]/g) query.split(/[\\\/]/g)
@ -942,55 +942,90 @@ var BaseBrowserPrototype = {
query = typeof(query) == typeof('str') ? query = typeof(query) == typeof('str') ?
[query] [query]
: query : query
// XXX not sure about this... query = query instanceof Array ?
//query = query instanceof Array ? query
// query .map(function(d){
// .map(function(d){ return d == '*' ?
// return d == '*' ? d
// d : d.indexOf('*') >= 0 ?
// : d.indexOf('*') > 0 ? new RegExp(d
// new RegExp(d .replace(/\*/g, '.*'))
// .replace(/\*/g, '.*')) : d})
// : d}) : query
// : query
var i = -1 var i = -1
return this.filter(function(e, p){ return this
i++ .filter(function(e, p){
return (query === e i++
|| ( return (query === e
// index... || (
typeof(query) == typeof(123) ? // index...
query == i typeof(query) == typeof(123) ?
// predicate... query == i
: query instanceof Function ? // predicate...
query.call(this, e, p) : query instanceof Function ?
// regular expression... // XXX revise signature...
: query instanceof RegExp ? query.call(this, e, p, i, this)
query.test(p.join('/')) // regular expression...
// direct path comparison... : query instanceof RegExp ?
: query instanceof Array ? query.test(p.join('/'))
query.cmp(p) // direct path comparison...
|| (query.length == p.length : query instanceof Array ?
&& query query.cmp(p)
.filter(function(q, i){ || (query.length == p.length
return q == '*' && query
|| (q instanceof RegExp .filter(function(q, i){
&& q.test(p[i])) return q == '*'
|| q == p[i] }) || (q instanceof RegExp
.length == p.length) && q.test(p[i]))
// XXX add attribute queries... || q == p[i] })
: false)) }, options) }, .length == p.length)
: false)) }, options) },
// XXX move this to a more logical spot... // XXX use cache for these...
// XXX should also take path... // XXX move these to a more logical spot...
indexOf: function(item){ // XXX these are almost identical -- reuse???
// XXX indexOf: function(item, options){
item = typeof(item) == typeof('str') ?
item.split(/[\\\/]/g)
: item
var Stop = new Error('.indexOf(..): Result found exception.')
var i = 0
try{
this.map(function(e, p){
if(item instanceof Array ? item.cmp(p) : (item === e)){
throw Stop }
i++
}, options)
} catch(e){
if(e === Stop){
return i
}
}
return -1
}, },
// XXX move this to a more logical spot... pathOf: function(item, options){
// XXX should also take index... var Stop = new Error('.pathOf(..): Result found exception.')
pathOf: function(item){
// XXX var path
var i = 0
try{
this.map(function(e, p){
path = p
if(typeof(item) == typeof(123) ? item == i : (item === e)){
throw Stop }
i++
}, options)
} catch(e){
if(e === Stop){
return path
}
}
return undefined
}, },
// XXX support: up/down/left/right/first/last/next/prev // XXX support: up/down/left/right/first/last/next/prev