Files
30-seconds-of-code/snippets/js/s/untildify.md
2023-05-07 16:07:29 +03:00

500 B

title, type, language, tags, cover, dateModified
title type language tags cover dateModified
Convert to absolute path snippet javascript
node
string
lighthouse 2020-10-22T20:24:44+03:00

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'