User:Bluedeck/source/noty.js
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google Chrome、Firefox、Microsoft Edge及Safari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
function acceptable_type(variable)
{
const type = typeof variable;
return (type === "string" || type === "symbol");
}
class Noty
{
constructor()
{
let note = {};
this.note = function(name = null, content = true) // always return this. it puts things into memory the returns this so you can chain it.
{
if(acceptable_type(name))
{
name in note ? note[name].push(content) : note[name] = [content];
}
return this;
};
this.new = function(name = null, content = true)
{
if(this.noting(name))
throw new Error("Cannot recreate existing note using assertive new method.");
else
return this.note(name, content);
};
this.noted = function(name = null) // INCONSISTENT RETURN VALUE returns the thing being noted and take it off memory. if nothing noted, return null. cannot distinguish between a null note and not noted.
{
if(acceptable_type(name) && name in note)
{
const content = note[name].pop();
if(note[name].length === 0)
delete note[name];
return content;
}
else
return null;
// noted returns data or null, but data itself could be null, in which case,
// noted cannot distinguish between whether something is not noted or something is noted as null. to determine that, ask 'notes' for help.
};
this.noting = function(name = null) // always returns true or false indicating if the entry is being noted.
{
if(acceptable_type(name))
return name in note;
else
return false;
};
this.dismiss = function(name = null) // INCONSISTENT RETURN VALUE returns array if the dismissed note exists, otherwise returns null
{
let note_array = name in note ? note[name] : null;
delete note[name];
return note_array;
};
this.notes = function() // always returns array, even when nothing is noted, then it returns empty array.
{
return Object.keys(note);
};
}
}
module.exports = Noty;