mirror of
https://github.com/flynx/pWiki.git
synced 2025-12-20 18:11:41 +00:00
cleanup, refactoring and docs...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
126962e569
commit
f66e03be90
85
pwiki2.js
85
pwiki2.js
@ -127,6 +127,7 @@ module.store = {
|
|||||||
exists: function(path){
|
exists: function(path){
|
||||||
return pWikiPath.normalize(path, 'string') in this },
|
return pWikiPath.normalize(path, 'string') in this },
|
||||||
|
|
||||||
|
// NOTE: a path is any attribute that contains '/'...
|
||||||
paths: function(){
|
paths: function(){
|
||||||
return Object.keys(this)
|
return Object.keys(this)
|
||||||
.filter(function(p){
|
.filter(function(p){
|
||||||
@ -139,26 +140,44 @@ module.store = {
|
|||||||
.map(function(p){
|
.map(function(p){
|
||||||
return [p, that[p]] }) },
|
return [p, that[p]] }) },
|
||||||
|
|
||||||
// XXX BUG: '/a*/' does not match '/a/b/...' -- need to replace
|
//
|
||||||
// pattern + with * for partial patterns...
|
// Resolve page for path
|
||||||
// XXX BUG: '*' and '**' seem to produce identical results...
|
// .match(<path>)
|
||||||
match: function(path){
|
// -> <path>
|
||||||
|
//
|
||||||
|
// Match paths (non-strict mode)
|
||||||
|
// .match(<pattern>)
|
||||||
|
// .match(<pattern>, false)
|
||||||
|
// -> [<path>, ...]
|
||||||
|
//
|
||||||
|
// Match pages (paths in strict mode)
|
||||||
|
// .match(<pattern>, true)
|
||||||
|
// -> [<path>, ...]
|
||||||
|
//
|
||||||
|
// In strict mode the trailing star in the pattern will only match
|
||||||
|
// actual existing pages, while in non-strict mode the pattern will
|
||||||
|
// match all sub-paths.
|
||||||
|
//
|
||||||
|
match: function(path, strict=false){
|
||||||
// pattern match * / **
|
// pattern match * / **
|
||||||
if(path.includes('*')
|
if(path.includes('*')
|
||||||
|| path.includes('**')){
|
|| path.includes('**')){
|
||||||
// XXX LEADING_SLASH
|
|
||||||
var pattern = new RegExp(`^\\/?${
|
var pattern = new RegExp(`^\\/?${
|
||||||
pWikiPath.normalize(path, 'string')
|
pWikiPath.normalize(path, 'string')
|
||||||
.replace(/\//, '\\/')
|
.replace(/\/$/g, '')
|
||||||
.replace(/\*\*/, '.+')
|
.replace(/\//g, '\\/')
|
||||||
.replace(/\*/, '[^\\/]+') }`)
|
.replace(/\*\*/g, '.+')
|
||||||
return this.paths()
|
.replace(/\*/g, '[^\\/]+') }`)
|
||||||
.filter(function(p){
|
return [...this.paths()
|
||||||
return pattern.test(p)}) }
|
.reduce(function(res, p){
|
||||||
|
var m = p.match(pattern)
|
||||||
|
m
|
||||||
|
&& (!strict
|
||||||
|
|| m[0] == p)
|
||||||
|
&& res.add(m[0])
|
||||||
|
return res }, new Set())] }
|
||||||
// search...
|
// search...
|
||||||
for(var p of pWikiPath.paths(path)){
|
for(var p of pWikiPath.paths(path)){
|
||||||
// XXX LEADING_SLASH
|
|
||||||
//if(p in this){
|
|
||||||
if(p in this
|
if(p in this
|
||||||
// NOTE: all paths at this point and in store are
|
// NOTE: all paths at this point and in store are
|
||||||
// absolute, so we check both with the leading '/'
|
// absolute, so we check both with the leading '/'
|
||||||
@ -168,34 +187,58 @@ module.store = {
|
|||||||
p.slice(1) in this
|
p.slice(1) in this
|
||||||
: ('/'+ p) in store)){
|
: ('/'+ p) in store)){
|
||||||
return p } } },
|
return p } } },
|
||||||
|
//
|
||||||
|
// Resolve page
|
||||||
|
// .get(<path>)
|
||||||
|
// -> <value>
|
||||||
|
//
|
||||||
|
// Resolve pages (non-strict mode)
|
||||||
|
// .get(<pattern>)
|
||||||
|
// .get(<pattern>, false)
|
||||||
|
// -> [<value>, .. ]
|
||||||
|
//
|
||||||
|
// Get pages (strict mode)
|
||||||
|
// .get(<pattern>, true)
|
||||||
|
// -> [<value>, .. ]
|
||||||
|
//
|
||||||
|
// In strict mode this will not try to resolve pages and will not
|
||||||
|
// return pages at paths that do not explicitly exist.
|
||||||
|
//
|
||||||
// XXX should this call actions???
|
// XXX should this call actions???
|
||||||
get: function(path){
|
get: function(path, strict=false){
|
||||||
var that = this
|
var that = this
|
||||||
path = this.match(path)
|
path = this.match(path, strict)
|
||||||
return path instanceof Array ?
|
return path instanceof Array ?
|
||||||
path.map(function(p){
|
path.map(function(p){
|
||||||
return that[p] })
|
return that[p]
|
||||||
|
?? that[that.match(p)] })
|
||||||
: this[path] },
|
: this[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...
|
||||||
|
//
|
||||||
// XXX should these return this or the data???
|
// XXX should these return this or the data???
|
||||||
update: function(path, data, mode='update'){
|
update: function(path, data, mode='update'){
|
||||||
path = pWikiPath.normalize(path, 'string')
|
path = pWikiPath.normalize('/'+ path, 'string')
|
||||||
path = path[path.length-1] == '/' ?
|
path = path[path.length-1] == '/' ?
|
||||||
path.slice(0, -1)
|
path.slice(0, -1)
|
||||||
: path
|
: path
|
||||||
data = mode == 'update' ?
|
this[path] =
|
||||||
Object.assign(this[path] || {}, data)
|
mode == 'update' ?
|
||||||
: data
|
Object.assign(
|
||||||
this[path] = data
|
this[path] ?? {},
|
||||||
|
data)
|
||||||
|
: data
|
||||||
return this },
|
return this },
|
||||||
|
// XXX revise...
|
||||||
delete: function(path){
|
delete: function(path){
|
||||||
path = pWikiPath.normalize(path, 'string')
|
path = pWikiPath.normalize(path, 'string')
|
||||||
path = path[path.length-1] == '/' ?
|
path = path[path.length-1] == '/' ?
|
||||||
path.slice(0, -1)
|
path.slice(0, -1)
|
||||||
: path
|
: path
|
||||||
|
// XXX revise...
|
||||||
delete this[path]
|
delete this[path]
|
||||||
|
delete this['/'+ path]
|
||||||
return this },
|
return this },
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user