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,20 +942,20 @@ 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
.filter(function(e, p){
i++ i++
return (query === e return (query === e
|| ( || (
@ -964,7 +964,8 @@ var BaseBrowserPrototype = {
query == i query == i
// predicate... // predicate...
: query instanceof Function ? : query instanceof Function ?
query.call(this, e, p) // XXX revise signature...
query.call(this, e, p, i, this)
// regular expression... // regular expression...
: query instanceof RegExp ? : query instanceof RegExp ?
query.test(p.join('/')) query.test(p.join('/'))
@ -979,18 +980,52 @@ var BaseBrowserPrototype = {
&& q.test(p[i])) && q.test(p[i]))
|| q == p[i] }) || q == p[i] })
.length == p.length) .length == p.length)
// XXX add attribute queries...
: false)) }, options) }, : 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