Skip to content
Open
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
13 changes: 13 additions & 0 deletions projects/rero/ng-core/src/lib/core/utils/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,17 @@ describe('Utils', () => {
expect(removeChars('"House"')).toEqual('House');
expect(removeChars('"House Philipp\'s"', ["'"])).toEqual('"House Philipps"');
});

it('should remove parentheses and backslashes by default', () => {
expect(removeChars('foo (bar) baz')).toEqual('foo bar baz');
expect(removeChars('foo\\bar')).toEqual('foobar');
});

it('should keep other Elasticsearch operators untouched by default', () => {
expect(removeChars('title:foo AND +bar -baz*')).toEqual('title:foo AND +bar -baz*');
});

it('should treat a custom hyphen as a literal char, not a range', () => {
expect(removeChars('abcxyz-', ['a', '-', 'z'])).toEqual('bcxy');
});
});
5 changes: 3 additions & 2 deletions projects/rero/ng-core/src/lib/core/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ export function capitalize(value: string): string {
return value.charAt(0).toUpperCase() + value.slice(1);
}

export function removeChars(value: string, chars: string[] = ['"']): string {
const re = new RegExp('[' + chars.join('') + ']', 'gi');
export function removeChars(value: string, chars: string[] = ['"', '(', ')', '\\']): string {
const escaped = chars.map((char) => char.replace(/[.*+?^${}()|[\]\\-]/g, '\\$&'));
const re = new RegExp('[' + escaped.join('') + ']', 'gi');
Comment thread
coderabbitai[bot] marked this conversation as resolved.
return value.replace(re, '');
}