Convert to absolute path

OSCC · JavaScript, Node, String · Oct 22, 2020

Converts a tilde path to an absolute path.

  • Use String.prototype.replace() with a regular expression and os.homedir() to replace the ~ in the start of the path with the home directory.
const untildify = str =>
  str.replace(/^~($|\/|\\)/, `${require('os').homedir()}$1`);
untildify('~/node'); // '/Users/aUser/node'

Recommended snippets

  • Colorize text

    JavaScript, Node

    Adds special characters to text to print in color in the console (combined with console.log()).

  • Encode string to Base64

    JavaScript, Node

    Creates a base-64 encoded ASCII string from a String object in which each character in the string is treated as a byte of binary data.

  • Decodes a string of data which has been encoded using base-64 encoding.