adjust regex for directory name

new regex allows for more flexible folder names, while still disallowing '.' and '..'
This commit is contained in:
Dane Horn 2022-05-14 10:57:40 +02:00
parent 0121fee6e4
commit 0988ec8226
2 changed files with 5 additions and 4 deletions

@ -51,7 +51,7 @@ export function isValidFilename(filename: string): boolean {
export function isValidDirectoryName(name: string): boolean {
// Allows alphanumerics, hyphens, underscores, and percentage signs.
// Name can begin with a single period, but otherwise cannot have any
const regex = /^.?[a-zA-Z0-9_-]+$/;
const regex = /^\.?(?:\w[_\-.%]*)+$/;
// match() returns null if no match is found
return name.match(regex) != null;

@ -102,17 +102,18 @@ describe("Terminal Directory Tests", function () {
expect(isValidDirectoryName(".a1")).toEqual(true);
expect(isValidDirectoryName("._foo")).toEqual(true);
expect(isValidDirectoryName("_foo")).toEqual(true);
expect(isValidDirectoryName("foo.dir")).toEqual(true);
expect(isValidDirectoryName("1.")).toEqual(true);
expect(isValidDirectoryName("foo.")).toEqual(true);
});
it("should return false for invalid directory names", function () {
expect(isValidDirectoryName("")).toEqual(false);
expect(isValidDirectoryName("foo.dir")).toEqual(false);
expect(isValidDirectoryName("1.")).toEqual(false);
expect(isValidDirectoryName("foo.")).toEqual(false);
expect(isValidDirectoryName("dir#")).toEqual(false);
expect(isValidDirectoryName("dir!")).toEqual(false);
expect(isValidDirectoryName("dir*")).toEqual(false);
expect(isValidDirectoryName(".")).toEqual(false);
expect(isValidDirectoryName("..")).toEqual(false);
});
});