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

Path Display Formatter

Create a utility that formats absolute file system paths for display by converting home directory paths to a more compact tilde notation.

Requirements

Implement a function that takes an absolute file path and returns a formatted version suitable for display:

  • When a path is within the user's home directory, replace the home directory portion with ~
  • The function should handle paths with trailing separators correctly, ensuring clean output without extra trailing separators
  • Paths that exactly match the home directory should return just ~ (without trailing separator)
  • Paths outside the home directory should be returned as normalized paths without modification
  • The solution should work across different operating systems (macOS, Linux, Windows)

Expected Behavior

The formatter should handle these scenarios:

  1. Standard home directory paths: Convert paths like /Users/username/documents to ~/documents
  2. Paths with trailing separators: Ensure paths like /Users/username/documents/ are converted to ~/documents (no trailing separator)
  3. Exact home directory match: Convert /Users/username/ to ~ (not ~/)
  4. Non-home paths: Leave paths like /var/log/system.log unchanged

Implementation

@generates

API

/**
 * Formats an absolute path by replacing the home directory with a tilde.
 * Handles trailing separators correctly to ensure clean output.
 *
 * @param {string} absolutePath - An absolute file system path
 * @returns {string} The formatted path with home directory replaced by ~, or the original path if not in home directory
 */
function formatPath(absolutePath) {
  // IMPLEMENTATION HERE
}

module.exports = formatPath;

Tests

  • Given /Users/username/documents, returns ~/documents @test
  • Given /Users/username/documents/ (with trailing separator), returns ~/documents (without trailing separator) @test
  • Given /Users/username/ (home directory with trailing separator), returns ~ @test
  • Given /Users/username (home directory without trailing separator), returns ~ @test

Dependencies { .dependencies }

tildify { .dependency }

Provides path tildification support for converting absolute paths to tilde notation.

Install with Tessl CLI

npx tessl i tessl/npm-tildify

tile.json