@fe-free/tool 6.0.21 → 6.0.22
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/CHANGELOG.md +2 -0
- package/package.json +1 -1
- package/src/copy.ts +1 -1
- package/src/environment.ts +7 -7
- package/src/pinyin/filter.ts +21 -11
- package/src/pinyin/pinyin.ts +10 -11
- package/src/sleep/index.ts +1 -1
- package/src/url/index.ts +27 -5
- package/src/use_global_request.tsx +16 -11
package/CHANGELOG.md
CHANGED
package/package.json
CHANGED
package/src/copy.ts
CHANGED
package/src/environment.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
function getUserAgent() {
|
|
1
|
+
function getUserAgent(): string {
|
|
2
2
|
if (typeof window === 'undefined') {
|
|
3
3
|
return '';
|
|
4
4
|
}
|
|
@@ -7,22 +7,22 @@ function getUserAgent() {
|
|
|
7
7
|
}
|
|
8
8
|
|
|
9
9
|
const Environment = {
|
|
10
|
-
isWxWork: () => {
|
|
10
|
+
isWxWork: (): boolean => {
|
|
11
11
|
return getUserAgent().includes('wxwork');
|
|
12
12
|
},
|
|
13
|
-
isWx: () => {
|
|
13
|
+
isWx: (): boolean => {
|
|
14
14
|
return getUserAgent().includes('micromessenger');
|
|
15
15
|
},
|
|
16
|
-
isH5: () => {
|
|
16
|
+
isH5: (): boolean => {
|
|
17
17
|
return getUserAgent().includes('mobile');
|
|
18
18
|
},
|
|
19
|
-
isDingTalk: () => {
|
|
19
|
+
isDingTalk: (): boolean => {
|
|
20
20
|
return getUserAgent().includes('dingtalk');
|
|
21
21
|
},
|
|
22
|
-
isAndroid: () => {
|
|
22
|
+
isAndroid: (): boolean => {
|
|
23
23
|
return getUserAgent().includes('android');
|
|
24
24
|
},
|
|
25
|
-
isIOS: () => {
|
|
25
|
+
isIOS: (): boolean => {
|
|
26
26
|
return getUserAgent().includes('iphone') || getUserAgent().includes('ipad');
|
|
27
27
|
},
|
|
28
28
|
};
|
package/src/pinyin/filter.ts
CHANGED
|
@@ -1,18 +1,28 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { isString, map } from 'lodash-es';
|
|
2
|
+
|
|
2
3
|
import { pinyin } from './pinyin';
|
|
3
4
|
|
|
4
|
-
|
|
5
|
+
function defaultWhat(value: string): string {
|
|
6
|
+
return value;
|
|
7
|
+
}
|
|
5
8
|
/** 字符串匹配,中文首字母拼音匹配,字母小写匹配 */
|
|
6
|
-
function pinyinFilter
|
|
9
|
+
function pinyinFilter(list: string[], filterText: string): string[];
|
|
10
|
+
function pinyinFilter<T>(list: T[], filterText: string, what: (v: T) => string): T[];
|
|
11
|
+
function pinyinFilter<T>(
|
|
12
|
+
list: T[] | string[],
|
|
13
|
+
filterText: string,
|
|
14
|
+
what?: ((v: T) => string) | ((v: string) => string),
|
|
15
|
+
): T[] | string[] {
|
|
7
16
|
if (!filterText) {
|
|
8
17
|
return list || [];
|
|
9
18
|
}
|
|
10
19
|
|
|
11
|
-
|
|
20
|
+
const loweredFilterText = filterText.toLowerCase();
|
|
21
|
+
const getValue = (what ?? defaultWhat) as (value: T | string) => string;
|
|
12
22
|
|
|
13
|
-
return filter(
|
|
14
|
-
return pinyinMatch(
|
|
15
|
-
});
|
|
23
|
+
return list.filter((v) => {
|
|
24
|
+
return pinyinMatch(getValue(v), loweredFilterText);
|
|
25
|
+
}) as T[] | string[];
|
|
16
26
|
}
|
|
17
27
|
|
|
18
28
|
function pinyinMatch(value: string, filterText: string) {
|
|
@@ -23,9 +33,9 @@ function pinyinMatch(value: string, filterText: string) {
|
|
|
23
33
|
}
|
|
24
34
|
w = w.toLowerCase();
|
|
25
35
|
// 全拼集合
|
|
26
|
-
const normal = map(pinyin(w), (
|
|
36
|
+
const normal = map(pinyin(w), (v) => v[0]).join('');
|
|
27
37
|
// 首字母集合
|
|
28
|
-
const firstLetter = map(pinyin(w, 'first_letter'), (
|
|
38
|
+
const firstLetter = map(pinyin(w, 'first_letter'), (v) => v[0]).join('');
|
|
29
39
|
|
|
30
40
|
return (
|
|
31
41
|
w.indexOf(filterText) > -1 ||
|
|
@@ -42,7 +52,7 @@ function pinyinMatchWithoutFirstLetter(value: string, filterText: string) {
|
|
|
42
52
|
}
|
|
43
53
|
w = w.toLowerCase();
|
|
44
54
|
// 全拼集合
|
|
45
|
-
const normal = map(pinyin(w), (
|
|
55
|
+
const normal = map(pinyin(w), (v) => v[0]).join('');
|
|
46
56
|
|
|
47
57
|
return w.indexOf(filterText) > -1 || normal.indexOf(filterText) > -1;
|
|
48
58
|
}
|
|
@@ -55,7 +65,7 @@ function pinyinMatchWithoutFullLetter(value: string, filterText: string) {
|
|
|
55
65
|
}
|
|
56
66
|
w = w.toLowerCase();
|
|
57
67
|
// 全拼集合
|
|
58
|
-
const firstLetter = map(pinyin(w, 'first_letter'), (
|
|
68
|
+
const firstLetter = map(pinyin(w, 'first_letter'), (v) => v[0]).join('');
|
|
59
69
|
|
|
60
70
|
return w.indexOf(filterText) > -1 || firstLetter.indexOf(filterText) > -1;
|
|
61
71
|
}
|
package/src/pinyin/pinyin.ts
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
import { forEach } from 'lodash-es';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
type PinyinStyle = 'first_letter';
|
|
4
|
+
type PinyinTransform = (source: string, style?: PinyinStyle) => string;
|
|
5
|
+
|
|
6
|
+
let instance: PinyinTransform | undefined;
|
|
7
|
+
|
|
8
|
+
const pinyin = (source: string, style?: PinyinStyle): string => {
|
|
5
9
|
if (instance) {
|
|
6
10
|
return instance(source, style);
|
|
7
11
|
}
|
|
@@ -667,7 +671,7 @@ const pinyin = (source: string, style?: 'first_letter') => {
|
|
|
667
671
|
const firstHanziCharCode = 19968; // 0x4e00
|
|
668
672
|
const lastHanziCharCode = 40869; // 0x9FA5
|
|
669
673
|
|
|
670
|
-
const convert = (target, style) => {
|
|
674
|
+
const convert = (target: string, style?: PinyinStyle): string => {
|
|
671
675
|
const charCode = target.charCodeAt(0);
|
|
672
676
|
// 不在比对范围内
|
|
673
677
|
if (charCode < firstHanziCharCode || charCode > lastHanziCharCode) {
|
|
@@ -702,12 +706,7 @@ const pinyin = (source: string, style?: 'first_letter') => {
|
|
|
702
706
|
return style === 'first_letter' ? pinyins[index].charAt(0) : pinyins[index];
|
|
703
707
|
};
|
|
704
708
|
|
|
705
|
-
const _pinyin = (source, style) => {
|
|
706
|
-
// 非字符
|
|
707
|
-
if (typeof source !== 'string') {
|
|
708
|
-
return source;
|
|
709
|
-
}
|
|
710
|
-
|
|
709
|
+
const _pinyin: PinyinTransform = (source, style) => {
|
|
711
710
|
let foundPinyin = '';
|
|
712
711
|
|
|
713
712
|
forEach(source, (target) => {
|
|
@@ -717,11 +716,11 @@ const pinyin = (source: string, style?: 'first_letter') => {
|
|
|
717
716
|
return foundPinyin;
|
|
718
717
|
};
|
|
719
718
|
|
|
720
|
-
return ((
|
|
719
|
+
return ((nextSource: string, nextStyle?: PinyinStyle) => {
|
|
721
720
|
if (!instance) {
|
|
722
721
|
instance = _pinyin;
|
|
723
722
|
}
|
|
724
|
-
return instance(
|
|
723
|
+
return instance(nextSource, nextStyle);
|
|
725
724
|
})(source, style);
|
|
726
725
|
};
|
|
727
726
|
|
package/src/sleep/index.ts
CHANGED
package/src/url/index.ts
CHANGED
|
@@ -1,8 +1,30 @@
|
|
|
1
|
-
type
|
|
1
|
+
type SearchParamPrimitive = string | number | boolean | null | undefined;
|
|
2
|
+
type SearchParamsRecord = Record<string, SearchParamPrimitive>;
|
|
3
|
+
type SearchParamsLike = SearchParamsRecord | URLSearchParams | string;
|
|
2
4
|
|
|
3
|
-
function
|
|
4
|
-
|
|
5
|
-
|
|
5
|
+
function toURLSearchParams(value: SearchParamsLike): URLSearchParams {
|
|
6
|
+
if (value instanceof URLSearchParams) {
|
|
7
|
+
return new URLSearchParams(value);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
if (typeof value === 'string') {
|
|
11
|
+
return new URLSearchParams(value);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const entries = Object.entries(value).flatMap(([key, currentValue]) => {
|
|
15
|
+
if (currentValue == null) {
|
|
16
|
+
return [];
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
return [[key, String(currentValue)]];
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
return new URLSearchParams(entries);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function mergeSearchParams(l: SearchParamsLike, r: SearchParamsLike): URLSearchParams {
|
|
26
|
+
const lsp = toURLSearchParams(l);
|
|
27
|
+
const rsp = toURLSearchParams(r);
|
|
6
28
|
|
|
7
29
|
return new URLSearchParams(`${lsp.toString()}&${rsp.toString()}`);
|
|
8
30
|
}
|
|
@@ -26,7 +48,7 @@ function buildURL(url: string, options?: BuildUrlOptions) {
|
|
|
26
48
|
|
|
27
49
|
if (hashSearchParams) {
|
|
28
50
|
// 转换成 URLSearchParams
|
|
29
|
-
const hashSP =
|
|
51
|
+
const hashSP = toURLSearchParams(hashSearchParams);
|
|
30
52
|
const hash = newURL.hash;
|
|
31
53
|
const index = hash.indexOf('?');
|
|
32
54
|
|
|
@@ -1,24 +1,29 @@
|
|
|
1
1
|
import { useRequest } from 'ahooks';
|
|
2
2
|
import type { Options, Plugin, Service } from 'ahooks/es/useRequest/src/types';
|
|
3
3
|
|
|
4
|
+
type GlobalRequestOptions<TData, TParams extends any[]> = Options<TData, TParams> & {
|
|
5
|
+
__ignore?: boolean;
|
|
6
|
+
};
|
|
7
|
+
|
|
4
8
|
function useGlobalRequest<TData, TParams extends any[]>(
|
|
5
9
|
service: Service<TData, TParams>,
|
|
6
10
|
options?: Options<TData, TParams>,
|
|
7
11
|
plugins?: Plugin<TData, TParams>[]
|
|
8
12
|
) {
|
|
13
|
+
const requestOptions: GlobalRequestOptions<TData, TParams> = {
|
|
14
|
+
onError: (error) => {
|
|
15
|
+
// 全局抛出错误
|
|
16
|
+
setTimeout(() => {
|
|
17
|
+
throw error;
|
|
18
|
+
}, 0);
|
|
19
|
+
},
|
|
20
|
+
...options,
|
|
21
|
+
__ignore: true,
|
|
22
|
+
};
|
|
23
|
+
|
|
9
24
|
return useRequest(
|
|
10
25
|
service,
|
|
11
|
-
|
|
12
|
-
onError: (error) => {
|
|
13
|
-
// 全局抛出错误
|
|
14
|
-
setTimeout(() => {
|
|
15
|
-
throw error;
|
|
16
|
-
}, 0);
|
|
17
|
-
},
|
|
18
|
-
...options,
|
|
19
|
-
// @ts-ignore
|
|
20
|
-
__ignore: true,
|
|
21
|
-
},
|
|
26
|
+
requestOptions,
|
|
22
27
|
plugins
|
|
23
28
|
);
|
|
24
29
|
}
|