-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- check plan on auth
- Loading branch information
Showing
4 changed files
with
135 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
const moment = require('moment') | ||
const chrono = require('chrono-node') | ||
const Task = require('imdone-core/lib/task') | ||
const _ = require('lodash') | ||
|
||
class Transformer { | ||
constructor (opts) { | ||
Object.assign(this, opts) | ||
} | ||
|
||
toJSON() { | ||
const pattern = this.pattern && this.pattern.toString() | ||
const clone = Object.assign({}, this) | ||
delete clone.config | ||
return Object.assign(clone,{pattern}) | ||
} | ||
|
||
parseDate(text, ref) { | ||
if (ref) ref = moment(ref) | ||
const results = new chrono.parse(text, ref) | ||
return moment(results[0].start.date()) | ||
} | ||
} | ||
|
||
const getTransformers = function (config) { | ||
return [ | ||
new Transformer({ | ||
config, | ||
name: "variable", | ||
pattern: /\$(\w+)/, | ||
vars: { | ||
now: (instance) => instance.parseDate('now').format(), | ||
today: (instance) => instance.parseDate('now').format('YYYY-MM-DD') | ||
}, | ||
exec: function (task) { | ||
return task.text.replace(this.pattern, (match, varName) => { | ||
if (!this.vars[varName]) return varName | ||
return this.vars[varName](this) | ||
}) | ||
} | ||
}), | ||
new Transformer({ | ||
config, | ||
name: "plain language due", | ||
pattern: /due\s.*?\./i, | ||
exec: function (task) { | ||
return task.text.replace(this.pattern, (match) => { | ||
const due = this.parseDate(match).format() | ||
return `due:${due}` | ||
}) | ||
} | ||
}), | ||
new Transformer({ | ||
config, | ||
name: "plain language remind", | ||
pattern: /remind me\s.*?(before)?\./i, | ||
exec: function (task) { | ||
return task.text.replace(this.pattern, (match, before) => { | ||
let dueDate | ||
if (before && task.meta.due) { | ||
dueDate = task.meta.due[0] | ||
match = match.replace('before', 'ago') | ||
} | ||
const remind = this.parseDate(match, dueDate).format() | ||
return `remind:${remind}` | ||
}) | ||
} | ||
}), | ||
new Transformer({ | ||
config, | ||
name: "autocomplete", | ||
exec: function (task) { | ||
const list = _.get(this.config, 'transformers.autocomplete.list') | ||
if (!list) return task.text | ||
if (task.meta.completed) return task.text | ||
if (task.list !== list) return task.text | ||
task.addMetaData("completed", this.parseDate('now').format()) | ||
task.updateMetaData() | ||
return task.text | ||
}, | ||
pattern: /^((?!\scompleted:).)*$/, | ||
list: _.get(config, 'transformers.autocomplete.list') | ||
}) | ||
] | ||
} | ||
|
||
const transformTask = function (task, transformers) { | ||
transformers.forEach(transformer => { | ||
task.text = transformer.exec(task) | ||
task.parseTodoTxt() | ||
}) | ||
return task | ||
} | ||
|
||
const transformTasks = function (config, tasks) { | ||
const transformers = getTransformers(config) | ||
return tasks.map(task => transformTask(new Task(task), transformers)) | ||
} | ||
|
||
module.exports = { | ||
getTransformers, | ||
transformTask, | ||
transformTasks, | ||
Transformer | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters