2021-07-05 12:29:04 +02:00
|
|
|
import {parseDate} from './time/parseDate'
|
|
|
|
import priorities from '../models/priorities.json'
|
|
|
|
|
2021-07-06 14:53:32 +02:00
|
|
|
const LABEL_PREFIX = '@'
|
|
|
|
const LIST_PREFIX = '#'
|
2021-07-05 12:29:04 +02:00
|
|
|
const PRIORITY_PREFIX = '!'
|
2021-07-06 14:53:32 +02:00
|
|
|
const ASSIGNEE_PREFIX = '+'
|
2021-07-05 12:29:04 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses task text for dates, assignees, labels, lists, priorities and returns an object with all found intents.
|
|
|
|
*
|
|
|
|
* @param text
|
|
|
|
*/
|
|
|
|
export const parseTaskText = text => {
|
|
|
|
const result = {
|
|
|
|
text: text,
|
|
|
|
date: null,
|
|
|
|
labels: [],
|
|
|
|
list: null,
|
|
|
|
priority: null,
|
|
|
|
assignees: [],
|
|
|
|
}
|
|
|
|
|
|
|
|
result.labels = getItemsFromPrefix(text, LABEL_PREFIX)
|
|
|
|
|
|
|
|
const lists = getItemsFromPrefix(text, LIST_PREFIX)
|
|
|
|
result.list = lists.length > 0 ? lists[0] : null
|
|
|
|
|
|
|
|
result.priority = getPriority(text)
|
|
|
|
|
|
|
|
result.assignees = getItemsFromPrefix(text, ASSIGNEE_PREFIX)
|
|
|
|
|
|
|
|
const {newText, date} = parseDate(text)
|
|
|
|
result.text = newText
|
|
|
|
result.date = date
|
|
|
|
|
|
|
|
return cleanupResult(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
const getItemsFromPrefix = (text, prefix) => {
|
|
|
|
const items = []
|
|
|
|
|
|
|
|
const itemParts = text.split(prefix)
|
|
|
|
itemParts.forEach((p, index) => {
|
|
|
|
// First part contains the rest
|
|
|
|
if (index < 1) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let labelText
|
2021-07-19 11:18:22 +02:00
|
|
|
if (p.charAt(0) === '\'') {
|
|
|
|
labelText = p.split('\'')[1]
|
|
|
|
} else if (p.charAt(0) === '"') {
|
|
|
|
labelText = p.split('"')[1]
|
2021-07-05 12:29:04 +02:00
|
|
|
} else {
|
|
|
|
// Only until the next space
|
|
|
|
labelText = p.split(' ')[0]
|
|
|
|
}
|
|
|
|
items.push(labelText)
|
|
|
|
})
|
|
|
|
|
|
|
|
return Array.from(new Set(items))
|
|
|
|
}
|
|
|
|
|
|
|
|
const getPriority = text => {
|
|
|
|
const ps = getItemsFromPrefix(text, PRIORITY_PREFIX)
|
|
|
|
if (ps.length === 0) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const p of ps) {
|
|
|
|
for (const pi in priorities) {
|
|
|
|
if (priorities[pi] === parseInt(p)) {
|
|
|
|
return parseInt(p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
const cleanupItemText = (text, items, prefix) => {
|
|
|
|
items.forEach(l => {
|
|
|
|
text = text
|
|
|
|
.replace(`${prefix}'${l}' `, '')
|
|
|
|
.replace(`${prefix}'${l}'`, '')
|
|
|
|
.replace(`${prefix}"${l}" `, '')
|
|
|
|
.replace(`${prefix}"${l}"`, '')
|
|
|
|
.replace(`${prefix}${l} `, '')
|
|
|
|
.replace(`${prefix}${l}`, '')
|
|
|
|
})
|
|
|
|
return text
|
|
|
|
}
|
|
|
|
|
|
|
|
const cleanupResult = result => {
|
|
|
|
result.text = cleanupItemText(result.text, result.labels, LABEL_PREFIX)
|
|
|
|
result.text = cleanupItemText(result.text, [result.list], LIST_PREFIX)
|
|
|
|
result.text = cleanupItemText(result.text, [result.priority], PRIORITY_PREFIX)
|
|
|
|
result.text = cleanupItemText(result.text, result.assignees, ASSIGNEE_PREFIX)
|
|
|
|
result.text = result.text.trim()
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|