Files
30-seconds-of-code/snippets/untildify.md
Angelos Chalaris 1731671564 Add untildify
Converts a tilde path to an absolute path.
2018-01-01 17:43:18 +02:00

14 lines
346 B
Markdown

### untildify
Converts a tilde path to an absolute path.
Use `String.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'
```