Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: extract tags from task summary #2425

Merged
merged 1 commit into from
Dec 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/store/storeHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -449,11 449,33 @@ function searchSubTasks(task, searchQuery) {
})
}

/**
* Parses a string to extract tags and a summary
*
* @param {string} str The string
* @return {object} The object containing the parsed results
*/
function parseString(str) {
const matches = str.matchAll(/\s?#([^\s#] )/g)
let summary = str
const tags = []
for (const match of matches) {
tags.push(match[1])
summary = summary.replace(match[0], '')
}
summary = summary.trim()
return {
summary,
tags,
}
}

export {
isTaskInList,
overdue,
isParentInList,
sort,
momentToICALTime,
searchSubTasks,
parseString,
}
7 changes: 5 additions & 2 deletions src/store/tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 23,7 @@

import { Calendar } from './calendars.js'
import { findVTODObyUid } from './cdav-requests.js'
import { isParentInList, momentToICALTime } from './storeHelper.js'
import { isParentInList, momentToICALTime, parseString } from './storeHelper.js'
import SyncStatus from '../models/syncStatus.js'
import Task from '../models/task.js'

Expand Down Expand Up @@ -295,7 295,7 @@
appendTasks(state, tasks = []) {
state.tasks = tasks.reduce(function(list, task) {
if (task instanceof Task) {
Vue.set(list, task.key, task)

Check warning on line 298 in src/store/tasks.js

View workflow job for this annotation

GitHub Actions / NPM lint

Caution: `Vue` also has a named export `set`. Check if you meant to write `import {set} from 'vue'` instead
} else {
console.error('Wrong task object', task)
}
Expand Down Expand Up @@ -703,8 703,11 @@
}
const task = new Task('BEGIN:VCALENDAR\nVERSION:2.0\nPRODID:-//Nextcloud Tasks v' appVersion '\nEND:VCALENDAR', taskData.calendar)

const parsed = parseString(taskData.summary)

task.created = ICAL.Time.now()
task.summary = taskData.summary
task.summary = parsed.summary
task.tags = parsed.tags
task.hidesubtasks = 0
if (taskData.priority) {
task.priority = taskData.priority
Expand Down
32 changes: 30 additions & 2 deletions tests/javascript/unit/store/storeHelper.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 1,4 @@
import { sort } from '../../../../src/store/storeHelper.js'
import { sort, parseString } from '../../../../src/store/storeHelper.js'
import Task from '../../../../src/models/task.js'

import { loadICS } from '../../../assets/loadAsset.js'
Expand All @@ -14,7 14,7 @@ const vCalendarNames = [

const tasks = vCalendarNames.map((vCalendar) => { return new Task(loadICS(vCalendar)) })

describe('storeHelper', () => {
describe('storeHelper - sort', () => {
'use strict'

it('Tests descending sort by due date.', () => {
Expand All @@ -31,3 31,31 @@ describe('storeHelper', () => {
expect(receivedTasks).toEqual(expectedTasks)
})
})

describe('storeHelper - parseString', () => {
'use strict'

it('returns the whole summary if no delimiters are present', () => {
const summary = 'plain summary without special delimiters'
const result = parseString(summary)
expect(result).toEqual({ summary, tags: [] })
})

it('returns the summary and single tag found', () => {
const summary = 'summary and #tag'
const result = parseString(summary)
expect(result).toEqual({ summary: 'summary and', tags: ['tag'] })
})

it('returns the summary and multiple tags found', () => {
const summary = 'summary and #tag1 #tag2'
const result = parseString(summary)
expect(result).toEqual({ summary: 'summary and', tags: ['tag1', 'tag2'] })
})

it('returns the summary and multiple tags found in varying order', () => {
const summary = '#tag1 summary and #tag2 #tag3 more summary #tag4'
const result = parseString(summary)
expect(result).toEqual({ summary: 'summary and more summary', tags: ['tag1', 'tag2', 'tag3', 'tag4'] })
})
})
Loading