utils-lib-js 1.4.6 → 1.5.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/dist/common/types.d.ts +47 -47
- package/dist/esm/array.js +8 -18
- package/dist/esm/base.js +41 -47
- package/dist/esm/element.js +9 -16
- package/dist/esm/event.js +38 -34
- package/dist/esm/function.js +53 -58
- package/dist/esm/index.js +10 -10
- package/dist/esm/index.mjs +692 -0
- package/dist/esm/object.js +159 -168
- package/dist/esm/request.js +235 -299
- package/dist/esm/static.js +14 -14
- package/dist/esm/storage.js +30 -27
- package/dist/esm/types.d.ts +47 -47
- package/dist/esm/types.js +1 -1
- package/package.json +42 -43
- package/pnpm-lock.yaml +0 -1536
package/dist/common/types.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export
|
|
1
|
+
export type IKey = string | symbol | number;
|
|
2
2
|
export interface IObject<T> {
|
|
3
3
|
[key: IKey]: T | IObject<any>;
|
|
4
4
|
}
|
|
@@ -7,56 +7,56 @@ export interface IPromise extends IObject<any> {
|
|
|
7
7
|
resolve: (res: any) => unknown;
|
|
8
8
|
reject: (err: any) => unknown;
|
|
9
9
|
}
|
|
10
|
-
export
|
|
10
|
+
export type IInstance<T> = {
|
|
11
11
|
_instance: Function;
|
|
12
12
|
} & T;
|
|
13
|
-
export
|
|
14
|
-
export
|
|
15
|
-
export
|
|
16
|
-
export
|
|
17
|
-
export
|
|
18
|
-
export
|
|
19
|
-
export
|
|
20
|
-
export
|
|
21
|
-
export
|
|
22
|
-
export
|
|
23
|
-
export
|
|
24
|
-
export
|
|
25
|
-
export
|
|
26
|
-
export
|
|
27
|
-
export
|
|
28
|
-
export
|
|
29
|
-
export
|
|
30
|
-
export
|
|
31
|
-
export
|
|
32
|
-
export
|
|
33
|
-
export
|
|
34
|
-
export
|
|
35
|
-
export
|
|
36
|
-
export
|
|
37
|
-
export
|
|
13
|
+
export type IDemoteArray<T> = Array<IDemoteArray<T> | T>;
|
|
14
|
+
export type IRandomNum = (min: number, max: number, bool?: boolean) => number;
|
|
15
|
+
export type IUrlSplit = (url: string) => IObject<any>;
|
|
16
|
+
export type IUrlJoin = (url: string, query: object) => string;
|
|
17
|
+
export type IGetType<T> = (data: any) => typeof data | T[keyof T] | "null";
|
|
18
|
+
export type IGetTypeByList = (data: any, whiteList: string[]) => boolean;
|
|
19
|
+
export type IGetValue = <T, U = IObject<T> | IObject<T>[IKey]>(object: U, key: string, defaultValue?: any) => U;
|
|
20
|
+
export type ISetValue = <T>(object: IObject<T>, key: string, value?: any) => IObject<T>;
|
|
21
|
+
export type IMixIn = <U extends IObject<any>>(target?: U, source?: IObject<any>, overwrite?: boolean) => U;
|
|
22
|
+
export type IEnumInversion = (target: IObject<string>) => IObject<string>;
|
|
23
|
+
export type ICloneDeep = (target?: any) => any;
|
|
24
|
+
export type ICreateObjectVariable = (type: string, source?: any) => any;
|
|
25
|
+
export type ICreateObject = <T, U extends T>(source: T) => U;
|
|
26
|
+
export type IInherit = <T extends Function>(source: T, target?: Function) => Function;
|
|
27
|
+
export type IGetInstance = (classProto: IInstance<FunctionConstructor>, overwrite?: boolean, ...params: any[]) => Function;
|
|
28
|
+
export type IClassDecorator = (params: IObject<any>) => <TFunction extends Function>(target: TFunction) => void;
|
|
29
|
+
export type IStringToJson = (target: string) => IObject<any> | null;
|
|
30
|
+
export type IJsonToString = (target: IObject<any>) => string;
|
|
31
|
+
export type IThrottle = (fn: Function, time: number) => (...args: any[]) => void;
|
|
32
|
+
export type IDebounce = (fn: Function, time: number) => (...args: any[]) => void;
|
|
33
|
+
export type IDefer = () => IPromise;
|
|
34
|
+
export type ICatchAwait<T extends Promise<any>> = (defer: T) => T;
|
|
35
|
+
export type IArrayRandom<T extends any[]> = (arr: T) => T;
|
|
36
|
+
export type IArrayUniq<T extends any[]> = (arr: T) => T;
|
|
37
|
+
export type IArrayDemote<T extends IDemoteArray<any>> = (arr: T, result?: T) => T;
|
|
38
38
|
interface IElementParams<T> {
|
|
39
39
|
ele: T | string;
|
|
40
40
|
style: CSSStyleDeclaration;
|
|
41
41
|
attr: Attr;
|
|
42
42
|
parent: T;
|
|
43
43
|
}
|
|
44
|
-
export
|
|
45
|
-
export
|
|
46
|
-
export
|
|
47
|
-
export
|
|
48
|
-
export
|
|
49
|
-
export
|
|
50
|
-
export
|
|
51
|
-
export
|
|
52
|
-
export
|
|
53
|
-
export
|
|
54
|
-
export
|
|
55
|
-
export
|
|
56
|
-
export
|
|
57
|
-
export
|
|
58
|
-
export
|
|
59
|
-
export
|
|
44
|
+
export type ICreateElement<T = HTMLElement> = (params: IElementParams<T>) => T;
|
|
45
|
+
export type IAddHandler = <T extends Document>(ele: T, type: string, handler: (e: Event) => void) => void;
|
|
46
|
+
export type IStopBubble = (e: Event) => void;
|
|
47
|
+
export type IStopDefault = (e: Event) => void;
|
|
48
|
+
export type IRemoveHandler = <T extends Document>(ele: T, type: string, handler: (e: Event) => void) => void;
|
|
49
|
+
export type IDispatchEvent = <T extends Document>(ele: T, data: any) => void;
|
|
50
|
+
export type IRequestParams<T> = T | IObject<any> | null;
|
|
51
|
+
export type IUrl = string;
|
|
52
|
+
export type IEnv = 'Window' | 'Node';
|
|
53
|
+
export type IDataType = "text" | "json" | "blob" | "formData" | "arrayBuffer";
|
|
54
|
+
export type IRequestMethods = "GET" | "POST" | "DELETE" | "PUT" | "OPTION" | "HEAD" | "PATCH";
|
|
55
|
+
export type IRequestBody = IRequestParams<BodyInit>;
|
|
56
|
+
export type IRequestHeaders = IRequestParams<HeadersInit>;
|
|
57
|
+
export type IRequestBaseFn = (url: IUrl, opts: IRequestOptions) => Promise<any>;
|
|
58
|
+
export type IRequestFn = (url?: IUrl, query?: IObject<any>, body?: IRequestBody, opts?: IRequestOptions) => Promise<any>;
|
|
59
|
+
export type IRequestOptions = {
|
|
60
60
|
method?: IRequestMethods;
|
|
61
61
|
query?: IRequestParams<IObject<any>>;
|
|
62
62
|
body?: IRequestBody;
|
|
@@ -66,13 +66,13 @@ export declare type IRequestOptions = {
|
|
|
66
66
|
timer?: number | unknown | null;
|
|
67
67
|
[key: string]: any;
|
|
68
68
|
};
|
|
69
|
-
export
|
|
69
|
+
export type IInterceptors = {
|
|
70
70
|
use(type: "request" | "response" | "error", fn: Function): IInterceptors;
|
|
71
71
|
get reqFn(): Function;
|
|
72
72
|
get resFn(): Function;
|
|
73
73
|
get errFn(): Function;
|
|
74
74
|
};
|
|
75
|
-
export
|
|
75
|
+
export type IRequestBase = {
|
|
76
76
|
readonly origin: string;
|
|
77
77
|
chackUrl: (url: IUrl) => boolean;
|
|
78
78
|
envDesc: () => IEnv;
|
|
@@ -86,12 +86,12 @@ export declare type IRequestBase = {
|
|
|
86
86
|
getDataByType: (type: IDataType, response: Response) => Promise<any>;
|
|
87
87
|
formatBodyString: (bodyString: string) => IObject<Function>;
|
|
88
88
|
};
|
|
89
|
-
export
|
|
89
|
+
export type IRequestInit = {
|
|
90
90
|
initDefaultParams: (url: IUrl, opts: IRequestOptions) => any;
|
|
91
91
|
initFetchParams: (url: IUrl, opts: IRequestOptions) => any;
|
|
92
92
|
initHttpParams: (url: IUrl, opts: IRequestOptions) => any;
|
|
93
93
|
};
|
|
94
|
-
export
|
|
94
|
+
export type IRequest = {
|
|
95
95
|
GET: IRequestFn;
|
|
96
96
|
POST: IRequestFn;
|
|
97
97
|
DELETE: IRequestFn;
|
package/dist/esm/array.js
CHANGED
|
@@ -1,18 +1,8 @@
|
|
|
1
|
-
import { getType } from "./index.js";
|
|
2
|
-
export var arrayRandom = function (arr) {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
};
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
};
|
|
10
|
-
export var arrayDemote = function (arr, result) {
|
|
11
|
-
if (result === void 0) {
|
|
12
|
-
result = [];
|
|
13
|
-
}
|
|
14
|
-
arr.forEach(function (i) {
|
|
15
|
-
return getType(i) === "array" ? arrayDemote(i, result) : result.push(i);
|
|
16
|
-
});
|
|
17
|
-
return result;
|
|
18
|
-
};
|
|
1
|
+
import { getType } from "./index.js";
|
|
2
|
+
export var arrayRandom = function (arr) { return arr.sort(function () { return Math.random() - 0.5; }); };
|
|
3
|
+
export var arrayUniq = function (arr) { return Array.from(new Set(arr)); };
|
|
4
|
+
export var arrayDemote = function (arr, result) {
|
|
5
|
+
if (result === void 0) { result = []; }
|
|
6
|
+
arr.forEach(function (i) { return getType(i) === "array" ? arrayDemote(i, result) : result.push(i); });
|
|
7
|
+
return result;
|
|
8
|
+
};
|
package/dist/esm/base.js
CHANGED
|
@@ -1,47 +1,41 @@
|
|
|
1
|
-
import { types } from "./index.js";
|
|
2
|
-
export var randomNum = function (min, max, bool) {
|
|
3
|
-
if (bool === void 0) {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
};
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
if (
|
|
22
|
-
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
if (whiteList === void 0) {
|
|
43
|
-
whiteList = [];
|
|
44
|
-
}
|
|
45
|
-
var __type = getType(data);
|
|
46
|
-
return whiteList.indexOf(__type) > 0;
|
|
47
|
-
};
|
|
1
|
+
import { types } from "./index.js";
|
|
2
|
+
export var randomNum = function (min, max, bool) {
|
|
3
|
+
if (bool === void 0) { bool = false; }
|
|
4
|
+
return Math.floor(Math.random() * (max - min + (bool ? 1 : 0)) + min);
|
|
5
|
+
};
|
|
6
|
+
export var urlSplit = function (url) {
|
|
7
|
+
var result = {};
|
|
8
|
+
if (!url.includes("?")) {
|
|
9
|
+
return result;
|
|
10
|
+
}
|
|
11
|
+
var params = url.split("?")[1].split("&");
|
|
12
|
+
params.forEach(function (i) {
|
|
13
|
+
var key = i.split("=")[0];
|
|
14
|
+
result[key] = i.split("=")[1];
|
|
15
|
+
});
|
|
16
|
+
return result;
|
|
17
|
+
};
|
|
18
|
+
export var urlJoin = function (url, query) {
|
|
19
|
+
if (query === void 0) { query = {}; }
|
|
20
|
+
var queryObject = Object.keys(query);
|
|
21
|
+
if (queryObject.length === 0)
|
|
22
|
+
return url;
|
|
23
|
+
var params = queryObject.map(function (i) { return "".concat(i, "=").concat(query[i]); });
|
|
24
|
+
return "".concat(url).concat(url.includes("?") ? "&" : '?').concat(params.join("&"));
|
|
25
|
+
};
|
|
26
|
+
export var getType = function (data) {
|
|
27
|
+
var type = typeof data;
|
|
28
|
+
if (data === null) {
|
|
29
|
+
return "null";
|
|
30
|
+
}
|
|
31
|
+
else if (type === "object") {
|
|
32
|
+
var key = Object.prototype.toString.call(data);
|
|
33
|
+
return types[key];
|
|
34
|
+
}
|
|
35
|
+
return type;
|
|
36
|
+
};
|
|
37
|
+
export var getTypeByList = function (data, whiteList) {
|
|
38
|
+
if (whiteList === void 0) { whiteList = []; }
|
|
39
|
+
var __type = getType(data);
|
|
40
|
+
return whiteList.indexOf(__type) > 0;
|
|
41
|
+
};
|
package/dist/esm/element.js
CHANGED
|
@@ -1,16 +1,9 @@
|
|
|
1
|
-
export var createElement = function (_a) {
|
|
2
|
-
var _b, _c;
|
|
3
|
-
var ele = _a.ele,
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
}));
|
|
11
|
-
attr && ((_c = Object.keys(style)) === null || _c === void 0 ? void 0 : _c.forEach(function (key) {
|
|
12
|
-
return element[key] = attr[key];
|
|
13
|
-
}));
|
|
14
|
-
parent && parent.appendChild(element);
|
|
15
|
-
return element;
|
|
16
|
-
};
|
|
1
|
+
export var createElement = function (_a) {
|
|
2
|
+
var _b, _c;
|
|
3
|
+
var ele = _a.ele, style = _a.style, attr = _a.attr, parent = _a.parent;
|
|
4
|
+
var element = ele instanceof HTMLElement ? ele : document.createElement(ele !== null && ele !== void 0 ? ele : 'div');
|
|
5
|
+
style && ((_b = Object.keys(style)) === null || _b === void 0 ? void 0 : _b.forEach(function (key) { return element.style[key] = style[key]; }));
|
|
6
|
+
attr && ((_c = Object.keys(style)) === null || _c === void 0 ? void 0 : _c.forEach(function (key) { return element[key] = attr[key]; }));
|
|
7
|
+
parent && parent.appendChild(element);
|
|
8
|
+
return element;
|
|
9
|
+
};
|
package/dist/esm/event.js
CHANGED
|
@@ -1,34 +1,38 @@
|
|
|
1
|
-
export var addHandler = function (ele, type, handler) {
|
|
2
|
-
if (ele.addEventListener) {
|
|
3
|
-
ele.addEventListener(type, handler, false);
|
|
4
|
-
}
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
event.
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
};
|
|
1
|
+
export var addHandler = function (ele, type, handler) {
|
|
2
|
+
if (ele.addEventListener) {
|
|
3
|
+
ele.addEventListener(type, handler, false);
|
|
4
|
+
}
|
|
5
|
+
else {
|
|
6
|
+
ele["on" + type] = handler;
|
|
7
|
+
}
|
|
8
|
+
};
|
|
9
|
+
export var stopBubble = function (event) {
|
|
10
|
+
event = event || window.event;
|
|
11
|
+
if (event.stopPropagation) {
|
|
12
|
+
event.stopPropagation();
|
|
13
|
+
}
|
|
14
|
+
else {
|
|
15
|
+
event.cancelBubble = false;
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
export var stopDefault = function (event) {
|
|
19
|
+
event = event || window.event;
|
|
20
|
+
if (event.preventDefault) {
|
|
21
|
+
event.preventDefault();
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
event.returnValue = false;
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
export var removeHandler = function (ele, type, handler) {
|
|
28
|
+
if (ele.removeEventListener) {
|
|
29
|
+
ele.removeEventListener(type, handler, false);
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
ele["on" + type] = null;
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
export var dispatchEvent = function (ele, data) {
|
|
36
|
+
var evts = new Event(data);
|
|
37
|
+
ele.dispatchEvent(evts);
|
|
38
|
+
};
|
package/dist/esm/function.js
CHANGED
|
@@ -1,58 +1,53 @@
|
|
|
1
|
-
var __spreadArray = this && this.__spreadArray || function (to, from, pack) {
|
|
2
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
3
|
-
if (ar || !(i in from)) {
|
|
4
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
5
|
-
ar[i] = from[i];
|
|
6
|
-
}
|
|
7
|
-
}
|
|
8
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
9
|
-
};
|
|
10
|
-
var _this = this;
|
|
11
|
-
export var throttle = function (fn, time) {
|
|
12
|
-
var _timer = null;
|
|
13
|
-
return function () {
|
|
14
|
-
var args = [];
|
|
15
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
16
|
-
args[_i] = arguments[_i];
|
|
17
|
-
}
|
|
18
|
-
if (_timer)
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
_timer
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
return [null, res];
|
|
55
|
-
}).catch(function (err) {
|
|
56
|
-
return [err];
|
|
57
|
-
});
|
|
58
|
-
};
|
|
1
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
2
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
3
|
+
if (ar || !(i in from)) {
|
|
4
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
5
|
+
ar[i] = from[i];
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
9
|
+
};
|
|
10
|
+
var _this = this;
|
|
11
|
+
export var throttle = function (fn, time) {
|
|
12
|
+
var _timer = null;
|
|
13
|
+
return function () {
|
|
14
|
+
var args = [];
|
|
15
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
16
|
+
args[_i] = arguments[_i];
|
|
17
|
+
}
|
|
18
|
+
if (_timer)
|
|
19
|
+
return;
|
|
20
|
+
_timer = setTimeout(function () {
|
|
21
|
+
fn.call.apply(fn, __spreadArray([_this], args, false));
|
|
22
|
+
_timer = null;
|
|
23
|
+
}, time);
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
export var debounce = function (fn, time) {
|
|
27
|
+
var _timer = null;
|
|
28
|
+
return function () {
|
|
29
|
+
var args = [];
|
|
30
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
31
|
+
args[_i] = arguments[_i];
|
|
32
|
+
}
|
|
33
|
+
if (_timer) {
|
|
34
|
+
clearTimeout(_timer);
|
|
35
|
+
_timer = null;
|
|
36
|
+
}
|
|
37
|
+
_timer = setTimeout(function () {
|
|
38
|
+
fn.call.apply(fn, __spreadArray([_this], args, false));
|
|
39
|
+
}, time);
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
export var defer = function () {
|
|
43
|
+
var resolve, reject;
|
|
44
|
+
return {
|
|
45
|
+
promise: new Promise(function (_resolve, _reject) {
|
|
46
|
+
resolve = _resolve;
|
|
47
|
+
reject = _reject;
|
|
48
|
+
}),
|
|
49
|
+
resolve: resolve,
|
|
50
|
+
reject: reject
|
|
51
|
+
};
|
|
52
|
+
};
|
|
53
|
+
export var catchAwait = function (defer) { return defer.then(function (res) { return [null, res]; }).catch(function (err) { return [err]; }); };
|
package/dist/esm/index.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
export * from "./object.js";
|
|
2
|
-
export * from "./base.js";
|
|
3
|
-
export * from "./array.js";
|
|
4
|
-
export * from "./function.js";
|
|
5
|
-
export * from "./element.js";
|
|
6
|
-
export * from "./static.js";
|
|
7
|
-
export * from "./types.js";
|
|
8
|
-
export * from "./request.js";
|
|
9
|
-
export * from "./event.js";
|
|
10
|
-
export * from "./storage.js";
|
|
1
|
+
export * from "./object.js";
|
|
2
|
+
export * from "./base.js";
|
|
3
|
+
export * from "./array.js";
|
|
4
|
+
export * from "./function.js";
|
|
5
|
+
export * from "./element.js";
|
|
6
|
+
export * from "./static.js";
|
|
7
|
+
export * from "./types.js";
|
|
8
|
+
export * from "./request.js";
|
|
9
|
+
export * from "./event.js";
|
|
10
|
+
export * from "./storage.js";
|