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: Add support for commas in numbers in search Queries (#147) #157

Merged
merged 1 commit into from
Oct 18, 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
61 changes: 61 additions & 0 deletions frontend/src/lib/__tests__/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
infoFromQuery,
isVariantMt,
isVariantMtHomopolymer,
removeCommasFromNumbers,
roundIt,
search,
separateIt
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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({
Expand Down
12 changes: 12 additions & 0 deletions frontend/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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()
Expand Down
Loading