Skip to content

Commit f8e94be

Browse files
committed
Adds tests and fixes quoteIfSpecialCharacters
Adds comprehensive unit tests for the quoteIfSpecialCharacters helper function. Improves the logic of the quoteIfSpecialCharacters function to correctly handle empty strings, already quoted strings, and whitespace. Trims the input before special character detection to avoid unnecessary quoting.
1 parent 3794855 commit f8e94be

File tree

2 files changed

+65
-5
lines changed

2 files changed

+65
-5
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { describe, expect, it } from 'vitest';
2+
3+
import { quoteIfSpecialCharacters } from './helpers.svelte';
4+
5+
describe('helpers.svelte', () => {
6+
it('quoteIfSpecialCharacters handles tabs and newlines', () => {
7+
expect(quoteIfSpecialCharacters('foo\tbar')).toBe('"foo\tbar"');
8+
expect(quoteIfSpecialCharacters('foo\nbar')).toBe('"foo\nbar"');
9+
});
10+
11+
it('quoteIfSpecialCharacters handles empty string and undefined/null', () => {
12+
expect(quoteIfSpecialCharacters('')).toBe('');
13+
expect(quoteIfSpecialCharacters(undefined)).toBeUndefined();
14+
expect(quoteIfSpecialCharacters(null)).toBeNull();
15+
});
16+
17+
it('quoteIfSpecialCharacters quotes strings with multiple spaces', () => {
18+
expect(quoteIfSpecialCharacters('foo bar')).toBe('"foo bar"');
19+
});
20+
21+
it('quoteIfSpecialCharacters quotes strings with leading and trailing spaces', () => {
22+
expect(quoteIfSpecialCharacters(' foo bar ')).toBe('"foo bar"');
23+
});
24+
25+
it('quoteIfSpecialCharacters does not alter already quoted strings', () => {
26+
expect(quoteIfSpecialCharacters('"foo bar"')).toBe('"foo bar"');
27+
});
28+
29+
it('quoteIfSpecialCharacters handles strings with only spaces', () => {
30+
expect(quoteIfSpecialCharacters(' ')).toBe('');
31+
});
32+
33+
it('quoteIfSpecialCharacters handles strings with unicode characters', () => {
34+
expect(quoteIfSpecialCharacters('фывапр')).toBe('фывапр'); // unicode is not special
35+
});
36+
37+
it('quoteIfSpecialCharacters handles strings with emojis', () => {
38+
expect(quoteIfSpecialCharacters('foo😊bar')).toBe('foo😊bar'); // emoji is not special
39+
});
40+
41+
it('quoteIfSpecialCharacters quotes all Lucene special characters', () => {
42+
const luceneSpecials = ['+', '-', '&&', '||', '!', '(', ')', '{', '}', '[', ']', '^', '"', '~', '*', '?', ':', '\\', '/'];
43+
for (const char of luceneSpecials) {
44+
expect(quoteIfSpecialCharacters(char)).toBe(`"${char}"`);
45+
expect(quoteIfSpecialCharacters(`foo${char}bar`)).toBe(`"foo${char}bar"`);
46+
}
47+
});
48+
});

src/Exceptionless.Web/ClientApp/src/lib/features/events/components/filters/helpers.svelte.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,26 @@ export function quote(value?: null | string): string | undefined {
8282
}
8383

8484
export function quoteIfSpecialCharacters(value?: null | string): null | string | undefined {
85+
if (!value) {
86+
return value;
87+
}
88+
89+
const trimmed = value.trim();
90+
if (!trimmed) {
91+
return trimmed;
92+
}
93+
94+
if (trimmed.length > 1 && trimmed.startsWith('"') && trimmed.endsWith('"')) {
95+
return trimmed;
96+
}
97+
8598
// Check for lucene special characters or whitespace
86-
const regex = /[+\-&|!(){}\[\]^"~*?:\\/\\s]/;
87-
88-
if (value && regex.test(value) {
89-
return quote(value);
99+
const regex = /[+\-&|!(){}[\]^"~*?:\\/\s]/;
100+
if (trimmed && regex.test(trimmed)) {
101+
return quote(trimmed);
90102
}
91103

92-
return value;
104+
return trimmed;
93105
}
94106

95107
export function shouldRefreshPersistentEventChanged(

0 commit comments

Comments
 (0)