cleaned up the alias code...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2018-11-25 17:13:34 +03:00
parent 9728c97ad2
commit a0767d3c11

View File

@ -99,11 +99,11 @@ var TagsPrototype = {
// Format: // Format:
// { // {
// <alias>: <tag>, // <alias>: <normalized-tag>,
// } // }
// //
// XXX Q: should we use these as a dict for normalized user input??? // XXX need introspection for this...
// ...i.e. 'A B C': 'abc' // ...should this be .aliases ???
__aliases: {}, __aliases: {},
@ -145,7 +145,7 @@ var TagsPrototype = {
return this return this
}, },
// //
// Resolve alias... // Resolve alias (recursive)...
// .alias(tag) // .alias(tag)
// -> value // -> value
// -> undefined // -> undefined
@ -159,22 +159,28 @@ var TagsPrototype = {
// -> this // -> this
// //
alias: function(tag, value){ alias: function(tag, value){
// XXX this seems a bit ugly...
var resolve = function(tag, seen){
seen = seen || []
// check for loops...
if(seen.indexOf(tag) >= 0){
throw new Error(`Recursive alias chain: "${
seen
.concat([seen[0]])
.join('" -> "') }"`) }
var next = this.__aliases[tag]
|| this.__aliases[this.normalize(tag)]
seen.push(tag)
return next != null ?
resolve(next, seen)
: seen.length > 1 ?
tag
: undefined
}.bind(this)
// resolve... // resolve...
if(arguments.length == 1){ if(arguments.length == 1){
var seen = [] return resolve(tag.trim())
var next
do {
tag = next || tag.trim()
seen.push(tag)
next = this.__aliases[tag]
|| this.__aliases[this.normalize(tag)]
// check for loops...
if(seen.indexOf(next) >= 0){
throw new Error(`Recursive alias chain: "${ seen.join('", "') }"`)
}
} while(!next)
return seen.length > 1 ? tag : undefined
// remove... // remove...
} else if(value == null){ } else if(value == null){
@ -187,17 +193,13 @@ var TagsPrototype = {
value = this.normalize(value) value = this.normalize(value)
// check for recursion... // check for recursion...
var chain = [value] var chain = []
var cur = value var target = resolve(value, chain)
var t = this.normalize(tag) if(target == tag || target == this.normalize(tag)){
do { throw new Error(`Creating a recursive alias chain: "${
cur = this.__aliases[cur] chain
|| this.__aliases[this.normalize(cur)] .concat([chain[0]])
chain.push(cur) .join('" -> "') }"`) }
if(cur == t){
throw new Error(`Creating a recursive alias chain: "${ chain.join('", "') }"`)
}
} while(cur)
this.__aliases[tag] = value this.__aliases[tag] = value
} }