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.
@@ -1,4 +1,4 @@
1
- export declare type IKey = string | symbol | number;
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 declare type IInstance<T> = {
10
+ export type IInstance<T> = {
11
11
  _instance: Function;
12
12
  } & T;
13
- export declare type IDemoteArray<T> = Array<IDemoteArray<T> | T>;
14
- export declare type IRandomNum = (min: number, max: number, bool?: boolean) => number;
15
- export declare type IUrlSplit = (url: string) => IObject<any>;
16
- export declare type IUrlJoin = (url: string, query: object) => string;
17
- export declare type IGetType<T> = (data: any) => typeof data | T[keyof T] | "null";
18
- export declare type IGetTypeByList = (data: any, whiteList: string[]) => boolean;
19
- export declare type IGetValue = <T, U = IObject<T> | IObject<T>[IKey]>(object: U, key: string, defaultValue?: any) => U;
20
- export declare type ISetValue = <T>(object: IObject<T>, key: string, value?: any) => IObject<T>;
21
- export declare type IMixIn = <U extends IObject<any>>(target?: U, source?: IObject<any>, overwrite?: boolean) => U;
22
- export declare type IEnumInversion = (target: IObject<string>) => IObject<string>;
23
- export declare type ICloneDeep = (target?: any) => any;
24
- export declare type ICreateObjectVariable = (type: string, source?: any) => any;
25
- export declare type ICreateObject = <T, U extends T>(source: T) => U;
26
- export declare type IInherit = <T extends Function>(source: T, target?: Function) => Function;
27
- export declare type IGetInstance = (classProto: IInstance<FunctionConstructor>, overwrite?: boolean, ...params: any[]) => Function;
28
- export declare type IClassDecorator = (params: IObject<any>) => <TFunction extends Function>(target: TFunction) => void;
29
- export declare type IStringToJson = (target: string) => IObject<any> | null;
30
- export declare type IJsonToString = (target: IObject<any>) => string;
31
- export declare type IThrottle = (fn: Function, time: number) => (...args: any[]) => void;
32
- export declare type IDebounce = (fn: Function, time: number) => (...args: any[]) => void;
33
- export declare type IDefer = () => IPromise;
34
- export declare type ICatchAwait<T extends Promise<any>> = (defer: T) => T;
35
- export declare type IArrayRandom<T extends any[]> = (arr: T) => T;
36
- export declare type IArrayUniq<T extends any[]> = (arr: T) => T;
37
- export declare type IArrayDemote<T extends IDemoteArray<any>> = (arr: T, result?: T) => T;
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 declare type ICreateElement<T = HTMLElement> = (params: IElementParams<T>) => T;
45
- export declare type IAddHandler = <T extends Document>(ele: T, type: string, handler: (e: Event) => void) => void;
46
- export declare type IStopBubble = (e: Event) => void;
47
- export declare type IStopDefault = (e: Event) => void;
48
- export declare type IRemoveHandler = <T extends Document>(ele: T, type: string, handler: (e: Event) => void) => void;
49
- export declare type IDispatchEvent = <T extends Document>(ele: T, data: any) => void;
50
- export declare type IRequestParams<T> = T | IObject<any> | null;
51
- export declare type IUrl = string;
52
- export declare type IEnv = 'Window' | 'Node';
53
- export declare type IDataType = "text" | "json" | "blob" | "formData" | "arrayBuffer";
54
- export declare type IRequestMethods = "GET" | "POST" | "DELETE" | "PUT" | "OPTION" | "HEAD" | "PATCH";
55
- export declare type IRequestBody = IRequestParams<BodyInit>;
56
- export declare type IRequestHeaders = IRequestParams<HeadersInit>;
57
- export declare type IRequestBaseFn = (url: IUrl, opts: IRequestOptions) => Promise<any>;
58
- export declare type IRequestFn = (url?: IUrl, query?: IObject<any>, body?: IRequestBody, opts?: IRequestOptions) => Promise<any>;
59
- export declare type IRequestOptions = {
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 declare type IInterceptors = {
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 declare type IRequestBase = {
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 declare type IRequestInit = {
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 declare type IRequest = {
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
- return arr.sort(function () {
4
- return Math.random() - 0.5;
5
- });
6
- };
7
- export var arrayUniq = function (arr) {
8
- return Array.from(new Set(arr));
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
- bool = false;
5
- }
6
- return Math.floor(Math.random() * (max - min + (bool ? 1 : 0)) + min);
7
- };
8
- export var urlSplit = function (url) {
9
- var result = {};
10
- if (!url.includes("?")) {
11
- return result;
12
- }
13
- var params = url.split("?")[1].split("&");
14
- params.forEach(function (i) {
15
- var key = i.split("=")[0];
16
- result[key] = i.split("=")[1];
17
- });
18
- return result;
19
- };
20
- export var urlJoin = function (url, query) {
21
- if (query === void 0) {
22
- query = {};
23
- }
24
- var queryObject = Object.keys(query);
25
- if (queryObject.length === 0) return url;
26
- var params = queryObject.map(function (i) {
27
- return "".concat(i, "=").concat(query[i]);
28
- });
29
- return "".concat(url).concat(url.includes("?") ? "&" : '?').concat(params.join("&"));
30
- };
31
- export var getType = function (data) {
32
- var type = typeof data;
33
- if (data === null) {
34
- return "null";
35
- } else if (type === "object") {
36
- var key = Object.prototype.toString.call(data);
37
- return types[key];
38
- }
39
- return type;
40
- };
41
- export var getTypeByList = function (data, whiteList) {
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
+ };
@@ -1,16 +1,9 @@
1
- export var createElement = function (_a) {
2
- var _b, _c;
3
- var ele = _a.ele,
4
- style = _a.style,
5
- attr = _a.attr,
6
- parent = _a.parent;
7
- var element = ele instanceof HTMLElement ? ele : document.createElement(ele !== null && ele !== void 0 ? ele : 'div');
8
- style && ((_b = Object.keys(style)) === null || _b === void 0 ? void 0 : _b.forEach(function (key) {
9
- return element.style[key] = style[key];
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
- } else {
5
- ele["on" + type] = handler;
6
- }
7
- };
8
- export var stopBubble = function (event) {
9
- event = event || window.event;
10
- if (event.stopPropagation) {
11
- event.stopPropagation();
12
- } else {
13
- event.cancelBubble = false;
14
- }
15
- };
16
- export var stopDefault = function (event) {
17
- event = event || window.event;
18
- if (event.preventDefault) {
19
- event.preventDefault();
20
- } else {
21
- event.returnValue = false;
22
- }
23
- };
24
- export var removeHandler = function (ele, type, handler) {
25
- if (ele.removeEventListener) {
26
- ele.removeEventListener(type, handler, false);
27
- } else {
28
- ele["on" + type] = null;
29
- }
30
- };
31
- export var dispatchEvent = function (ele, data) {
32
- var evts = new Event(data);
33
- ele.dispatchEvent(evts);
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
+ };
@@ -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) return;
19
- _timer = setTimeout(function () {
20
- fn.call.apply(fn, __spreadArray([_this], args, false));
21
- _timer = null;
22
- }, time);
23
- };
24
- };
25
- export var debounce = function (fn, time) {
26
- var _timer = null;
27
- return function () {
28
- var args = [];
29
- for (var _i = 0; _i < arguments.length; _i++) {
30
- args[_i] = arguments[_i];
31
- }
32
- if (_timer) {
33
- clearTimeout(_timer);
34
- _timer = null;
35
- }
36
- _timer = setTimeout(function () {
37
- fn.call.apply(fn, __spreadArray([_this], args, false));
38
- }, time);
39
- };
40
- };
41
- export var defer = function () {
42
- var resolve, reject;
43
- return {
44
- promise: new Promise(function (_resolve, _reject) {
45
- resolve = _resolve;
46
- reject = _reject;
47
- }),
48
- resolve: resolve,
49
- reject: reject
50
- };
51
- };
52
- export var catchAwait = function (defer) {
53
- return defer.then(function (res) {
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";