forked from jmhobbs/jsTodoTxt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsTodoTxt.min.js
9 lines (9 loc) · 3.56 KB
/
jsTodoTxt.min.js
1
2
3
4
5
6
7
8
9
/*!
* jsTodoTxt v0.4.0
* Built on 2013-10-02
* https://github.com/jmhobbs/jsTodoTxt
*
* Copyright 2011-2013, John Hobbs
* Licensed under the MIT license.
* https://github.com/jmhobbs/jsTodoTxt/blob/master/LICENSE
*/function TodoTxtItem(a){this.reset=function(){this.text=null,this.priority=null,this.complete=!1,this.completed=null,this.date=null,this.contexts=null,this.projects=null},this.dateString=function(){return this.date?this.date.getFullYear()+"-"+(this.date.getMonth()+1<10?"0":"")+(this.date.getMonth()+1)+"-"+(this.date.getDate()<10?"0":"")+this.date.getDate():null},this.completedString=function(){return this.completed?this.completed.getFullYear()+"-"+(this.completed.getMonth()+1<10?"0":"")+(this.completed.getMonth()+1)+"-"+(this.completed.getDate()<10?"0":"")+this.completed.getDate():null},this.toString=function(){var a=this.text;return null!==this.date&&(a=this.dateString()+" "+a),null!==this.priority&&(a="("+this.priority+") "+a),this.complete&&null!==this.completed&&(a="x "+this.completedString()+" "+a),null!==this.projects&&(a=a+" +"+this.projects.join(" +")),null!==this.contexts&&(a=a+" @"+this.contexts.join(" @")),a},this.parse=function(a){var b;this.reset(),a=a.replace(TodoTxt._trim_re,""),a=a.replace("\r","");var c=TodoTxt._complete_re.exec(a);null!==c&&(this.complete=!0,b=c[1].split("-"),this.completed=new Date(b[0],b[1]-1,b[2]),a=a.replace(TodoTxt._complete_replace_re,""));var d=TodoTxt._priority_re.exec(a);null!==d&&(this.priority=d[1],a=a.replace(TodoTxt._priority_replace_re,""));var e=TodoTxt._date_re.exec(a);null!==e&&(b=e[1].split("-"),this.date=new Date(b[0],b[1]-1,b[2]),a=a.replace(TodoTxt._date_replace_re,""));var f=a.match(TodoTxt._context_re);if(null!==f){var g;for(this.contexts=[],g=0;g<f.length;g++)this.contexts.push(f[g].substr(1));a=a.replace(TodoTxt._context_replace_re," ")}var h=a.match(TodoTxt._project_re);if(null!==h){for(this.projects=[],g=0;g<h.length;g++)this.projects.push(h[g].substr(1));a=a.replace(TodoTxt._project_replace_re," ")}if(a=a.replace(TodoTxt._trim_re,""),this.text=a,""===this.text)throw new Exception("Empty Task")},this._getNumOfNulls=function(a,b){return null==a&&null==b?2:null==a||null==b?1:0},this._arraysAreEqual=function(a,b){if(2==this._getNumOfNulls(a,b))return!0;if(1==this._getNumOfNulls(a,b))return!1;if(a.length!=b.length)return!1;for(item in a)if(-1==b.indexOf(a[item]))return!1;return!0},this._datesAreEqual=function(a,b){return 2==this._getNumOfNulls(a,b)?!0:1==this._getNumOfNulls(a,b)?!1:0==a.setHours(0,0,0,0)-b.setHours(0,0,0,0)},this.equals=function(a){var b=["date","completed"];for(i in b)if(!this._datesAreEqual(this[b[i]],a[b[i]]))return!1;var c=["complete","priority","text"];for(i in c)if(this[c[i]]!=a[c[i]])return!1;var d=["projects","contexts"];for(i in d)if(!this._arraysAreEqual(this[d[i]],a[d[i]]))return!1;return!0},"string"==typeof a?this.parse(a):this.reset()}var TodoTxt={_trim_re:/^\s+|\s+$/g,_complete_re:/^x\s([0-9]{4}-[0-9]{1,2}-[0-9]{1,2})\s+/i,_complete_replace_re:/^x\s([0-9]{4}-[0-9]{1,2}-[0-9]{1,2})\s+/i,_date_re:/^([0-9]{4}-[0-9]{1,2}-[0-9]{1,2})/,_date_replace_re:/^[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}\s*/,_priority_re:/^\(([A-Z])\)/,_priority_replace_re:/^\([A-Z]\)\s*/,_context_re:/@(\S+)/g,_context_replace_re:/\s*@\S+\s*/g,_project_re:/\+(\S+)/g,_project_replace_re:/\s*\+\S+\s*/g,parse:function(a){var b,c=[],d=a.split("\n");for(b=0;b<d.length;b++)try{c.push(new TodoTxtItem(d[b]))}catch(e){}return c},parseLine:function(a){return new TodoTxtItem(a)},render:function(a){var b,c=[];for(b in a)a.hasOwnProperty(b)&&c.push(a[b].toString());return c.join("\n")},renderItem:function(a){return a.toString()}};