Convert an absolute path to a tilde path by replacing the user's home directory with ~
93
Evaluation — 93%
↑ 1.09xAgent success when using this tile
Build a utility function that formats file system paths for user-friendly display in command-line applications. The function should convert absolute paths to a shortened format when they're inside the user's home directory, making them easier to read and more portable across different systems.
Your function should accept an absolute file system path and return a display-friendly version:
~) for brevity. (current directory), or .. (parent directory) references~// Typical use case - subdirectory in home
formatPath('/Users/alice/projects/myapp/src')
// => '~/projects/myapp/src'
// Home directory itself
formatPath('/Users/alice')
// => '~'
// Path with unnecessary separators and relative references
formatPath('/Users/alice/projects/../projects/./myapp')
// => '~/projects/myapp'
// Path outside home directory
formatPath('/var/log/system.log')
// => '/var/log/system.log'
// Relative path
formatPath('relative/path')
// => 'relative/path'/Users/testuser/documents/file.txt where /Users/testuser is the home directory, the function returns ~/documents/file.txt @test/Users/testuser, the function returns ~ @test/Users/testuser/dir1/../dir2/./file.txt where /Users/testuser is the home directory, the function returns ~/dir2/file.txt @test/var/log/app.log that is outside the home directory, the function returns /var/log/app.log @test@generates
/**
* Format a file system path for display by converting home directory paths to tilde notation
* and normalizing path separators and relative references.
*
* @param {string} absolutePath - The file system path to format
* @returns {string} The formatted path with home directory as ~ (if applicable) and normalized separators
*/
function formatPath(absolutePath) {
// Implementation here
}
module.exports = { formatPath };Provides path-to-tilde conversion and normalization functionality.
@satisfied-by
Install with Tessl CLI
npx tessl i tessl/npm-tildify