2021-07-25 12:45:17 +02:00
|
|
|
import {parseDate} from '../helpers/time/parseDate'
|
|
|
|
import _priorities from '../models/priorities.json'
|
|
|
|
|
|
|
|
const LABEL_PREFIX: string = '@'
|
|
|
|
const LIST_PREFIX: string = '#'
|
|
|
|
const PRIORITY_PREFIX: string = '!'
|
|
|
|
const ASSIGNEE_PREFIX: string = '+'
|
|
|
|
|
|
|
|
const priorities: Priorites = _priorities
|
|
|
|
|
|
|
|
interface Priorites {
|
|
|
|
UNSET: number,
|
|
|
|
LOW: number,
|
|
|
|
MEDIUM: number,
|
|
|
|
HIGH: number,
|
|
|
|
URGENT: number,
|
|
|
|
DO_NOW: number,
|
|
|
|
}
|
2021-07-05 12:29:04 +02:00
|
|
|
|
2021-07-25 12:45:17 +02:00
|
|
|
interface ParsedTaskText {
|
|
|
|
text: string,
|
|
|
|
date: Date | null,
|
|
|
|
labels: string[],
|
|
|
|
list: string | null,
|
|
|
|
priority: number | null,
|
|
|
|
assignees: string[],
|
|
|
|
}
|
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
|
|
|
|
*/
|
2021-07-25 12:45:17 +02:00
|
|
|
export const parseTaskText = (text: string): ParsedTaskText => {
|
|
|
|
const result: ParsedTaskText = {
|
2021-07-05 12:29:04 +02:00
|
|
|
text: text,
|
|
|
|
date: null,
|
|
|
|
labels: [],
|
|
|
|
list: null,
|
|
|
|
priority: null,
|
|
|
|
assignees: [],
|
|
|
|
}
|
|
|
|
|
|
|
|
result.labels = getItemsFromPrefix(text, LABEL_PREFIX)
|
|
|
|
|
2021-07-25 12:45:17 +02:00
|
|
|
const lists: string[] = getItemsFromPrefix(text, LIST_PREFIX)
|
2021-07-05 12:29:04 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2021-07-25 12:45:17 +02:00
|
|
|
const getItemsFromPrefix = (text: string, prefix: string): string[] => {
|
|
|
|
const items: string[] = []
|
2021-07-05 12:29:04 +02:00
|
|
|
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2021-07-25 12:45:17 +02:00
|
|
|
const getPriority = (text: string): number | null => {
|
2021-07-05 12:29:04 +02:00
|
|
|
const ps = getItemsFromPrefix(text, PRIORITY_PREFIX)
|
|
|
|
if (ps.length === 0) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const p of ps) {
|
2021-07-25 12:45:17 +02:00
|
|
|
for (const pi of Object.values(priorities)) {
|
|
|
|
if (pi === parseInt(p)) {
|
2021-07-05 12:29:04 +02:00
|
|
|
return parseInt(p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
2021-07-25 12:45:17 +02:00
|
|
|
const cleanupItemText = (text: string, items: string[], prefix: string): string => {
|
2021-07-05 12:29:04 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-07-25 12:45:17 +02:00
|
|
|
const cleanupResult = (result: ParsedTaskText): ParsedTaskText => {
|
2021-07-05 12:29:04 +02:00
|
|
|
result.text = cleanupItemText(result.text, result.labels, LABEL_PREFIX)
|
2021-07-25 12:45:17 +02:00
|
|
|
result.text = result.list !== null ? cleanupItemText(result.text, [result.list], LIST_PREFIX) : result.text
|
|
|
|
result.text = result.priority !== null ? cleanupItemText(result.text, [String(result.priority)], PRIORITY_PREFIX) : result.text
|
2021-07-05 12:29:04 +02:00
|
|
|
result.text = cleanupItemText(result.text, result.assignees, ASSIGNEE_PREFIX)
|
|
|
|
result.text = result.text.trim()
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|