expandDotPath function

String expandDotPath(
  1. String path
)

Expands a path that starts with '.' or '..' to an absolute path.

assume the current user is 'kaguya' and the current working directory is:

  • Windows: C:\Users\kaguya\Downloads\ft\
  • MacOS: /Users/kaguya/Downloads/ft/
  • Linux: /home/kaguya/Downloads/ft/

expand ./README.md is

  • Windows: C:\Users\kaguya\Downloads\ft\README.md
  • MacOS: /Users/kaguya/Downloads/ft/README.md
  • Linux: /home/kaguya/Downloads/ft/README.md

expand ../ft.tgz is

  • Windows: C:\Users\kaguya\Downloads\ft.tgz
  • MacOS: /Users/kaguya/Downloads/ft.tgz
  • Linux: /home/kaguya/Downloads/ft.tgz

Implementation

String expandDotPath(String path) {
  if (!path.startsWith('.')) return path;

  String currPath = Directory.current.path;
  String absPath = p.canonicalize(p.join(currPath, path));

  return absPath;
}