1
0

fix(filters): replace project titles at the match position, not anywhere in the filter string

This fixes a bug where the project title would not be replaced correctly in cases where the project title contained parts of the word "project".

Resolves https://kolaente.dev/vikunja/vikunja/issues/2194
This commit is contained in:
kolaente 2024-03-12 22:05:26 +01:00
parent cf6b476b7d
commit e1c972d64d
No known key found for this signature in database
GPG Key ID: F40E70337AB24C9B
2 changed files with 20 additions and 3 deletions

View File

@ -18,7 +18,7 @@ describe('Filter Transformation', () => {
'labels': 'labels', 'labels': 'labels',
} }
describe('For api', () => { describe('For API', () => {
for (const c in fieldCases) { for (const c in fieldCases) {
it('should transform all filter params for ' + c + ' to snake_case', () => { it('should transform all filter params for ' + c + ' to snake_case', () => {
const transformed = transformFilterStringForApi(c + ' = ipsum', nullTitleToIdResolver, nullTitleToIdResolver) const transformed = transformFilterStringForApi(c + ' = ipsum', nullTitleToIdResolver, nullTitleToIdResolver)
@ -97,6 +97,16 @@ describe('Filter Transformation', () => {
expect(transformed).toBe('project in 1, 2') expect(transformed).toBe('project in 1, 2')
}) })
it('should resolve projects at the correct position', () => {
const transformed = transformFilterStringForApi(
'project = pr',
nullTitleToIdResolver,
(title: string) => 1,
)
expect(transformed).toBe('project = 1')
})
}) })
describe('To API', () => { describe('To API', () => {
@ -138,7 +148,7 @@ describe('Filter Transformation', () => {
expect(transformed).toBe('labels = lorem && dueDate = now && labels = ipsum') expect(transformed).toBe('labels = lorem && dueDate = now && labels = ipsum')
}) })
it('should correctly resolve multiple labels in', () => { it('should correctly resolve multiple labels in', () => {
const transformed = transformFilterStringFromApi( const transformed = transformFilterStringFromApi(
'labels in 1, 2', 'labels in 1, 2',

View File

@ -108,12 +108,19 @@ export function transformFilterStringForApi(
keywords = keyword.trim().split(',').map(k => k.trim()) keywords = keyword.trim().split(',').map(k => k.trim())
} }
let replaced = keyword
keywords.forEach(k => { keywords.forEach(k => {
const projectId = projectResolver(k) const projectId = projectResolver(k)
if (projectId !== null) { if (projectId !== null) {
filter = filter.replace(k, String(projectId)) replaced = replaced.replace(k, String(projectId))
} }
}) })
const actualKeywordStart = (match?.index || 0) + prefix.length
filter = filter.substring(0, actualKeywordStart) +
replaced +
filter.substring(actualKeywordStart + keyword.length)
} }
} }
}) })