User:Bluedeck/source/identify.js
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google Chrome、Firefox、Microsoft Edge及Safari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
let Id = (function(){
function tested(str_or_regex, candidate)
{
if(typeof str_or_regex === "string")
return str_or_regex === candidate;
else if(str_or_regex instanceof RegExp)
return str_or_regex.test(candidate);
else
return false;
}
class Identify
{
constructor(left = "(", assigner = ":", right = ")") // for the sake of rapid development let's say these are strings not regexps.
{
let message = "";
let memory = {};
this.set = function(string)
{
message = string;
memory = {};
return this;
};
this.find = function(key) // this key can be regexp.
{
if(key in memory)
return memory[key];
let array = message.split(left || right);
let found = false;
let content = [];
for(let i=0; i<array.length; i++)
{
let chamber = array[i].split(right || left)[0];
let index = chamber.indexOf(assigner);
if(index === -1)
continue;
let candidate_key = chamber.substr(0, index);
if(!tested(key, candidate_key))
continue;
content.push(chamber.substr(index + assigner.length));
found = true;
}
return found ? memory[key] = content : null;
};
this.change = function(key, content)
{
memory[key] = String(content);
return this;
};
this.get = function()
{
};
}
}
return Identify;
})();