mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
feat: new helper functions to replace regexes #807
Implemented new StringUtil helper functions: splitCommaSeparated, sanitizeUrlPart, isValidFileName. Added helper functions to HeaderUtil: matchesAuthorizationScheme, hasScheme. Added unit tests for the new helper functions. Refactored codebase to use helper functions instead of regexes if applicable.
This commit is contained in:
committed by
Joachim Van Herwegen
parent
1b7cc1ea3a
commit
283c301f08
34
test/unit/util/StringUtil.test.ts
Normal file
34
test/unit/util/StringUtil.test.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import {
|
||||
sanitizeUrlPart,
|
||||
splitCommaSeparated,
|
||||
isValidFileName,
|
||||
} from '../../../src/util/StringUtil';
|
||||
|
||||
describe('HeaderUtil', (): void => {
|
||||
describe('#sanitizeUrlPart', (): void => {
|
||||
it('sanitizes part of a URL by replacing non-word characters with dashes (\'-\').', (): void => {
|
||||
expect(sanitizeUrlPart('$path segment containing=non-word+chars'))
|
||||
.toBe('-path-segment-containing-non-word-chars');
|
||||
});
|
||||
});
|
||||
|
||||
describe('#splitCommaSeparated', (): void => {
|
||||
it('splits strings containing commas into parts based on the location of these commas.', (): void => {
|
||||
expect(splitCommaSeparated('this,is,a,comma-separated,string'))
|
||||
.toEqual([ 'this', 'is', 'a', 'comma-separated', 'string' ]);
|
||||
});
|
||||
it('handles strings without commas by returning an array containing solely the original string.', (): void => {
|
||||
const strVal = 'this string has no commas';
|
||||
expect(splitCommaSeparated(strVal)).toEqual([ strVal ]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('#validateFileName', (): void => {
|
||||
it('returns true if the provided file name is valid.', (): void => {
|
||||
expect(isValidFileName('valid-file.test')).toBeTruthy();
|
||||
});
|
||||
it('returns false if the provided file name is invalid.', (): void => {
|
||||
expect(isValidFileName('$%^*')).toBeFalsy();
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user