Skip to content

Commit

Permalink
feat: include question in the filename
Browse files Browse the repository at this point in the history
  • Loading branch information
rpidanny committed Jul 4, 2024
1 parent 125354e commit 352ed52
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 11 deletions.
6 changes: 3 additions & 3 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ const config: JestConfigWithTsJest = {
],
coverageThreshold: {
global: {
statements: 95,
branches: 90,
statements: 96,
branches: 91,
functions: 94,
lines: 95,
lines: 96,
},
},
}
Expand Down
38 changes: 38 additions & 0 deletions src/services/search/paper-search.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
})
})
25 changes: 17 additions & 8 deletions src/services/search/paper-search.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ISearchOptions, 'keywords' | 'filterPattern' | 'question'>,
): 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,
'-',
)
Expand All @@ -65,7 +74,7 @@ export class PaperSearchService {
}

public async exportToCSV(filePath: string, opts: ISearchOptions): Promise<string> {
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,
Expand Down

0 comments on commit 352ed52

Please sign in to comment.