cleanup and refactoring...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2018-12-25 00:22:27 +03:00
parent 36ac03d34f
commit 1851a4943d

View File

@ -78,6 +78,16 @@ var normalizeSplit = function(args){
/*********************************************************************/ /*********************************************************************/
// Helpers...
var makeSplitter = function(separator){
return function(...tags){
var sp = this[separator]
return normalizeSplit(tags)
.map(function(tag){
return tag.split(sp) })
.flat()
.unique() } }
var BaseTagsClassPrototype = { var BaseTagsClassPrototype = {
// NOTE: do not include 'g' flag here, it will make the RE objects // NOTE: do not include 'g' flag here, it will make the RE objects
@ -88,14 +98,21 @@ var BaseTagsClassPrototype = {
SET_SEPARATOR: /:+/, SET_SEPARATOR: /:+/,
COMBINED_SEPARATOR: /[:\\\/]+/, COMBINED_SEPARATOR: /[:\\\/]+/,
splitSet: function(str){
return str.split(this.SET_SEPARATOR) },
splitPath: function(str){
return str.split(this.PATH_SEPARATOR) },
// Utils... // Utils...
// //
//
// .splitSet(tag)
// .splitSet(tag, ..)
// .splitSet([tag, ..])
// -> parts
//
// NOTE: these will combine the parts of all the tags...
splitSet: makeSplitter('SET_SEPARATOR'),
splitPath: makeSplitter('PATH_SEPARATOR'),
splitTag: makeSplitter('COMBINED_SEPARATOR'),
// Normalize tags...
//
// .normalize(tag) // .normalize(tag)
// -> ntag // -> ntag
// //
@ -151,19 +168,6 @@ var BaseTagsClassPrototype = {
res.pop() res.pop()
: res : res
}, },
// Split the tag into individual singular tags...
//
// Example:
// 'a:b/c' -> ['a', 'b', 'c']
//
subTags: function(...tags){
var that = this
return this.normalize(normalizeSplit(tags))
.map(function(tag){
return tag.split(that.COMBINED_SEPARATOR) })
.flat()
.unique() },
// Query parser... // Query parser...
// //
// NOTE: this is loosely based on Slang's parser... // NOTE: this is loosely based on Slang's parser...
@ -255,11 +259,6 @@ var BaseTagsClassPrototype = {
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// XXX should we store normalized and non-normalized tags for reference???
// ...there are two ways to think of this:
// 1) both (a-la flickr) -- keep both, use normalized internally
// 2) only normalized -- simpler but may surprise the user and
// not be as pretty...
// XXX should we split out the non-basic stuff??? // XXX should we split out the non-basic stuff???
// like: // like:
// .makePathsPersistent() // .makePathsPersistent()
@ -333,7 +332,7 @@ var BaseTagsPrototype = {
splitSet: BaseTagsClassPrototype.splitSet, splitSet: BaseTagsClassPrototype.splitSet,
splitPath: BaseTagsClassPrototype.splitPath, splitPath: BaseTagsClassPrototype.splitPath,
normalize: BaseTagsClassPrototype.normalize, normalize: BaseTagsClassPrototype.normalize,
subTags: BaseTagsClassPrototype.subTags, splitTag: BaseTagsClassPrototype.splitTag,
parseQuery: BaseTagsClassPrototype.parseQuery, parseQuery: BaseTagsClassPrototype.parseQuery,
@ -662,7 +661,7 @@ var BaseTagsPrototype = {
// split tags + include original list... // split tags + include original list...
.run(function(){ .run(function(){
return this return this
.concat(that.subTags(this)) .concat(that.splitTag(this))
.unique() }) .unique() })
.filter(function(t){ .filter(function(t){
// XXX should this search up the path??? // XXX should this search up the path???
@ -789,7 +788,7 @@ var BaseTagsPrototype = {
}, },
// 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.splitTag(this.tags(...arguments)).unique() },
paths: function(value){ paths: function(value){
var sp = that.PATH_SEPARATOR var sp = that.PATH_SEPARATOR
return this.tags(value) return this.tags(value)
@ -1842,19 +1841,12 @@ var TagsWithDictPrototype = {
// side-effect of saving non-normalized values to .dict // side-effect of saving non-normalized values to .dict
// //
normalizeSave: function(...tags){ normalizeSave: function(...tags){
var sp = this.COMBINED_SEPARATOR
var dict = this.dict = this.dict || {} var dict = this.dict = this.dict || {}
var res = this.normalize(...tags) var res = this.splitTag(this.normalize(...tags))
tags = normalizeSplit(tags) tags = this.splitTag(normalizeSplit(tags))
.map(function(tag){
return tag.split(sp) })
.flat()
;(res instanceof Array ? res : [res]) ;(res instanceof Array ? res : [res])
.map(function(tag){
return tag.split(sp) })
.flat()
.forEach(function(tag, i){ .forEach(function(tag, i){
tag = tag.trim() tag = tag.trim()
var value = tags[i].trim() var value = tags[i].trim()
@ -1907,12 +1899,9 @@ var TagsWithDictPrototype = {
// //
// NOTE: an orphan is a dict entry for a tag that is no longer used. // NOTE: an orphan is a dict entry for a tag that is no longer used.
cleanupDict: function(){ cleanupDict: function(){
// XXX is this the full list??? // get tags list...
var sp = this.COMBINED_SEPARATOR
var tags = new Set(this.singleTags() var tags = new Set(this.singleTags()
.concat(this.definitionPaths() .concat(this.splitTag(this.definitionPaths())
.map(function(p){
return p.split(sp) })
.flat())) .flat()))
var dict = this.dict var dict = this.dict
dict dict