added path pattern metadata + sorting of pages in a pattern

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2022-05-19 01:57:41 +03:00
parent c0cd9a4000
commit 8cc0e178b2

View File

@ -293,6 +293,7 @@ module.BaseStore = {
// pattern match * / ** // pattern match * / **
if(path.includes('*') if(path.includes('*')
|| path.includes('**')){ || path.includes('**')){
var order = (this.metadata(path) || {}).order || []
// NOTE: we are matching full paths only here so leading and // NOTE: we are matching full paths only here so leading and
// trainling '/' are optional... // trainling '/' are optional...
var pattern = new RegExp(`^\\/?${ var pattern = new RegExp(`^\\/?${
@ -302,13 +303,17 @@ module.BaseStore = {
.replace(/\*\*/g, '.+') .replace(/\*\*/g, '.+')
.replace(/\*/g, '[^\\/]+') }`) .replace(/\*/g, '[^\\/]+') }`)
return [...this.paths() return [...this.paths()
.reduce(function(res, p){ .reduce(function(res, p){
var m = p.match(pattern) // skip metadata paths...
m if(p.includes('*')){
&& (!strict return res }
|| m[0] == p) var m = p.match(pattern)
&& res.add(m[0]) m
return res }, new Set())] } && (!strict
|| m[0] == p)
&& res.add(m[0])
return res }, new Set())]
.sortAs(order) }
// search... // search...
for(var p of module.path.paths(path)){ for(var p of module.path.paths(path)){
p = this.exists(p) p = this.exists(p)
@ -350,6 +355,12 @@ module.BaseStore = {
?? ((this.next || {}).__get__ ?? ((this.next || {}).__get__
&& this.next.__get__(path))) }, && this.next.__get__(path))) },
// NOTE: setting metadata is done via .update(..)
metadata: function(path){
path = this.exists(path)
return path
&& this.__get__(path) },
// NOTE: deleting and updating only applies to explicit matching // NOTE: deleting and updating only applies to explicit matching
// paths -- no page acquisition is performed... // paths -- no page acquisition is performed...
// NOTE: edit methods are local-only... // NOTE: edit methods are local-only...
@ -357,6 +368,7 @@ module.BaseStore = {
// XXX do we copy the data here or modify it???? // XXX do we copy the data here or modify it????
// XXX BUG: for path '/' this adds an entry at '', but when getting // XXX BUG: for path '/' this adds an entry at '', but when getting
// '/', the later is not found... // '/', the later is not found...
// XXX BUG: updating a pattern path will result in a broken store...
__update__: function(key, data, mode='update'){ __update__: function(key, data, mode='update'){
this.data[key] = data this.data[key] = data
return this }, return this },
@ -691,6 +703,7 @@ object.Constructor('BasePage', {
// XXX should writing to this move the page??? // XXX should writing to this move the page???
//set dir(value){ }, //set dir(value){ },
// history... // history...
// //
//* XXX HISTORY... //* XXX HISTORY...
@ -738,6 +751,15 @@ object.Constructor('BasePage', {
set data(value){ set data(value){
this.store.update(this.location, value) }, this.store.update(this.location, value) },
// metadata...
//
// NOTE: in the general case this is the same as .data but in also allows
// storing of data (metadata) for pattern paths...
get metadata(){
return this.store.metadata(this.location) },
set metadata(value){
this.store.update(this.location, value) },
// number of matching pages... // number of matching pages...
get length(){ get length(){
var p = this.match(this.location) var p = this.match(this.location)
@ -778,6 +800,26 @@ object.Constructor('BasePage', {
reduce: function(func, dfl){ reduce: function(func, dfl){
return this.each().reduce(func, dfl) }, return this.each().reduce(func, dfl) },
// sorting...
//
sort: function(cmp){
// not sorting single pages...
if(this.length <= 1){
return this }
// sort...
this.metadata =
{ order: this.each()
.sort(...arguments)
.map(function(p){
return p.path }) }
return this },
reverse: function(){
// not sorting single pages...
if(this.length <= 1){
return this }
this.metadata = { order: this.match().reverse() }
return this },
// //
// Clone a page optionally asigning data into it... // Clone a page optionally asigning data into it...
// .clone() // .clone()
@ -1784,6 +1826,14 @@ object.Constructor('Page', BasePage, {
// page parser... // page parser...
// //
/* XXX do we actually need this???
__macroParseArgs: function(args, skip, state){
for(var [key, value] of Object.entries(args)){
if(skip.includes(skip)){
continue }
typeof(value) == 'string'
&& args[key] = this.parse(value, state) } },
//*/
__parser__: module.parser, __parser__: module.parser,
parse: function(text, state){ parse: function(text, state){
// .parser(<state>) // .parser(<state>)
@ -2028,6 +2078,10 @@ pwiki
location: '/test/c/y', location: '/test/c/y',
text: 'y', text: 'y',
}) })
.update({
location: '/test/d/z',
text: 'z',
})
//*/ //*/