diff --git a/frontend/src/lib/__tests__/utils.spec.ts b/frontend/src/lib/__tests__/utils.spec.ts index 548f576b..09c205ab 100644 --- a/frontend/src/lib/__tests__/utils.spec.ts +++ b/frontend/src/lib/__tests__/utils.spec.ts @@ -7,6 +7,7 @@ import { infoFromQuery, isVariantMt, isVariantMtHomopolymer, + removeCommasFromNumbers, roundIt, search, separateIt @@ -111,6 +112,33 @@ describe.concurrent('isVariantMtHomopolymer method', () => { }) }) +describe.concurrent('removeCommasFromNumbers method', () => { + it('should remove commas from numbers', () => { + const result = removeCommasFromNumbers('1,234,567,890') + expect(result).toBe('1234567890') + }) + + it('should return the same string if no commas', () => { + const result = removeCommasFromNumbers('1234567890') + expect(result).toBe('1234567890') + }) + + it('should return the same string if empty', () => { + const result = removeCommasFromNumbers('') + expect(result).toBe('') + }) + + it('should not remove commas from strings', () => { + const result = removeCommasFromNumbers('foo,foo,bar') + expect(result).toBe('foo,foo,bar') + }) + + it('should not remove commas from numbers in strings', () => { + const result = removeCommasFromNumbers('foo,1,234,567,890,bar') + expect(result).toBe('foo,1234567890,bar') + }) +}) + describe.concurrent('search method', async () => { beforeEach(() => { // we make `DottyClient.toSpdi` return null / fail every time @@ -142,6 +170,39 @@ describe.concurrent('search method', async () => { }) }) + it('should return "variant" route location for Variant queries with commas in position', async () => { + const result = await search('chr37:12,345:A:G', 'ghcr37') + expect(result).toEqual({ + name: 'variant', + params: { + searchTerm: 'chr37:12345:A:G', + genomeRelease: 'ghcr37' + } + }) + }) + + it('should return "cnv" route location for SV quries', async () => { + const result = await search('DEL:chr37:12345:123456', 'ghcr37') + expect(result).toEqual({ + name: 'cnv', + params: { + searchTerm: 'DEL:chr37:12345:123456', + genomeRelease: 'ghcr37' + } + }) + }) + + it('should return "cnv" route location for SV quries with commas in start and end', async () => { + const result = await search('DEL:chr37:12,345:123,456', 'ghcr37') + expect(result).toEqual({ + name: 'cnv', + params: { + searchTerm: 'DEL:chr37:12345:123456', + genomeRelease: 'ghcr37' + } + }) + }) + it('should return "genes" route location for general queries', async () => { const result = await search('TP53', 'ghcr37') expect(result).toEqual({ diff --git a/frontend/src/lib/utils.ts b/frontend/src/lib/utils.ts index cca70c79..1fba0f78 100644 --- a/frontend/src/lib/utils.ts +++ b/frontend/src/lib/utils.ts @@ -82,6 +82,17 @@ export const isVariantMtHomopolymer = (smallVar: any): boolean => { } } +/** + * Removes commas from numbers within a string without affecting words. + * For example, it'll convert "chr17:41,197,708:T:G" to "chr17:41197708:T:G". + * + * @param str Input string possibly containing numbers with commas. + * @returns Sanitized string with commas removed from numbers. + */ +export function removeCommasFromNumbers(str: string): string { + return str.replace(/(\d),(?=\d)/g, '$1') +} + /** * Take a `searchTerm` and return a route location that can be used to navigate to * the correct page. @@ -164,6 +175,7 @@ export const search = async (searchTerm: string, genomeRelease: string) => { ] ] + searchTerm = removeCommasFromNumbers(searchTerm) for (const [regexp, getRoute] of SEARCH_REGEXPS) { if (regexp.test(searchTerm)) { const routeLocation = getRoute()