dart-lang/path

Why does .dirname(String) return '.' instead of '' when passed an empty (non-null) string

Closed this issue · 1 comments

Why does the .dirname() method return a period instead of an empty string when passed an empty string?
It's a pain to filter when passing the output to the ui before the incoming path is populated by the user. Perhaps there is a good reason?

This is the offending source code within the path package:

    final parsed = _parse(path);
    parsed.removeTrailingSeparators();
    if (parsed.parts.isEmpty) return parsed.root ?? '.';
    if (parsed.parts.length == 1) return parsed.root ?? '.';
    parsed.parts.removeLast();
    parsed.separators.removeLast();
    parsed.removeTrailingSeparators();
    return parsed.toString();
  }
lrhn commented

Most likely reason is that can always do Directory(dirname(somePath)).
The empty string is not a valid directory name, and dirname always returns a valid directory name.

The code predates the current repository (which is 11 years old). I don't think we'll ever know precisely why the design is the way it is.