mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2024-11-17 21:23:54 +01:00
clean up regex and adjust tests to new criteria
This commit is contained in:
parent
0988ec8226
commit
1a9d3ee998
@ -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);
|
||||
});
|
||||
});
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user