minor cleanup and refactoring...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2018-12-29 23:55:34 +03:00
parent c05d713aa5
commit 2b9234a2d3

View File

@ -407,6 +407,7 @@ var BaseTagsPrototype = {
// Set([ <tag>, ... ]) // Set([ <tag>, ... ])
// //
// NOTE: this is expected to contain normalized values only... // NOTE: this is expected to contain normalized values only...
// non-normalized values may and will get matched incorrectly.
persistent: null, persistent: null,
// Tag definitions... // Tag definitions...
@ -872,21 +873,21 @@ var BaseTagsPrototype = {
// - remove all paths that match it after it (tail) // - remove all paths that match it after it (tail)
// - next path // - next path
// //
uniquePaths: function(...list){ uniquePaths: function(...tags){
var that = this var that = this
var PS = this.PATH_SEPARATOR var PS = this.PATH_SEPARATOR
return (list.length == 0 ? return (tags.length == 0 ?
this.paths() this.paths()
: this.normalize(normalizeSplit(list))) : 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){ return that.splitPath(tag) })
.sort(function(a, b){ return b.length - a.length }) .sort(function(a, b){ return b.length - a.length })
.map(function(p){ return p.join(PS) }) .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, list){ .map(function(p, i, tags){
// skip []... // skip []...
!(p instanceof Array) !(p instanceof Array)
&& list && tags
// only handle the tail... // only handle the tail...
.slice(i+1) .slice(i+1)
.forEach(function(o, j){ .forEach(function(o, j){
@ -894,7 +895,7 @@ var BaseTagsPrototype = {
!(o instanceof Array) !(o instanceof Array)
&& that.directMatch(o, p) && that.directMatch(o, p)
// match -> replace the matching elem with [] // match -> replace the matching elem with []
&& (list[i+j+1] = []) }) && (tags[i+j+1] = []) })
return p }) return p })
.flat() }, .flat() },
@ -967,12 +968,8 @@ var BaseTagsPrototype = {
// get all tags... // get all tags...
} else { } else {
return Object.keys(this.__index || {}) return Object.keys(this.__index || {})
//.concat([...(this.persistentAll || [])] //.concat([...(this.persistentAll || [])])
.concat([...(this.persistent || [])] .concat([...(this.persistent || [])])
// XXX do we need to normalize here???
// ...do we trust .persistent???
.map(function(t){
return that.normalize(t) }))
.unique() .unique()
} }
}, },
@ -1048,9 +1045,6 @@ var BaseTagsPrototype = {
// Shorthands to: // Shorthands to:
// ts.directMatch(tag).map(func.bind(ts)) // and friends... // ts.directMatch(tag).map(func.bind(ts)) // and friends...
// XXX not sure if we need these...
// XXX do we need a .values(..) variant of each???
// mapValues: makeIter('map', 'values'),
map: makeIter('map'), map: makeIter('map'),
filter: makeIter('filter'), filter: makeIter('filter'),
forEach: makeIter('forEach'), forEach: makeIter('forEach'),
@ -1059,6 +1053,8 @@ var BaseTagsPrototype = {
// Edit API... // Edit API...
// //
// Tag values...
//
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]
@ -1073,34 +1069,41 @@ var BaseTagsPrototype = {
return this return this
}, },
// Remove tags...
// //
// Remove tags... // Remove tags...
// .untag(tags) // .untag(tag)
// .untag(tags, '*') // .untag(tag, '*')
// -> this // -> this
// //
// Remove tags form values... // Remove tags form values...
// .untag(tags, values) // .untag(tag, values)
// -> this // -> this
// //
// // Remove // // Remove tags form given tags...
// .untag(tags, values, tag, ..) // .untag(tag, values, tag, ..)
// -> this // -> tags
// NOTE: this has no effect on the state...
// NOTE: values is ignored here...
//
//
// How this does matching is slightly different to .directMatch(..)
// or .match(..) in the following ways:
// 1) if tag contains no '*' it is match 1:1, i.e. only tags matching
// exactly will get removed.
// 2) if tag contains at leas one '*' then all matching tags will
// get removed.
// //
// Pattern syntax:
// a - untag only explicit a
// "a" - the same as above
// *a* - remove all matching a
// //
// NOTE: .untag(tags, '*', ..) is similar to .removeTag(tags, ..) // NOTE: .untag(tags, '*', ..) is similar to .removeTag(tags, ..)
// but not identical. The differences being: // but not identical. The differences being:
// .untag(..) // .untag(..)
// - removes whole tags only // - removes whole tags only
// .removesTag(..) // .removeTag(..)
// - can modify tags // - can modify tags
//
// NOTE: this supports tag patterns (see: .match(..)) // NOTE: this supports tag patterns (see: .match(..))
// NOTE: non-pattern tags are matched explicitly. // NOTE: non-pattern tags are matched explicitly.
//
// XXX do we do .match(..) or .directMatch(..) here for patterns??? // XXX do we do .match(..) or .directMatch(..) here for patterns???
untag: function(tag, value, ...tags){ untag: function(tag, value, ...tags){
var that = this var that = this
@ -1179,7 +1182,6 @@ var BaseTagsPrototype = {
// //
// NOTE: this supports tag patterns (see: ,match(..)) // NOTE: this supports tag patterns (see: ,match(..))
// //
// XXX should this return true/false or 'on'/'off'???
toggle: 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]
@ -2089,7 +2091,6 @@ var TagsWithHandlersPrototype = {
handleSpecialTag: function(tags, ...args){ handleSpecialTag: function(tags, ...args){
var that = this var that = this
var handlers = this.__special_tag_handlers__ || {} var handlers = this.__special_tag_handlers__ || {}
tags = this.normalize(tags)
return tags instanceof Array ? return tags instanceof Array ?
tags.map(function(tag){ tags.map(function(tag){
return that.handleSpecialTag(tag, ...args) }) return that.handleSpecialTag(tag, ...args) })
@ -2101,8 +2102,7 @@ var TagsWithHandlersPrototype = {
// mnemonic... // mnemonic...
return p == that.normalize(p) return p == that.normalize(p)
// get the matching handler keys... // get the matching handler keys...
&& that.directMatch(p, tags) && that.directMatch(p, tags) })
})
// resolve handler aliases... // resolve handler aliases...
.map(function(match){ .map(function(match){
do { do {
@ -2123,9 +2123,9 @@ var TagsWithHandlersPrototype = {
var cur = tag var cur = tag
&& handler.call(that, tag, ...args) && handler.call(that, tag, ...args)
// if a handler returned null continue with tag as-is... // if a handler returned null continue with tag as-is...
return cur == null ? tag : cur }, tags) return cur == null ? tag : cur }, that.normalize(tags))
// no handlers -> return as-is... // no handlers -> return as-is...
|| tags) }, || that.normalize(tags)) },
// handler: action(tag, 'tag') // handler: action(tag, 'tag')
tag: function(tags, value){ tag: function(tags, value){