18 lines
399 B
Markdown
18 lines
399 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'
|
|
```
|