Convert an absolute path to a tilde path by replacing the user's home directory with ~
npx @tessl/cli install tessl/npm-tildify@3.0.0Tildify 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.
npm install tildifyimport tildify from 'tildify';TypeScript:
import tildify from 'tildify';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'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 convertReturns:
string: The path with the home directory replaced by ~, or the original path if it doesn't start with the home directoryBehavior:
path.normalize()os.homedir())/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'