@anjianshi/utils 1.3.0 → 1.3.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.
Files changed (4) hide show
  1. package/package.json +1 -1
  2. package/src/url.ts +34 -0
  3. package/url.d.ts +12 -0
  4. package/url.js +34 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anjianshi/utils",
3
- "version": "1.3.0",
3
+ "version": "1.3.1",
4
4
  "description": "Common JavaScript Utils",
5
5
  "homepage": "https://github.com/anjianshi/js-utils",
6
6
  "bugs": {
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
  * 用此函数代替可避免此问题