mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2026-05-05 23:27:55 +02:00
prettify, sorry for the big ass commit
This commit is contained in:
+162
-132
@@ -12,11 +12,11 @@
|
||||
* Removes leading forward slash ("/") from a string.
|
||||
*/
|
||||
export function removeLeadingSlash(s: string): string {
|
||||
if (s.startsWith("/")) {
|
||||
return s.slice(1);
|
||||
}
|
||||
if (s.startsWith("/")) {
|
||||
return s.slice(1);
|
||||
}
|
||||
|
||||
return s;
|
||||
return s;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -24,11 +24,11 @@ export function removeLeadingSlash(s: string): string {
|
||||
* Note that this will also remove the slash if it is the leading slash (i.e. if s = "/")
|
||||
*/
|
||||
export function removeTrailingSlash(s: string): string {
|
||||
if (s.endsWith("/")) {
|
||||
return s.slice(0, -1);
|
||||
}
|
||||
if (s.endsWith("/")) {
|
||||
return s.slice(0, -1);
|
||||
}
|
||||
|
||||
return s;
|
||||
return s;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -36,12 +36,12 @@ export function removeTrailingSlash(s: string): string {
|
||||
* not the entire filepath
|
||||
*/
|
||||
export function isValidFilename(filename: string): boolean {
|
||||
// Allows alphanumerics, hyphens, underscores, and percentage signs
|
||||
// Must have a file extension
|
||||
const regex = /^[.a-zA-Z0-9_-]+[.][a-zA-Z0-9]+(?:-\d+(?:\.\d*)?%-INC)?$/;
|
||||
// Allows alphanumerics, hyphens, underscores, and percentage signs
|
||||
// Must have a file extension
|
||||
const regex = /^[.a-zA-Z0-9_-]+[.][a-zA-Z0-9]+(?:-\d+(?:\.\d*)?%-INC)?$/;
|
||||
|
||||
// match() returns null if no match is found
|
||||
return filename.match(regex) != null;
|
||||
// match() returns null if no match is found
|
||||
return filename.match(regex) != null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -49,12 +49,12 @@ 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 = /^.?[a-zA-Z0-9_-]+$/;
|
||||
// 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_-]+$/;
|
||||
|
||||
// match() returns null if no match is found
|
||||
return name.match(regex) != null;
|
||||
// match() returns null if no match is found
|
||||
return name.match(regex) != null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -63,34 +63,38 @@ export function isValidDirectoryName(name: string): boolean {
|
||||
* if the directories actually exist on Terminal
|
||||
*/
|
||||
export function isValidDirectoryPath(path: string): boolean {
|
||||
let t_path: string = path;
|
||||
let t_path: string = path;
|
||||
|
||||
if (t_path.length === 0) { return false; }
|
||||
if (t_path.length === 1) {
|
||||
return t_path === "/";
|
||||
if (t_path.length === 0) {
|
||||
return false;
|
||||
}
|
||||
if (t_path.length === 1) {
|
||||
return t_path === "/";
|
||||
}
|
||||
|
||||
// A full path must have a leading slash, but we'll ignore it for the checks
|
||||
if (t_path.startsWith("/")) {
|
||||
t_path = t_path.slice(1);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Trailing slash does not matter
|
||||
t_path = removeTrailingSlash(t_path);
|
||||
|
||||
// Check that every section of the path is a valid directory name
|
||||
const dirs = t_path.split("/");
|
||||
for (const dir of dirs) {
|
||||
// Special case, "." and ".." are valid for path
|
||||
if (dir === "." || dir === "..") {
|
||||
continue;
|
||||
}
|
||||
|
||||
// A full path must have a leading slash, but we'll ignore it for the checks
|
||||
if (t_path.startsWith("/")) {
|
||||
t_path = t_path.slice(1);
|
||||
} else {
|
||||
return false;
|
||||
if (!isValidDirectoryName(dir)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Trailing slash does not matter
|
||||
t_path = removeTrailingSlash(t_path);
|
||||
|
||||
// Check that every section of the path is a valid directory name
|
||||
const dirs = t_path.split("/");
|
||||
for (const dir of dirs) {
|
||||
// Special case, "." and ".." are valid for path
|
||||
if (dir === "." || dir === "..") { continue; }
|
||||
if (!isValidDirectoryName(dir)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -98,26 +102,32 @@ export function isValidDirectoryPath(path: string): boolean {
|
||||
* proper formatting. It dose NOT check if the file actually exists on Terminal
|
||||
*/
|
||||
export function isValidFilePath(path: string): boolean {
|
||||
if (path == null || typeof path !== "string") { return false; }
|
||||
const t_path = path;
|
||||
if (path == null || typeof path !== "string") {
|
||||
return false;
|
||||
}
|
||||
const t_path = path;
|
||||
|
||||
// Impossible for filename to have less than length of 3
|
||||
if (t_path.length < 3) { return false; }
|
||||
// Impossible for filename to have less than length of 3
|
||||
if (t_path.length < 3) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Full filepath can't end with trailing slash because it must be a file
|
||||
if (t_path.endsWith("/")) { return false; }
|
||||
// Full filepath can't end with trailing slash because it must be a file
|
||||
if (t_path.endsWith("/")) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Everything after the last forward slash is the filename. Everything before
|
||||
// it is the file path
|
||||
const fnSeparator = t_path.lastIndexOf("/");
|
||||
if (fnSeparator === -1) {
|
||||
return isValidFilename(t_path);
|
||||
}
|
||||
// Everything after the last forward slash is the filename. Everything before
|
||||
// it is the file path
|
||||
const fnSeparator = t_path.lastIndexOf("/");
|
||||
if (fnSeparator === -1) {
|
||||
return isValidFilename(t_path);
|
||||
}
|
||||
|
||||
const fn = t_path.slice(fnSeparator + 1);
|
||||
const dirPath = t_path.slice(0, fnSeparator + 1);
|
||||
const fn = t_path.slice(fnSeparator + 1);
|
||||
const dirPath = t_path.slice(0, fnSeparator + 1);
|
||||
|
||||
return isValidDirectoryPath(dirPath) && isValidFilename(fn);
|
||||
return isValidDirectoryPath(dirPath) && isValidFilename(fn);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -126,16 +136,20 @@ export function isValidFilePath(path: string): boolean {
|
||||
* If there is no first parent directory, then it returns "/" for root
|
||||
*/
|
||||
export function getFirstParentDirectory(path: string): string {
|
||||
let t_path = path;
|
||||
t_path = removeLeadingSlash(t_path);
|
||||
t_path = removeTrailingSlash(t_path);
|
||||
let t_path = path;
|
||||
t_path = removeLeadingSlash(t_path);
|
||||
t_path = removeTrailingSlash(t_path);
|
||||
|
||||
if (t_path.lastIndexOf("/") === -1) { return "/"; }
|
||||
if (t_path.lastIndexOf("/") === -1) {
|
||||
return "/";
|
||||
}
|
||||
|
||||
const dirs = t_path.split("/");
|
||||
if (dirs.length === 0) { return "/"; }
|
||||
const dirs = t_path.split("/");
|
||||
if (dirs.length === 0) {
|
||||
return "/";
|
||||
}
|
||||
|
||||
return dirs[0] + "/";
|
||||
return dirs[0] + "/";
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -146,98 +160,114 @@ export function getFirstParentDirectory(path: string): string {
|
||||
* If there are no parent directories, it returns the empty string
|
||||
*/
|
||||
export function getAllParentDirectories(path: string): string {
|
||||
const t_path = path;
|
||||
const lastSlash = t_path.lastIndexOf("/");
|
||||
if (lastSlash === -1) { return ""; }
|
||||
const t_path = path;
|
||||
const lastSlash = t_path.lastIndexOf("/");
|
||||
if (lastSlash === -1) {
|
||||
return "";
|
||||
}
|
||||
|
||||
return t_path.slice(0, lastSlash + 1);
|
||||
return t_path.slice(0, lastSlash + 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a file path refers to a file in the root directory.
|
||||
*/
|
||||
export function isInRootDirectory(path: string): boolean {
|
||||
if (!isValidFilePath(path)) { return false; }
|
||||
if (path == null || path.length === 0) { return false; }
|
||||
if (!isValidFilePath(path)) {
|
||||
return false;
|
||||
}
|
||||
if (path == null || path.length === 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return (path.lastIndexOf("/") <= 0);
|
||||
return path.lastIndexOf("/") <= 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Evaluates a directory path, including the processing of linux dots.
|
||||
* Returns the full, proper path, or null if an invalid path is passed in
|
||||
*/
|
||||
export function evaluateDirectoryPath(path: string, currPath?: string): string | null {
|
||||
let t_path = path;
|
||||
export function evaluateDirectoryPath(
|
||||
path: string,
|
||||
currPath?: string,
|
||||
): string | null {
|
||||
let t_path = path;
|
||||
|
||||
// If the path begins with a slash, then its an absolute path. Otherwise its relative
|
||||
// For relative paths, we need to prepend the current directory
|
||||
if (!t_path.startsWith("/") && currPath != null) {
|
||||
t_path = currPath + (currPath.endsWith("/") ? "" : "/") + t_path;
|
||||
// If the path begins with a slash, then its an absolute path. Otherwise its relative
|
||||
// For relative paths, we need to prepend the current directory
|
||||
if (!t_path.startsWith("/") && currPath != null) {
|
||||
t_path = currPath + (currPath.endsWith("/") ? "" : "/") + t_path;
|
||||
}
|
||||
|
||||
if (!isValidDirectoryPath(t_path)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Trim leading/trailing slashes
|
||||
t_path = removeLeadingSlash(t_path);
|
||||
t_path = removeTrailingSlash(t_path);
|
||||
|
||||
const dirs = t_path.split("/");
|
||||
const reconstructedPath: string[] = [];
|
||||
|
||||
for (const dir of dirs) {
|
||||
if (dir === ".") {
|
||||
// Current directory, do nothing
|
||||
continue;
|
||||
} else if (dir === "..") {
|
||||
// Parent directory
|
||||
const res = reconstructedPath.pop();
|
||||
if (res == null) {
|
||||
return null; // Array was empty, invalid path
|
||||
}
|
||||
} else {
|
||||
reconstructedPath.push(dir);
|
||||
}
|
||||
}
|
||||
|
||||
if (!isValidDirectoryPath(t_path)) { return null; }
|
||||
|
||||
// Trim leading/trailing slashes
|
||||
t_path = removeLeadingSlash(t_path);
|
||||
t_path = removeTrailingSlash(t_path);
|
||||
|
||||
const dirs = t_path.split("/");
|
||||
const reconstructedPath: string[] = [];
|
||||
|
||||
for (const dir of dirs) {
|
||||
if (dir === ".") {
|
||||
// Current directory, do nothing
|
||||
continue;
|
||||
} else if (dir === "..") {
|
||||
// Parent directory
|
||||
const res = reconstructedPath.pop();
|
||||
if (res == null) {
|
||||
return null; // Array was empty, invalid path
|
||||
}
|
||||
} else {
|
||||
reconstructedPath.push(dir);
|
||||
}
|
||||
}
|
||||
|
||||
return "/" + reconstructedPath.join("/");
|
||||
return "/" + reconstructedPath.join("/");
|
||||
}
|
||||
|
||||
/**
|
||||
* Evaluates a file path, including the processing of linux dots.
|
||||
* Returns the full, proper path, or null if an invalid path is passed in
|
||||
*/
|
||||
export function evaluateFilePath(path: string, currPath?: string): string | null {
|
||||
let t_path = path;
|
||||
export function evaluateFilePath(
|
||||
path: string,
|
||||
currPath?: string,
|
||||
): string | null {
|
||||
let t_path = path;
|
||||
|
||||
// If the path begins with a slash, then its an absolute path. Otherwise its relative
|
||||
// For relative paths, we need to prepend the current directory
|
||||
if (!t_path.startsWith("/") && currPath != null) {
|
||||
t_path = currPath + (currPath.endsWith("/") ? "" : "/") + t_path;
|
||||
// If the path begins with a slash, then its an absolute path. Otherwise its relative
|
||||
// For relative paths, we need to prepend the current directory
|
||||
if (!t_path.startsWith("/") && currPath != null) {
|
||||
t_path = currPath + (currPath.endsWith("/") ? "" : "/") + t_path;
|
||||
}
|
||||
|
||||
if (!isValidFilePath(t_path)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Trim leading/trailing slashes
|
||||
t_path = removeLeadingSlash(t_path);
|
||||
|
||||
const dirs = t_path.split("/");
|
||||
const reconstructedPath: string[] = [];
|
||||
|
||||
for (const dir of dirs) {
|
||||
if (dir === ".") {
|
||||
// Current directory, do nothing
|
||||
continue;
|
||||
} else if (dir === "..") {
|
||||
// Parent directory
|
||||
const res = reconstructedPath.pop();
|
||||
if (res == null) {
|
||||
return null; // Array was empty, invalid path
|
||||
}
|
||||
} else {
|
||||
reconstructedPath.push(dir);
|
||||
}
|
||||
}
|
||||
|
||||
if (!isValidFilePath(t_path)) { return null; }
|
||||
|
||||
// Trim leading/trailing slashes
|
||||
t_path = removeLeadingSlash(t_path);
|
||||
|
||||
const dirs = t_path.split("/");
|
||||
const reconstructedPath: string[] = [];
|
||||
|
||||
for (const dir of dirs) {
|
||||
if (dir === ".") {
|
||||
// Current directory, do nothing
|
||||
continue;
|
||||
} else if (dir === "..") {
|
||||
// Parent directory
|
||||
const res = reconstructedPath.pop();
|
||||
if (res == null) {
|
||||
return null; // Array was empty, invalid path
|
||||
}
|
||||
} else {
|
||||
reconstructedPath.push(dir);
|
||||
}
|
||||
}
|
||||
|
||||
return "/" + reconstructedPath.join("/");
|
||||
return "/" + reconstructedPath.join("/");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user