-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnote.min.js
12 lines (12 loc) · 4.73 KB
/
note.min.js
1
2
3
4
5
6
7
8
9
10
11
12
function Note(a){if("string"===typeof a)return this.loadFromString(a);for(var b in a)a.hasOwnProperty(b)&&(this[b]="object"===typeof a[b]?new Note(a[b]):a[b])}Note.strRepeat=function(a,b){for(var c="",d=0;d<b;d++)c+=" ";return c};Note.union=function(){var a=Note.unionSingle(arguments[0],arguments[1]),b;for(b in arguments)if(1!==b&&(a=Note.unionSingle(a,arguments[b]),!a.length()))break;return a};
Note.unionSingle=function(a,b){var c=new Note;if(!(b instanceof Note))return c;for(var d in a.names())a[d]instanceof Note&&(b[d]&&b[d]instanceof Note)&&(c[d]=Note.unionSingle(a[d],b[d])),a[d]===b[d]&&(c[d]=a[d]);return c};Note.prototype.privates=[];Note.prototype.clear=function(){for(var a in this.names())delete this[a];return this};Note.prototype.clone=function(){return new Note(this)};
Note.prototype.diff=function(a){var b=new Note;a instanceof Note||(a=new Note(a));for(var c in this.names())if(c in a)if(typeof this[c]!=typeof a[c])"object"===typeof a[c]?b[c]=new Note(a[c]):this[c]!=a[c]&&(b[c]=a[c]);else if("object"!=typeof this[c])this[c]!=a[c]&&(b[c]=a[c]);else{var d=this[c].diff(a[c]);d.length()&&(b[c]=d)}else b[c]="";for(c in a.names())if(!this.isPublicProperty(c)&&(!(a instanceof Note)||a.isPublicProperty(c)))b[c]="object"!=typeof a[c]?a[c]:a[c]instanceof Note?new Note(a[c].toString()):
new Note(a);return b};Note.prototype.get=function(a,b){if(!a)return!1;b||(b=this);var c=a.split(/ /g),d=c.shift();return!(d in b)?!1:!c.length?b[d]:this.get(c.join(" "),b[d])};Note.prototype.isPrivateProperty=function(a){for(var b in this.privates)if(this.privates[b]===a)return!0;return!1};Note.prototype.isPublicProperty=function(a){return!this.hasOwnProperty(a)?!1:!this.isPrivateProperty(a)};Note.prototype.length=function(){var a=0,b;for(b in this)this.isPublicProperty(b)&&a++;return a};
Note.prototype.loadFromString=function(a){a=a.replace(/^[\n ]*/,"");a=a.replace(/\n\r/g,"\n").replace(/\r\n/g,"\n");a=a.replace(/[\n ]*$/,"");a=a.replace(/\n\n+/,"\n");a=a.split(/\n(?! )/g);for(var b in a){var c=a[b];if(matches=c.match(/^([^ ]+)(\n|$)/))this[matches[1]]=new Note(c.substr(matches[1].length).replace(/\n /g,"\n"));else if(matches=c.match(/^([^ ]+) /))this[matches[1]]=c.substr(matches[1].length+1).replace(/^\n /,"").replace(/\n /g,"\n")}return this};
Note.prototype.names=function(){var a={},b;for(b in this)this.isPublicProperty(b)&&(a[b]=b);return a};Note.prototype.next=function(a){var b=this.toArray();a=b.indexOf(a)+1;return b[a]?b[a]:b[0]};
Note.prototype.patch=function(a){"string"===typeof a&&(a=new Note(a));for(var b in a)a.hasOwnProperty(b)&&!this.isPrivateProperty(b)&&("string"===typeof a[b]?""===a[b]?delete this[b]:this[b]=a[b]:"number"===typeof a[b]?this[b]=a[b]:a[b]instanceof Note&&!a[b].length()?delete this[b]:this[b]=this[b]instanceof Note?this[b].patch(a[b]):new Note(a[b]));return this};Note.prototype.prev=function(a){var b=this.toSortedArray();a=b.indexOf(a)-1;return 0<=a?b[a]:b[b.length-1]};
Note.prototype.retrieve=function(a){var b=new Note,c;for(c in a.names())this[c]&&(!(a[c]instanceof Note)||!a[c].length()?b[c]=this[c]:this[c]instanceof Note&&(b[c]=this[c].retrieve(a[c])));return b};Note.prototype.set=function(a,b){for(var c=a.split(/ /g),d=new Note,e=d,f=0;f<c.length-1;f++)e=e[c[f]]=new Note;e[c[f]]=b;this.patch(d);return this};Note.prototype.stringify=function(){return this.toJavascript()};
Note.prototype.toArray=function(){var a=[],b;for(b in this)this.hasOwnProperty(b)&&a.push(b);return a};Note.prototype.toJavascript=function(){return"new Note('"+this.toString().replace(/\n/g,"\\n").replace(/\'/g,"\\'")+"')"};Note.prototype.toJSON=function(){return JSON.stringify(this)};Note.prototype.toObject=function(){var a={},b;for(b in this)this.isPublicProperty(b)&&(a[b]=this[b]instanceof Note?this[b].toObject():this[b]);return a};
Note.prototype.toSortedArray=function(){var a=[],b;for(b in this.names())this.isPublicProperty(b)&&a.push(b);return a=a.sort()};Note.prototype.toSortedNote=function(a){var b=[],c;for(c in this.names())b.push(c);b.sort();a&&b.reverse();a=new Note;for(var d in b)a[b[d]]=this[b[d]];return a};Note.prototype.toSortedNoteBy=function(a,b){var c=[],d;for(d in this.names())c.push([d,this[d][a]]);c=c.sort(function(a,c){return b?c[1]-a[1]:a[1]-c[1]});d=new Note;for(var e in c)d[c[e][0]]=this[c[e][0]];return d};
Note.prototype.toString=function(a){a=a||0;var b="",c;for(c in this)this.isPublicProperty(c)&&("undefined"===typeof this[c]?b+="\n":(b+=Note.strRepeat(" ",a)+c,b=this[c]instanceof Note?b+("\n"+this[c].toString(a+1)):"object"===typeof this[c]?b+("\n"+(new Note(this[c])).toString(a+1)):""===this[c].toString()?b+"\n":this[c].toString().match(/\n/)?b+(" \n"+Note.strRepeat(" ",a+1)+this[c].toString().replace(/\n/g,"\n"+Note.strRepeat(" ",a+1))+"\n"):b+(" "+this[c].toString()+"\n")));return b};
"undefined"!=typeof exports&&(module.exports=Note);