From a82c0d80f0bb89f4fa6312f9dac27fa24fe39561 Mon Sep 17 00:00:00 2001 From: redhoodsu Date: Thu, 30 May 2024 20:03:03 +0800 Subject: [PATCH] feat: add tildify --- DOC.md | 20 ++++++++++++++++++++ DOC_CN.md | 20 ++++++++++++++++++++ cspell.json | 1 + i18n/tildify.md | 8 ++++++++ index.json | 12 ++++++++++++ src/tildify.js | 35 +++++++++++++++++++++++++++++++++++ test/tildify.js | 13 +++++++++++++ 7 files changed, 109 insertions(+) create mode 100644 i18n/tildify.md create mode 100644 src/tildify.js create mode 100644 test/tildify.js diff --git a/DOC.md b/DOC.md index 07dbd10a..bb7cf5fc 100644 --- a/DOC.md +++ b/DOC.md @@ -12301,6 +12301,26 @@ fs.createReadStream('in.txt') .pipe(fs.createWriteStream('out.txt')); ``` +## tildify + +Convert absolute path to tilde path. + +
+Type Definition +
+function tildify(path: string): string;
+
+
+ +|Name |Desc | +|------|---------------| +|path |Path to convert| +|return|Tilde path | + +```javascript +tildify('/home/surunzi/dev'); // -> '~/dev' +``` + ## timeAgo Format datetime with *** time ago statement. diff --git a/DOC_CN.md b/DOC_CN.md index eda7e3c8..618046c9 100644 --- a/DOC_CN.md +++ b/DOC_CN.md @@ -12292,6 +12292,26 @@ fs.createReadStream('in.txt') .pipe(fs.createWriteStream('out.txt')); ``` +## tildify + +将绝对路径转换为波浪线路径。 + +
+类型定义 +
+function tildify(path: string): string;
+
+
+ +|参数名|说明| +|-----|---| +|path|要转换的路径| +|返回值|波浪线路径| + +```javascript +tildify('/home/surunzi/dev'); // -> '~/dev' +``` + ## timeAgo 将时间格式化成多久之前的形式。 diff --git a/cspell.json b/cspell.json index 4ebb9b3e..c4003c73 100644 --- a/cspell.json +++ b/cspell.json @@ -412,6 +412,7 @@ "template", "throttle", "through", + "tildify", "timeAgo", "times", "timeTaken", diff --git a/i18n/tildify.md b/i18n/tildify.md new file mode 100644 index 00000000..443863e5 --- /dev/null +++ b/i18n/tildify.md @@ -0,0 +1,8 @@ +## CN + +将绝对路径转换为波浪线路径。 + +|参数名|说明| +|-----|---| +|path|要转换的路径| +|返回值|波浪线路径| diff --git a/index.json b/index.json index 651f101b..7aac8c52 100644 --- a/index.json +++ b/index.json @@ -6366,6 +6366,18 @@ "node" ] }, + "tildify": { + "dependencies": [ + "startWith" + ], + "description": "Convert absolute path to tilde path.", + "env": [ + "node" + ], + "test": [ + "node" + ] + }, "timeAgo": { "dependencies": [ "isDate", diff --git a/src/tildify.js b/src/tildify.js new file mode 100644 index 00000000..1541f2bc --- /dev/null +++ b/src/tildify.js @@ -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); +}; diff --git a/test/tildify.js b/test/tildify.js new file mode 100644 index 00000000..fa498ea2 --- /dev/null +++ b/test/tildify.js @@ -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'); + } +});