cleanup formatting...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2023-05-18 23:09:28 +03:00
parent 8678d79424
commit 297f958e07

View File

@ -107,10 +107,12 @@ var util = require('lib/util')
// -> list // -> list
// //
var normalizeSplit = function(args){ var normalizeSplit = function(args){
return ((args.length == 1 && args[0] instanceof Array) ? return ((args.length == 1
&& args[0] instanceof Array) ?
args.pop() args.pop()
: args) : args)
.slice() } .slice() }
// Normalize return value from Object.processor.run(..)... // Normalize return value from Object.processor.run(..)...
// //
// Create processor... // Create processor...
@ -128,10 +130,13 @@ var normalizeSplit = function(args){
// NOTE: for an example see normalizeSplit(..) docs above. // NOTE: for an example see normalizeSplit(..) docs above.
var normalizeRes = function(args){ var normalizeRes = function(args){
return function(value){ return function(value){
value = value || this value = value
return (args.length == 1 && !(args[0] instanceof Array)) ? || this
return (args.length == 1
&& !(args[0] instanceof Array)) ?
value[0] value[0]
: value } } : value } }
// Normalize return value... // Normalize return value...
// //
// normalizeResValue(value, args) // normalizeResValue(value, args)
@ -157,17 +162,21 @@ var makeSplitter = function(separator, unique){
return tag.split(SP) }) return tag.split(SP) })
.flat() .flat()
.run(function(){ .run(function(){
return unique ? this.unique() : this }) } } return unique ?
this.unique()
: this }) } }
var makeJoiner = function(separator){ var makeJoiner = function(separator){
return function(...items){ return function(...items){
return normalizeSplit(items).join(this[separator]) } } return normalizeSplit(items)
.join(this[separator]) } }
// Make iterator/reducer methods a-la Array.prototype.map(..) and friends // Make iterator/reducer methods a-la Array.prototype.map(..) and friends
// and Array.prototype.reduce(..).... // and Array.prototype.reduce(..)....
var makeIter = function(name, lister){ var makeIter = function(name, lister){
return function(tag, func){ return function(tag, func){
var args = [...arguments] var args = [...arguments]
func = args.pop() func = args.pop()
tag = args.pop() || '*' tag = args.pop()
|| '*'
var res = this[lister || 'directMatch'](tag)[name](func.bind(this)) var res = this[lister || 'directMatch'](tag)[name](func.bind(this))
return res == null ? return res == null ?
this this
@ -177,8 +186,10 @@ var makeReducer = function(lister){
var args = [...arguments] var args = [...arguments]
initial = args.pop() initial = args.pop()
func = args.pop() func = args.pop()
tag = args.pop() || '*' tag = args.pop()
return this[lister || 'directMatch'](tag).reduce(func.bind(this), initial) } } || '*'
return this[lister || 'directMatch'](tag)
.reduce(func.bind(this), initial) } }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -304,7 +315,9 @@ var BaseTagsClassPrototype = {
.join(PS) }) }, .join(PS) }) },
// get all the single tags that make up a compound tag... // get all the single tags that make up a compound tag...
subTags: function(...tags){ subTags: function(...tags){
return this.splitTag(...tags).flat(Infinity) }, return this
.splitTag(...tags)
.flat(Infinity) },
normalizeTagStr: function(...tags){ normalizeTagStr: function(...tags){
var ILLEGAL_CHARS = this.TAG_ILLEGAL_CHARS var ILLEGAL_CHARS = this.TAG_ILLEGAL_CHARS
@ -406,7 +419,8 @@ var BaseTagsClassPrototype = {
query query
: query : query
// split by strings whitespace and block comments... // split by strings whitespace and block comments...
.split(this.__query_lexer || this.constructor.__query_lexer) .split(this.__query_lexer
|| this.constructor.__query_lexer)
// parse numbers... // parse numbers...
// XXX do we need number parsing??? // XXX do we need number parsing???
.map(function(e){ .map(function(e){
@ -414,25 +428,30 @@ var BaseTagsClassPrototype = {
if(/^[-+]?[0-9]+\.[0-9]+$/.test(e)){ if(/^[-+]?[0-9]+\.[0-9]+$/.test(e)){
e = parseFloat(e) e = parseFloat(e)
} else if(/^[-+]?[0-9]+$/.test(e)){ } else if(/^[-+]?[0-9]+$/.test(e)){
e = parseInt(e) e = parseInt(e) }
} return e })
return e
})
// remove undefined groups... // remove undefined groups...
.filter(function(e){ .filter(function(e){
// NOTE: in JS 0 == '' is true ;) // NOTE: in JS 0 == '' is true ;)
return e !== undefined && e !== '' }) return e !== undefined
&& e !== '' })
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){
@ -510,7 +529,8 @@ var BaseTagsPrototype = {
// //
// This will combine .persistent and .definitionPaths() // This will combine .persistent and .definitionPaths()
get persistentAll(){ get persistentAll(){
return (this.__persistent || new Set()) return (this.__persistent
|| new Set())
.unite(this.definitionPaths()) }, .unite(this.definitionPaths()) },
@ -641,7 +661,9 @@ var BaseTagsPrototype = {
} 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...
if(b == null || b instanceof Array){ if(b == null || b instanceof Array){
@ -689,7 +711,8 @@ var BaseTagsPrototype = {
// NOTE: this does the same job as adding .definitions to // NOTE: this does the same job as adding .definitions to
// .persistent but much much faster... // .persistent but much much faster...
var expand = function(tags, res){ var expand = function(tags, res){
res = (res || new Set()).unite(tags) res = (res || new Set())
.unite(tags)
tags = tags tags = tags
.map(function(tag){ .map(function(tag){
@ -835,7 +858,9 @@ var BaseTagsPrototype = {
: search(target, tag, seen.add(tag)) }, false) }, false) } : search(target, tag, seen.add(tag)) }, false) }, false) }
var seen = new Set() var seen = new Set()
var res = (quoted || root || base var res = (quoted
|| root
|| base
|| b instanceof Array || b instanceof Array
|| b instanceof Set || b instanceof Set
|| typeof(b) == typeof('str')) ? || typeof(b) == typeof('str')) ?
@ -950,9 +975,12 @@ var BaseTagsPrototype = {
this.paths() this.paths()
: this.normalize(normalizeSplit(tags))) : this.normalize(normalizeSplit(tags)))
// sort by number of path elements (longest first)... // sort by number of path elements (longest first)...
.map(function(tag){ return that.splitPath(tag) }) .map(function(tag){
.sort(function(a, b){ return b.length - a.length }) return that.splitPath(tag) })
.map(function(p){ return p.join(PS) }) .sort(function(a, b){
return b.length - a.length })
.map(function(p){
return p.join(PS) })
// remove all paths in tail that match the current... // remove all paths in tail that match the current...
.map(function(p, i, tags){ .map(function(p, i, tags){
// skip []... // skip []...
@ -1028,8 +1056,10 @@ var BaseTagsPrototype = {
// get tags of specific value... // get tags of specific value...
} else if(value){ } else if(value){
return Object.entries(this.__index || {}) return Object.entries(this.__index || {})
.filter(function(e){ return e[1].has(value) }) .filter(function(e){
.map(function(e){ return e[0] }) return e[1].has(value) })
.map(function(e){
return e[0] })
.flat() .flat()
.unique() .unique()
@ -1041,7 +1071,9 @@ var BaseTagsPrototype = {
.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() },
paths: function(value){ paths: function(value){
var PP = this.PATH_SEPARATOR_PATTERN var PP = this.PATH_SEPARATOR_PATTERN
return this.tags(value) return this.tags(value)
@ -1067,7 +1099,8 @@ var BaseTagsPrototype = {
.filter(function(e){ .filter(function(e){
return tag == '*' return tag == '*'
|| that.match(tag, e[0]) }) || that.match(tag, e[0]) })
.map(function(s){ return [...s[1]] }) .map(function(s){
return [...s[1]] })
.flat() .flat()
.unique() }, .unique() },
@ -1185,7 +1218,10 @@ var BaseTagsPrototype = {
return that.isPattern(tag) ? return that.isPattern(tag) ?
// resolve tag patterns... // resolve tag patterns...
// XXX is .match(..) to broad here??? // XXX is .match(..) to broad here???
that.match(tag, local ? tags : null) that.match(tag,
local ?
tags
: null)
: tag }) : tag })
.flat(Infinity) .flat(Infinity)
@ -1195,12 +1231,12 @@ var BaseTagsPrototype = {
// do the untagging... // do the untagging...
.forEach(function(tag){ .forEach(function(tag){
if(!(tag in index)){ if(!(tag in index)){
return return }
}
var s = value == '*' ? var s = value == '*' ?
new Set() new Set()
: (index[tag] || new Set()).subtract(value) : (index[tag]
|| new Set()).subtract(value)
// remove empty sets... // remove empty sets...
if(s.size == 0){ if(s.size == 0){
@ -1208,8 +1244,7 @@ var BaseTagsPrototype = {
// update... // update...
} else { } else {
index[tag] = s index[tag] = s }
}
}), this) }, }), this) },
// //
// Toggle tag for each values... // Toggle tag for each values...
@ -1252,7 +1287,10 @@ var BaseTagsPrototype = {
// NOTE: this is cheating -- if tag is a list it will get // NOTE: this is cheating -- if tag is a list it will get
// stringified before the test... // stringified before the test...
var pattern = this.isPattern(tag) var pattern = this.isPattern(tag)
var ntag = this.normalize(tag instanceof Array ? tag : [tag]) var ntag = this.normalize(
tag instanceof Array ?
tag
: [tag])
// can't set pattern as tag... // can't set pattern as tag...
if(pattern && action == 'on'){ if(pattern && action == 'on'){
@ -1280,7 +1318,10 @@ var BaseTagsPrototype = {
.reduce(function(res, v){ .reduce(function(res, v){
var state = that.tags(v, tag) var state = that.tags(v, tag)
res.res.push(state ? 'off' : 'on') res.res.push(
state ?
'off'
: 'on')
state ? state ?
res.untag.push(v) res.untag.push(v)
: res.tag.push(v) : res.tag.push(v)
@ -1633,11 +1674,14 @@ var BaseTagsPrototype = {
// //
// //
togglePersistent: function(...tags){ togglePersistent: function(...tags){
action = ['on', 'off', 'toggle', '?'].includes(tags[tags.length-1]) ? action = ['on', 'off', 'toggle', '?']
tags.pop() .includes(tags[tags.length-1]) ?
: 'toggle' tags.pop()
: 'toggle'
tags = normalizeSplit(tags) tags = normalizeSplit(tags)
var persistent = this.persistent = this.persistent || new Set() var persistent = this.persistent =
this.persistent
|| new Set()
return this.normalize(tags) return this.normalize(tags)
.map(function(tag){ .map(function(tag){
return action == 'on' ? return action == 'on' ?
@ -1917,10 +1961,15 @@ var BaseTagsPrototype = {
return prefix == null ? return prefix == null ?
list list
: list : list
.reduce(function(res, e){ .reduce(
return res[res.length-1] == prefix ? function(res, e){
res.slice(0, -1).concat(e instanceof Array ? e : [e]) return res[res.length-1] == prefix ?
: res.concat([e]) }, []) res.slice(0, -1).concat(
e instanceof Array ?
e
: [e])
: res.concat([e]) },
[])
.filter(function(e){ .filter(function(e){
return e != prefix }) } return e != prefix }) }
// Query Language pre-processor... // Query Language pre-processor...