-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for replaceCommaSeparatedValue
- Loading branch information
Showing
7 changed files
with
80 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
static/app/components/searchQueryBuilder/tokens/filter/replaceCommaSeparatedValue.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import {replaceCommaSeparatedValue} from 'sentry/components/searchQueryBuilder/tokens/filter/replaceCommaSeparatedValue'; | ||
|
||
describe('replaceCommaSeparatedValue', function () { | ||
it('replaces a value without commas', function () { | ||
expect(replaceCommaSeparatedValue('foo', 3, 'bar')).toBe('bar'); | ||
}); | ||
|
||
it('replaces an empty value at end', function () { | ||
expect(replaceCommaSeparatedValue('foo,', 4, 'bar')).toBe('foo,bar'); | ||
}); | ||
|
||
it('replaces an empty value at start', function () { | ||
expect(replaceCommaSeparatedValue(',foo', 0, 'bar')).toBe('bar,foo'); | ||
}); | ||
|
||
it('replaces an empty value in middle', function () { | ||
expect(replaceCommaSeparatedValue('foo,,baz', 4, 'bar')).toBe('foo,bar,baz'); | ||
}); | ||
|
||
it('replaces an non-empty value at end', function () { | ||
expect(replaceCommaSeparatedValue('foo,abc', 4, 'bar')).toBe('foo,bar'); | ||
}); | ||
|
||
it('replaces an non-empty value at start', function () { | ||
expect(replaceCommaSeparatedValue('abc,foo', 0, 'bar')).toBe('bar,foo'); | ||
}); | ||
|
||
it('replaces an non-empty value in middle', function () { | ||
expect(replaceCommaSeparatedValue('foo,abc,baz', 4, 'bar')).toBe('foo,bar,baz'); | ||
}); | ||
}); |
41 changes: 41 additions & 0 deletions
41
static/app/components/searchQueryBuilder/tokens/filter/replaceCommaSeparatedValue.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import {parseMultiSelectFilterValue} from 'sentry/components/searchQueryBuilder/tokens/filter/parsers/string/parser'; | ||
|
||
/** | ||
* Replaces the focused parameter (at cursorPosition) with the new value. | ||
* If cursorPosition is null, will default to the end of the string. | ||
* | ||
* Example: | ||
* replaceCommaSeparatedValue('foo,bar,baz', 5, 'new') => 'foo,new,baz' | ||
*/ | ||
export function replaceCommaSeparatedValue( | ||
value: string, | ||
cursorPosition: number | null, | ||
replacement: string | ||
) { | ||
const parsed = parseMultiSelectFilterValue(value); | ||
|
||
if (!parsed) { | ||
return value; | ||
} | ||
|
||
if (cursorPosition === null) { | ||
cursorPosition = value.length; | ||
} | ||
|
||
const matchingIndex = parsed.items.findIndex( | ||
item => | ||
item.value && | ||
item.value?.location.start.offset <= cursorPosition && | ||
item.value?.location.end.offset >= cursorPosition | ||
); | ||
|
||
if (matchingIndex === -1) { | ||
return replacement; | ||
} | ||
|
||
return [ | ||
...parsed.items.slice(0, matchingIndex).map(item => item.value?.text ?? ''), | ||
replacement, | ||
...parsed.items.slice(matchingIndex + 1).map(item => item.value?.text ?? ''), | ||
].join(','); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters