Skip to content

toValidFilePath

Requires a Node.js runtime ('qsu/node')

Remove invalid or unnecessary characters in the path.

. and .. segments are resolved, and the result is always absolute. A .. cannot climb above the root, so ../../etc/passwd becomes /etc/passwd.

. and .. segments are resolved, and a path that collapses to nothing returns the root.

Parameters

NameTypeRequiredDefault
filePathstringStringstr
isWindowsnamedkeywordbooleanbool
Whether the target operating system to be checked is Windows

namedThese parameters are passed as named parameters.

keywordThese parameters are passed as keyword arguments, or as a single dict in their place.

Returns

string

String

str

Examples

javascript
toValidFilePath('C:\\Windows\\System32\\', true); // 'C:\Windows\System32'
toValidFilePath('home/user/.bashrc'); // '/home/user/.bashrc'
toValidFilePath('/home/user/../test'); // '/home/test'
dart
toValidFilePath('C:\\Windows\\System32\\', isWindows: true); // 'C:\Windows\System32'
toValidFilePath('home/user/.bashrc'); // '/home/user/.bashrc'
toValidFilePath('/home/user/../test'); // '/home/test'
python
toValidFilePath('C:\\Windows\\System32\\', True) # 'C:\Windows\System32'
toValidFilePath('home/user/.bashrc') # '/home/user/.bashrc'
toValidFilePath('/home/user/../test') # '/home/test'

Released under the MIT License