@cloudcome/utils-core 1.19.1 → 1.20.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +72 -73
- package/dist/array.cjs +181 -125
- package/dist/array.cjs.map +1 -1
- package/dist/array.mjs +181 -133
- package/dist/array.mjs.map +1 -1
- package/dist/async.cjs +181 -171
- package/dist/async.cjs.map +1 -1
- package/dist/async.mjs +181 -174
- package/dist/async.mjs.map +1 -1
- package/dist/base64.cjs +16 -12
- package/dist/base64.cjs.map +1 -1
- package/dist/base64.mjs +17 -14
- package/dist/base64.mjs.map +1 -1
- package/dist/cache.cjs +79 -67
- package/dist/cache.cjs.map +1 -1
- package/dist/cache.d.ts +3 -3
- package/dist/cache.mjs +80 -71
- package/dist/cache.mjs.map +1 -1
- package/dist/color.cjs +478 -167
- package/dist/color.cjs.map +1 -1
- package/dist/color.d.ts +3 -3
- package/dist/color.mjs +480 -197
- package/dist/color.mjs.map +1 -1
- package/dist/crypto.cjs +474 -687
- package/dist/crypto.cjs.map +1 -1
- package/dist/crypto.mjs +476 -693
- package/dist/crypto.mjs.map +1 -1
- package/dist/date.cjs +972 -161
- package/dist/date.cjs.map +1 -1
- package/dist/date.d.ts +2 -2
- package/dist/date.mjs +962 -191
- package/dist/date.mjs.map +1 -1
- package/dist/dict.cjs +90 -52
- package/dist/dict.cjs.map +1 -1
- package/dist/dict.d.ts +1 -1
- package/dist/dict.mjs +91 -54
- package/dist/dict.mjs.map +1 -1
- package/dist/easing.cjs +104 -103
- package/dist/easing.cjs.map +1 -1
- package/dist/easing.mjs +105 -133
- package/dist/easing.mjs.map +1 -1
- package/dist/emitter.cjs +101 -96
- package/dist/emitter.cjs.map +1 -1
- package/dist/emitter.mjs +101 -97
- package/dist/emitter.mjs.map +1 -1
- package/dist/env.cjs +43 -9
- package/dist/env.cjs.map +1 -1
- package/dist/env.d.ts +1 -1
- package/dist/env.mjs +43 -15
- package/dist/env.mjs.map +1 -1
- package/dist/error.cjs +26 -5
- package/dist/error.cjs.map +1 -1
- package/dist/error.mjs +26 -7
- package/dist/error.mjs.map +1 -1
- package/dist/exception.cjs +38 -20
- package/dist/exception.cjs.map +1 -1
- package/dist/exception.d.ts +5 -5
- package/dist/exception.mjs +38 -21
- package/dist/exception.mjs.map +1 -1
- package/dist/function.cjs +128 -68
- package/dist/function.cjs.map +1 -1
- package/dist/function.mjs +128 -72
- package/dist/function.mjs.map +1 -1
- package/dist/index.cjs +8 -3
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +9 -5
- package/dist/index.mjs.map +1 -1
- package/dist/number.cjs +10 -14
- package/dist/number.mjs +2 -15
- package/dist/object/get-set.d.ts +2 -2
- package/dist/object/merge.d.ts +2 -2
- package/dist/object.cjs +369 -106
- package/dist/object.cjs.map +1 -1
- package/dist/object.mjs +366 -115
- package/dist/object.mjs.map +1 -1
- package/dist/path.cjs +144 -55
- package/dist/path.cjs.map +1 -1
- package/dist/path.mjs +144 -62
- package/dist/path.mjs.map +1 -1
- package/dist/promise.cjs +84 -43
- package/dist/promise.cjs.map +1 -1
- package/dist/promise.mjs +85 -50
- package/dist/promise.mjs.map +1 -1
- package/dist/qs.cjs +63 -39
- package/dist/qs.cjs.map +1 -1
- package/dist/qs.mjs +64 -42
- package/dist/qs.mjs.map +1 -1
- package/dist/regexp.cjs +118 -35
- package/dist/regexp.cjs.map +1 -1
- package/dist/regexp.mjs +119 -46
- package/dist/regexp.mjs.map +1 -1
- package/dist/string.cjs +12 -15
- package/dist/string.mjs +2 -16
- package/dist/string2.cjs +378 -142
- package/dist/string2.cjs.map +1 -1
- package/dist/string2.mjs +259 -143
- package/dist/string2.mjs.map +1 -1
- package/dist/time.cjs +136 -59
- package/dist/time.cjs.map +1 -1
- package/dist/time.mjs +136 -65
- package/dist/time.mjs.map +1 -1
- package/dist/timer.cjs +122 -112
- package/dist/timer.cjs.map +1 -1
- package/dist/timer.mjs +123 -115
- package/dist/timer.mjs.map +1 -1
- package/dist/tree.cjs +207 -112
- package/dist/tree.cjs.map +1 -1
- package/dist/tree.mjs +207 -116
- package/dist/tree.mjs.map +1 -1
- package/dist/try/curry.d.ts +1 -1
- package/dist/try.cjs +36 -37
- package/dist/try.cjs.map +1 -1
- package/dist/try.mjs +35 -37
- package/dist/try.mjs.map +1 -1
- package/dist/type.cjs +126 -24
- package/dist/type.cjs.map +1 -1
- package/dist/type.d.ts +2 -2
- package/dist/type.mjs +128 -45
- package/dist/type.mjs.map +1 -1
- package/dist/types.cjs +0 -2
- package/dist/types.d.ts +1 -1
- package/dist/types.mjs +0 -2
- package/dist/unique.cjs +41 -38
- package/dist/unique.cjs.map +1 -1
- package/dist/unique.mjs +42 -41
- package/dist/unique.mjs.map +1 -1
- package/dist/url.cjs +39 -30
- package/dist/url.cjs.map +1 -1
- package/dist/url.mjs +40 -33
- package/dist/url.mjs.map +1 -1
- package/dist/version.cjs +51 -33
- package/dist/version.cjs.map +1 -1
- package/dist/version.mjs +51 -35
- package/dist/version.mjs.map +1 -1
- package/package.json +109 -110
- package/dist/const.cjs +0 -14
- package/dist/const.cjs.map +0 -1
- package/dist/const.mjs +0 -15
- package/dist/const.mjs.map +0 -1
- package/dist/core.cjs +0 -362
- package/dist/core.cjs.map +0 -1
- package/dist/core.mjs +0 -363
- package/dist/core.mjs.map +0 -1
- package/dist/crypto/md5.d.mts +0 -1
- package/dist/crypto/sha1.d.mts +0 -1
- package/dist/crypto/sha256.d.mts +0 -1
- package/dist/crypto/sha512.d.mts +0 -1
- package/dist/each.cjs +0 -18
- package/dist/each.cjs.map +0 -1
- package/dist/each.mjs +0 -19
- package/dist/each.mjs.map +0 -1
- package/dist/merge.cjs +0 -87
- package/dist/merge.cjs.map +0 -1
- package/dist/merge.mjs +0 -88
- package/dist/merge.mjs.map +0 -1
- package/dist/number.cjs.map +0 -1
- package/dist/number.mjs.map +0 -1
- package/dist/string.cjs.map +0 -1
- package/dist/string.mjs.map +0 -1
- package/dist/types.cjs.map +0 -1
- package/dist/types.mjs.map +0 -1
package/dist/path.cjs
CHANGED
|
@@ -1,76 +1,164 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
-
const
|
|
2
|
+
const require_array = require("./array.cjs");
|
|
3
|
+
//#region src/path.ts
|
|
4
|
+
/**
|
|
5
|
+
* 判断是否是当前目录标记
|
|
6
|
+
* @param {string} slice - 路径片段
|
|
7
|
+
* @returns {boolean} - 如果是当前目录标记'.'则返回true,否则返回false
|
|
8
|
+
*/
|
|
4
9
|
function _isCurrentSlice(slice) {
|
|
5
|
-
|
|
10
|
+
return slice === ".";
|
|
6
11
|
}
|
|
12
|
+
/**
|
|
13
|
+
* 判断是否是上级目录标记
|
|
14
|
+
* @param {string} slice - 路径片段
|
|
15
|
+
* @returns {boolean} - 如果是上级目录标记'..'则返回true,否则返回false
|
|
16
|
+
*/
|
|
7
17
|
function _isParentSlice(slice) {
|
|
8
|
-
|
|
18
|
+
return slice === "..";
|
|
9
19
|
}
|
|
20
|
+
/**
|
|
21
|
+
* 判断是否是绝对路径
|
|
22
|
+
* @param {string} path - 路径字符串
|
|
23
|
+
* @returns {boolean} - 如果是绝对路径则返回true,否则返回false
|
|
24
|
+
* @example
|
|
25
|
+
* ```typescript
|
|
26
|
+
* const isAbs = isAbsolutePath('/path/to/file');
|
|
27
|
+
* console.log(isAbs); // 输出: true
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
10
30
|
function isAbsolutePath(path) {
|
|
11
|
-
|
|
31
|
+
return path.startsWith("/");
|
|
12
32
|
}
|
|
33
|
+
/**
|
|
34
|
+
* 判断是否是相对路径
|
|
35
|
+
* @param {string} path - 路径字符串
|
|
36
|
+
* @returns {boolean} - 如果是相对路径则返回true,否则返回false
|
|
37
|
+
* @example
|
|
38
|
+
* ```typescript
|
|
39
|
+
* const isRel = isRelativePath('path/to/file');
|
|
40
|
+
* console.log(isRel); // 输出: true
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
13
43
|
function isRelativePath(path) {
|
|
14
|
-
|
|
44
|
+
return !isAbsolutePath(path);
|
|
15
45
|
}
|
|
46
|
+
/**
|
|
47
|
+
* 标准化路径
|
|
48
|
+
* @param {string} path - 要标准化的路径字符串。
|
|
49
|
+
* @returns {string} - 标准化后的路径字符串。
|
|
50
|
+
* @example
|
|
51
|
+
* ```typescript
|
|
52
|
+
* const normalizedPath = pathNormalize('/path///to///file');
|
|
53
|
+
* console.log(normalizedPath); // 输出: '/path/to/file'
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
16
56
|
function pathNormalize(path) {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
continue;
|
|
40
|
-
}
|
|
41
|
-
push(slice);
|
|
42
|
-
}
|
|
43
|
-
return points.join("/") || (isAbs ? "/" : ".");
|
|
57
|
+
const slices = path.replace(/\\/g, "/").replace(/\/{2,}/g, "/").replace(/\.{3,}/g, "..").replace(/\/\.\//g, "/").split("/").map((point) => point.trim());
|
|
58
|
+
const points = [];
|
|
59
|
+
const isAbs = slices[0] === "";
|
|
60
|
+
const push = (point) => {
|
|
61
|
+
points.push(point);
|
|
62
|
+
};
|
|
63
|
+
const back = () => {
|
|
64
|
+
if (points.length === 1 && isAbs) return;
|
|
65
|
+
if (points.length === 0 || points.at(-1) === "..") points.push("..");
|
|
66
|
+
else points.pop();
|
|
67
|
+
};
|
|
68
|
+
for (const slice of slices) {
|
|
69
|
+
const isCurrent = _isCurrentSlice(slice);
|
|
70
|
+
const isParent = _isParentSlice(slice);
|
|
71
|
+
if (isCurrent) continue;
|
|
72
|
+
if (isParent) {
|
|
73
|
+
back();
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
push(slice);
|
|
77
|
+
}
|
|
78
|
+
return points.join("/") || (isAbs ? "/" : ".");
|
|
44
79
|
}
|
|
80
|
+
/**
|
|
81
|
+
* 路径合并
|
|
82
|
+
* @param {string} from - 起始路径。
|
|
83
|
+
* @param {...string[]} to - 要合并的路径片段。
|
|
84
|
+
* @returns {string} - 合并后的路径字符串。
|
|
85
|
+
* @example
|
|
86
|
+
* ```typescript
|
|
87
|
+
* const fullPath = pathJoin('/path', '/to', 'file');
|
|
88
|
+
* console.log(fullPath); // 输出: '/path/to/file'
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
45
91
|
function pathJoin(from, ...to) {
|
|
46
|
-
|
|
92
|
+
return pathNormalize([from, ...to].join("/"));
|
|
47
93
|
}
|
|
94
|
+
/**
|
|
95
|
+
* 解析路径
|
|
96
|
+
* @param {string} from - 起始路径
|
|
97
|
+
* @param {...string[]} to - 要解析的路径片段
|
|
98
|
+
* @returns {string} - 解析后的绝对路径
|
|
99
|
+
* @example
|
|
100
|
+
* ```typescript
|
|
101
|
+
* const resolvedPath = pathResolve('/path', '/to', 'file');
|
|
102
|
+
* console.log(resolvedPath); // 输出: '/to/file'
|
|
103
|
+
* ```
|
|
104
|
+
*/
|
|
48
105
|
function pathResolve(from, ...to) {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
},
|
|
61
|
-
true
|
|
62
|
-
);
|
|
63
|
-
return pathJoin(lastStartPath, ...paths.slice(lastStartIndex + 1));
|
|
106
|
+
const paths = [from, ...to].map(pathNormalize);
|
|
107
|
+
let lastStartPath = from;
|
|
108
|
+
let lastStartIndex = 0;
|
|
109
|
+
require_array.arrayEach(paths, (path, index) => {
|
|
110
|
+
if (isAbsolutePath(path)) {
|
|
111
|
+
lastStartPath = path;
|
|
112
|
+
lastStartIndex = index;
|
|
113
|
+
return false;
|
|
114
|
+
}
|
|
115
|
+
}, true);
|
|
116
|
+
return pathJoin(lastStartPath, ...paths.slice(lastStartIndex + 1));
|
|
64
117
|
}
|
|
118
|
+
/**
|
|
119
|
+
* 将相对路径转换为标准的相对路径格式(添加'./'前缀)
|
|
120
|
+
*
|
|
121
|
+
* @param {string} path - 要处理的路径字符串
|
|
122
|
+
* @returns {string} 处理后的路径字符串
|
|
123
|
+
*
|
|
124
|
+
* @example <caption>处理绝对路径</caption>
|
|
125
|
+
* ```typescript
|
|
126
|
+
* const result = pathRelativize('/path/to/file');
|
|
127
|
+
* console.log(result); // 输出: '/path/to/file'
|
|
128
|
+
* ```
|
|
129
|
+
*
|
|
130
|
+
* @example <caption>处理已带'./'前缀的相对路径</caption>
|
|
131
|
+
* ```typescript
|
|
132
|
+
* const result = pathRelativize('./path/to/file');
|
|
133
|
+
* console.log(result); // 输出: './path/to/file'
|
|
134
|
+
* ```
|
|
135
|
+
*
|
|
136
|
+
* @example <caption>处理不带'./'前缀的相对路径</caption>
|
|
137
|
+
* ```typescript
|
|
138
|
+
* const result = pathRelativize('path/to/file');
|
|
139
|
+
* console.log(result); // 输出: './path/to/file'
|
|
140
|
+
* ```
|
|
141
|
+
*/
|
|
65
142
|
function pathRelativize(path) {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
143
|
+
if (isAbsolutePath(path)) return path;
|
|
144
|
+
if (path.startsWith("./")) return path;
|
|
145
|
+
if (path.startsWith("../")) return path;
|
|
146
|
+
return `./${path}`;
|
|
70
147
|
}
|
|
148
|
+
/**
|
|
149
|
+
* 获取路径的目录部分
|
|
150
|
+
* @param {string} path - 输入的路径字符串
|
|
151
|
+
* @returns {string} - 返回路径的目录部分
|
|
152
|
+
* @example
|
|
153
|
+
* ```typescript
|
|
154
|
+
* const dir = pathDirname('/path/to/file.txt');
|
|
155
|
+
* console.log(dir); // 输出: '/path/to'
|
|
156
|
+
* ```
|
|
157
|
+
*/
|
|
71
158
|
function pathDirname(path) {
|
|
72
|
-
|
|
159
|
+
return pathJoin(path, "..");
|
|
73
160
|
}
|
|
161
|
+
//#endregion
|
|
74
162
|
exports.isAbsolutePath = isAbsolutePath;
|
|
75
163
|
exports.isRelativePath = isRelativePath;
|
|
76
164
|
exports.pathDirname = pathDirname;
|
|
@@ -78,4 +166,5 @@ exports.pathJoin = pathJoin;
|
|
|
78
166
|
exports.pathNormalize = pathNormalize;
|
|
79
167
|
exports.pathRelativize = pathRelativize;
|
|
80
168
|
exports.pathResolve = pathResolve;
|
|
81
|
-
|
|
169
|
+
|
|
170
|
+
//# sourceMappingURL=path.cjs.map
|
package/dist/path.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"path.cjs","sources":["../src/path.ts"],"sourcesContent":["import { arrayEach } from './array';\n\n/**\n * 判断是否是当前目录标记\n * @param {string} slice - 路径片段\n * @returns {boolean} - 如果是当前目录标记'.'则返回true,否则返回false\n */\nfunction _isCurrentSlice(slice: string): boolean {\n return slice === '.';\n}\n\n/**\n * 判断是否是上级目录标记\n * @param {string} slice - 路径片段\n * @returns {boolean} - 如果是上级目录标记'..'则返回true,否则返回false\n */\nfunction _isParentSlice(slice: string): boolean {\n return slice === '..';\n}\n\n/**\n * 判断是否是绝对路径\n * @param {string} path - 路径字符串\n * @returns {boolean} - 如果是绝对路径则返回true,否则返回false\n * @example\n * ```typescript\n * const isAbs = isAbsolutePath('/path/to/file');\n * console.log(isAbs); // 输出: true\n * ```\n */\nexport function isAbsolutePath(path: string): boolean {\n return path.startsWith('/');\n}\n\n/**\n * 判断是否是相对路径\n * @param {string} path - 路径字符串\n * @returns {boolean} - 如果是相对路径则返回true,否则返回false\n * @example\n * ```typescript\n * const isRel = isRelativePath('path/to/file');\n * console.log(isRel); // 输出: true\n * ```\n */\nexport function isRelativePath(path: string): boolean {\n return !isAbsolutePath(path);\n}\n\n/**\n * 标准化路径\n * @param {string} path - 要标准化的路径字符串。\n * @returns {string} - 标准化后的路径字符串。\n * @example\n * ```typescript\n * const normalizedPath = pathNormalize('/path///to///file');\n * console.log(normalizedPath); // 输出: '/path/to/file'\n * ```\n */\nexport function pathNormalize(path: string): string {\n const slices = path\n .replace(/\\\\/g, '/')\n .replace(/\\/{2,}/g, '/')\n .replace(/\\.{3,}/g, '..')\n .replace(/\\/\\.\\//g, '/')\n .split('/')\n .map((point) => point.trim());\n const points: string[] = [];\n const isAbs = slices[0] === '';\n\n const push = (point: string) => {\n points.push(point);\n };\n\n const back = () => {\n // 绝对路径不能退到根目录\n if (points.length === 1 && isAbs) return;\n\n //\n if (points.length === 0 || points.at(-1) === '..') {\n points.push('..');\n } else {\n points.pop();\n }\n };\n\n for (const slice of slices) {\n const isCurrent = _isCurrentSlice(slice);\n const isParent = _isParentSlice(slice);\n\n // // 未进入实际路径\n // if (!inPoints) {\n // push(slice);\n // inPoints = !isCurrent && !isParent;\n // continue;\n // }\n\n if (isCurrent) {\n continue;\n }\n\n if (isParent) {\n back();\n continue;\n }\n\n push(slice);\n }\n\n return points.join('/') || (isAbs ? '/' : '.');\n}\n\n/**\n * 路径合并\n * @param {string} from - 起始路径。\n * @param {...string[]} to - 要合并的路径片段。\n * @returns {string} - 合并后的路径字符串。\n * @example\n * ```typescript\n * const fullPath = pathJoin('/path', '/to', 'file');\n * console.log(fullPath); // 输出: '/path/to/file'\n * ```\n */\nexport function pathJoin(from: string, ...to: string[]): string {\n return pathNormalize([from, ...to].join('/'));\n}\n\n/**\n * 解析路径\n * @param {string} from - 起始路径\n * @param {...string[]} to - 要解析的路径片段\n * @returns {string} - 解析后的绝对路径\n * @example\n * ```typescript\n * const resolvedPath = pathResolve('/path', '/to', 'file');\n * console.log(resolvedPath); // 输出: '/to/file'\n * ```\n */\nexport function pathResolve(from: string, ...to: string[]): string {\n const paths = [from, ...to].map(pathNormalize);\n\n let lastStartPath = from;\n let lastStartIndex = 0;\n\n arrayEach(\n paths,\n (path, index) => {\n if (isAbsolutePath(path)) {\n lastStartPath = path;\n lastStartIndex = index;\n return false;\n }\n },\n true,\n );\n\n return pathJoin(lastStartPath, ...paths.slice(lastStartIndex + 1));\n}\n\n/**\n * 将相对路径转换为标准的相对路径格式(添加'./'前缀)\n *\n * @param {string} path - 要处理的路径字符串\n * @returns {string} 处理后的路径字符串\n *\n * @example <caption>处理绝对路径</caption>\n * ```typescript\n * const result = pathRelativize('/path/to/file');\n * console.log(result); // 输出: '/path/to/file'\n * ```\n *\n * @example <caption>处理已带'./'前缀的相对路径</caption>\n * ```typescript\n * const result = pathRelativize('./path/to/file');\n * console.log(result); // 输出: './path/to/file'\n * ```\n *\n * @example <caption>处理不带'./'前缀的相对路径</caption>\n * ```typescript\n * const result = pathRelativize('path/to/file');\n * console.log(result); // 输出: './path/to/file'\n * ```\n */\nexport function pathRelativize(path: string): string {\n if (isAbsolutePath(path)) return path;\n if (path.startsWith('./')) return path;\n if (path.startsWith('../')) return path;\n return `./${path}`;\n}\n\n/**\n * 获取路径的目录部分\n * @param {string} path - 输入的路径字符串\n * @returns {string} - 返回路径的目录部分\n * @example\n * ```typescript\n * const dir = pathDirname('/path/to/file.txt');\n * console.log(dir); // 输出: '/path/to'\n * ```\n */\nexport function pathDirname(path: string): string {\n return pathJoin(path, '..');\n}\n"],"
|
|
1
|
+
{"version":3,"file":"path.cjs","names":[],"sources":["../src/path.ts"],"sourcesContent":["import { arrayEach } from './array';\n\n/**\n * 判断是否是当前目录标记\n * @param {string} slice - 路径片段\n * @returns {boolean} - 如果是当前目录标记'.'则返回true,否则返回false\n */\nfunction _isCurrentSlice(slice: string): boolean {\n return slice === '.';\n}\n\n/**\n * 判断是否是上级目录标记\n * @param {string} slice - 路径片段\n * @returns {boolean} - 如果是上级目录标记'..'则返回true,否则返回false\n */\nfunction _isParentSlice(slice: string): boolean {\n return slice === '..';\n}\n\n/**\n * 判断是否是绝对路径\n * @param {string} path - 路径字符串\n * @returns {boolean} - 如果是绝对路径则返回true,否则返回false\n * @example\n * ```typescript\n * const isAbs = isAbsolutePath('/path/to/file');\n * console.log(isAbs); // 输出: true\n * ```\n */\nexport function isAbsolutePath(path: string): boolean {\n return path.startsWith('/');\n}\n\n/**\n * 判断是否是相对路径\n * @param {string} path - 路径字符串\n * @returns {boolean} - 如果是相对路径则返回true,否则返回false\n * @example\n * ```typescript\n * const isRel = isRelativePath('path/to/file');\n * console.log(isRel); // 输出: true\n * ```\n */\nexport function isRelativePath(path: string): boolean {\n return !isAbsolutePath(path);\n}\n\n/**\n * 标准化路径\n * @param {string} path - 要标准化的路径字符串。\n * @returns {string} - 标准化后的路径字符串。\n * @example\n * ```typescript\n * const normalizedPath = pathNormalize('/path///to///file');\n * console.log(normalizedPath); // 输出: '/path/to/file'\n * ```\n */\nexport function pathNormalize(path: string): string {\n const slices = path\n .replace(/\\\\/g, '/')\n .replace(/\\/{2,}/g, '/')\n .replace(/\\.{3,}/g, '..')\n .replace(/\\/\\.\\//g, '/')\n .split('/')\n .map((point) => point.trim());\n const points: string[] = [];\n const isAbs = slices[0] === '';\n\n const push = (point: string) => {\n points.push(point);\n };\n\n const back = () => {\n // 绝对路径不能退到根目录\n if (points.length === 1 && isAbs) return;\n\n //\n if (points.length === 0 || points.at(-1) === '..') {\n points.push('..');\n } else {\n points.pop();\n }\n };\n\n for (const slice of slices) {\n const isCurrent = _isCurrentSlice(slice);\n const isParent = _isParentSlice(slice);\n\n // // 未进入实际路径\n // if (!inPoints) {\n // push(slice);\n // inPoints = !isCurrent && !isParent;\n // continue;\n // }\n\n if (isCurrent) {\n continue;\n }\n\n if (isParent) {\n back();\n continue;\n }\n\n push(slice);\n }\n\n return points.join('/') || (isAbs ? '/' : '.');\n}\n\n/**\n * 路径合并\n * @param {string} from - 起始路径。\n * @param {...string[]} to - 要合并的路径片段。\n * @returns {string} - 合并后的路径字符串。\n * @example\n * ```typescript\n * const fullPath = pathJoin('/path', '/to', 'file');\n * console.log(fullPath); // 输出: '/path/to/file'\n * ```\n */\nexport function pathJoin(from: string, ...to: string[]): string {\n return pathNormalize([from, ...to].join('/'));\n}\n\n/**\n * 解析路径\n * @param {string} from - 起始路径\n * @param {...string[]} to - 要解析的路径片段\n * @returns {string} - 解析后的绝对路径\n * @example\n * ```typescript\n * const resolvedPath = pathResolve('/path', '/to', 'file');\n * console.log(resolvedPath); // 输出: '/to/file'\n * ```\n */\nexport function pathResolve(from: string, ...to: string[]): string {\n const paths = [from, ...to].map(pathNormalize);\n\n let lastStartPath = from;\n let lastStartIndex = 0;\n\n arrayEach(\n paths,\n (path, index) => {\n if (isAbsolutePath(path)) {\n lastStartPath = path;\n lastStartIndex = index;\n return false;\n }\n },\n true,\n );\n\n return pathJoin(lastStartPath, ...paths.slice(lastStartIndex + 1));\n}\n\n/**\n * 将相对路径转换为标准的相对路径格式(添加'./'前缀)\n *\n * @param {string} path - 要处理的路径字符串\n * @returns {string} 处理后的路径字符串\n *\n * @example <caption>处理绝对路径</caption>\n * ```typescript\n * const result = pathRelativize('/path/to/file');\n * console.log(result); // 输出: '/path/to/file'\n * ```\n *\n * @example <caption>处理已带'./'前缀的相对路径</caption>\n * ```typescript\n * const result = pathRelativize('./path/to/file');\n * console.log(result); // 输出: './path/to/file'\n * ```\n *\n * @example <caption>处理不带'./'前缀的相对路径</caption>\n * ```typescript\n * const result = pathRelativize('path/to/file');\n * console.log(result); // 输出: './path/to/file'\n * ```\n */\nexport function pathRelativize(path: string): string {\n if (isAbsolutePath(path)) return path;\n if (path.startsWith('./')) return path;\n if (path.startsWith('../')) return path;\n return `./${path}`;\n}\n\n/**\n * 获取路径的目录部分\n * @param {string} path - 输入的路径字符串\n * @returns {string} - 返回路径的目录部分\n * @example\n * ```typescript\n * const dir = pathDirname('/path/to/file.txt');\n * console.log(dir); // 输出: '/path/to'\n * ```\n */\nexport function pathDirname(path: string): string {\n return pathJoin(path, '..');\n}\n"],"mappings":";;;;;;;;AAOA,SAAS,gBAAgB,OAAwB;CAC/C,OAAO,UAAU;;;;;;;AAQnB,SAAS,eAAe,OAAwB;CAC9C,OAAO,UAAU;;;;;;;;;;;;AAanB,SAAgB,eAAe,MAAuB;CACpD,OAAO,KAAK,WAAW,IAAI;;;;;;;;;;;;AAa7B,SAAgB,eAAe,MAAuB;CACpD,OAAO,CAAC,eAAe,KAAK;;;;;;;;;;;;AAa9B,SAAgB,cAAc,MAAsB;CAClD,MAAM,SAAS,KACZ,QAAQ,OAAO,IAAI,CACnB,QAAQ,WAAW,IAAI,CACvB,QAAQ,WAAW,KAAK,CACxB,QAAQ,WAAW,IAAI,CACvB,MAAM,IAAI,CACV,KAAK,UAAU,MAAM,MAAM,CAAC;CAC/B,MAAM,SAAmB,EAAE;CAC3B,MAAM,QAAQ,OAAO,OAAO;CAE5B,MAAM,QAAQ,UAAkB;EAC9B,OAAO,KAAK,MAAM;;CAGpB,MAAM,aAAa;EAEjB,IAAI,OAAO,WAAW,KAAK,OAAO;EAGlC,IAAI,OAAO,WAAW,KAAK,OAAO,GAAG,GAAG,KAAK,MAC3C,OAAO,KAAK,KAAK;OAEjB,OAAO,KAAK;;CAIhB,KAAK,MAAM,SAAS,QAAQ;EAC1B,MAAM,YAAY,gBAAgB,MAAM;EACxC,MAAM,WAAW,eAAe,MAAM;EAStC,IAAI,WACF;EAGF,IAAI,UAAU;GACZ,MAAM;GACN;;EAGF,KAAK,MAAM;;CAGb,OAAO,OAAO,KAAK,IAAI,KAAK,QAAQ,MAAM;;;;;;;;;;;;;AAc5C,SAAgB,SAAS,MAAc,GAAG,IAAsB;CAC9D,OAAO,cAAc,CAAC,MAAM,GAAG,GAAG,CAAC,KAAK,IAAI,CAAC;;;;;;;;;;;;;AAc/C,SAAgB,YAAY,MAAc,GAAG,IAAsB;CACjE,MAAM,QAAQ,CAAC,MAAM,GAAG,GAAG,CAAC,IAAI,cAAc;CAE9C,IAAI,gBAAgB;CACpB,IAAI,iBAAiB;CAErB,cAAA,UACE,QACC,MAAM,UAAU;EACf,IAAI,eAAe,KAAK,EAAE;GACxB,gBAAgB;GAChB,iBAAiB;GACjB,OAAO;;IAGX,KACD;CAED,OAAO,SAAS,eAAe,GAAG,MAAM,MAAM,iBAAiB,EAAE,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;AA2BpE,SAAgB,eAAe,MAAsB;CACnD,IAAI,eAAe,KAAK,EAAE,OAAO;CACjC,IAAI,KAAK,WAAW,KAAK,EAAE,OAAO;CAClC,IAAI,KAAK,WAAW,MAAM,EAAE,OAAO;CACnC,OAAO,KAAK;;;;;;;;;;;;AAad,SAAgB,YAAY,MAAsB;CAChD,OAAO,SAAS,MAAM,KAAK"}
|
package/dist/path.mjs
CHANGED
|
@@ -1,81 +1,163 @@
|
|
|
1
1
|
import { arrayEach } from "./array.mjs";
|
|
2
|
+
//#region src/path.ts
|
|
3
|
+
/**
|
|
4
|
+
* 判断是否是当前目录标记
|
|
5
|
+
* @param {string} slice - 路径片段
|
|
6
|
+
* @returns {boolean} - 如果是当前目录标记'.'则返回true,否则返回false
|
|
7
|
+
*/
|
|
2
8
|
function _isCurrentSlice(slice) {
|
|
3
|
-
|
|
9
|
+
return slice === ".";
|
|
4
10
|
}
|
|
11
|
+
/**
|
|
12
|
+
* 判断是否是上级目录标记
|
|
13
|
+
* @param {string} slice - 路径片段
|
|
14
|
+
* @returns {boolean} - 如果是上级目录标记'..'则返回true,否则返回false
|
|
15
|
+
*/
|
|
5
16
|
function _isParentSlice(slice) {
|
|
6
|
-
|
|
17
|
+
return slice === "..";
|
|
7
18
|
}
|
|
19
|
+
/**
|
|
20
|
+
* 判断是否是绝对路径
|
|
21
|
+
* @param {string} path - 路径字符串
|
|
22
|
+
* @returns {boolean} - 如果是绝对路径则返回true,否则返回false
|
|
23
|
+
* @example
|
|
24
|
+
* ```typescript
|
|
25
|
+
* const isAbs = isAbsolutePath('/path/to/file');
|
|
26
|
+
* console.log(isAbs); // 输出: true
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
8
29
|
function isAbsolutePath(path) {
|
|
9
|
-
|
|
30
|
+
return path.startsWith("/");
|
|
10
31
|
}
|
|
32
|
+
/**
|
|
33
|
+
* 判断是否是相对路径
|
|
34
|
+
* @param {string} path - 路径字符串
|
|
35
|
+
* @returns {boolean} - 如果是相对路径则返回true,否则返回false
|
|
36
|
+
* @example
|
|
37
|
+
* ```typescript
|
|
38
|
+
* const isRel = isRelativePath('path/to/file');
|
|
39
|
+
* console.log(isRel); // 输出: true
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
11
42
|
function isRelativePath(path) {
|
|
12
|
-
|
|
43
|
+
return !isAbsolutePath(path);
|
|
13
44
|
}
|
|
45
|
+
/**
|
|
46
|
+
* 标准化路径
|
|
47
|
+
* @param {string} path - 要标准化的路径字符串。
|
|
48
|
+
* @returns {string} - 标准化后的路径字符串。
|
|
49
|
+
* @example
|
|
50
|
+
* ```typescript
|
|
51
|
+
* const normalizedPath = pathNormalize('/path///to///file');
|
|
52
|
+
* console.log(normalizedPath); // 输出: '/path/to/file'
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
14
55
|
function pathNormalize(path) {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
continue;
|
|
38
|
-
}
|
|
39
|
-
push(slice);
|
|
40
|
-
}
|
|
41
|
-
return points.join("/") || (isAbs ? "/" : ".");
|
|
56
|
+
const slices = path.replace(/\\/g, "/").replace(/\/{2,}/g, "/").replace(/\.{3,}/g, "..").replace(/\/\.\//g, "/").split("/").map((point) => point.trim());
|
|
57
|
+
const points = [];
|
|
58
|
+
const isAbs = slices[0] === "";
|
|
59
|
+
const push = (point) => {
|
|
60
|
+
points.push(point);
|
|
61
|
+
};
|
|
62
|
+
const back = () => {
|
|
63
|
+
if (points.length === 1 && isAbs) return;
|
|
64
|
+
if (points.length === 0 || points.at(-1) === "..") points.push("..");
|
|
65
|
+
else points.pop();
|
|
66
|
+
};
|
|
67
|
+
for (const slice of slices) {
|
|
68
|
+
const isCurrent = _isCurrentSlice(slice);
|
|
69
|
+
const isParent = _isParentSlice(slice);
|
|
70
|
+
if (isCurrent) continue;
|
|
71
|
+
if (isParent) {
|
|
72
|
+
back();
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
push(slice);
|
|
76
|
+
}
|
|
77
|
+
return points.join("/") || (isAbs ? "/" : ".");
|
|
42
78
|
}
|
|
79
|
+
/**
|
|
80
|
+
* 路径合并
|
|
81
|
+
* @param {string} from - 起始路径。
|
|
82
|
+
* @param {...string[]} to - 要合并的路径片段。
|
|
83
|
+
* @returns {string} - 合并后的路径字符串。
|
|
84
|
+
* @example
|
|
85
|
+
* ```typescript
|
|
86
|
+
* const fullPath = pathJoin('/path', '/to', 'file');
|
|
87
|
+
* console.log(fullPath); // 输出: '/path/to/file'
|
|
88
|
+
* ```
|
|
89
|
+
*/
|
|
43
90
|
function pathJoin(from, ...to) {
|
|
44
|
-
|
|
91
|
+
return pathNormalize([from, ...to].join("/"));
|
|
45
92
|
}
|
|
93
|
+
/**
|
|
94
|
+
* 解析路径
|
|
95
|
+
* @param {string} from - 起始路径
|
|
96
|
+
* @param {...string[]} to - 要解析的路径片段
|
|
97
|
+
* @returns {string} - 解析后的绝对路径
|
|
98
|
+
* @example
|
|
99
|
+
* ```typescript
|
|
100
|
+
* const resolvedPath = pathResolve('/path', '/to', 'file');
|
|
101
|
+
* console.log(resolvedPath); // 输出: '/to/file'
|
|
102
|
+
* ```
|
|
103
|
+
*/
|
|
46
104
|
function pathResolve(from, ...to) {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
},
|
|
59
|
-
true
|
|
60
|
-
);
|
|
61
|
-
return pathJoin(lastStartPath, ...paths.slice(lastStartIndex + 1));
|
|
105
|
+
const paths = [from, ...to].map(pathNormalize);
|
|
106
|
+
let lastStartPath = from;
|
|
107
|
+
let lastStartIndex = 0;
|
|
108
|
+
arrayEach(paths, (path, index) => {
|
|
109
|
+
if (isAbsolutePath(path)) {
|
|
110
|
+
lastStartPath = path;
|
|
111
|
+
lastStartIndex = index;
|
|
112
|
+
return false;
|
|
113
|
+
}
|
|
114
|
+
}, true);
|
|
115
|
+
return pathJoin(lastStartPath, ...paths.slice(lastStartIndex + 1));
|
|
62
116
|
}
|
|
117
|
+
/**
|
|
118
|
+
* 将相对路径转换为标准的相对路径格式(添加'./'前缀)
|
|
119
|
+
*
|
|
120
|
+
* @param {string} path - 要处理的路径字符串
|
|
121
|
+
* @returns {string} 处理后的路径字符串
|
|
122
|
+
*
|
|
123
|
+
* @example <caption>处理绝对路径</caption>
|
|
124
|
+
* ```typescript
|
|
125
|
+
* const result = pathRelativize('/path/to/file');
|
|
126
|
+
* console.log(result); // 输出: '/path/to/file'
|
|
127
|
+
* ```
|
|
128
|
+
*
|
|
129
|
+
* @example <caption>处理已带'./'前缀的相对路径</caption>
|
|
130
|
+
* ```typescript
|
|
131
|
+
* const result = pathRelativize('./path/to/file');
|
|
132
|
+
* console.log(result); // 输出: './path/to/file'
|
|
133
|
+
* ```
|
|
134
|
+
*
|
|
135
|
+
* @example <caption>处理不带'./'前缀的相对路径</caption>
|
|
136
|
+
* ```typescript
|
|
137
|
+
* const result = pathRelativize('path/to/file');
|
|
138
|
+
* console.log(result); // 输出: './path/to/file'
|
|
139
|
+
* ```
|
|
140
|
+
*/
|
|
63
141
|
function pathRelativize(path) {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
142
|
+
if (isAbsolutePath(path)) return path;
|
|
143
|
+
if (path.startsWith("./")) return path;
|
|
144
|
+
if (path.startsWith("../")) return path;
|
|
145
|
+
return `./${path}`;
|
|
68
146
|
}
|
|
147
|
+
/**
|
|
148
|
+
* 获取路径的目录部分
|
|
149
|
+
* @param {string} path - 输入的路径字符串
|
|
150
|
+
* @returns {string} - 返回路径的目录部分
|
|
151
|
+
* @example
|
|
152
|
+
* ```typescript
|
|
153
|
+
* const dir = pathDirname('/path/to/file.txt');
|
|
154
|
+
* console.log(dir); // 输出: '/path/to'
|
|
155
|
+
* ```
|
|
156
|
+
*/
|
|
69
157
|
function pathDirname(path) {
|
|
70
|
-
|
|
158
|
+
return pathJoin(path, "..");
|
|
71
159
|
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
pathJoin,
|
|
77
|
-
pathNormalize,
|
|
78
|
-
pathRelativize,
|
|
79
|
-
pathResolve
|
|
80
|
-
};
|
|
81
|
-
//# sourceMappingURL=path.mjs.map
|
|
160
|
+
//#endregion
|
|
161
|
+
export { isAbsolutePath, isRelativePath, pathDirname, pathJoin, pathNormalize, pathRelativize, pathResolve };
|
|
162
|
+
|
|
163
|
+
//# sourceMappingURL=path.mjs.map
|
package/dist/path.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"path.mjs","sources":["../src/path.ts"],"sourcesContent":["import { arrayEach } from './array';\n\n/**\n * 判断是否是当前目录标记\n * @param {string} slice - 路径片段\n * @returns {boolean} - 如果是当前目录标记'.'则返回true,否则返回false\n */\nfunction _isCurrentSlice(slice: string): boolean {\n return slice === '.';\n}\n\n/**\n * 判断是否是上级目录标记\n * @param {string} slice - 路径片段\n * @returns {boolean} - 如果是上级目录标记'..'则返回true,否则返回false\n */\nfunction _isParentSlice(slice: string): boolean {\n return slice === '..';\n}\n\n/**\n * 判断是否是绝对路径\n * @param {string} path - 路径字符串\n * @returns {boolean} - 如果是绝对路径则返回true,否则返回false\n * @example\n * ```typescript\n * const isAbs = isAbsolutePath('/path/to/file');\n * console.log(isAbs); // 输出: true\n * ```\n */\nexport function isAbsolutePath(path: string): boolean {\n return path.startsWith('/');\n}\n\n/**\n * 判断是否是相对路径\n * @param {string} path - 路径字符串\n * @returns {boolean} - 如果是相对路径则返回true,否则返回false\n * @example\n * ```typescript\n * const isRel = isRelativePath('path/to/file');\n * console.log(isRel); // 输出: true\n * ```\n */\nexport function isRelativePath(path: string): boolean {\n return !isAbsolutePath(path);\n}\n\n/**\n * 标准化路径\n * @param {string} path - 要标准化的路径字符串。\n * @returns {string} - 标准化后的路径字符串。\n * @example\n * ```typescript\n * const normalizedPath = pathNormalize('/path///to///file');\n * console.log(normalizedPath); // 输出: '/path/to/file'\n * ```\n */\nexport function pathNormalize(path: string): string {\n const slices = path\n .replace(/\\\\/g, '/')\n .replace(/\\/{2,}/g, '/')\n .replace(/\\.{3,}/g, '..')\n .replace(/\\/\\.\\//g, '/')\n .split('/')\n .map((point) => point.trim());\n const points: string[] = [];\n const isAbs = slices[0] === '';\n\n const push = (point: string) => {\n points.push(point);\n };\n\n const back = () => {\n // 绝对路径不能退到根目录\n if (points.length === 1 && isAbs) return;\n\n //\n if (points.length === 0 || points.at(-1) === '..') {\n points.push('..');\n } else {\n points.pop();\n }\n };\n\n for (const slice of slices) {\n const isCurrent = _isCurrentSlice(slice);\n const isParent = _isParentSlice(slice);\n\n // // 未进入实际路径\n // if (!inPoints) {\n // push(slice);\n // inPoints = !isCurrent && !isParent;\n // continue;\n // }\n\n if (isCurrent) {\n continue;\n }\n\n if (isParent) {\n back();\n continue;\n }\n\n push(slice);\n }\n\n return points.join('/') || (isAbs ? '/' : '.');\n}\n\n/**\n * 路径合并\n * @param {string} from - 起始路径。\n * @param {...string[]} to - 要合并的路径片段。\n * @returns {string} - 合并后的路径字符串。\n * @example\n * ```typescript\n * const fullPath = pathJoin('/path', '/to', 'file');\n * console.log(fullPath); // 输出: '/path/to/file'\n * ```\n */\nexport function pathJoin(from: string, ...to: string[]): string {\n return pathNormalize([from, ...to].join('/'));\n}\n\n/**\n * 解析路径\n * @param {string} from - 起始路径\n * @param {...string[]} to - 要解析的路径片段\n * @returns {string} - 解析后的绝对路径\n * @example\n * ```typescript\n * const resolvedPath = pathResolve('/path', '/to', 'file');\n * console.log(resolvedPath); // 输出: '/to/file'\n * ```\n */\nexport function pathResolve(from: string, ...to: string[]): string {\n const paths = [from, ...to].map(pathNormalize);\n\n let lastStartPath = from;\n let lastStartIndex = 0;\n\n arrayEach(\n paths,\n (path, index) => {\n if (isAbsolutePath(path)) {\n lastStartPath = path;\n lastStartIndex = index;\n return false;\n }\n },\n true,\n );\n\n return pathJoin(lastStartPath, ...paths.slice(lastStartIndex + 1));\n}\n\n/**\n * 将相对路径转换为标准的相对路径格式(添加'./'前缀)\n *\n * @param {string} path - 要处理的路径字符串\n * @returns {string} 处理后的路径字符串\n *\n * @example <caption>处理绝对路径</caption>\n * ```typescript\n * const result = pathRelativize('/path/to/file');\n * console.log(result); // 输出: '/path/to/file'\n * ```\n *\n * @example <caption>处理已带'./'前缀的相对路径</caption>\n * ```typescript\n * const result = pathRelativize('./path/to/file');\n * console.log(result); // 输出: './path/to/file'\n * ```\n *\n * @example <caption>处理不带'./'前缀的相对路径</caption>\n * ```typescript\n * const result = pathRelativize('path/to/file');\n * console.log(result); // 输出: './path/to/file'\n * ```\n */\nexport function pathRelativize(path: string): string {\n if (isAbsolutePath(path)) return path;\n if (path.startsWith('./')) return path;\n if (path.startsWith('../')) return path;\n return `./${path}`;\n}\n\n/**\n * 获取路径的目录部分\n * @param {string} path - 输入的路径字符串\n * @returns {string} - 返回路径的目录部分\n * @example\n * ```typescript\n * const dir = pathDirname('/path/to/file.txt');\n * console.log(dir); // 输出: '/path/to'\n * ```\n */\nexport function pathDirname(path: string): string {\n return pathJoin(path, '..');\n}\n"],"
|
|
1
|
+
{"version":3,"file":"path.mjs","names":[],"sources":["../src/path.ts"],"sourcesContent":["import { arrayEach } from './array';\n\n/**\n * 判断是否是当前目录标记\n * @param {string} slice - 路径片段\n * @returns {boolean} - 如果是当前目录标记'.'则返回true,否则返回false\n */\nfunction _isCurrentSlice(slice: string): boolean {\n return slice === '.';\n}\n\n/**\n * 判断是否是上级目录标记\n * @param {string} slice - 路径片段\n * @returns {boolean} - 如果是上级目录标记'..'则返回true,否则返回false\n */\nfunction _isParentSlice(slice: string): boolean {\n return slice === '..';\n}\n\n/**\n * 判断是否是绝对路径\n * @param {string} path - 路径字符串\n * @returns {boolean} - 如果是绝对路径则返回true,否则返回false\n * @example\n * ```typescript\n * const isAbs = isAbsolutePath('/path/to/file');\n * console.log(isAbs); // 输出: true\n * ```\n */\nexport function isAbsolutePath(path: string): boolean {\n return path.startsWith('/');\n}\n\n/**\n * 判断是否是相对路径\n * @param {string} path - 路径字符串\n * @returns {boolean} - 如果是相对路径则返回true,否则返回false\n * @example\n * ```typescript\n * const isRel = isRelativePath('path/to/file');\n * console.log(isRel); // 输出: true\n * ```\n */\nexport function isRelativePath(path: string): boolean {\n return !isAbsolutePath(path);\n}\n\n/**\n * 标准化路径\n * @param {string} path - 要标准化的路径字符串。\n * @returns {string} - 标准化后的路径字符串。\n * @example\n * ```typescript\n * const normalizedPath = pathNormalize('/path///to///file');\n * console.log(normalizedPath); // 输出: '/path/to/file'\n * ```\n */\nexport function pathNormalize(path: string): string {\n const slices = path\n .replace(/\\\\/g, '/')\n .replace(/\\/{2,}/g, '/')\n .replace(/\\.{3,}/g, '..')\n .replace(/\\/\\.\\//g, '/')\n .split('/')\n .map((point) => point.trim());\n const points: string[] = [];\n const isAbs = slices[0] === '';\n\n const push = (point: string) => {\n points.push(point);\n };\n\n const back = () => {\n // 绝对路径不能退到根目录\n if (points.length === 1 && isAbs) return;\n\n //\n if (points.length === 0 || points.at(-1) === '..') {\n points.push('..');\n } else {\n points.pop();\n }\n };\n\n for (const slice of slices) {\n const isCurrent = _isCurrentSlice(slice);\n const isParent = _isParentSlice(slice);\n\n // // 未进入实际路径\n // if (!inPoints) {\n // push(slice);\n // inPoints = !isCurrent && !isParent;\n // continue;\n // }\n\n if (isCurrent) {\n continue;\n }\n\n if (isParent) {\n back();\n continue;\n }\n\n push(slice);\n }\n\n return points.join('/') || (isAbs ? '/' : '.');\n}\n\n/**\n * 路径合并\n * @param {string} from - 起始路径。\n * @param {...string[]} to - 要合并的路径片段。\n * @returns {string} - 合并后的路径字符串。\n * @example\n * ```typescript\n * const fullPath = pathJoin('/path', '/to', 'file');\n * console.log(fullPath); // 输出: '/path/to/file'\n * ```\n */\nexport function pathJoin(from: string, ...to: string[]): string {\n return pathNormalize([from, ...to].join('/'));\n}\n\n/**\n * 解析路径\n * @param {string} from - 起始路径\n * @param {...string[]} to - 要解析的路径片段\n * @returns {string} - 解析后的绝对路径\n * @example\n * ```typescript\n * const resolvedPath = pathResolve('/path', '/to', 'file');\n * console.log(resolvedPath); // 输出: '/to/file'\n * ```\n */\nexport function pathResolve(from: string, ...to: string[]): string {\n const paths = [from, ...to].map(pathNormalize);\n\n let lastStartPath = from;\n let lastStartIndex = 0;\n\n arrayEach(\n paths,\n (path, index) => {\n if (isAbsolutePath(path)) {\n lastStartPath = path;\n lastStartIndex = index;\n return false;\n }\n },\n true,\n );\n\n return pathJoin(lastStartPath, ...paths.slice(lastStartIndex + 1));\n}\n\n/**\n * 将相对路径转换为标准的相对路径格式(添加'./'前缀)\n *\n * @param {string} path - 要处理的路径字符串\n * @returns {string} 处理后的路径字符串\n *\n * @example <caption>处理绝对路径</caption>\n * ```typescript\n * const result = pathRelativize('/path/to/file');\n * console.log(result); // 输出: '/path/to/file'\n * ```\n *\n * @example <caption>处理已带'./'前缀的相对路径</caption>\n * ```typescript\n * const result = pathRelativize('./path/to/file');\n * console.log(result); // 输出: './path/to/file'\n * ```\n *\n * @example <caption>处理不带'./'前缀的相对路径</caption>\n * ```typescript\n * const result = pathRelativize('path/to/file');\n * console.log(result); // 输出: './path/to/file'\n * ```\n */\nexport function pathRelativize(path: string): string {\n if (isAbsolutePath(path)) return path;\n if (path.startsWith('./')) return path;\n if (path.startsWith('../')) return path;\n return `./${path}`;\n}\n\n/**\n * 获取路径的目录部分\n * @param {string} path - 输入的路径字符串\n * @returns {string} - 返回路径的目录部分\n * @example\n * ```typescript\n * const dir = pathDirname('/path/to/file.txt');\n * console.log(dir); // 输出: '/path/to'\n * ```\n */\nexport function pathDirname(path: string): string {\n return pathJoin(path, '..');\n}\n"],"mappings":";;;;;;;AAOA,SAAS,gBAAgB,OAAwB;CAC/C,OAAO,UAAU;;;;;;;AAQnB,SAAS,eAAe,OAAwB;CAC9C,OAAO,UAAU;;;;;;;;;;;;AAanB,SAAgB,eAAe,MAAuB;CACpD,OAAO,KAAK,WAAW,IAAI;;;;;;;;;;;;AAa7B,SAAgB,eAAe,MAAuB;CACpD,OAAO,CAAC,eAAe,KAAK;;;;;;;;;;;;AAa9B,SAAgB,cAAc,MAAsB;CAClD,MAAM,SAAS,KACZ,QAAQ,OAAO,IAAI,CACnB,QAAQ,WAAW,IAAI,CACvB,QAAQ,WAAW,KAAK,CACxB,QAAQ,WAAW,IAAI,CACvB,MAAM,IAAI,CACV,KAAK,UAAU,MAAM,MAAM,CAAC;CAC/B,MAAM,SAAmB,EAAE;CAC3B,MAAM,QAAQ,OAAO,OAAO;CAE5B,MAAM,QAAQ,UAAkB;EAC9B,OAAO,KAAK,MAAM;;CAGpB,MAAM,aAAa;EAEjB,IAAI,OAAO,WAAW,KAAK,OAAO;EAGlC,IAAI,OAAO,WAAW,KAAK,OAAO,GAAG,GAAG,KAAK,MAC3C,OAAO,KAAK,KAAK;OAEjB,OAAO,KAAK;;CAIhB,KAAK,MAAM,SAAS,QAAQ;EAC1B,MAAM,YAAY,gBAAgB,MAAM;EACxC,MAAM,WAAW,eAAe,MAAM;EAStC,IAAI,WACF;EAGF,IAAI,UAAU;GACZ,MAAM;GACN;;EAGF,KAAK,MAAM;;CAGb,OAAO,OAAO,KAAK,IAAI,KAAK,QAAQ,MAAM;;;;;;;;;;;;;AAc5C,SAAgB,SAAS,MAAc,GAAG,IAAsB;CAC9D,OAAO,cAAc,CAAC,MAAM,GAAG,GAAG,CAAC,KAAK,IAAI,CAAC;;;;;;;;;;;;;AAc/C,SAAgB,YAAY,MAAc,GAAG,IAAsB;CACjE,MAAM,QAAQ,CAAC,MAAM,GAAG,GAAG,CAAC,IAAI,cAAc;CAE9C,IAAI,gBAAgB;CACpB,IAAI,iBAAiB;CAErB,UACE,QACC,MAAM,UAAU;EACf,IAAI,eAAe,KAAK,EAAE;GACxB,gBAAgB;GAChB,iBAAiB;GACjB,OAAO;;IAGX,KACD;CAED,OAAO,SAAS,eAAe,GAAG,MAAM,MAAM,iBAAiB,EAAE,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;AA2BpE,SAAgB,eAAe,MAAsB;CACnD,IAAI,eAAe,KAAK,EAAE,OAAO;CACjC,IAAI,KAAK,WAAW,KAAK,EAAE,OAAO;CAClC,IAAI,KAAK,WAAW,MAAM,EAAE,OAAO;CACnC,OAAO,KAAK;;;;;;;;;;;;AAad,SAAgB,YAAY,MAAsB;CAChD,OAAO,SAAS,MAAM,KAAK"}
|