refactoring...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2021-01-19 03:28:29 +03:00
parent a6dac37f01
commit cd2dabac38
2 changed files with 49 additions and 126 deletions

View File

@ -2797,6 +2797,7 @@ module.BaseData =
var DataWithTagsPrototype = { var DataWithTagsPrototype = {
__proto__: DataPrototype, __proto__: DataPrototype,
//__tags: null,
get tags(){ get tags(){
return (this.__tags = this.__tags || new tags.Tags()) }, return (this.__tags = this.__tags || new tags.Tags()) },
set tags(value){ set tags(value){

View File

@ -425,32 +425,21 @@ var BaseTagsClassPrototype = {
var brace = function(code, b){ var brace = function(code, b){
var res = [] var res = []
while(code.length > 0){ while(code.length > 0){
var c = code.shift() var c = code.shift()
if(c == '[' || c == '('){ if(c == '[' || c == '('){
res.push( brace(code, c == '[' ? ']' : ')') ) res.push( brace(code, c == '[' ? ']' : ')') )
} else if(c == b){ } else if(c == b){
return res return res
} else if(c == ']' || c == ')'){ } else if(c == ']' || c == ')'){
throw new SyntaxError(`.parseQuery(..): Unexpected "${c}".`) throw new SyntaxError(`.parseQuery(..): Unexpected "${c}".`)
} else { } else {
res.push(c) res.push(c) } }
}
}
if(b != null){ if(b != null){
throw new SyntaxError(`.parseQuery(..): Expecting "${b}" got end of query.`) throw new SyntaxError(
} `.parseQuery(..): Expecting "${b}" got end of query.`) }
return res }
return res return brace(query) },
}
return brace(query)
},
} }
@ -645,15 +634,13 @@ var BaseTagsPrototype = {
a = arguments.length == 0 ? '*' : a a = arguments.length == 0 ? '*' : a
if(b instanceof Function){ if(b instanceof Function){
cmp = b cmp = b
b = null b = null }
}
if(typeof(b) == typeof(true)){ if(typeof(b) == typeof(true)){
no_definitions = b no_definitions = b
b = null b = null
} else if(typeof(cmp) == typeof(true)){ } else if(typeof(cmp) == typeof(true)){
no_definitions = cmp no_definitions = cmp
cmp = null cmp = null }
}
b = b instanceof Set ? [...b] : b b = b instanceof Set ? [...b] : b
// no given tags or multiple tags -> filter... // no given tags or multiple tags -> filter...
@ -758,9 +745,7 @@ var BaseTagsPrototype = {
a.slice(1) a.slice(1)
: a : a
}, sa) }, sa)
.length == 0) .length == 0) } },
}
},
// Match tags... // Match tags...
// //
// This is the same as .directMatch(..) but also uses persistent // This is the same as .directMatch(..) but also uses persistent
@ -832,8 +817,7 @@ var BaseTagsPrototype = {
return paths(tag) return paths(tag)
.reduce(function(res, path){ .reduce(function(res, path){
if(res){ if(res){
return res return res }
}
path = that.splitPath(path) path = that.splitPath(path)
// restrict direction... // restrict direction...
@ -869,8 +853,7 @@ var BaseTagsPrototype = {
res res
// if a is a path then it must exist and search it's tail... // if a is a path then it must exist and search it's tail...
: this.directMatch(a).length > 0 : this.directMatch(a).length > 0
&& search(this.splitPath(a).pop(), b) && search(this.splitPath(a).pop(), b) },
},
// Search tags... // Search tags...
// //
// Search the tags... // Search the tags...
@ -1032,7 +1015,6 @@ var BaseTagsPrototype = {
// //
tags: function(value, ...tags){ tags: function(value, ...tags){
var that = this var that = this
// check if value is tagged by tags.., // check if value is tagged by tags..,
if(value && tags.length > 0){ if(value && tags.length > 0){
tags = normalizeSplit(tags) tags = normalizeSplit(tags)
@ -1040,9 +1022,7 @@ var BaseTagsPrototype = {
while(tags.length > 0){ while(tags.length > 0){
var t = tags.shift() var t = tags.shift()
if(!u.has(t) && this.match(t, u).length == 0){ if(!u.has(t) && this.match(t, u).length == 0){
return false return false } }
}
}
return true return true
// get tags of specific value... // get tags of specific value...
@ -1058,9 +1038,7 @@ var BaseTagsPrototype = {
return Object.keys(this.__index || {}) return Object.keys(this.__index || {})
//.concat([...(this.persistentAll || [])]) //.concat([...(this.persistentAll || [])])
.concat([...(this.persistent || [])]) .concat([...(this.persistent || [])])
.unique() .unique() } },
}
},
// Same as .tags(..) but returns a list of single tags... // Same as .tags(..) but returns a list of single tags...
singleTags: function(value, ...tags){ singleTags: function(value, ...tags){
return this.subTags(this.tags(...arguments)).unique() }, return this.subTags(this.tags(...arguments)).unique() },
@ -1154,9 +1132,7 @@ var BaseTagsPrototype = {
index[tag] = tag in index ? index[tag] = tag in index ?
index[tag].unite(value) index[tag].unite(value)
: new Set(value) }) : new Set(value) })
return this },
return this
},
// Remove tags... // Remove tags...
// //
// Remove tags... // Remove tags...
@ -1234,8 +1210,7 @@ var BaseTagsPrototype = {
} else { } else {
index[tag] = s index[tag] = s
} }
}), this) }), this) },
},
// //
// Toggle tag for each values... // Toggle tag for each values...
// .toggle(tag, value) // .toggle(tag, value)
@ -1281,8 +1256,8 @@ var BaseTagsPrototype = {
// can't set pattern as tag... // can't set pattern as tag...
if(pattern && action == 'on'){ if(pattern && action == 'on'){
throw new TypeError(`.toggle(..): will not toggle on "${tag}": pattern and not a tag.`) throw new TypeError(
} `.toggle(..): will not toggle on "${tag}": pattern and not a tag.`) }
return action == 'on' ? return action == 'on' ?
this.tag(tag, values) this.tag(tag, values)
@ -1320,8 +1295,7 @@ var BaseTagsPrototype = {
this.untag.length > 0 this.untag.length > 0
&& that.untag(tag, this.untag) && that.untag(tag, this.untag)
return this.res return this.res }) },
}) },
// Replace tags... // Replace tags...
// //
@ -1382,8 +1356,7 @@ var BaseTagsPrototype = {
if(tag instanceof Function){ if(tag instanceof Function){
to = tag to = tag
tag = '*' tag = '*' }
}
to = to instanceof Function ? to = to instanceof Function ?
to to
: this.normalize(to) : this.normalize(to)
@ -1399,42 +1372,33 @@ var BaseTagsPrototype = {
// no change to tag... // no change to tag...
if(tag == target || target == undefined){ if(tag == target || target == undefined){
return [tag] return [tag] }
}
if(!local){ if(!local){
// persistent... // persistent...
if(persistent.has(tag)){ if(persistent.has(tag)){
persistent.delete(tag) persistent.delete(tag)
target != '' target != ''
&& persistent.add(target) && persistent.add(target) }
}
// index... // index...
if(tag in index){ if(tag in index){
target != '' target != ''
&& (index[target] = index[tag].unite(index[target] || [])) && (index[target] = index[tag].unite(index[target] || []))
delete index[tag] delete index[tag] }
}
// definitions (key)... // definitions (key)...
if(tag in definitions){ if(tag in definitions){
target != '' target != ''
&& that.define(target, definitions[tag].join(that.SET_SEPARATOR)) && that.define(target, definitions[tag].join(that.SET_SEPARATOR))
delete definitions[tag] delete definitions[tag] }
}
// definitions (value)... // definitions (value)...
if(def_index.has(tag)){ if(def_index.has(tag)){
def_index.get(tag) def_index.get(tag)
.forEach(function(key){ .forEach(function(key){
that.define(key, target == '' ? null : target) }) that.define(key, target == '' ? null : target) }) } }
}
}
return target == '' ? return target == '' ?
[] []
: [target] : [target] }
}
// do the processing... // do the processing...
var res = local ? var res = local ?
@ -1463,8 +1427,7 @@ var BaseTagsPrototype = {
return local ? return local ?
res res
: this : this },
},
// Rename a tag... // Rename a tag...
// //
// Rename tag... // Rename tag...
@ -1486,7 +1449,8 @@ var BaseTagsPrototype = {
// XXX should we be more pedantic here??? // XXX should we be more pedantic here???
tag = this.normalize(tag) tag = this.normalize(tag)
if(tag == ''){ if(tag == ''){
throw new Error(`.rename(..): first argument can not be an empty string.`) } throw new Error(
`.rename(..): first argument can not be an empty string.`) }
if(/[:\\\/]/.test(tag)){ if(/[:\\\/]/.test(tag)){
throw new Error( throw new Error(
`.rename(..): only support singular tag renaming, got: "${tag}"`) } `.rename(..): only support singular tag renaming, got: "${tag}"`) }
@ -1509,8 +1473,7 @@ var BaseTagsPrototype = {
return this.replace(tag, return this.replace(tag,
function(from){ function(from){
return from.replace(pattern, target) }, ...tags) return from.replace(pattern, target) }, ...tags) },
},
// NOTE: this is a short hand to .rename(tag, '', ..) for extra // NOTE: this is a short hand to .rename(tag, '', ..) for extra
// docs see that... // docs see that...
removeTag: function(tag, ...tags){ removeTag: function(tag, ...tags){
@ -1539,8 +1502,7 @@ var BaseTagsPrototype = {
Object.entries(this.__index || {}) Object.entries(this.__index || {})
.forEach(function(e){ .forEach(function(e){
this.__index[e[0]] = e[1].intersect(values) }) this.__index[e[0]] = e[1].intersect(values) })
return this return this },
},
// Remove the given values... // Remove the given values...
// //
// .remove(value, ..) // .remove(value, ..)
@ -1552,8 +1514,7 @@ var BaseTagsPrototype = {
Object.entries(this.__index || {}) Object.entries(this.__index || {})
.forEach(function(e){ .forEach(function(e){
this.__index[e[0]] = e[1].subtract(values) }) this.__index[e[0]] = e[1].subtract(values) })
return this return this },
},
// Get/set/remove tag definitions... // Get/set/remove tag definitions...
// //
@ -1614,20 +1575,17 @@ var BaseTagsPrototype = {
: Object.entries(tag)) : Object.entries(tag))
.forEach(function(e){ .forEach(function(e){
that.define(...e) }) that.define(...e) })
return this return this }
}
tag = this.normalize(tag) tag = this.normalize(tag)
if(/[:\\\/]/.test(tag)){ if(/[:\\\/]/.test(tag)){
throw new Error(`.alias(..): tag must be a single tag, got: "${tag}`) } throw new Error(`.alias(..): tag must be a single tag, got: "${tag}`) }
// get/resolve... // get/resolve...
if(arguments.length == 1){ if(arguments.length == 1){
// NOTE: we expect there to be only one definition... // NOTE: we expect there to be only one definition...
return this.match(tag +PS, [...Object.keys(definitions) || []]) return this.match(tag +PS, [...Object.keys(definitions) || []])
.map(function(d){ .map(function(d){
return definitions[d].join(SP) })[0] return definitions[d].join(SP) })[0]
// remove... // remove...
} else if(value == null){ } else if(value == null){
// delete empty .definitions // delete empty .definitions
@ -1637,7 +1595,6 @@ var BaseTagsPrototype = {
// clear the index... // clear the index...
delete (definitions || {})[tag] delete (definitions || {})[tag]
// set... // set...
} else { } else {
value = this.normalize(value) value = this.normalize(value)
@ -1650,10 +1607,8 @@ var BaseTagsPrototype = {
// update the index... // update the index...
this.definitions = definitions || {} this.definitions = definitions || {}
this.definitions[tag] = this.splitSet(value) this.definitions[tag] = this.splitSet(value) }
} return this },
return this
},
// Toggle a tag to persistent/non-persistent... // Toggle a tag to persistent/non-persistent...
// //
@ -1758,8 +1713,7 @@ var BaseTagsPrototype = {
Object.keys(index).length > 0 Object.keys(index).length > 0
&& this.__index == null && this.__index == null
&& (this.__index = index) && (this.__index = index)
return this return this },
},
// Query API... // Query API...
@ -2062,46 +2016,36 @@ var BaseTagsPrototype = {
// //
json: function(){ json: function(){
var res = {} var res = {}
// definitions... // definitions...
this.definitions this.definitions
&& Object.keys(this.definitions).length > 0 && Object.keys(this.definitions).length > 0
&& (res.definitions = Object.assign({}, this.definitions)) && (res.definitions = Object.assign({}, this.definitions))
// persistent tags... // persistent tags...
this.persistent this.persistent
&& this.persistent.size > 0 && this.persistent.size > 0
&& (res.persistent = [...this.persistent]) && (res.persistent = [...this.persistent])
// tags... // tags...
res.tags = {} res.tags = {}
Object.entries(this.__index || {}) Object.entries(this.__index || {})
.forEach(function(e){ .forEach(function(e){
// XXX should we serialize the items here??? // XXX should we serialize the items here???
res.tags[e[0]] = [...e[1]] }) res.tags[e[0]] = [...e[1]] })
return res },
return res
},
load: function(json){ load: function(json){
var that = this var that = this
// definitions... // definitions...
json.definitions json.definitions
&& (this.definitions = Object.assign({}, json.definitions)) && (this.definitions = Object.assign({}, json.definitions))
// persistent tags... // persistent tags...
json.persistent json.persistent
&& (this.persistent = new Set(json.persistent)) && (this.persistent = new Set(json.persistent))
// tags... // tags...
json.tags json.tags
&& (this.__index = {}) && (this.__index = {})
&& Object.entries(json.tags) && Object.entries(json.tags)
.forEach(function(e){ .forEach(function(e){
that.__index[e[0]] = new Set(e[1]) }) that.__index[e[0]] = new Set(e[1]) })
return this },
return this
},
__init__: function(json){ __init__: function(json){
@ -2244,8 +2188,7 @@ var TagsWithHandlersPrototype = {
// XXX can we avoid doing this here??? // XXX can we avoid doing this here???
if(tag instanceof Function){ if(tag instanceof Function){
to = tag to = tag
tag = '*' tag = '*' }
}
return object.parentCall(TagsWithHandlersPrototype.replace, this, return object.parentCall(TagsWithHandlersPrototype.replace, this,
tag, tag,
arguments.length <= 2 ? arguments.length <= 2 ?
@ -2255,8 +2198,7 @@ var TagsWithHandlersPrototype = {
var res = to.call(this, ...arguments) var res = to.call(this, ...arguments)
typeof(res) == typeof('str') typeof(res) == typeof('str')
&& this.handleSpecialTag(res, 'replace', tag) && this.handleSpecialTag(res, 'replace', tag)
return res return res }
}
: this.handleSpecialTag(to, 'replace', tag)) : this.handleSpecialTag(to, 'replace', tag))
: to, : to,
...[...arguments].slice(2)) }, ...[...arguments].slice(2)) },
@ -2352,8 +2294,7 @@ var TagsWithDictPrototype = {
.concat([value]) .concat([value])
.unique()) }) .unique()) })
return res return res },
},
// Translate normalized tag to the dict form... // Translate normalized tag to the dict form...
// //
@ -2382,8 +2323,7 @@ var TagsWithDictPrototype = {
// XXX do we want to .match(..) here??? // XXX do we want to .match(..) here???
removeOrphansFromDict: function(...tags){ removeOrphansFromDict: function(...tags){
if(!this.dict){ if(!this.dict){
return this return this }
}
var that = this var that = this
var dict = this.dict var dict = this.dict
@ -2401,21 +2341,18 @@ var TagsWithDictPrototype = {
tags = tags tags = tags
.filter(function(tag){ .filter(function(tag){
return !index.has(tag) }) return !index.has(tag) })
// check specific tags... // check specific tags...
// NOTE: this is geared towards a small number of input tags... // NOTE: this is geared towards a small number of input tags...
} else { } else {
tags = tags tags = tags
.filter(function(tag){ .filter(function(tag){
return that.match(tag).length == 0 }) return that.match(tag).length == 0 }) }
}
tags tags
.forEach(function(tag){ .forEach(function(tag){
delete dict[tag] }) delete dict[tag] })
return this return this },
},
// Save/clean dict on prototype methods... // Save/clean dict on prototype methods...
@ -2432,8 +2369,7 @@ var TagsWithDictPrototype = {
// XXX can we avoid doing this here??? // XXX can we avoid doing this here???
if(tag instanceof Function){ if(tag instanceof Function){
to = tag to = tag
tag = '*' tag = '*' }
}
var can_remove = [] var can_remove = []
var res = object.parentCall(TagsWithDictPrototype.replace, this, var res = object.parentCall(TagsWithDictPrototype.replace, this,
@ -2450,22 +2386,17 @@ var TagsWithDictPrototype = {
: this.normalizeSave(to)) : this.normalizeSave(to))
: to, : to,
...[...arguments].slice(2)) ...[...arguments].slice(2))
typeof(tag) == typeof('str') typeof(tag) == typeof('str')
&& this.removeOrphansFromDict(can_remove) && this.removeOrphansFromDict(can_remove)
return res return res },
},
togglePersistent: function(...tags){ togglePersistent: function(...tags){
this.normalizeSave(tags) this.normalizeSave(tags)
var res = object.parentCall(TagsWithDictPrototype.togglePersistent, this, ...arguments) var res = object.parentCall(TagsWithDictPrototype.togglePersistent, this, ...arguments)
this.removeOrphansFromDict(res this.removeOrphansFromDict(res
.map(function(r, i){ .map(function(r, i){
return r == 'off' ? tags[i] : [] }) return r == 'off' ? tags[i] : [] })
.flat()) .flat())
return res return res },
},
define: function(tag, value){ define: function(tag, value){
arguments.length > 1 arguments.length > 1
&& value != null && value != null
@ -2475,8 +2406,7 @@ var TagsWithDictPrototype = {
value == null value == null
&& this.removeOrphansFromDict(tag) && this.removeOrphansFromDict(tag)
return res return res },
},
// Serialization... // Serialization...
// //
@ -2491,29 +2421,21 @@ var TagsWithDictPrototype = {
// } // }
json: function(){ json: function(){
var res = object.parentCall(TagsWithDictPrototype.json, this, ...arguments) var res = object.parentCall(TagsWithDictPrototype.json, this, ...arguments)
// dict...
this.dict this.dict
&& Object.keys(this.dict).length > 0 && Object.keys(this.dict).length > 0
&& (res.dict = {}) && (res.dict = {})
&& Object.entries(this.dict) && Object.entries(this.dict)
.forEach(function(e){ .forEach(function(e){
res.dict[e[0]] = e[1].slice() }) res.dict[e[0]] = e[1].slice() })
return res },
return res
},
load: function(json){ load: function(json){
var that = this var that = this
// dict...
json.dict json.dict
&& (this.dict = {}) && (this.dict = {})
&& Object.entries(json.dict) && Object.entries(json.dict)
.forEach(function(e){ .forEach(function(e){
that.dict[e[0]] = e[1].slice() }) that.dict[e[0]] = e[1].slice() })
return object.parentCall(TagsWithDictPrototype.load, this, ...arguments) },
return object.parentCall(TagsWithDictPrototype.load, this, ...arguments)
},
} }