CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-tildify

Convert an absolute path to a tilde path by replacing the user's home directory with ~

93

1.09x

Evaluation93%

1.09x

Agent success when using this tile

Overview
Eval results
Files

task.mdevals/scenario-8/

Path Display Formatter

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.

Requirements

Your function should accept an absolute file system path and return a display-friendly version:

  1. Path Simplification: When a path starts with the user's home directory, replace that portion with a tilde symbol (~) for brevity
  2. Input Normalization: Handle paths that may contain redundant separators, . (current directory), or .. (parent directory) references
  3. Edge Cases:
    • The home directory itself should become just ~
    • Paths outside the home directory should be returned in normalized form
    • Relative paths should be returned in normalized form
  4. Cross-platform Support: Work correctly on different operating systems (macOS, Linux, Windows) with their respective path conventions

Examples

// 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'

Test Cases

  • Given a path /Users/testuser/documents/file.txt where /Users/testuser is the home directory, the function returns ~/documents/file.txt @test
  • Given a path that is exactly the home directory /Users/testuser, the function returns ~ @test
  • Given a path with redundant elements /Users/testuser/dir1/../dir2/./file.txt where /Users/testuser is the home directory, the function returns ~/dir2/file.txt @test
  • Given a path /var/log/app.log that is outside the home directory, the function returns /var/log/app.log @test

Implementation

@generates

API

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

Dependencies { .dependencies }

tildify { .dependency }

Provides path-to-tilde conversion and normalization functionality.

@satisfied-by

Install with Tessl CLI

npx tessl i tessl/npm-tildify

tile.json