or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-tildify

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

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/tildify@3.0.x

To install, run

npx @tessl/cli install tessl/npm-tildify@3.0.0

index.mddocs/

Tildify

Tildify converts absolute file system paths to tilde notation by replacing the user's home directory with ~. It provides a lightweight, zero-dependency utility for displaying user-friendly path representations in CLI tools and development utilities.

Package Information

  • Package Name: tildify
  • Package Type: npm
  • Language: JavaScript/TypeScript
  • Installation: npm install tildify
  • Node.js: ^12.20.0 || ^14.13.1 || >=16.0.0

Core Imports

import tildify from 'tildify';

TypeScript:

import tildify from 'tildify';

Basic Usage

import tildify from 'tildify';

// Convert home directory path
tildify('/Users/sindresorhus/dev');
//=> '~/dev'

// Home directory itself becomes ~
tildify('/Users/sindresorhus');
//=> '~'

// Paths outside home directory remain unchanged
tildify('/usr/local/bin');
//=> '/usr/local/bin'

// Relative paths remain unchanged
tildify('relative/path');
//=> 'relative/path'

Capabilities

Path Conversion

Converts absolute paths to tilde notation when the path starts with the user's home directory.

/**
 * Convert an absolute path to a tilde path by replacing the user's home directory with ~
 * @param absolutePath - The absolute file system path to convert
 * @returns The path with home directory replaced by ~, or original path if not under home directory
 */
function tildify(absolutePath: string): string;

Parameters:

  • absolutePath (string): The absolute file system path to convert

Returns:

  • string: The path with the home directory replaced by ~, or the original path if it doesn't start with the home directory

Behavior:

  • Normalizes the input path using Node.js path.normalize()
  • Only converts paths that start exactly with the user's home directory (obtained via os.homedir())
  • Handles cross-platform path separators correctly
  • Returns relative paths unchanged
  • Returns paths that contain the home directory in the middle unchanged
  • Paths like /home/usernamefoo/file are NOT converted (must be exactly /home/username/)

Usage Examples:

import tildify from 'tildify';

// Basic conversion
tildify('/Users/sindresorhus/projects/awesome-project');
//=> '~/projects/awesome-project'

// Home directory edge case
tildify('/Users/sindresorhus');
//=> '~'

// Non-home paths remain unchanged  
tildify('/usr/local/bin/node');
//=> '/usr/local/bin/node'

// Relative paths remain unchanged
tildify('./local/path');
//=> './local/path'

// Similar but not exact home directory paths remain unchanged
tildify('/Users/sindresorhusabc/file');
//=> '/Users/sindresorhusabc/file'