基于操作系统确定路径字符串类型的方法
Way to affirm path string type based on operating system
我正在 运行 不同操作系统上的测试,我期待路径格式返回 posix。
这是我遇到的错误类型:
Uncaught AssertionError: expected "..\foo.txt" to equal "../foo.txt"
我怎样才能确认像 posixAffirm("../foo.txt")
这样的路径并让它根据 windows 或 posix.
呈现动态正确的路径格式字符串
这是我使用的 TypeScript
代码片段:
class StringUtils {
// SiwachGaurav's version from
static replaceAll(str: string, find: string, replace: string): string {
return str.replace(new RegExp(find.replace(/[-\/\^$*+?.()|[\]{}]/g,'\$&'),'g'), replace);
}
// Returns file path with OS-native slashes
static toNativePath(str: string): string {
var os = ExternalInterfaces.require_os();
if (os.platform() == "win32")
// Convert Unix to Windows
return StringUtils.replaceAll(str, "/", "\");
else
// Convert Windows to Unix
return StringUtils.replaceAll(str, "\", "/");
}
// Returns file path with POSIX-style slashes
static toPosixPath(str: string): string {
// Convert Windows to Unix
return StringUtils.replaceAll(str, "\", "/");
}
}
检查两个路径是否指向同一个
StringUtils.toPosixPath("..\foo.txt") == StringUtils.toPosixPath("../foo.txt")
传递 node.js
文件的路径 I/O
StringUtils.toNativePath("../foo.txt")
StringUtils.toNativePath("..\foo.txt")
我正在 运行 不同操作系统上的测试,我期待路径格式返回 posix。
这是我遇到的错误类型:
Uncaught AssertionError: expected "..\foo.txt" to equal "../foo.txt"
我怎样才能确认像 posixAffirm("../foo.txt")
这样的路径并让它根据 windows 或 posix.
这是我使用的 TypeScript
代码片段:
class StringUtils {
// SiwachGaurav's version from
static replaceAll(str: string, find: string, replace: string): string {
return str.replace(new RegExp(find.replace(/[-\/\^$*+?.()|[\]{}]/g,'\$&'),'g'), replace);
}
// Returns file path with OS-native slashes
static toNativePath(str: string): string {
var os = ExternalInterfaces.require_os();
if (os.platform() == "win32")
// Convert Unix to Windows
return StringUtils.replaceAll(str, "/", "\");
else
// Convert Windows to Unix
return StringUtils.replaceAll(str, "\", "/");
}
// Returns file path with POSIX-style slashes
static toPosixPath(str: string): string {
// Convert Windows to Unix
return StringUtils.replaceAll(str, "\", "/");
}
}
检查两个路径是否指向同一个
StringUtils.toPosixPath("..\foo.txt") == StringUtils.toPosixPath("../foo.txt")
传递
node.js
文件的路径 I/OStringUtils.toNativePath("../foo.txt")
StringUtils.toNativePath("..\foo.txt")