pathsEqual function
Compares multiple paths for equality after normalization.
This function normalizes all provided paths using the same formatting options
and then compares them to determine if they represent the same endpoint.
This is useful for route matching and path comparison in web applications.
paths List of path strings to compare for equality
normSlashs If true, converts backslashes to forward slashes (default: true)
endWithSlash If true, ensures paths end with a forward slash (default: true)
startWithSlash If true, ensures paths start with a forward slash (default: true)
Returns true if all paths are equivalent after normalization, false otherwise.
Returns true if the paths list is empty.
Example usage:
bool isEqual = pathsEqual(['/api/users/', 'api\\users', '/api/users']);
// Result: true (all represent the same path)
bool isDifferent = pathsEqual(['/api/users', '/api/posts']);
// Result: false (different endpoints)
Implementation
bool pathsEqual(
List<String> paths, {
bool normSlashs = true,
endWithSlash = true,
startWithSlash = true,
}) {
if (paths.isEmpty) {
return true;
}
var path1 = endpointNorm(
[paths[0]],
normSlashs: normSlashs,
endWithSlash: endWithSlash,
startWithSlash: startWithSlash,
);
for (var path in paths) {
var path2 = endpointNorm(
[path],
normSlashs: normSlashs,
endWithSlash: endWithSlash,
startWithSlash: startWithSlash,
);
if (path1 != path2) {
return false;
}
}
return true;
}