clean up regex and adjust tests to new criteria

This commit is contained in:
Dane Horn 2022-05-14 15:57:25 +02:00
parent 0988ec8226
commit 1a9d3ee998
2 changed files with 8 additions and 5 deletions

@ -49,9 +49,11 @@ export function isValidFilename(filename: string): boolean {
* not an entire path
*/
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 = /^\.?(?:\w[_\-.%]*)+$/;
// A valid directory name:
// Must be at least 1 character long
// Can only include characters in the character set [-.%a-zA-Z0-9_]
// Cannot end with a '.'
const regex = /^(?:[-.%]|\w)*[-%a-zA-Z0-9_]$/;
// match() returns null if no match is found
return name.match(regex) != null;

@ -103,17 +103,18 @@ describe("Terminal Directory Tests", function () {
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("👨‍💻")).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);
expect(isValidDirectoryName("1.")).toEqual(false);
expect(isValidDirectoryName("foo.")).toEqual(false);
});
});