pathNorm function
Normalizes a given path
by removing leading slashes and optionally converting
backslashes to forward slashes.
The normSlashs
parameter, if set to true
, replaces all backslashes (\
)
with forward slashes (/
) in the normalized path.
Example usage:
String normalizedPath = pathNorm('C:\\User\\Documents\\file.txt', normSlashs: true);
Implementation
String pathNorm(String path, {bool? normSlashs, endWithSlash = false}) {
path = p.normalize(path);
if (path.startsWith('/') || path.startsWith("\\")) path = path.substring(1);
if (normSlashs != null && normSlashs) {
path = path.replaceAll('\\', '/');
}
if (endWithSlash && !path.endsWith('/')) {
path += '/';
}
return path;
}