todo.txt parser
Converts todo.txt files into javascript objects
Install
Install via npm:
npm i --save todo-parser
Usage
Right now you can parse todo.txt files and strings to js objects. The other way is right now not possible but coming soon!
Single line parser
To parse a single todo.txt task line like this one:
'(A) 2017-10-03 Write great readme @todo-parser +github'
into its javscript object representation you can use the Parser
class:
const Parser = Parser; let parser = ; let taskObject = parser;
Out comes a taskObject looking like this:
original: '(A) test message @todo-parser +github' priority: 'A' creationDate: Date'2017-10-03' completionDate: null description: 'test message' context: 'github' project: 'todo-parser' done: false
File parser
To go nuts and parse a complete todo.txt file into an array of todo objects there is the FileParser
class. In contrary to the Parser
it works asynchronious and takes two arguments:
- The file path to the todo.txt file
- The callback to execute after parsing the file or an error occured
const FileParser = FileParser; let parser = ; parser;
Custom parser
The package comes with a bunch of default parser for the standard todo.txt format. If you want to parse additional attributes like the popular due:2017-10-12
you can register your own parsing methods.
The method you provide will get passed the a data object you can modify and extend. It also contains the todo line to parse and looks like this:
original: '(A) test message @todo-parser +github' residue: ' test message ' priority: 'A' creationDate: Date'2017-10-03' completionDate: null description: 'test message' context: 'github' project: 'todo-parser' done: false
The data.original
attribute contains the complete todo.txt line.
In the residue
attribute a subset is stored from which all parsed attributes are removed. After all todo.txt parts are parsed it will be used as the description
, so if you don't want your special attribute to appear in the todo description, remove it from the residue
;) .
To add a custom method to the Parser you can pass it via the #register()
method.
So if you want to parse out the due:2017-10-12
lines for example, you can register following method to the parser:
let { let match = dataoriginal if match !== null datadueDate = match1 dataresidue = dataresidue }parserlet data = parserlet expected = '2017-10-30'assert
Override default parser
If you don't like the way todo-parser parses the default values you can override the standard parser with your own methods. I wouldn't like that but I give you the opportunity to do it so I shouldn't moan about it ;)
So if you want to do this blasphemy you can register your own method with the parser.override(name, method)
function.
There are 7 default parser:
- done
- priority
- creationDate
- completionDate
- context
- project
- description
The override method takes two arguments:
- The name of the default parser to override
- The method to use instead
let { //do something}parser