cleanup and refactoring...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2018-12-13 03:27:23 +03:00
parent 15f8f12b90
commit f237adc073
2 changed files with 63 additions and 67 deletions

View File

@ -240,7 +240,6 @@ var DataPrototype = {
// XXX should this default to top or bottom ribbon??? // XXX should this default to top or bottom ribbon???
get base(){ get base(){
//return this.__base || this.ribbon_order.slice(-1)[0] },
return this.__base || this.ribbon_order[0] }, return this.__base || this.ribbon_order[0] },
set base(value){ set base(value){
this.__base = value }, this.__base = value },
@ -412,7 +411,6 @@ var DataPrototype = {
if(skip_undefined && lst[i] == null){ if(skip_undefined && lst[i] == null){
continue continue
} }
//if(lst.indexOf(lst[i]) != i){
if(lst_idx[lst[i]] != i){ if(lst_idx[lst[i]] != i){
lst.splice(i, 1) lst.splice(i, 1)
i -= 1 i -= 1
@ -736,11 +734,9 @@ var DataPrototype = {
/*********************************************** Introspection ***/ /*********************************************** Introspection ***/
get length(){ get length(){
return this.order.length return this.order.length },
},
get ribbonLength(){ get ribbonLength(){
return this.getImages(this.getRibbon()).len return this.getImages(this.getRibbon()).len },
},
// Get image // Get image
@ -3366,7 +3362,7 @@ var DataWithTags2Prototype = {
toggleTag: function(tag, gids, action){ toggleTag: function(tag, gids, action){
gids = gids == null || gids == 'current' ? this.getImage() : gids gids = gids == null || gids == 'current' ? this.getImage() : gids
var res = this.tags.toggleTag(tag, gids, action) var res = this.tags.toggle(tag, gids, action)
return res === this.tags ? return res === this.tags ?
this this

View File

@ -36,11 +36,11 @@ var util = require('lib/util')
var TagsClassPrototype = { var TagsClassPrototype = {
// Utils... // Utils...
// //
// .normalizeTags(tag) // .normalize(tag)
// -> ntag // -> ntag
// //
// .normalizeTags(tag, ...) // .normalize(tag, ...)
// .normalizeTags([tag, ...]) // .normalize([tag, ...])
// -> [ntag, ...] // -> [ntag, ...]
// //
// NOTE: tag set order is not significant. // NOTE: tag set order is not significant.
@ -54,7 +54,7 @@ var TagsClassPrototype = {
// c:b/a -> b/a:c - sort paths within sets // c:b/a -> b/a:c - sort paths within sets
// XXX should we support priority braces, i.e. c:(b/a) // XXX should we support priority braces, i.e. c:(b/a)
// XXX do we support leading '/' ??? // XXX do we support leading '/' ???
normalizeTags: function(...tags){ normalize: function(...tags){
var that = this var that = this
var tagRemovedChars = (this.config || {})['tagRemovedChars'] var tagRemovedChars = (this.config || {})['tagRemovedChars']
tagRemovedChars = tagRemovedChars instanceof RegExp ? tagRemovedChars = tagRemovedChars instanceof RegExp ?
@ -96,7 +96,7 @@ var TagsClassPrototype = {
tags = (tags.length == 1 && tags[0] instanceof Array) ? tags = (tags.length == 1 && tags[0] instanceof Array) ?
tags.pop() tags.pop()
: tags : tags
return this.normalizeTags(tags) return this.normalize(tags)
.map(function(tag){ .map(function(tag){
return tag.split(/[:\\\/]/g) }) return tag.split(/[:\\\/]/g) })
.flat() .flat()
@ -266,8 +266,8 @@ var TagsPrototype = {
// Utils... // Utils...
// //
// proxies to class methods... // proxies to class methods...
normalizeTags: function(...tags){ normalize: function(...tags){
return this.constructor.normalizeTags.call(this, ...tags) }, return this.constructor.normalize.call(this, ...tags) },
subTags: function(...tags){ subTags: function(...tags){
return this.constructor.subTags.call(this, ...tags) }, return this.constructor.subTags.call(this, ...tags) },
parseQuery: function(query){ parseQuery: function(query){
@ -358,8 +358,8 @@ var TagsPrototype = {
// match two tags... // match two tags...
} else { } else {
// normalized match... // normalized match...
a = this.normalizeTags(a) a = this.normalize(a)
b = this.normalizeTags(b) b = this.normalize(b)
// special case: *tag* pattern... // special case: *tag* pattern...
a = /^\*[^:\\\/]*\*$/.test(a) ? a = /^\*[^:\\\/]*\*$/.test(a) ?
@ -548,7 +548,7 @@ var TagsPrototype = {
return Object.keys(this.__index || {}) return Object.keys(this.__index || {})
.concat([...(this.__persistent_tags || [])] .concat([...(this.__persistent_tags || [])]
.map(function(t){ .map(function(t){
return that.normalizeTags(t) })) return that.normalize(t) }))
.unique() .unique()
} }
}, },
@ -568,7 +568,7 @@ var TagsPrototype = {
// NOTE: this does not support any query syntax... // NOTE: this does not support any query syntax...
values: function(tag){ values: function(tag){
var that = this var that = this
tag = this.normalizeTags(tag || '*') tag = this.normalize(tag || '*')
return [...new Set( return [...new Set(
Object.entries(this.__index || {}) Object.entries(this.__index || {})
.filter(function(e){ .filter(function(e){
@ -605,7 +605,7 @@ var TagsPrototype = {
.concat([seen[0]]) .concat([seen[0]])
.join('" -> "') }"`) } .join('" -> "') }"`) }
var next = aliases[tag] var next = aliases[tag]
|| aliases[this.normalizeTags(tag)] || aliases[this.normalize(tag)]
seen.push(tag) seen.push(tag)
return next != null ? return next != null ?
resolve(next, seen) resolve(next, seen)
@ -621,17 +621,17 @@ var TagsPrototype = {
// remove... // remove...
} else if(value == null){ } else if(value == null){
delete aliases[tag.trim()] delete aliases[tag.trim()]
delete aliases[this.normalizeTags(tag)] delete aliases[this.normalize(tag)]
// set... // set...
} else { } else {
tag = tag.trim() tag = tag.trim()
value = this.normalizeTags(value) value = this.normalize(value)
// check for recursion... // check for recursion...
var chain = [] var chain = []
var target = resolve(value, chain) var target = resolve(value, chain)
if(target == tag || target == this.normalizeTags(tag)){ if(target == tag || target == this.normalize(tag)){
throw new Error(`.alias(..): Creating a recursive alias chain: "${ throw new Error(`.alias(..): Creating a recursive alias chain: "${
chain chain
.concat([chain[0]]) .concat([chain[0]])
@ -646,7 +646,7 @@ var TagsPrototype = {
tag: function(tags, value){ tag: function(tags, value){
var that = this var that = this
value = value instanceof Array ? value : [value] value = value instanceof Array ? value : [value]
tags = this.normalizeTags(tags instanceof Array ? tags : [tags]) tags = this.normalize(tags instanceof Array ? tags : [tags])
var index = this.__index = this.__index || {} var index = this.__index = this.__index || {}
value.forEach(function(value){ value.forEach(function(value){
@ -663,7 +663,7 @@ var TagsPrototype = {
var index = this.__index = this.__index || {} var index = this.__index = this.__index || {}
value = value instanceof Array ? value : [value] value = value instanceof Array ? value : [value]
tags = this.normalizeTags(tags instanceof Array ? tags : [tags]) tags = this.normalize(tags instanceof Array ? tags : [tags])
.map(function(tag){ .map(function(tag){
return /\*/.test(tag) ? return /\*/.test(tag) ?
// resolve tag patterns... // resolve tag patterns...
@ -686,8 +686,8 @@ var TagsPrototype = {
}, },
// //
// Toggle tag for each values... // Toggle tag for each values...
// .toggleTag(tag, value) // .toggle(tag, value)
// .toggleTag(tag, values) // .toggle(tag, values)
// -> [bool|null, ..] // -> [bool|null, ..]
// NOTE: if tag is a tag pattern (contains '*') this will toggle // NOTE: if tag is a tag pattern (contains '*') this will toggle
// matching tags values off as expected but ignore toggling // matching tags values off as expected but ignore toggling
@ -695,23 +695,23 @@ var TagsPrototype = {
// corresponding position. // corresponding position.
// //
// Toggle tag on for all values... // Toggle tag on for all values...
// .toggleTag(tag, value, 'on') // .toggle(tag, value, 'on')
// .toggleTag(tag, values, 'on') // .toggle(tag, values, 'on')
// -> this // -> this
// NOTE: this will throw an exception if tag is a tag pattern, // NOTE: this will throw an exception if tag is a tag pattern,
// this is not symmetrical to how .toggleTag(.., .., 'off') // this is not symmetrical to how .toggle(.., .., 'off')
// behaves. // behaves.
// //
// Toggle tag off for all values... // Toggle tag off for all values...
// .toggleTag(tag, value, 'off') // .toggle(tag, value, 'off')
// .toggleTag(tag, values, 'off') // .toggle(tag, values, 'off')
// -> this // -> this
// NOTE: if tag is a tag pattern this will remove all matching // NOTE: if tag is a tag pattern this will remove all matching
// tags, this is not fully symmetrical to .toggleTag(.., .., 'on') // tags, this is not fully symmetrical to .toggle(.., .., 'on')
// //
// Check if tag is set on value(s)... // Check if tag is set on value(s)...
// .toggleTag(tag, value, '?') // .toggle(tag, value, '?')
// .toggleTag(tag, values, '?') // .toggle(tag, values, '?')
// -> [bool, ..] // -> [bool, ..]
// //
// //
@ -719,15 +719,15 @@ var TagsPrototype = {
// //
// XXX do we need this??? // XXX do we need this???
// ...seems a bit overcomplicated... // ...seems a bit overcomplicated...
toggleTag: function(tag, values, action){ toggle: function(tag, values, action){
var that = this var that = this
values = values instanceof Array ? values : [values] values = values instanceof Array ? values : [values]
var pattern = /\*/.test(tag) var pattern = /\*/.test(tag)
var ntag = this.normalizeTags(tag) var ntag = this.normalize(tag)
// can't set pattern as tag... // can't set pattern as tag...
if(pattern && action != 'on'){ if(pattern && action != 'on'){
throw new TypeError(`.toggleTag(..): 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' ?
@ -797,7 +797,7 @@ var TagsPrototype = {
this.__persistent_tags = this.__persistent_tags =
this.__persistent_tags || new Set() this.__persistent_tags || new Set()
return this.normalizeTags(tags) return this.normalize(tags)
.map(function(tag){ .map(function(tag){
return action == 'on' ? return action == 'on' ?
(persistent.add(tag), 'on') (persistent.add(tag), 'on')
@ -814,12 +814,12 @@ var TagsPrototype = {
// //
// Rename tag... // Rename tag...
// .renameTag(from, to) // .rename(from, to)
// -> this // -> this
// //
// Rename a tag in list of tags... // Rename a tag in list of tags...
// .renameTag(from, to, tag, ...) // .rename(from, to, tag, ...)
// .renameTag(from, to, [tag, ...]) // .rename(from, to, [tag, ...])
// -> tags // -> tags
// //
// NOTE: if to is '' this will remove all occurrences of from. // NOTE: if to is '' this will remove all occurrences of from.
@ -829,61 +829,61 @@ var TagsPrototype = {
// XXX need to sanitize from -- it can not contain regex characters... // XXX need to sanitize from -- it can not contain regex characters...
// ...should we guard against this??? // ...should we guard against this???
// XXX should both sides of the alias be renamed??? // XXX should both sides of the alias be renamed???
renameTag: function(from, to, ...tags){ rename: function(tag, to, ...tags){
var that = this var that = this
from = this.normalizeTags(from) tag = this.normalize(tag)
if(from == ''){ if(tag == ''){
throw new Error(`.renameTag(..): first argument can not be an empty string.`) } throw new Error(`.rename(..): first argument can not be an empty string.`) }
if(/[:\\\/]/.test(from)){ if(/[:\\\/]/.test(tag)){
throw new Error( throw new Error(
`.renameTag(..): only support singular tag renaming, got: "${from}"`) } `.rename(..): only support singular tag renaming, got: "${tag}"`) }
// XXX too strict??? // XXX too strict???
if(!/^[a-z0-9]+$/.test(from)){ if(!/^[a-z0-9]+$/.test(tag)){
throw new Error( throw new Error(
`.renameTag(..): first argument must be a valid single tag, got: "${from}"`) } `.rename(..): first argument must be a valid single tag, got: "${tag}"`) }
to = this.normalizeTags(to) to = this.normalize(to)
if(/[\\\/]/.test(to)){ if(/[\\\/]/.test(to)){
throw new Error( throw new Error(
`.renameTag(..): only support tags and tag sets as renaming target, got: "${to}"`) } `.rename(..): only support tags and tag sets as renaming target, got: "${to}"`) }
tags = new Set((tags[0] instanceof Array && tags.length == 1) ? tags = new Set((tags[0] instanceof Array && tags.length == 1) ?
tags[0] tags[0]
: tags) : tags)
// prepare for the replacement... // prepare for the replacement...
var pattern = new RegExp(`(^|[:\\\\\\/])${from}(?=$|[:\\\\\\/])`, 'g') var pattern = new RegExp(`(^|[:\\\\\\/])${tag}(?=$|[:\\\\\\/])`, 'g')
var target = `$1${to}` var target = `$1${to}`
var patchSet = function(s){ var patchSet = function(s){
that.match(from, [...s || []]) that.match(tag, [...s || []])
.forEach(function(tag){ .forEach(function(tag){
s.delete(tag) s.delete(tag)
var t = that.normalizeTags(tag.replace(pattern, target)) var t = that.normalize(tag.replace(pattern, target))
t != '' t != ''
&& s.add(t) && s.add(t)
}) })
return s return s
} }
var patchObj = function(o, patchValue){ var patchObj = function(o, patchValue){
that.match(from, Object.keys(o || {})) that.match(tag, Object.keys(o || {}))
.forEach(function(tag){ .forEach(function(m){
var value = o[tag] var value = o[m]
delete o[tag] delete o[m]
var t = that.normalizeTags(tag.replace(pattern, target)) var t = that.normalize(m.replace(pattern, target))
t != '' t != ''
&& (o[t] = value) && (o[t] = value)
}) })
patchValue patchValue
&& Object.keys(o || {}) && Object.keys(o || {})
.forEach(function(tag){ .forEach(function(m){
var v = o[tag] var v = o[m]
if(that.match(from, v)){ if(that.match(tag, v)){
var t = that.normalizeTags(v.replace(pattern, target)) var t = that.normalize(v.replace(pattern, target))
t == '' ? t == '' ?
(delete o[tag]) (delete o[m])
: (o[tag] = t) : (o[m] = t)
} }
}) })
return o return o
@ -902,10 +902,10 @@ var TagsPrototype = {
return this return this
}, },
// NOTE: this is a short hand to .renameTag(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){
return this.renameTag(tag, '', ...tags) }, return this.rename(tag, '', ...tags) },