cleanup and refactoring...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2022-11-29 16:07:12 +03:00
parent def671ffba
commit 2e035d0b4b
4 changed files with 62 additions and 80 deletions

View File

@ -573,8 +573,6 @@ object.Constructor('BasePage', {
this.metadata =
{ order: await this.store.sort(this.path, ...cmp) }
return this },
// XXX EXPERIMENTAL -- move this to store???
// .sortAs(<name>)
// .sortAs([<path>, .. ])
sortAs: async function(order){
@ -585,7 +583,6 @@ object.Constructor('BasePage', {
return pwpath.sanitize(p) }) }
: { order: (await this.metadata)['order_'+ order] }
return this },
// XXX EXPERIMENTAL -- move this to store???
// .saveSortAs(<name>)
// .saveSortAs(<name>, [<path>, .. ])
saveSortAs: async function(name, order=null){
@ -593,7 +590,6 @@ object.Constructor('BasePage', {
?? (await this.metadata).order
this.metadata = {['order_'+ name]: order}
return this },
reverse: async function(){
// not sorting single pages...
if(this.length <= 1){
@ -744,7 +740,8 @@ function(spec, func){
// function...
func = args.pop()
// arg sepc...
;(args.length > 0 && args[args.length-1] instanceof Array)
;(args.length > 0
&& args[args.length-1] instanceof Array)
&& (func.arg_spec = args.pop())
// XXX do we need anything else like .doc, attrs???
return func }

View File

@ -478,7 +478,7 @@ module.BaseParser = {
// error...
}catch(err){
console.error(err)
yield await page.parse(
yield page.parse(
// XXX add line number and page path...
'@include("./ParseError'
+':path='
@ -495,8 +495,7 @@ module.BaseParser = {
return '%'+ c.charCodeAt().toString(16) })
.replace(/:/g, '&colon;')
.replace(/=/g, '&equals;')
+'")')
return } },
+'")') } },
// recursively resolve and enumerate the ast...
//

View File

@ -38,6 +38,7 @@ var makeDecoder = function(name, encode, chars){
return String.fromCharCode('0x'+c) }) } }
//---------------------------------------------------------------------
// Path...
@ -93,22 +94,6 @@ module = {
quote: makeEncoder('quote', 'UNENCODED_PATH'),
unquote: makeDecoder('unquote', 'quote', 'UNENCODED_PATH'),
/*/ XXX NORMCACHE...
__normalized_cache_threshold: 100,
__normalized_cache_size: 4096,
__normalized_cache: undefined,
get _normalized_cache(){
var norm = this.__normalized =
this.__normalized
?? new Set()
// trim to size...
var l = norm.size
var lim = this.__normalized_cache_size ?? 1000
var t = this.__normalized_cache_threshold ?? 100
if(l > lim){
norm = this.__normalized = new Set([...norm].slice(Math.max(l - lim - t, t))) }
return norm },
//*/
// Path utils...
//
@ -119,12 +104,6 @@ module = {
// NOTE: trailing/leading '/' are represented by '' at end/start of
// path list...
normalize: function(path='.', format='auto'){
/*/ XXX NORMCACHE...
if(typeof(path) == 'string'
&& format != 'array'
&& this._normalized_cache.has(path)){
return path }
//*/
format = format == 'auto' ?
(path instanceof Array ?
'array'
@ -170,19 +149,6 @@ module = {
&& (path.push(
path.pop()
.replace(/:*$/, '')))
/*/ XXX NORMCACHE...
var res = format == 'string' ?
// special case: root -> keep '/'
((root
&& path.length == 1
&& path[0] == '') ?
('/'+ path.join('/'))
: path.join('/'))
: path
typeof(res) == 'string'
&& this._normalized_cache.add(res)
return res },
/*/
return format == 'string' ?
// special case: root -> keep '/'
((root
@ -191,7 +157,6 @@ module = {
('/'+ path.join('/'))
: path.join('/'))
: path },
//*/
sanitize: function(path, format='auto'){
format = format == 'auto' ?
(path instanceof Array ?

View File

@ -158,6 +158,27 @@ object.Constructor('JournalDB', {
})
var awaitOrDo =
module.awaitOrDo =
function(data, func){
if(arguments.length > 2){
data = [...arguments]
func = data.pop()
var promise = data
.reduce(function(res, e){
return res
|| e instanceof Promise }, false)
return promise ?
Promise.all(data)
.then(function(res){
return func(...res) })
: func(...data)
// single data item...
} else {
return data instanceof Promise ?
data.then(func)
: func(data) } }
//---------------------------------------------------------------------
// Store...
@ -646,25 +667,6 @@ module.BaseStore = {
//
// .exists(<path>)
// -> <promise>
// -> <normalized-path>
// -> false
//
exists: function(path){
var {path, args} =
pwpath.splitArgs(
pwpath.sanitize(path, 'string'))
var test = function(paths){
return paths.includes(path) ?
pwpath.joinArgs(path, args)
: undefined }
var paths = this.paths
return paths instanceof Promise ?
paths.then(test)
: test(paths) },
//
// .sort(<pattern>, <by>, ..)
// .sort([<path>, ..], <by>, ..)
@ -827,26 +829,45 @@ module.BaseStore = {
.map(function([p]){
return p }) },
//
// .exists(<path>)
// -> <promise>
// -> <normalized-path>
// -> false
//
exists: function(path){
var {path, args} =
pwpath.splitArgs(
pwpath.sanitize(path, 'string'))
return awaitOrDo(
this.paths,
function(paths){
return paths.includes(path) ?
pwpath.joinArgs(path, args)
: undefined }) },
// find the closest existing alternative path...
find: async function(path, strict=false){
find: function(path, strict=false){
var {path, args} = pwpath.splitArgs(path)
args = pwpath.joinArgs('', args)
// build list of existing page candidates...
var names = await this.names
var pages = new Set(
pwpath.names(path)
.map(function(name){
return names[name] ?? [] })
.flat())
// select accessible candidate...
for(var p of pwpath.paths(path, !!strict)){
if(pages.has(p)){
return p+args }
p = p[0] == '/' ?
p.slice(1)
: '/'+p
if(pages.has(p)){
return p+args } } },
return awaitOrDo(
this.names,
function(names){
// build list of existing page candidates...
var pages = new Set(
pwpath.names(path)
.map(function(name){
return names[name] ?? [] })
.flat())
// select accessible candidate...
for(var p of pwpath.paths(path, !!strict)){
if(pages.has(p)){
return p+args }
p = p[0] == '/' ?
p.slice(1)
: '/'+p
if(pages.has(p)){
return p+args } } }) },
//
// Resolve page for path
// .match(<path>)