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

Path Display Formatter

Overview

Build a utility that formats file system paths for display in a user interface. The utility should make paths more readable by shortening paths within the user's home directory, while keeping all other paths in their original form for clarity and accuracy.

Requirements

Implement a path formatting function that:

  1. Accepts absolute file system paths as input
  2. Returns shortened, user-friendly versions of paths within the home directory
  3. Preserves paths outside the home directory exactly as provided
  4. Handles edge cases like the home directory itself

The formatter should work across different operating systems (macOS, Linux, Windows).

Dependencies { .dependencies }

tildify { .dependency }

Provides path formatting utilities.

Implementation

Create a module src/path-formatter.js that exports a default function formatPath(absolutePath):

  • Input: An absolute file system path (string)
  • Output: A formatted path (string)

The function should:

  • Convert paths within the user's home directory to a shortened format using tilde notation
  • Return paths outside the home directory unchanged
  • Handle the home directory itself appropriately

Test Cases

Create test file src/path-formatter.test.js:

Test 1: Format path within home directory { .test }

// Given a path within the user's home directory
const homedir = require('os').homedir();
const testPath = `${homedir}/documents/project`;

// When formatted
const result = formatPath(testPath);

// Then it should be shortened
// Expected: result starts with '~/' and contains 'documents/project'
assert(result.startsWith('~/'));
assert(result.includes('documents/project'));

Test 2: Preserve system paths { .test }

// Given system paths outside the home directory
const systemPaths = [
  '/var/log/system.log',
  '/usr/local/bin/node',
  '/etc/config.json'
];

// When formatted
const results = systemPaths.map(path => formatPath(path));

// Then they should remain unchanged
assert.deepEqual(results, systemPaths);

Test 3: Handle home directory root { .test }

// Given the home directory path itself
const homedir = require('os').homedir();

// When formatted
const result = formatPath(homedir);

// Then it should return the shortened form
// Expected: result equals '~'
assert.strictEqual(result, '~');

Test 4: Preserve paths with similar prefixes { .test }

// Given a path that looks similar to home but isn't actually within it
// (This tests that the formatter doesn't incorrectly match partial paths)
const homedir = require('os').homedir();
// If home is '/home/user', test with '/home/userdata/file.txt'
const similarPath = homedir + 'data/file.txt';

// When formatted
const result = formatPath(similarPath);

// Then it should remain unchanged (it's not actually in the home directory)
// Expected: result equals similarPath
assert.strictEqual(result, similarPath);

Success Criteria

Your implementation should:

  • Correctly shorten paths within the home directory
  • Never modify paths outside the home directory
  • Handle edge cases (home directory root, similar path prefixes)
  • Work consistently across different operating systems
  • Pass all provided test cases

Install with Tessl CLI

npx tessl i tessl/npm-tildify

tile.json