This is piece of TypeScript
code that I use:
class StringUtils {
// SiwachGaurav's version from http://stackoverflow.com/questions/1144783/replacing-all-occurrences-of-a-string-in-javascript
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, "\\", "/");
}
}
Check if two paths point to the same
StringUtils.toPosixPath("..\\foo.txt") == StringUtils.toPosixPath("../foo.txt")
Pass path to a node.js
file I/O
StringUtils.toNativePath("../foo.txt")
StringUtils.toNativePath("..\\foo.txt")