@wzyjs/utils 0.0.3
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/dist/cjs/hooks/index.d.ts +1 -0
- package/dist/cjs/hooks/useRequestPro.d.ts +11 -0
- package/dist/cjs/hooks/useRequestPro.js +37 -0
- package/dist/cjs/index.d.ts +22 -0
- package/dist/cjs/index.js +78 -0
- package/dist/cjs/tools/index.d.ts +3 -0
- package/dist/cjs/tools/object.d.ts +1 -0
- package/dist/cjs/tools/object.js +8 -0
- package/dist/cjs/tools/other.d.ts +6 -0
- package/dist/cjs/tools/other.js +24 -0
- package/dist/cjs/tools/string.d.ts +5 -0
- package/dist/cjs/tools/string.js +57 -0
- package/dist/cjs/types/index.d.ts +15 -0
- package/dist/esm/hooks/index.d.ts +1 -0
- package/dist/esm/hooks/useRequestPro.d.ts +11 -0
- package/dist/esm/hooks/useRequestPro.js +35 -0
- package/dist/esm/index.d.ts +22 -0
- package/dist/esm/index.js +21 -0
- package/dist/esm/tools/index.d.ts +3 -0
- package/dist/esm/tools/object.d.ts +1 -0
- package/dist/esm/tools/object.js +6 -0
- package/dist/esm/tools/other.d.ts +6 -0
- package/dist/esm/tools/other.js +22 -0
- package/dist/esm/tools/string.d.ts +5 -0
- package/dist/esm/tools/string.js +51 -0
- package/dist/esm/types/index.d.ts +15 -0
- package/package.json +49 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './useRequestPro';
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Options, Service, Result } from 'ahooks/lib/useRequest/src/types';
|
|
2
|
+
import { RequestRes } from '../types';
|
|
3
|
+
interface UserOptions<P extends any[], R> extends Options<R, P> {
|
|
4
|
+
alertErrorMessage?: false | string;
|
|
5
|
+
alertSuccessMessage?: true | string;
|
|
6
|
+
}
|
|
7
|
+
interface IResult<R extends RequestRes, P extends any[]> extends Omit<Result<R, P>, 'data'> {
|
|
8
|
+
data?: R['data'];
|
|
9
|
+
}
|
|
10
|
+
export declare const useRequestPro: <P extends any[], R extends RequestRes<any>>(reqPromise: Service<R, P>, userOptions: UserOptions<P, R>) => IResult<R, P>;
|
|
11
|
+
export {};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var antd = require('antd');
|
|
4
|
+
var lodash = require('lodash');
|
|
5
|
+
var ahooks = require('ahooks');
|
|
6
|
+
|
|
7
|
+
const useRequestPro = (reqPromise, userOptions) => {
|
|
8
|
+
const { alertErrorMessage = true, alertSuccessMessage = false } = userOptions;
|
|
9
|
+
const defaultOptions = {
|
|
10
|
+
debounceWait: 300,
|
|
11
|
+
throttleWait: 300,
|
|
12
|
+
onError: (e, params) => {
|
|
13
|
+
if (alertErrorMessage) {
|
|
14
|
+
antd.message.error(typeof alertErrorMessage === 'string' ? alertErrorMessage : e.message || '请求失败');
|
|
15
|
+
}
|
|
16
|
+
userOptions?.onError?.(e, params);
|
|
17
|
+
},
|
|
18
|
+
onSuccess: (res, params) => {
|
|
19
|
+
if (!res || !res.success) {
|
|
20
|
+
defaultOptions.onError(new Error(res?.message), params);
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
if (alertSuccessMessage) {
|
|
24
|
+
antd.message.success(typeof alertSuccessMessage === 'string' ? alertSuccessMessage : res.message || '请求成功');
|
|
25
|
+
}
|
|
26
|
+
userOptions?.onSuccess?.(res, params);
|
|
27
|
+
},
|
|
28
|
+
};
|
|
29
|
+
// delete userOptions.onSuccess
|
|
30
|
+
// delete userOptions.onError
|
|
31
|
+
const options = Object.assign(defaultOptions, lodash.omit(userOptions, ['onSuccess', 'onError']));
|
|
32
|
+
const state = ahooks.useRequest(reqPromise, options);
|
|
33
|
+
state.data = state.data?.data;
|
|
34
|
+
return state;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
exports.useRequestPro = useRequestPro;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import dayjs from 'dayjs';
|
|
2
|
+
import copy from 'copy-to-clipboard';
|
|
3
|
+
import classnames from 'classnames';
|
|
4
|
+
import consola from 'consola';
|
|
5
|
+
import md5 from 'md5';
|
|
6
|
+
export { copy, consola, classnames, dayjs, md5 };
|
|
7
|
+
export * from 'ahooks';
|
|
8
|
+
import { omit, pick, cloneDeep, isEqual, merge, isObject, isBoolean, isString, isNumber } from 'lodash';
|
|
9
|
+
export { omit, pick, cloneDeep, isEqual, merge, isObject, isBoolean, isString, isNumber };
|
|
10
|
+
export declare const localforage: {
|
|
11
|
+
config: {
|
|
12
|
+
(options: LocalForageOptions): boolean;
|
|
13
|
+
(options: string): any;
|
|
14
|
+
(): LocalForageOptions;
|
|
15
|
+
};
|
|
16
|
+
setItem: <T>(key: string, value: T, callback?: ((err: any, value: T) => void) | undefined) => Promise<T>;
|
|
17
|
+
getItem: <T_1>(key: string, callback?: ((err: any, value: T_1 | null) => void) | undefined) => Promise<T_1 | null>;
|
|
18
|
+
removeItem: (key: string, callback?: ((err: any) => void) | undefined) => Promise<void>;
|
|
19
|
+
};
|
|
20
|
+
export * from './tools';
|
|
21
|
+
export * from './hooks';
|
|
22
|
+
export * from './types';
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var dayjs = require('dayjs');
|
|
4
|
+
var copyToClipboard = require('copy-to-clipboard');
|
|
5
|
+
var classnames = require('classnames');
|
|
6
|
+
var consola = require('consola');
|
|
7
|
+
var md5 = require('md5');
|
|
8
|
+
var ahooks = require('ahooks');
|
|
9
|
+
var lodash = require('lodash');
|
|
10
|
+
var localforage$1 = require('localforage');
|
|
11
|
+
var string = require('./tools/string.js');
|
|
12
|
+
var object = require('./tools/object.js');
|
|
13
|
+
var other = require('./tools/other.js');
|
|
14
|
+
var useRequestPro = require('./hooks/useRequestPro.js');
|
|
15
|
+
|
|
16
|
+
const localforage = {
|
|
17
|
+
config: localforage$1.config,
|
|
18
|
+
setItem: localforage$1.setItem,
|
|
19
|
+
getItem: localforage$1.getItem,
|
|
20
|
+
removeItem: localforage$1.removeItem,
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
exports.dayjs = dayjs;
|
|
24
|
+
exports.copy = copyToClipboard;
|
|
25
|
+
exports.classnames = classnames;
|
|
26
|
+
exports.consola = consola;
|
|
27
|
+
exports.md5 = md5;
|
|
28
|
+
Object.defineProperty(exports, 'cloneDeep', {
|
|
29
|
+
enumerable: true,
|
|
30
|
+
get: function () { return lodash.cloneDeep; }
|
|
31
|
+
});
|
|
32
|
+
Object.defineProperty(exports, 'isBoolean', {
|
|
33
|
+
enumerable: true,
|
|
34
|
+
get: function () { return lodash.isBoolean; }
|
|
35
|
+
});
|
|
36
|
+
Object.defineProperty(exports, 'isEqual', {
|
|
37
|
+
enumerable: true,
|
|
38
|
+
get: function () { return lodash.isEqual; }
|
|
39
|
+
});
|
|
40
|
+
Object.defineProperty(exports, 'isNumber', {
|
|
41
|
+
enumerable: true,
|
|
42
|
+
get: function () { return lodash.isNumber; }
|
|
43
|
+
});
|
|
44
|
+
Object.defineProperty(exports, 'isObject', {
|
|
45
|
+
enumerable: true,
|
|
46
|
+
get: function () { return lodash.isObject; }
|
|
47
|
+
});
|
|
48
|
+
Object.defineProperty(exports, 'isString', {
|
|
49
|
+
enumerable: true,
|
|
50
|
+
get: function () { return lodash.isString; }
|
|
51
|
+
});
|
|
52
|
+
Object.defineProperty(exports, 'merge', {
|
|
53
|
+
enumerable: true,
|
|
54
|
+
get: function () { return lodash.merge; }
|
|
55
|
+
});
|
|
56
|
+
Object.defineProperty(exports, 'omit', {
|
|
57
|
+
enumerable: true,
|
|
58
|
+
get: function () { return lodash.omit; }
|
|
59
|
+
});
|
|
60
|
+
Object.defineProperty(exports, 'pick', {
|
|
61
|
+
enumerable: true,
|
|
62
|
+
get: function () { return lodash.pick; }
|
|
63
|
+
});
|
|
64
|
+
exports.getChineseByStr = string.getChineseByStr;
|
|
65
|
+
exports.getStrLength = string.getStrLength;
|
|
66
|
+
exports.jsonParse = string.jsonParse;
|
|
67
|
+
exports.replaceAll = string.replaceAll;
|
|
68
|
+
exports.replaceByRules = string.replaceByRules;
|
|
69
|
+
exports.checkAttr = object.checkAttr;
|
|
70
|
+
exports.handleParams = other.handleParams;
|
|
71
|
+
exports.useRequestPro = useRequestPro.useRequestPro;
|
|
72
|
+
exports.localforage = localforage;
|
|
73
|
+
Object.keys(ahooks).forEach(function (k) {
|
|
74
|
+
if (k !== 'default' && !exports.hasOwnProperty(k)) Object.defineProperty(exports, k, {
|
|
75
|
+
enumerable: true,
|
|
76
|
+
get: function () { return ahooks[k]; }
|
|
77
|
+
});
|
|
78
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const checkAttr: <I>(list: I[], attr: keyof I, value?: I[keyof I] | undefined) => boolean;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { KeyValue, OrderParams, Pagination, SortParams } from '../types';
|
|
2
|
+
export declare const handleParams: (params: Pagination & KeyValue, sort?: SortParams) => {
|
|
3
|
+
page: Pagination;
|
|
4
|
+
where: KeyValue<string, string | number | boolean | RegExp>;
|
|
5
|
+
order?: OrderParams;
|
|
6
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// 处理 ProComponents 中 ProTable 参数中的 page 和 查询条件 和 排序
|
|
4
|
+
const handleParams = (params, sort) => {
|
|
5
|
+
const { current, pageSize, ...other } = params;
|
|
6
|
+
return {
|
|
7
|
+
page: {
|
|
8
|
+
current: current || 1,
|
|
9
|
+
pageSize: pageSize || 10,
|
|
10
|
+
},
|
|
11
|
+
where: Object.entries(other).reduce((acc, [key, value]) => {
|
|
12
|
+
if (key && value) {
|
|
13
|
+
acc[key] = typeof value === 'string' ? new RegExp(value) : value;
|
|
14
|
+
}
|
|
15
|
+
return acc;
|
|
16
|
+
}, {}),
|
|
17
|
+
order: sort && {
|
|
18
|
+
field: Object.keys(sort)[0],
|
|
19
|
+
type: Object.values(sort)[0] === 'ascend' ? 'asc' : 'desc',
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
exports.handleParams = handleParams;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export declare const getChineseByStr: (str: string) => string;
|
|
2
|
+
export declare const getStrLength: (value: string) => number;
|
|
3
|
+
export declare const replaceAll: (str: string, searchValue: string, replaceValue: string) => string;
|
|
4
|
+
export declare const replaceByRules: (str: string, rules: [string, string][]) => string;
|
|
5
|
+
export declare const jsonParse: (value: string | object) => object | any[];
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// 获取字符串中的中文字符串
|
|
4
|
+
const getChineseByStr = (str) => {
|
|
5
|
+
if (!str) {
|
|
6
|
+
return '';
|
|
7
|
+
}
|
|
8
|
+
const match = str.match(/[\u4e00-\u9fa5]/g);
|
|
9
|
+
if (!match) {
|
|
10
|
+
return '';
|
|
11
|
+
}
|
|
12
|
+
return match.join('');
|
|
13
|
+
};
|
|
14
|
+
// 获取字符串中的长度,中文算 2 个字符
|
|
15
|
+
const getStrLength = (value) => {
|
|
16
|
+
if (!value) {
|
|
17
|
+
return 0;
|
|
18
|
+
}
|
|
19
|
+
const chineseLength = getChineseByStr(value).length;
|
|
20
|
+
return (value.length - chineseLength) + chineseLength * 2;
|
|
21
|
+
};
|
|
22
|
+
// 替换全部指定字符串
|
|
23
|
+
const replaceAll = (str, searchValue, replaceValue) => {
|
|
24
|
+
if (!str || !searchValue || !replaceValue) {
|
|
25
|
+
return str || '';
|
|
26
|
+
}
|
|
27
|
+
str = str.replace(searchValue, replaceValue);
|
|
28
|
+
if (!str.includes(searchValue)) {
|
|
29
|
+
return str;
|
|
30
|
+
}
|
|
31
|
+
return replaceAll(str, searchValue, replaceValue);
|
|
32
|
+
};
|
|
33
|
+
// 根据规则替换字符串
|
|
34
|
+
const replaceByRules = (str, rules) => {
|
|
35
|
+
rules.forEach(([searchValue, replaceValue]) => {
|
|
36
|
+
str = str.replaceAll(searchValue, replaceValue);
|
|
37
|
+
});
|
|
38
|
+
return str;
|
|
39
|
+
};
|
|
40
|
+
// JSON.parse
|
|
41
|
+
const jsonParse = (value) => {
|
|
42
|
+
if (typeof value === 'object') {
|
|
43
|
+
return value;
|
|
44
|
+
}
|
|
45
|
+
try {
|
|
46
|
+
return JSON.parse(value);
|
|
47
|
+
}
|
|
48
|
+
catch (err) {
|
|
49
|
+
return {};
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
exports.getChineseByStr = getChineseByStr;
|
|
54
|
+
exports.getStrLength = getStrLength;
|
|
55
|
+
exports.jsonParse = jsonParse;
|
|
56
|
+
exports.replaceAll = replaceAll;
|
|
57
|
+
exports.replaceByRules = replaceByRules;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export interface RequestRes<D = any> {
|
|
2
|
+
success?: boolean;
|
|
3
|
+
message?: string;
|
|
4
|
+
data?: D;
|
|
5
|
+
}
|
|
6
|
+
export type KeyValue<D extends object | string = string, V = string | number | boolean> = Record<D extends string ? string : keyof D, V>;
|
|
7
|
+
export interface Pagination {
|
|
8
|
+
current?: number;
|
|
9
|
+
pageSize?: number;
|
|
10
|
+
}
|
|
11
|
+
export type SortParams<D extends object | string = string> = Record<D extends string ? string : keyof D, 'ascend' | 'descend'>;
|
|
12
|
+
export type OrderParams<D extends object | string = string> = {
|
|
13
|
+
field: D extends string ? string : keyof D;
|
|
14
|
+
type: 'asc' | 'desc';
|
|
15
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './useRequestPro';
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Options, Service, Result } from 'ahooks/lib/useRequest/src/types';
|
|
2
|
+
import { RequestRes } from '../types';
|
|
3
|
+
interface UserOptions<P extends any[], R> extends Options<R, P> {
|
|
4
|
+
alertErrorMessage?: false | string;
|
|
5
|
+
alertSuccessMessage?: true | string;
|
|
6
|
+
}
|
|
7
|
+
interface IResult<R extends RequestRes, P extends any[]> extends Omit<Result<R, P>, 'data'> {
|
|
8
|
+
data?: R['data'];
|
|
9
|
+
}
|
|
10
|
+
export declare const useRequestPro: <P extends any[], R extends RequestRes<any>>(reqPromise: Service<R, P>, userOptions: UserOptions<P, R>) => IResult<R, P>;
|
|
11
|
+
export {};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { message } from 'antd';
|
|
2
|
+
import { omit } from 'lodash';
|
|
3
|
+
import { useRequest } from 'ahooks';
|
|
4
|
+
|
|
5
|
+
const useRequestPro = (reqPromise, userOptions) => {
|
|
6
|
+
const { alertErrorMessage = true, alertSuccessMessage = false } = userOptions;
|
|
7
|
+
const defaultOptions = {
|
|
8
|
+
debounceWait: 300,
|
|
9
|
+
throttleWait: 300,
|
|
10
|
+
onError: (e, params) => {
|
|
11
|
+
if (alertErrorMessage) {
|
|
12
|
+
message.error(typeof alertErrorMessage === 'string' ? alertErrorMessage : e.message || '请求失败');
|
|
13
|
+
}
|
|
14
|
+
userOptions?.onError?.(e, params);
|
|
15
|
+
},
|
|
16
|
+
onSuccess: (res, params) => {
|
|
17
|
+
if (!res || !res.success) {
|
|
18
|
+
defaultOptions.onError(new Error(res?.message), params);
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
if (alertSuccessMessage) {
|
|
22
|
+
message.success(typeof alertSuccessMessage === 'string' ? alertSuccessMessage : res.message || '请求成功');
|
|
23
|
+
}
|
|
24
|
+
userOptions?.onSuccess?.(res, params);
|
|
25
|
+
},
|
|
26
|
+
};
|
|
27
|
+
// delete userOptions.onSuccess
|
|
28
|
+
// delete userOptions.onError
|
|
29
|
+
const options = Object.assign(defaultOptions, omit(userOptions, ['onSuccess', 'onError']));
|
|
30
|
+
const state = useRequest(reqPromise, options);
|
|
31
|
+
state.data = state.data?.data;
|
|
32
|
+
return state;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export { useRequestPro };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import dayjs from 'dayjs';
|
|
2
|
+
import copy from 'copy-to-clipboard';
|
|
3
|
+
import classnames from 'classnames';
|
|
4
|
+
import consola from 'consola';
|
|
5
|
+
import md5 from 'md5';
|
|
6
|
+
export { copy, consola, classnames, dayjs, md5 };
|
|
7
|
+
export * from 'ahooks';
|
|
8
|
+
import { omit, pick, cloneDeep, isEqual, merge, isObject, isBoolean, isString, isNumber } from 'lodash';
|
|
9
|
+
export { omit, pick, cloneDeep, isEqual, merge, isObject, isBoolean, isString, isNumber };
|
|
10
|
+
export declare const localforage: {
|
|
11
|
+
config: {
|
|
12
|
+
(options: LocalForageOptions): boolean;
|
|
13
|
+
(options: string): any;
|
|
14
|
+
(): LocalForageOptions;
|
|
15
|
+
};
|
|
16
|
+
setItem: <T>(key: string, value: T, callback?: ((err: any, value: T) => void) | undefined) => Promise<T>;
|
|
17
|
+
getItem: <T_1>(key: string, callback?: ((err: any, value: T_1 | null) => void) | undefined) => Promise<T_1 | null>;
|
|
18
|
+
removeItem: (key: string, callback?: ((err: any) => void) | undefined) => Promise<void>;
|
|
19
|
+
};
|
|
20
|
+
export * from './tools';
|
|
21
|
+
export * from './hooks';
|
|
22
|
+
export * from './types';
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export { default as dayjs } from 'dayjs';
|
|
2
|
+
export { default as copy } from 'copy-to-clipboard';
|
|
3
|
+
export { default as classnames } from 'classnames';
|
|
4
|
+
export { default as consola } from 'consola';
|
|
5
|
+
export { default as md5 } from 'md5';
|
|
6
|
+
export * from 'ahooks';
|
|
7
|
+
export { cloneDeep, isBoolean, isEqual, isNumber, isObject, isString, merge, omit, pick } from 'lodash';
|
|
8
|
+
import { config, setItem, getItem, removeItem } from 'localforage';
|
|
9
|
+
export { getChineseByStr, getStrLength, jsonParse, replaceAll, replaceByRules } from './tools/string.js';
|
|
10
|
+
export { checkAttr } from './tools/object.js';
|
|
11
|
+
export { handleParams } from './tools/other.js';
|
|
12
|
+
export { useRequestPro } from './hooks/useRequestPro.js';
|
|
13
|
+
|
|
14
|
+
const localforage = {
|
|
15
|
+
config,
|
|
16
|
+
setItem,
|
|
17
|
+
getItem,
|
|
18
|
+
removeItem,
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export { localforage };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const checkAttr: <I>(list: I[], attr: keyof I, value?: I[keyof I] | undefined) => boolean;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { KeyValue, OrderParams, Pagination, SortParams } from '../types';
|
|
2
|
+
export declare const handleParams: (params: Pagination & KeyValue, sort?: SortParams) => {
|
|
3
|
+
page: Pagination;
|
|
4
|
+
where: KeyValue<string, string | number | boolean | RegExp>;
|
|
5
|
+
order?: OrderParams;
|
|
6
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// 处理 ProComponents 中 ProTable 参数中的 page 和 查询条件 和 排序
|
|
2
|
+
const handleParams = (params, sort) => {
|
|
3
|
+
const { current, pageSize, ...other } = params;
|
|
4
|
+
return {
|
|
5
|
+
page: {
|
|
6
|
+
current: current || 1,
|
|
7
|
+
pageSize: pageSize || 10,
|
|
8
|
+
},
|
|
9
|
+
where: Object.entries(other).reduce((acc, [key, value]) => {
|
|
10
|
+
if (key && value) {
|
|
11
|
+
acc[key] = typeof value === 'string' ? new RegExp(value) : value;
|
|
12
|
+
}
|
|
13
|
+
return acc;
|
|
14
|
+
}, {}),
|
|
15
|
+
order: sort && {
|
|
16
|
+
field: Object.keys(sort)[0],
|
|
17
|
+
type: Object.values(sort)[0] === 'ascend' ? 'asc' : 'desc',
|
|
18
|
+
},
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export { handleParams };
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export declare const getChineseByStr: (str: string) => string;
|
|
2
|
+
export declare const getStrLength: (value: string) => number;
|
|
3
|
+
export declare const replaceAll: (str: string, searchValue: string, replaceValue: string) => string;
|
|
4
|
+
export declare const replaceByRules: (str: string, rules: [string, string][]) => string;
|
|
5
|
+
export declare const jsonParse: (value: string | object) => object | any[];
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
// 获取字符串中的中文字符串
|
|
2
|
+
const getChineseByStr = (str) => {
|
|
3
|
+
if (!str) {
|
|
4
|
+
return '';
|
|
5
|
+
}
|
|
6
|
+
const match = str.match(/[\u4e00-\u9fa5]/g);
|
|
7
|
+
if (!match) {
|
|
8
|
+
return '';
|
|
9
|
+
}
|
|
10
|
+
return match.join('');
|
|
11
|
+
};
|
|
12
|
+
// 获取字符串中的长度,中文算 2 个字符
|
|
13
|
+
const getStrLength = (value) => {
|
|
14
|
+
if (!value) {
|
|
15
|
+
return 0;
|
|
16
|
+
}
|
|
17
|
+
const chineseLength = getChineseByStr(value).length;
|
|
18
|
+
return (value.length - chineseLength) + chineseLength * 2;
|
|
19
|
+
};
|
|
20
|
+
// 替换全部指定字符串
|
|
21
|
+
const replaceAll = (str, searchValue, replaceValue) => {
|
|
22
|
+
if (!str || !searchValue || !replaceValue) {
|
|
23
|
+
return str || '';
|
|
24
|
+
}
|
|
25
|
+
str = str.replace(searchValue, replaceValue);
|
|
26
|
+
if (!str.includes(searchValue)) {
|
|
27
|
+
return str;
|
|
28
|
+
}
|
|
29
|
+
return replaceAll(str, searchValue, replaceValue);
|
|
30
|
+
};
|
|
31
|
+
// 根据规则替换字符串
|
|
32
|
+
const replaceByRules = (str, rules) => {
|
|
33
|
+
rules.forEach(([searchValue, replaceValue]) => {
|
|
34
|
+
str = str.replaceAll(searchValue, replaceValue);
|
|
35
|
+
});
|
|
36
|
+
return str;
|
|
37
|
+
};
|
|
38
|
+
// JSON.parse
|
|
39
|
+
const jsonParse = (value) => {
|
|
40
|
+
if (typeof value === 'object') {
|
|
41
|
+
return value;
|
|
42
|
+
}
|
|
43
|
+
try {
|
|
44
|
+
return JSON.parse(value);
|
|
45
|
+
}
|
|
46
|
+
catch (err) {
|
|
47
|
+
return {};
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
export { getChineseByStr, getStrLength, jsonParse, replaceAll, replaceByRules };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export interface RequestRes<D = any> {
|
|
2
|
+
success?: boolean;
|
|
3
|
+
message?: string;
|
|
4
|
+
data?: D;
|
|
5
|
+
}
|
|
6
|
+
export type KeyValue<D extends object | string = string, V = string | number | boolean> = Record<D extends string ? string : keyof D, V>;
|
|
7
|
+
export interface Pagination {
|
|
8
|
+
current?: number;
|
|
9
|
+
pageSize?: number;
|
|
10
|
+
}
|
|
11
|
+
export type SortParams<D extends object | string = string> = Record<D extends string ? string : keyof D, 'ascend' | 'descend'>;
|
|
12
|
+
export type OrderParams<D extends object | string = string> = {
|
|
13
|
+
field: D extends string ? string : keyof D;
|
|
14
|
+
type: 'asc' | 'desc';
|
|
15
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@wzyjs/utils",
|
|
3
|
+
"version": "0.0.3",
|
|
4
|
+
"description": "> TODO: description",
|
|
5
|
+
"author": "wzy <657189555@qq.com>",
|
|
6
|
+
"license": "ISC",
|
|
7
|
+
"main": "dist/cjs/index.js",
|
|
8
|
+
"module": "dist/esm/index.js",
|
|
9
|
+
"typings": "dist/esm/index.d.ts",
|
|
10
|
+
"scripts": {
|
|
11
|
+
"dev": "rollup -c -w --bundleConfigAsCjs",
|
|
12
|
+
"build": "rollup -c --bundleConfigAsCjs"
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist"
|
|
16
|
+
],
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"ahooks": "^3.7.2",
|
|
19
|
+
"antd": "^5.0.1",
|
|
20
|
+
"classnames": "^2.3.2",
|
|
21
|
+
"consola": "^2.15.3",
|
|
22
|
+
"copy-to-clipboard": "^3.3.3",
|
|
23
|
+
"dayjs": "^1.11.6",
|
|
24
|
+
"localforage": "^1.10.0",
|
|
25
|
+
"lodash": "^4.17.21",
|
|
26
|
+
"md5": "^2.3.0",
|
|
27
|
+
"react": "^18.2.0",
|
|
28
|
+
"react-dom": "^18.2.0",
|
|
29
|
+
"tslib": "^2.4.1"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@rollup/plugin-commonjs": "^23.0.3",
|
|
33
|
+
"@rollup/plugin-node-resolve": "^15.0.1",
|
|
34
|
+
"@types/lodash": "^4.14.189",
|
|
35
|
+
"@types/md5": "^2.3.2",
|
|
36
|
+
"@types/node": "^18.11.11",
|
|
37
|
+
"rollup": "^3.6.0",
|
|
38
|
+
"rollup-plugin-typescript2": "^0.34.1",
|
|
39
|
+
"typescript": "^4.9.3"
|
|
40
|
+
},
|
|
41
|
+
"publishConfig": {
|
|
42
|
+
"access": "public"
|
|
43
|
+
},
|
|
44
|
+
"repository": {
|
|
45
|
+
"type": "git",
|
|
46
|
+
"url": "https://gitee.com/wang-zhenyu/app.git"
|
|
47
|
+
},
|
|
48
|
+
"gitHead": "9561be09765107cdae54703e67fa53fd08a77329"
|
|
49
|
+
}
|