@vtj/utils 0.0.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/LICENSE +21 -0
- package/cdn/index.js +22 -0
- package/lib/index.js +4495 -0
- package/package.json +36 -0
- package/types/axios.d.ts +4 -0
- package/types/cookie.d.ts +10 -0
- package/types/crypto.d.ts +1 -0
- package/types/dayjs.d.ts +2 -0
- package/types/file.d.ts +10 -0
- package/types/index.d.ts +11 -0
- package/types/jsonp.d.ts +9 -0
- package/types/raf.d.ts +2 -0
- package/types/request.d.ts +47 -0
- package/types/storage.d.ts +46 -0
- package/types/url.d.ts +44 -0
- package/types/util.d.ts +162 -0
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@vtj/utils",
|
|
3
|
+
"private": false,
|
|
4
|
+
"version": "0.0.1",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"setup": "npm install --registry=https://registry.npmmirror.com",
|
|
8
|
+
"dev": "cross-env ENV_TYPE=dev vite",
|
|
9
|
+
"build": "vue-tsc && vite build && npm run build:cdn",
|
|
10
|
+
"build:cdn": "vue-tsc && cross-env CDN=true vite build",
|
|
11
|
+
"test:unit": "vitest"
|
|
12
|
+
},
|
|
13
|
+
"devDependencies": {
|
|
14
|
+
"@vtj/cli": "^0.0.2",
|
|
15
|
+
"@vtj/deps": "^0.0.1"
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"lib",
|
|
19
|
+
"types",
|
|
20
|
+
"cdn"
|
|
21
|
+
],
|
|
22
|
+
"main": "lib/index.js",
|
|
23
|
+
"module": "lib/index.js",
|
|
24
|
+
"types": "types/index.d.ts",
|
|
25
|
+
"exports": {
|
|
26
|
+
".": {
|
|
27
|
+
"types": "./types/index.d.ts",
|
|
28
|
+
"import": "./lib/index.js",
|
|
29
|
+
"require": "./lib/index.js"
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
"gitHead": "30f61dda4b0669f479d467845a57ce267fcdafb3",
|
|
33
|
+
"publishConfig": {
|
|
34
|
+
"access": "public"
|
|
35
|
+
}
|
|
36
|
+
}
|
package/types/axios.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export interface ICookieOptions {
|
|
2
|
+
expires?: number;
|
|
3
|
+
path?: string;
|
|
4
|
+
domain?: string;
|
|
5
|
+
secure?: boolean;
|
|
6
|
+
sameSite?: string;
|
|
7
|
+
}
|
|
8
|
+
export declare function set(name: string, value: string, opts?: ICookieOptions): void;
|
|
9
|
+
export declare function get(name: string): string | undefined;
|
|
10
|
+
export declare function remove(name: string, opts?: ICookieOptions): void;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function md5(content: string): string;
|
package/types/dayjs.d.ts
ADDED
package/types/file.d.ts
ADDED
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export * from './util';
|
|
2
|
+
export * from './axios';
|
|
3
|
+
export * from './request';
|
|
4
|
+
export * from './raf';
|
|
5
|
+
export * as cookie from './cookie';
|
|
6
|
+
export * as storage from './storage';
|
|
7
|
+
export * as crypto from './crypto';
|
|
8
|
+
export * as url from './url';
|
|
9
|
+
export * as file from './file';
|
|
10
|
+
export { dayjs } from './dayjs';
|
|
11
|
+
export { jsonp } from './jsonp';
|
package/types/jsonp.d.ts
ADDED
package/types/raf.d.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { AxiosRequestConfig, AxiosResponse } from './axios';
|
|
2
|
+
import { AxiosError } from './axios';
|
|
3
|
+
import instance from './axios';
|
|
4
|
+
declare const LOCAL_REQUEST_ID = "Local-Request-Id";
|
|
5
|
+
export interface ApiResponse<T = any> {
|
|
6
|
+
code: number;
|
|
7
|
+
data: T | null;
|
|
8
|
+
msg: string;
|
|
9
|
+
success: boolean;
|
|
10
|
+
promise?: Promise<any>;
|
|
11
|
+
}
|
|
12
|
+
export interface ApiSettings<OriginResponse, ValidSuccess> {
|
|
13
|
+
loading?: boolean;
|
|
14
|
+
showLoading?: () => void;
|
|
15
|
+
hideLoading?: () => void;
|
|
16
|
+
loadingTime?: number;
|
|
17
|
+
type?: 'form' | 'json' | 'data';
|
|
18
|
+
originResponse?: OriginResponse;
|
|
19
|
+
validSuccess?: ValidSuccess;
|
|
20
|
+
validate?: (res: AxiosResponse) => boolean;
|
|
21
|
+
failMessage?: boolean;
|
|
22
|
+
showError?: (msg: string, e: AxiosError | AxiosResponse<ApiResponse>) => void;
|
|
23
|
+
injectHeaders?: (config: ApiRequestConfig) => Record<string, string>;
|
|
24
|
+
defaults?: Record<string, any>;
|
|
25
|
+
trim?: boolean;
|
|
26
|
+
picked?: boolean;
|
|
27
|
+
pickFilter?: (v: any) => boolean;
|
|
28
|
+
skipWarnResponseCode?: number;
|
|
29
|
+
skipWarnExecutor?: () => void;
|
|
30
|
+
skipWarnCallback?: () => void;
|
|
31
|
+
skipWarnFinally?: () => void;
|
|
32
|
+
skipWarn?: boolean;
|
|
33
|
+
}
|
|
34
|
+
export declare type ApiRequestConfig<RequestData = any, OriginResponse = boolean, ValidSuccess = boolean> = AxiosRequestConfig<RequestData> & {
|
|
35
|
+
settings?: ApiSettings<OriginResponse, ValidSuccess>;
|
|
36
|
+
};
|
|
37
|
+
declare const __settings__: ApiSettings<boolean, boolean>;
|
|
38
|
+
export declare function setRequestSettings(settings: ApiSettings<boolean, boolean>): void;
|
|
39
|
+
declare function request<Req = any, Res = any>(options: ApiRequestConfig<Req, false, true>, currentSettings?: ApiSettings<boolean, boolean>): Promise<Res>;
|
|
40
|
+
declare function request<Req = any, Res = any>(options: ApiRequestConfig<Req, false, false>, currentSettings?: ApiSettings<boolean, boolean>): Promise<ApiResponse<Res>>;
|
|
41
|
+
declare function request<Req = any, Res = any>(options: ApiRequestConfig<Req, true, boolean>, currentSettings?: ApiSettings<boolean, boolean>): Promise<AxiosResponse<ApiResponse<Res>>>;
|
|
42
|
+
export declare function setRequest(success: any, fail?: any): any;
|
|
43
|
+
export declare function setResponse(success: any, fail?: any): any;
|
|
44
|
+
declare function createApi<Req = any, Res = any>(options: string | ApiRequestConfig<Req, false, true>): (data?: Req, currentSettings?: ApiSettings<boolean, boolean>) => Promise<Res>;
|
|
45
|
+
declare function createApi<Req = any, Res = any>(options: string | ApiRequestConfig<Req, false, false>): (data?: Req, currentSettings?: ApiSettings<boolean, boolean>) => Promise<ApiResponse<Res>>;
|
|
46
|
+
declare function createApi<Req = any, Res = any>(options: string | ApiRequestConfig<Req, true, boolean>): (data?: Req, currentSettings?: ApiSettings<boolean, boolean>) => Promise<AxiosResponse<ApiResponse<Res>>>;
|
|
47
|
+
export { request, createApi, instance as axiosInstance, __settings__ as requestSettings, LOCAL_REQUEST_ID };
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 缓存操作模块,提供sessionStorage和localStorage操作
|
|
3
|
+
* @module storage
|
|
4
|
+
* @author 陈华春
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* 保存缓存
|
|
8
|
+
* @param {string} key 缓存key
|
|
9
|
+
* @param {String|Object|Array} value 缓存值,对象、数组类型自动JSON.stringify成字符串
|
|
10
|
+
* @param {object} [opts] 选项
|
|
11
|
+
* @param {Object} [opts.type=cache] 存储方式 local、 session、cache
|
|
12
|
+
* @param {number} [opts.expired=0] 过期时间,单位毫秒
|
|
13
|
+
* @param {string} [opts.prefix=__NewPearl__] key 前缀
|
|
14
|
+
*/
|
|
15
|
+
export declare function save(key: string, value?: any, opts?: {}): void;
|
|
16
|
+
/**
|
|
17
|
+
* 获取缓存
|
|
18
|
+
* @param {string} key 缓存key
|
|
19
|
+
* @param {object} [opts] 选项
|
|
20
|
+
* @param {Object} [opts.type=cache] 存储方式 local、 session、cache
|
|
21
|
+
* @param {number} [opts.expired=0] 过期时间,单位毫秒
|
|
22
|
+
* @param {string} [opts.prefix=__NewPearl__] key 前缀
|
|
23
|
+
* @returns {String|Object|Array}
|
|
24
|
+
*/
|
|
25
|
+
export declare function get(key: string, opts?: {}): any;
|
|
26
|
+
/**
|
|
27
|
+
* 删除缓存
|
|
28
|
+
* @param {string} key 缓存key
|
|
29
|
+
* @param {object} [opts] 选项
|
|
30
|
+
* @param {Object} [opts.type=cache] 存储方式 local、 session、cache
|
|
31
|
+
* @param {string} [opts.prefix=__NewPearl__] key 前缀
|
|
32
|
+
*/
|
|
33
|
+
export declare function remove(key: string, opts?: {}): void;
|
|
34
|
+
/**
|
|
35
|
+
* 删除全部缓存
|
|
36
|
+
* @param {object} [opts] 选项
|
|
37
|
+
* @param {Object} [opts.type=cache] 存储方式 local、 session、cache
|
|
38
|
+
*/
|
|
39
|
+
export declare function clear(opts?: {}): void;
|
|
40
|
+
declare const _default: {
|
|
41
|
+
save: typeof save;
|
|
42
|
+
get: typeof get;
|
|
43
|
+
remove: typeof remove;
|
|
44
|
+
clear: typeof clear;
|
|
45
|
+
};
|
|
46
|
+
export default _default;
|
package/types/url.d.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
export declare const UrlRegex: RegExp;
|
|
2
|
+
export declare function isUrl(txt: string): boolean;
|
|
3
|
+
/**
|
|
4
|
+
* 获取当前页面的 host
|
|
5
|
+
* @param {boolean} includePath 带上 pathname
|
|
6
|
+
* @return {string}
|
|
7
|
+
*/
|
|
8
|
+
export declare function getCurrentHost(includePath: boolean): string;
|
|
9
|
+
/**
|
|
10
|
+
* 获取指定url的host
|
|
11
|
+
* @param {string} url
|
|
12
|
+
* @return {string} host
|
|
13
|
+
*/
|
|
14
|
+
export declare function getHost(url?: string): string;
|
|
15
|
+
/**
|
|
16
|
+
* 键值对转换成查询字符串
|
|
17
|
+
* @param {object} query 键值对,对象
|
|
18
|
+
* @returns {string} 查询参数字符串
|
|
19
|
+
*/
|
|
20
|
+
export declare function stringify(query: Record<string, any>): string;
|
|
21
|
+
/**
|
|
22
|
+
* 参数字符串转换成对象形式,如:a=1&b=2 转换成 {a:1, b:2}
|
|
23
|
+
* @param {String} str 需要转换的字符串
|
|
24
|
+
* @param {String} [sep=&] 连接符,可选,默认 &
|
|
25
|
+
* @param {String} [eq==] 键值间隔符,可选,默认 =
|
|
26
|
+
* @returns {Object}
|
|
27
|
+
*/
|
|
28
|
+
export declare function parse(str: string, sep?: string, eq?: string): Record<string, any>;
|
|
29
|
+
/**
|
|
30
|
+
* 在url追加参数
|
|
31
|
+
* @param {string} url 原本的url
|
|
32
|
+
* @param {string|object} query 需要追加的参数,Object|String
|
|
33
|
+
* @returns {string} 追加参数后的url
|
|
34
|
+
*/
|
|
35
|
+
export declare function append(url: string, query: string | Record<string, any>): string;
|
|
36
|
+
declare const _default: {
|
|
37
|
+
isUrl: typeof isUrl;
|
|
38
|
+
getCurrentHost: typeof getCurrentHost;
|
|
39
|
+
getHost: typeof getHost;
|
|
40
|
+
stringify: typeof stringify;
|
|
41
|
+
parse: typeof parse;
|
|
42
|
+
append: typeof append;
|
|
43
|
+
};
|
|
44
|
+
export default _default;
|
package/types/util.d.ts
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
export { get, set, isPlainObject, cloneDeep, merge, debounce, throttle, isEqual, camelCase, upperFirst, template } from 'lodash-es';
|
|
2
|
+
export declare function uid(): string;
|
|
3
|
+
export declare function uuid(split?: boolean): string;
|
|
4
|
+
export declare function isFunction(val: any): boolean;
|
|
5
|
+
/**
|
|
6
|
+
* 判断传入的参数是否为null,undefined或者空字符串
|
|
7
|
+
* @param arg
|
|
8
|
+
* @return {boolean}
|
|
9
|
+
*/
|
|
10
|
+
export declare function isEmpty(arg: any): boolean;
|
|
11
|
+
/**
|
|
12
|
+
* 判断字符串是否为空
|
|
13
|
+
* @param str
|
|
14
|
+
*/
|
|
15
|
+
export declare function isTrimEmpty(str: string): boolean;
|
|
16
|
+
/**
|
|
17
|
+
* 判断传入的参数不为为null,undefined或者空字符串
|
|
18
|
+
* @param arg
|
|
19
|
+
* @return {boolean}
|
|
20
|
+
*/
|
|
21
|
+
export declare function isNotEmpty(arg: string | null | undefined): boolean;
|
|
22
|
+
/**
|
|
23
|
+
* @desc 判断value是不是一个对象
|
|
24
|
+
* @param value
|
|
25
|
+
* @return boolean
|
|
26
|
+
*/
|
|
27
|
+
export declare function isObject(value: any): boolean;
|
|
28
|
+
/**
|
|
29
|
+
* 提取对象属性
|
|
30
|
+
* @param object
|
|
31
|
+
* @param filter
|
|
32
|
+
* @returns
|
|
33
|
+
*/
|
|
34
|
+
export declare function pick(object: Record<string, any>, filter?: (v: any) => boolean): Record<string, any>;
|
|
35
|
+
/**
|
|
36
|
+
* 递归对象或数组清除文本类型值的两边空格
|
|
37
|
+
* @param {Object|Array} obj
|
|
38
|
+
* @return {Object|Array}
|
|
39
|
+
*/
|
|
40
|
+
export declare function trim(obj: any): any;
|
|
41
|
+
/**
|
|
42
|
+
* 截取几位小数
|
|
43
|
+
*/
|
|
44
|
+
export declare function toFixed(value: number, number: number | undefined, round: boolean): number;
|
|
45
|
+
export declare function delay(val?: number): Promise<unknown>;
|
|
46
|
+
/**
|
|
47
|
+
* @desc 判断value是不是一个布尔值
|
|
48
|
+
* @param value
|
|
49
|
+
* @return {boolean}
|
|
50
|
+
*/
|
|
51
|
+
export declare function isBoolean(value: any): boolean;
|
|
52
|
+
/**
|
|
53
|
+
* 树拷贝
|
|
54
|
+
* @param source
|
|
55
|
+
* @param childrenProperty
|
|
56
|
+
* @param validate 校验元素是否需要拷贝的函数
|
|
57
|
+
* @param copyMethod 自定义复制方法,如果传入了copyMethod,propertyArray就变为无效
|
|
58
|
+
* @param propertyArray 需要拷贝的属性
|
|
59
|
+
* @return {array}
|
|
60
|
+
*/
|
|
61
|
+
export declare function copyTree({ source, childrenProperty, validate, copyMethod, propertyArray }: {
|
|
62
|
+
source: Array<any>;
|
|
63
|
+
childrenProperty: string;
|
|
64
|
+
validate?: Function;
|
|
65
|
+
copyMethod: Function;
|
|
66
|
+
propertyArray: Array<string>;
|
|
67
|
+
}): any[];
|
|
68
|
+
/**
|
|
69
|
+
* 对比两棵树是否一样
|
|
70
|
+
* @param reference
|
|
71
|
+
* @param target
|
|
72
|
+
* @param childrenProperty
|
|
73
|
+
* @param validate
|
|
74
|
+
* @return {boolean}
|
|
75
|
+
*/
|
|
76
|
+
export declare function compareTree({ reference, target, childrenProperty, validate }: {
|
|
77
|
+
reference: any;
|
|
78
|
+
target: any;
|
|
79
|
+
childrenProperty: string;
|
|
80
|
+
validate: Function;
|
|
81
|
+
}): boolean;
|
|
82
|
+
/**
|
|
83
|
+
* 获取所有的叶子节点
|
|
84
|
+
* @param tree
|
|
85
|
+
* @param childrenProperty
|
|
86
|
+
* @return {array}
|
|
87
|
+
*/
|
|
88
|
+
export declare function getLeafList({ tree, childrenProperty }: {
|
|
89
|
+
tree: Array<any>;
|
|
90
|
+
childrenProperty: string;
|
|
91
|
+
}): any[];
|
|
92
|
+
/**
|
|
93
|
+
* 阿里云oss图片路劲转换
|
|
94
|
+
* @return {string|*}
|
|
95
|
+
*/
|
|
96
|
+
export declare function ossTransferSrc({ src, width, height }: {
|
|
97
|
+
src: string;
|
|
98
|
+
width: number;
|
|
99
|
+
height: number;
|
|
100
|
+
}): string;
|
|
101
|
+
/**
|
|
102
|
+
* 将树转成二维数组
|
|
103
|
+
* @param tree
|
|
104
|
+
* @param childrenProperty
|
|
105
|
+
* @param validate 是否转换的校验函数,返回true/false
|
|
106
|
+
* @return {array}
|
|
107
|
+
*/
|
|
108
|
+
export declare function treeToMultiDimensionArray({ tree, childrenProperty, validate }: {
|
|
109
|
+
tree: Array<any>;
|
|
110
|
+
childrenProperty: string;
|
|
111
|
+
validate: Function;
|
|
112
|
+
}): any[];
|
|
113
|
+
/**
|
|
114
|
+
* @desc 获取树的深度
|
|
115
|
+
* @param tree 数组
|
|
116
|
+
* @param childrenProperty 子节点的属性名,默认是children
|
|
117
|
+
* @return {number} 返回深度值,如[]空数组是0, [{}]有一个元素是1, [{children[]}]也是1,[{children[{}]}]也是2
|
|
118
|
+
*/
|
|
119
|
+
export declare function getTreeDeep({ tree, childrenProperty }: {
|
|
120
|
+
tree: Array<any>;
|
|
121
|
+
childrenProperty: string;
|
|
122
|
+
}): number;
|
|
123
|
+
/**
|
|
124
|
+
* 将数组转成逗号分隔字符串
|
|
125
|
+
* @param source
|
|
126
|
+
* @param rowKey
|
|
127
|
+
* @return {string|*}
|
|
128
|
+
*/
|
|
129
|
+
export declare function fromArrayToCommaDivideString({ source, rowKey }: {
|
|
130
|
+
source: Array<any>;
|
|
131
|
+
rowKey: string;
|
|
132
|
+
}): string;
|
|
133
|
+
/**
|
|
134
|
+
* 将逗号分隔字符串转成数组
|
|
135
|
+
* @param source
|
|
136
|
+
* @param rowKey
|
|
137
|
+
* @return {*[]|any}
|
|
138
|
+
*/
|
|
139
|
+
export declare function fromCommaDivideStringToArray({ source, rowKey }: {
|
|
140
|
+
source: Array<any> | string;
|
|
141
|
+
rowKey: string;
|
|
142
|
+
}): any[];
|
|
143
|
+
/**
|
|
144
|
+
* 将数组转成json字符串
|
|
145
|
+
* @param source
|
|
146
|
+
* @param rowKey
|
|
147
|
+
* @return {string|*}
|
|
148
|
+
*/
|
|
149
|
+
export declare function fromArrayToJsonString({ source, rowKey }: {
|
|
150
|
+
source: Array<any>;
|
|
151
|
+
rowKey: string;
|
|
152
|
+
}): string;
|
|
153
|
+
/**
|
|
154
|
+
* 将json字符串转成数组
|
|
155
|
+
* @param source
|
|
156
|
+
* @param rowKey
|
|
157
|
+
* @return {*[]|any}
|
|
158
|
+
*/
|
|
159
|
+
export declare function fromJsonStringToArray({ source, rowKey }: {
|
|
160
|
+
source: Array<any> | string;
|
|
161
|
+
rowKey: string;
|
|
162
|
+
}): any[];
|