Skip to content

Commit

Permalink
feat: add tildify
Browse files Browse the repository at this point in the history
  • Loading branch information
surunzi committed May 30, 2024
1 parent e911a69 commit a82c0d8
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 0 deletions.
20 changes: 20 additions & 0 deletions DOC.md
Original file line number Diff line number Diff line change
Expand Up @@ -12301,6 +12301,26 @@ fs.createReadStream('in.txt')
.pipe(fs.createWriteStream('out.txt'));
```

## tildify

Convert absolute path to tilde path.

<details>
<summary>Type Definition</summary>
<pre>
<code class="language-typescript">function tildify(path: string): string;</code>
</pre>
</details>

|Name |Desc |
|------|---------------|
|path |Path to convert|
|return|Tilde path |

```javascript
tildify('/home/surunzi/dev'); // -> '~/dev'
```

## timeAgo

Format datetime with *** time ago statement.
Expand Down
20 changes: 20 additions & 0 deletions DOC_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -12292,6 +12292,26 @@ fs.createReadStream('in.txt')
.pipe(fs.createWriteStream('out.txt'));
```

## tildify

将绝对路径转换为波浪线路径。

<details>
<summary>类型定义</summary>
<pre>
<code class="language-typescript">function tildify(path: string): string;</code>
</pre>
</details>

|参数名|说明|
|-----|---|
|path|要转换的路径|
|返回值|波浪线路径|

```javascript
tildify('/home/surunzi/dev'); // -> '~/dev'
```

## timeAgo

将时间格式化成多久之前的形式。
Expand Down
1 change: 1 addition & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@
"template",
"throttle",
"through",
"tildify",
"timeAgo",
"times",
"timeTaken",
Expand Down
8 changes: 8 additions & 0 deletions i18n/tildify.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
## CN

将绝对路径转换为波浪线路径。

|参数名|说明|
|-----|---|
|path|要转换的路径|
|返回值|波浪线路径|
12 changes: 12 additions & 0 deletions index.json
Original file line number Diff line number Diff line change
Expand Up @@ -6366,6 +6366,18 @@
"node"
]
},
"tildify": {
"dependencies": [
"startWith"
],
"description": "Convert absolute path to tilde path.",
"env": [
"node"
],
"test": [
"node"
]
},
"timeAgo": {
"dependencies": [
"isDate",
Expand Down
35 changes: 35 additions & 0 deletions src/tildify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/* Convert absolute path to tilde path.
*
* |Name |Desc |
* |------|---------------|
* |path |Path to convert|
* |return|Tilde path |
*/

/* example
* tildify('/home/surunzi/dev'); // -> '~/dev'
*/

/* module
* env: node
*/

/* typescript
* export declare function tildify(path: string): string;
*/

_('startWith');

const path = require('path');
const os = require('os');

const homeDir = os.homedir();

exports = function(p) {
p = path.normalize(p) + path.sep;
if (startWith(p, homeDir)) {
p = p.replace(homeDir + path.sep, `~${path.sep}`);
}

return p.slice(0, -1);
};
13 changes: 13 additions & 0 deletions test/tildify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const os = require('os');
const homeDir = os.homedir();

const isWindows = util.isWindows;

it('basic', function() {
if (isWindows) {
expect(tildify(homeDir + '\\dev')).to.equal('~\\dev');
} else {
expect(tildify(homeDir + '/dev')).to.equal('~/dev');
expect(tildify('/usr/dev')).to.equal('/usr/dev');
}
});

0 comments on commit a82c0d8

Please sign in to comment.