pathNorm function

String pathNorm(
  1. String path, {
  2. bool? normSlashs,
  3. dynamic endWithSlash = false,
})

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;
}