1

I'm running a test on different operating systems and I'm expecting a path format to come back with posix.

Here's the kind of error I'm getting:

Uncaught AssertionError: expected "..\\foo.txt" to equal "../foo.txt"

How can I affirm a path like posixAffirm("../foo.txt") and have it render out a dynamic correct path format string based on windows or posix.

ThomasReggi
  • 55,053
  • 85
  • 237
  • 424

1 Answers1

0

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")

xmojmr
  • 8,073
  • 5
  • 31
  • 54