Files
30-seconds-of-code/snippets/untildify.md
Angelos Chalaris 611729214a Snippet format update
To match the starter (for the migration)
2019-08-13 10:29:12 +03:00

16 lines
394 B
Markdown

---
title: untildify
tags: node,string,beginner
---
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.
```js
const untildify = str => str.replace(/^~($|\/|\\)/, `${require('os').homedir()}$1`);
```
```js
untildify('~/node'); // '/Users/aUser/node'
```