diff --git a/jest.config.ts b/jest.config.ts index b0d9a05..01526c2 100644 --- a/jest.config.ts +++ b/jest.config.ts @@ -48,10 +48,10 @@ const config: JestConfigWithTsJest = { ], coverageThreshold: { global: { - statements: 95, - branches: 90, + statements: 96, + branches: 91, functions: 94, - lines: 95, + lines: 96, }, }, } diff --git a/src/services/search/paper-search.service.spec.ts b/src/services/search/paper-search.service.spec.ts index 01b4994..9eb9738 100644 --- a/src/services/search/paper-search.service.spec.ts +++ b/src/services/search/paper-search.service.spec.ts @@ -292,4 +292,42 @@ describe('PaperSearchService', () => { expect(mockCsvWriter.end).toHaveBeenCalled() }) }) + + describe('getFilePath', () => { + it('should return the given path if it is not a directory', () => { + const path = service.getFilePath('file.csv', { keywords: 'some keywords' }) + + expect(path).toBe('file.csv') + }) + + it('should return a generated path if the given path is a directory', () => { + jest.spyOn(ioService, 'isDirectory').mockReturnValue(true) + + const path = service.getFilePath('data/exports/', { keywords: 'some keywords' }) + + expect(path).toBe('data/exports/some-keywords_1719677868856.csv') + }) + + it('should return a generated path with filter pattern if the given path is a directory and filterPattern is provided', () => { + jest.spyOn(ioService, 'isDirectory').mockReturnValue(true) + + const path = service.getFilePath('data/exports/', { + keywords: 'some keywords', + filterPattern: 'cas9', + }) + + expect(path).toBe('data/exports/some-keywords_cas9_1719677868856.csv') + }) + + it('should return a generated path with question if the given path is a directory and question is provided', () => { + jest.spyOn(ioService, 'isDirectory').mockReturnValue(true) + + const path = service.getFilePath('data/exports/', { + keywords: 'some keywords', + question: 'Is it better than RNN?', + }) + + expect(path).toBe('data/exports/some-keywords_Is-it-better-than-RNN-_1719677868856.csv') + }) + }) }) diff --git a/src/services/search/paper-search.service.ts b/src/services/search/paper-search.service.ts index 5b17d84..41a6f8e 100644 --- a/src/services/search/paper-search.service.ts +++ b/src/services/search/paper-search.service.ts @@ -48,15 +48,24 @@ export class PaperSearchService { return papers } - public getFilePath(path: string, keywords: string, filterPattern?: string): string { - if (!this.ioService.isDirectory(path)) { - return path - } + public getFilePath( + path: string, + { + filterPattern, + keywords, + question, + }: Pick, + ): string { + if (!this.ioService.isDirectory(path)) return path + + const sanitize = (input?: string) => input?.replace(/[^\w-]/g, '-') ?? '' + + const sanitizedFilter = sanitize(filterPattern) + const sanitizedKeywords = sanitize(keywords) + const sanitizedQuestion = sanitize(question) - const sanitizedFilter = filterPattern?.replace(/[^\w-]/g, '-') - const sanitizedKeywords = keywords.replace(/[^\w-]/g, '-') const fileName = - `${sanitizedKeywords}${sanitizedFilter ? `_${sanitizedFilter}` : ''}_${Date.now()}.csv`.replace( + `${sanitizedKeywords}${sanitizedFilter ? `_${sanitizedFilter}` : ''}${sanitizedQuestion ? `_${sanitizedQuestion}` : ''}_${Date.now()}.csv`.replace( /-+/g, '-', ) @@ -65,7 +74,7 @@ export class PaperSearchService { } public async exportToCSV(filePath: string, opts: ISearchOptions): Promise { - const fullPath = this.getFilePath(filePath, opts.keywords, opts.filterPattern) + const fullPath = this.getFilePath(filePath, opts) const outputWriter = await this.ioService.getCsvStreamWriter(fullPath) await this.search({ ...opts,