@anjianshi/utils 1.3.0 → 1.3.2
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/LICENSE +21 -0
- package/package.json +2 -2
- package/src/url.ts +34 -0
- package/url.d.ts +12 -0
- package/url.js +34 -0
- package/validators/array.d.ts +1 -1
- package/validators/boolean.d.ts +1 -1
- package/validators/number.d.ts +1 -1
- package/validators/object.d.ts +1 -1
- package/validators/string.d.ts +1 -1
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 安坚实
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@anjianshi/utils",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.2",
|
|
4
4
|
"description": "Common JavaScript Utils",
|
|
5
5
|
"homepage": "https://github.com/anjianshi/js-utils",
|
|
6
6
|
"bugs": {
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
"@anjianshi/presets-eslint-node": "^1.0.3",
|
|
33
33
|
"@anjianshi/presets-eslint-typescript": "^1.0.3",
|
|
34
34
|
"@anjianshi/presets-prettier": "^1.0.0",
|
|
35
|
-
"@anjianshi/presets-typescript": "^1.0.
|
|
35
|
+
"@anjianshi/presets-typescript": "^1.0.2",
|
|
36
36
|
"@types/debug": "^4.1.9",
|
|
37
37
|
"@types/lodash": "^4.14.199",
|
|
38
38
|
"@types/node": "^20.8.6",
|
package/src/url.ts
CHANGED
|
@@ -131,6 +131,40 @@ export function combineUrl(origUrl: string, query: StringifyQuery = {}, hash: st
|
|
|
131
131
|
return newUrl
|
|
132
132
|
}
|
|
133
133
|
|
|
134
|
+
/**
|
|
135
|
+
* 移除路径中所有非必须的 "/"
|
|
136
|
+
* 清理后的字符串只有这几种可能的格式:''、'abc'、'abc/def'
|
|
137
|
+
* 例如 /abc/def 和 abc/def/ 都会变成 abc/def
|
|
138
|
+
*
|
|
139
|
+
* 注意:此操作不会统一大小写,因此不保证处理后两个字符串在代码层面全等(a === b)
|
|
140
|
+
*/
|
|
141
|
+
export function clearSlash(path: string) {
|
|
142
|
+
if (path.startsWith('/')) path = path.slice(1)
|
|
143
|
+
if (path.endsWith('/')) path = path.slice(0, -1)
|
|
144
|
+
path = path.replace(/\/+/g, '/')
|
|
145
|
+
return path
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* 合并几段路径,保证合并处只有一个斜杠
|
|
150
|
+
*/
|
|
151
|
+
export function joinPath(...nodes: string[]) {
|
|
152
|
+
// - node 为 '' 时忽略 node
|
|
153
|
+
// - path 可能的格式:'' 'a' 'a/' ‘/a/'
|
|
154
|
+
// - path 为 '',则 node 开头 '/' 保持原样
|
|
155
|
+
// - 否则,根据 path 结尾有没有 '/',决定 node 开头带不带 '/'
|
|
156
|
+
// - node 开头、结尾若有多个 '/' 均替换成单个
|
|
157
|
+
return nodes.reduce((path, node) => {
|
|
158
|
+
if (!node) return path
|
|
159
|
+
type Matched = RegExpExecArray & { 1: string; 2: string; 3: string }
|
|
160
|
+
const [, origPrefix, content, origSuffix] = /^(\/*)(.*?)(\/*)$/.exec(node)! as Matched
|
|
161
|
+
const prefix = (path === '' ? !!origPrefix : !path.endsWith('/')) ? '/' : ''
|
|
162
|
+
const suffix = origSuffix ? '/' : ''
|
|
163
|
+
const result = `${path}${prefix}${content}${suffix}`
|
|
164
|
+
return result
|
|
165
|
+
}, '')
|
|
166
|
+
}
|
|
167
|
+
|
|
134
168
|
/**
|
|
135
169
|
* decodeURIComponent() 对于不规范编码的字符串可能会报错(例如字符串里出现了“%”)
|
|
136
170
|
* 用此函数代替可避免此问题
|
package/url.d.ts
CHANGED
|
@@ -56,6 +56,18 @@ export declare function splitUrl(url: string, bare?: boolean): {
|
|
|
56
56
|
* hash string 带不带开头的 '#' 皆可。会代替 url 已有的 hash。
|
|
57
57
|
*/
|
|
58
58
|
export declare function combineUrl(origUrl: string, query?: StringifyQuery, hash?: string): string;
|
|
59
|
+
/**
|
|
60
|
+
* 移除路径中所有非必须的 "/"
|
|
61
|
+
* 清理后的字符串只有这几种可能的格式:''、'abc'、'abc/def'
|
|
62
|
+
* 例如 /abc/def 和 abc/def/ 都会变成 abc/def
|
|
63
|
+
*
|
|
64
|
+
* 注意:此操作不会统一大小写,因此不保证处理后两个字符串在代码层面全等(a === b)
|
|
65
|
+
*/
|
|
66
|
+
export declare function clearSlash(path: string): string;
|
|
67
|
+
/**
|
|
68
|
+
* 合并几段路径,保证合并处只有一个斜杠
|
|
69
|
+
*/
|
|
70
|
+
export declare function joinPath(...nodes: string[]): string;
|
|
59
71
|
/**
|
|
60
72
|
* decodeURIComponent() 对于不规范编码的字符串可能会报错(例如字符串里出现了“%”)
|
|
61
73
|
* 用此函数代替可避免此问题
|
package/url.js
CHANGED
|
@@ -95,6 +95,40 @@ export function combineUrl(origUrl, query = {}, hash = '') {
|
|
|
95
95
|
newUrl += `#${newHash}`;
|
|
96
96
|
return newUrl;
|
|
97
97
|
}
|
|
98
|
+
/**
|
|
99
|
+
* 移除路径中所有非必须的 "/"
|
|
100
|
+
* 清理后的字符串只有这几种可能的格式:''、'abc'、'abc/def'
|
|
101
|
+
* 例如 /abc/def 和 abc/def/ 都会变成 abc/def
|
|
102
|
+
*
|
|
103
|
+
* 注意:此操作不会统一大小写,因此不保证处理后两个字符串在代码层面全等(a === b)
|
|
104
|
+
*/
|
|
105
|
+
export function clearSlash(path) {
|
|
106
|
+
if (path.startsWith('/'))
|
|
107
|
+
path = path.slice(1);
|
|
108
|
+
if (path.endsWith('/'))
|
|
109
|
+
path = path.slice(0, -1);
|
|
110
|
+
path = path.replace(/\/+/g, '/');
|
|
111
|
+
return path;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* 合并几段路径,保证合并处只有一个斜杠
|
|
115
|
+
*/
|
|
116
|
+
export function joinPath(...nodes) {
|
|
117
|
+
// - node 为 '' 时忽略 node
|
|
118
|
+
// - path 可能的格式:'' 'a' 'a/' ‘/a/'
|
|
119
|
+
// - path 为 '',则 node 开头 '/' 保持原样
|
|
120
|
+
// - 否则,根据 path 结尾有没有 '/',决定 node 开头带不带 '/'
|
|
121
|
+
// - node 开头、结尾若有多个 '/' 均替换成单个
|
|
122
|
+
return nodes.reduce((path, node) => {
|
|
123
|
+
if (!node)
|
|
124
|
+
return path;
|
|
125
|
+
const [, origPrefix, content, origSuffix] = /^(\/*)(.*?)(\/*)$/.exec(node);
|
|
126
|
+
const prefix = (path === '' ? !!origPrefix : !path.endsWith('/')) ? '/' : '';
|
|
127
|
+
const suffix = origSuffix ? '/' : '';
|
|
128
|
+
const result = `${path}${prefix}${content}${suffix}`;
|
|
129
|
+
return result;
|
|
130
|
+
}, '');
|
|
131
|
+
}
|
|
98
132
|
/**
|
|
99
133
|
* decodeURIComponent() 对于不规范编码的字符串可能会报错(例如字符串里出现了“%”)
|
|
100
134
|
* 用此函数代替可避免此问题
|
package/validators/array.d.ts
CHANGED
|
@@ -16,5 +16,5 @@ export type TupleOptions = {
|
|
|
16
16
|
tuple: Validator[];
|
|
17
17
|
};
|
|
18
18
|
export declare class ArrayValidator extends Validator<ArrayOptions | TupleOptions> {
|
|
19
|
-
validate(field: string, value: unknown): import("
|
|
19
|
+
validate(field: string, value: unknown): import("@/lang/may-success.js").Failed<void> | import("@/lang/may-success.js").Success<unknown>;
|
|
20
20
|
}
|
package/validators/boolean.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { Validator } from './base.js';
|
|
2
2
|
export declare class BooleanValidator extends Validator {
|
|
3
|
-
validate(field: string, value: unknown): import("
|
|
3
|
+
validate(field: string, value: unknown): import("@/lang/may-success.js").Failed<void> | import("@/lang/may-success.js").Success<unknown>;
|
|
4
4
|
}
|
package/validators/number.d.ts
CHANGED
|
@@ -11,5 +11,5 @@ export interface NumberOptions {
|
|
|
11
11
|
}
|
|
12
12
|
export declare class NumberValidator extends Validator<NumberOptions> {
|
|
13
13
|
constructor(options?: Partial<BaseOptions & NumberOptions>);
|
|
14
|
-
validate(field: string, value: unknown): import("
|
|
14
|
+
validate(field: string, value: unknown): import("@/lang/may-success.js").Failed<void> | import("@/lang/may-success.js").Success<unknown>;
|
|
15
15
|
}
|
package/validators/object.d.ts
CHANGED
|
@@ -16,5 +16,5 @@ export type RecordOptions = {
|
|
|
16
16
|
max?: number;
|
|
17
17
|
};
|
|
18
18
|
export declare class ObjectValidator extends Validator<StructOptions | RecordOptions> {
|
|
19
|
-
validate(field: string, value: unknown): import("
|
|
19
|
+
validate(field: string, value: unknown): import("@/lang/may-success.js").Failed<void> | import("@/lang/may-success.js").Success<unknown>;
|
|
20
20
|
}
|
package/validators/string.d.ts
CHANGED
|
@@ -13,5 +13,5 @@ export interface StringOptions {
|
|
|
13
13
|
}
|
|
14
14
|
export declare class StringValidator extends Validator<StringOptions> {
|
|
15
15
|
constructor(options?: Partial<BaseOptions & StringOptions>);
|
|
16
|
-
validate(field: string, value: unknown): import("
|
|
16
|
+
validate(field: string, value: unknown): import("@/lang/may-success.js").Failed<void> | import("@/lang/may-success.js").Success<unknown>;
|
|
17
17
|
}
|