utils-lib-js 1.4.0 → 1.4.2

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.
@@ -166,11 +166,11 @@ var classDecorator = function (params) {
166
166
  };
167
167
  exports.classDecorator = classDecorator;
168
168
  var stringToJson = function (target) {
169
- if ((0, index_js_1.getType)(target) !== "string") return {};
169
+ if ((0, index_js_1.getType)(target) !== "string") return null;
170
170
  try {
171
171
  return JSON.parse(target);
172
172
  } catch (error) {
173
- return {};
173
+ return null;
174
174
  }
175
175
  };
176
176
  exports.stringToJson = stringToJson;
@@ -196,14 +196,16 @@ var RequestInit = function (_super) {
196
196
  };
197
197
  _this.initFetchParams = function (url, opts) {
198
198
  var _a, _b;
199
- var params = _this.initAbort(_this.initDefaultParams(url, opts));
200
- return (_b = (_a = _this.reqFn) === null || _a === void 0 ? void 0 : _a.call(_this, params)) !== null && _b !== void 0 ? _b : params;
199
+ var _temp = _this.initAbort(_this.initDefaultParams(url, opts));
200
+ var params = (_b = (_a = _this.reqFn) === null || _a === void 0 ? void 0 : _a.call(_this, _temp)) !== null && _b !== void 0 ? _b : _temp;
201
+ return params;
201
202
  };
202
203
  _this.initHttpParams = function (url, opts) {
203
204
  var _a, _b;
204
- var params = _this.initAbort(_this.initDefaultParams(url, opts));
205
+ var _temp = _this.initAbort(_this.initDefaultParams(url, opts));
206
+ var params = (_b = (_a = _this.reqFn) === null || _a === void 0 ? void 0 : _a.call(_this, _temp)) !== null && _b !== void 0 ? _b : _temp;
205
207
  var options = parse(params.url, true);
206
- return (_b = (_a = _this.reqFn) === null || _a === void 0 ? void 0 : _a.call(_this, __assign(__assign({}, params), options))) !== null && _b !== void 0 ? _b : __assign(__assign({}, params), options);
208
+ return __assign(__assign({}, params), options);
207
209
  };
208
210
  return _this;
209
211
  }
@@ -26,7 +26,7 @@ export declare type ICreateObject = <T, U extends T>(source: T) => U;
26
26
  export declare type IInherit = <T extends Function>(source: T, target?: Function) => Function;
27
27
  export declare type IGetInstance = (classProto: IInstance<FunctionConstructor>, overwrite?: boolean, ...params: any[]) => Function;
28
28
  export declare type IClassDecorator = (params: IObject<any>) => <TFunction extends Function>(target: TFunction) => void;
29
- export declare type IStringToJson = (target: string) => IObject<any>;
29
+ export declare type IStringToJson = (target: string) => IObject<any> | null;
30
30
  export declare type IJsonToString = (target: IObject<any>) => string;
31
31
  export declare type IThrottle = (fn: Function, time: number) => (...args: any[]) => void;
32
32
  export declare type IDebounce = (fn: Function, time: number) => (...args: any[]) => void;
@@ -151,11 +151,11 @@ export var classDecorator = function (params) {
151
151
  };
152
152
  };
153
153
  export var stringToJson = function (target) {
154
- if (getType(target) !== "string") return {};
154
+ if (getType(target) !== "string") return null;
155
155
  try {
156
156
  return JSON.parse(target);
157
157
  } catch (error) {
158
- return {};
158
+ return null;
159
159
  }
160
160
  };
161
161
  export var jsonToString = function (target) {
@@ -192,14 +192,16 @@ var RequestInit = function (_super) {
192
192
  };
193
193
  _this.initFetchParams = function (url, opts) {
194
194
  var _a, _b;
195
- var params = _this.initAbort(_this.initDefaultParams(url, opts));
196
- return (_b = (_a = _this.reqFn) === null || _a === void 0 ? void 0 : _a.call(_this, params)) !== null && _b !== void 0 ? _b : params;
195
+ var _temp = _this.initAbort(_this.initDefaultParams(url, opts));
196
+ var params = (_b = (_a = _this.reqFn) === null || _a === void 0 ? void 0 : _a.call(_this, _temp)) !== null && _b !== void 0 ? _b : _temp;
197
+ return params;
197
198
  };
198
199
  _this.initHttpParams = function (url, opts) {
199
200
  var _a, _b;
200
- var params = _this.initAbort(_this.initDefaultParams(url, opts));
201
+ var _temp = _this.initAbort(_this.initDefaultParams(url, opts));
202
+ var params = (_b = (_a = _this.reqFn) === null || _a === void 0 ? void 0 : _a.call(_this, _temp)) !== null && _b !== void 0 ? _b : _temp;
201
203
  var options = parse(params.url, true);
202
- return (_b = (_a = _this.reqFn) === null || _a === void 0 ? void 0 : _a.call(_this, __assign(__assign({}, params), options))) !== null && _b !== void 0 ? _b : __assign(__assign({}, params), options);
204
+ return __assign(__assign({}, params), options);
203
205
  };
204
206
  return _this;
205
207
  }
@@ -26,7 +26,7 @@ export declare type ICreateObject = <T, U extends T>(source: T) => U;
26
26
  export declare type IInherit = <T extends Function>(source: T, target?: Function) => Function;
27
27
  export declare type IGetInstance = (classProto: IInstance<FunctionConstructor>, overwrite?: boolean, ...params: any[]) => Function;
28
28
  export declare type IClassDecorator = (params: IObject<any>) => <TFunction extends Function>(target: TFunction) => void;
29
- export declare type IStringToJson = (target: string) => IObject<any>;
29
+ export declare type IStringToJson = (target: string) => IObject<any> | null;
30
30
  export declare type IJsonToString = (target: IObject<any>) => string;
31
31
  export declare type IThrottle = (fn: Function, time: number) => (...args: any[]) => void;
32
32
  export declare type IDebounce = (fn: Function, time: number) => (...args: any[]) => void;
package/package.json CHANGED
@@ -1,43 +1,44 @@
1
- {
2
- "name": "utils-lib-js",
3
- "version": "1.4.0",
4
- "description": "JavaScript工具函数,封装的一些常用的js函数",
5
- "main": "./dist/common/index.js",
6
- "types": "./dist/common/index.d.ts",
7
- "module": "./dist/esm/index.js",
8
- "exports": {
9
- "import": "./dist/esm/index.js",
10
- "require": "./dist/common/index.js"
11
- },
12
- "scripts": {
13
- "debug:esm": "start cmd /k pnpm run build:hot:esm",
14
- "debug:node": "start cmd /k pnpm run build:hot:node & pnpm run node:hot",
15
- "node:hot": "nodemon server.js",
16
- "build:hot:esm": "tsc -p tsconfig.es.json -w",
17
- "build:hot:node": "tsc -p tsconfig.json -w",
18
- "build": "pnpm run tsc:build && pnpm run babel:mjs && pnpm run babel:cjs",
19
- "tsc:build": "rm -fr dist && tsc -p tsconfig.json && tsc -p tsconfig.es.json",
20
- "publish": "pnpm run build && pnpm publish",
21
- "babel:mjs": "babel dist/esm --out-dir dist/esm",
22
- "babel:cjs": "babel dist/common --out-dir dist/common",
23
- "test": "echo \"Error: no test specified\" && exit 1"
24
- },
25
- "repository": {
26
- "type": "git",
27
- "url": "https://gitee.com/DieHunter/utils-lib-js.git"
28
- },
29
- "keywords": [
30
- "utils",
31
- "tools",
32
- "lib"
33
- ],
34
- "author": "",
35
- "license": "ISC",
36
- "devDependencies": {
37
- "@types/node": "^18.7.15",
38
- "babel-cli": "^6.26.0"
39
- },
40
- "dependencies": {
41
- "abort-controller": "^3.0.0"
42
- }
1
+ {
2
+ "name": "utils-lib-js",
3
+ "version": "1.4.2",
4
+ "description": "JavaScript工具函数,封装的一些常用的js函数",
5
+ "main": "./dist/common/index.js",
6
+ "types": "./dist/common/index.d.ts",
7
+ "module": "./dist/esm/index.js",
8
+ "exports": {
9
+ "import": "./dist/esm/index.js",
10
+ "require": "./dist/common/index.js"
11
+ },
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "https://gitee.com/DieHunter/utils-lib-js.git"
15
+ },
16
+ "keywords": [
17
+ "utils",
18
+ "tools",
19
+ "lib"
20
+ ],
21
+ "author": "",
22
+ "license": "ISC",
23
+ "devDependencies": {
24
+ "@types/node": "^18.7.15",
25
+ "babel-cli": "^6.26.0"
26
+ },
27
+ "dependencies": {
28
+ "abort-controller": "^3.0.0"
29
+ },
30
+ "scripts": {
31
+ "debug:esm": "start cmd /k pnpm run build:hot:esm",
32
+ "debug:node": "start cmd /k pnpm run build:hot:node & pnpm run node:hot",
33
+ "node:hot": "nodemon server.js",
34
+ "build:hot:esm": "tsc -p tsconfig.es.json -w",
35
+ "build:hot:node": "tsc -p tsconfig.json -w",
36
+ "build": "pnpm run tsc:build && pnpm run babel:mjs && pnpm run babel:cjs",
37
+ "tsc:build": "rm -fr dist && tsc -p tsconfig.json && tsc -p tsconfig.es.json",
38
+ "build:publish": "pnpm run build && pnpm publish",
39
+ "babel:mjs": "babel dist/esm --out-dir dist/esm",
40
+ "babel:cjs": "babel dist/common --out-dir dist/common",
41
+ "test": "echo \"Error: no test specified\" && exit 1"
42
+ },
43
+ "readme": "# utils-lib-js\r\n\r\n#### 介绍\r\nJavaScript工具函数,封装的一些常用的js函数\r\n\r\n#### 软件架构\r\n软件架构说明\r\n\r\n\r\n#### 安装教程\r\n\r\n1. pnpm i\r\n2. pnpm build\r\n\r\n#### 使用说明\r\n\r\n1. pnpm build(构建)\r\n2. pnpm debug(调试源码)\r\n\r\n#### 参与贡献\r\n\r\n1. Fork 本仓库\r\n2. 新建 Feat_xxx 分支\r\n3. 提交代码\r\n4. 新建 Pull Request\r\n\r\n#### 功能\r\n接口:\r\n\r\nexport type IKey = string | symbol | number\r\n\r\n##### 对象类型\r\n\r\nexport interface IObject<T> {\r\n [key: IKey]: T | IObject<any>\r\n}\r\n\r\nexport interface IPromise extends IObject<any> {\r\n promise: Promise<void>\r\n resolve: (res: any) => unknown\r\n reject: (err: any) => unknown\r\n}\r\n\r\nexport type IInstance<T> = {\r\n _instance: Function\r\n} & T\r\n\r\nexport type IDemoteArray<T> = Array<IDemoteArray<T> | T>\r\n\r\n##### base\r\n\r\n/**产生区间随机数\r\n* @param {number} min 最小区间\r\n* @param {number} max 最大区间\r\n* @param {boolean} bool 包含最大值\r\n* @return {number} 随机数\r\n**/\r\n\r\nexport type IRandomNum = (min: number, max: number, bool?: boolean) => number\r\n\r\n/**获取url的参数\r\n* @param {string} url 待截取的地址\r\n* @return {object} 参数对象\r\n**/\r\n\r\nexport type IUrlSplit = (url: string) => IObject<any>\r\n\r\n/**添加url的参数\r\n* @param {string} url 待添加参数的地址\r\n* @param {object} query 待添加的参数\r\n* @return {string} 添加参数后的url\r\n**/\r\n\r\nexport type IUrlJoin = (url: string, query: object) => string\r\n\r\n/**获取数据类型\r\n* @param {any} data 待检测数据\r\n* @return {string} 数据类型\r\n**/\r\n\r\nexport type IGetType<T> = (data: any) => typeof data | T[keyof T] | \"null\";\r\n\r\n/**批量判断数据类型\r\n* @param {any} data 待检测数据\r\n* @param {any} whiteList 数据类型名单\r\n* @return {boolean} 是否在白名单中\r\n**/\r\n\r\nexport type IGetTypeByList = (data: any, whiteList: string[]) => boolean;\r\n\r\n\r\n##### object\r\n\r\n/**lodash中的 _.get(),获取对象某级属性\r\n* @param {IObject} object 目标对象\r\n* @param {string} key 对象层级\r\n* @param {any} defaultValue 未取得时默认值\r\n* @return {IObject[IKey]} 对象某个属性\r\n**/\r\n\r\nexport type IGetValue = <T, U = IObject<T> | IObject<T>[IKey]>(object: U, key: string, defaultValue?: any) => U\r\n\r\n/**lodash中的 _.set(),赋值对象某级属性\r\n* @param {IObject} object 目标对象\r\n* @param {string} key 对象层级\r\n* @param {any} value 需要赋的值\r\n* @return {IObject} 目标对象\r\n**/\r\n\r\nexport type ISetValue = <T>(object: IObject<T>, key: string, value?: any) => IObject<T>\r\n\r\n/**对象混入\r\n* @param {IObject} target 目标对象\r\n* @param {string} source 需要混入的对象集合\r\n* @param {boolean} overwrite 是否重写覆盖原有属性\r\n* @return {IObject} 目标对象\r\n**/\r\n\r\nexport type IMixIn = <U extends IObject<any>>(target?: U, source?: IObject<any>, overwrite?: boolean) => U\r\n\r\n/**枚举值反向映射\r\n* @param {IObject<string>} target 目标对象\r\n* @return {IObject<string>} 目标对象\r\n**/\r\n\r\nexport type IEnumInversion = (target: IObject<string>) => IObject<string>\r\n\r\n/**对象复制\r\n* @param {IObject<any>} target 目标对象\r\n* @return {IObject} 目标对象\r\n**/\r\n\r\nexport type ICloneDeep = (target?: any) => any\r\n\r\n/**生成 对象 类型的初始值\r\n* @param {string} type 数据类型\r\n* @param {any} __init 初始值\r\n* @return {any} 目标对象\r\n**/\r\n\r\nexport type ICreateObjectVariable = (type: string, source?: any) => any\r\n\r\n/**Object.create 根据源对象产出新对象\r\n* @param {Function|Object} source 源对象\r\n* @return {Function|Object} 对象产物\r\n**/\r\n\r\nexport type ICreateObject = <T, U extends T>(source: T) => U\r\n\r\n/**类的继承\r\n* @param {Function} source 源对象\r\n* @return {Function} 继承产物\r\n**/\r\n\r\nexport type IInherit = <T extends Function>(source: T, target?: Function) => Function\r\n\r\n/**生成类的实例单例\r\n* @param {Function} classProto 类\r\n* @param {Boolean} overwrite 是否覆盖已有单例\r\n* @param {any[]} params 构造函数的参数\r\n* @return {IObject} 实例化的单例\r\n**/\r\n\r\nexport type IGetInstance = (classProto: IInstance<FunctionConstructor>, overwrite?: boolean, ...params: any[]) => Function\r\n\r\n/**通过装饰器将属性混入类中\r\n* @param {IObject<any>} params 混入的属性\r\n* @return {ClassDecorator} 装饰器钩子函数\r\n**/\r\n\r\nexport type IClassDecorator = (params: IObject<any>) => <TFunction extends Function>(target: TFunction) => void\r\n\r\n/**JSON.parse封装\r\n* @param {string} target 字符串\r\n* @return {IObject<any>} 对象\r\n**/\r\n\r\nexport type IStringToJson = (target: string) => IObject<any>\r\n\r\n/**JSON.stringify封装\r\n* @param {IObject<any>} target 对象\r\n* @return {string} 字符串\r\n**/\r\n\r\nexport type IJsonToString = (target: IObject<any>) => string\r\n\r\n##### function\r\n\r\n/**节流(throttle):高频事件触发,但在 n 秒内只会执行一次\r\n* @param {Function} fn 节流处理的函数\r\n* @param {number} time 执行间隔/毫秒\r\n* @return {Function} 处理后的函数\r\n**/\r\n\r\nexport type IThrottle = (fn: Function, time: number) => (...args: any[]) => void\r\n\r\n/**防抖(debounce):触发高频事件后 n 秒内函数只会执行一次\r\n* @param {Function} fn 防抖处理的函数\r\n* @param {number} time 允许运行函数间隔/毫秒\r\n* @return {Function} 处理后的函数\r\n**/\r\n\r\nexport type IDebounce = (fn: Function, time: number) => (...args: any[]) => void\r\n\r\n/**\r\n * Promise扁平化,避免Promise嵌套\r\n * @returns {Promise,resolve,reject} \r\n */\r\n\r\nexport type IDefer = () => IPromise\r\n\r\n/**await与try catch 捕获异常处理方法\r\n * @param {Promise<any>} defer 延迟函数\r\n * @returns {Promise<any>} [error, result]\r\n */\r\n\r\nexport type ICatchAwait<T extends Promise<any>> = (defer: T) => T\r\n\r\n##### array\r\n\r\n/**数组乱序\r\n * @param {Array<any>} arr 目标数组\r\n * @returns {Array<any>} 乱序后的数组\r\n */\r\n \r\nexport type IArrayRandom<T extends any[]> = (arr: T) => T\r\n\r\n/**数组数组去重\r\n * @param {Array<any>} arr 目标数组\r\n * @returns {Array<any>} 去重后的数组\r\n */\r\n\r\nexport type IArrayUniq<T extends any[]> = (arr: T) => T\r\n\r\n/**数组扁平化\r\n * @param {Array<any>} arr 目标数组\r\n * @returns {Array<any>} 扁平化的数组\r\n */\r\n\r\nexport type IArrayDemote<T extends IDemoteArray<any>> = (arr: T, result?: T) => T\r\n\r\n##### element\r\n\r\n/**IElementParams\r\n * @param {string} ele 标签类型\r\n * @param {CSSStyleDeclaration} style 样式\r\n * @param {Attr} attr 属性\r\n * @param {object} parent 父元素\r\n */\r\n\r\ninterface IElementParams<T> {\r\n ele: T | string\r\n style: CSSStyleDeclaration\r\n attr: Attr\r\n parent: T\r\n}\r\n\r\n/**新增标签,设置属性及样式\r\n * @param {IElementParams} params 配置\r\n * @return {ElementObject} 生成的标签\r\n */\r\n\r\nexport type ICreateElement<T = HTMLElement> = (params: IElementParams<T>) => T\r\n\r\n##### event\r\n\r\n/**浏览器事件\r\n * @param {Document} ele 标签\r\n * @param {string} type 事件类型\r\n * @param {(e: Event) => void} handler 事件回调\r\n * @return {void}\r\n */\r\n\r\nexport type IAddHandler = <T extends Document>(ele: T, type: string, handler: (e: Event) => void) => void\r\n\r\n/**取消事件冒泡\r\n * @param {Event} e 浏览器事件对象\r\n * @return {void}\r\n */\r\n\r\nexport type IStopBubble = (e: Event) => void\r\n\r\n/**取消默认事件\r\n * @param {Event} e 浏览器事件对象\r\n * @return {void}\r\n */\r\n\r\nexport type IStopDefault = (e: Event) => void\r\n\r\n/**取消浏览器事件\r\n * @param {Document} ele 标签\r\n * @param {string} type 事件类型\r\n * @param {(e: Event) => void} handler 事件回调\r\n * @return {void}\r\n */\r\n\r\nexport type IRemoveHandler = <T extends Document>(ele: T, type: string, handler: (e: Event) => void) => void\r\n\r\n/**取消默认事件\r\n * @param {Event} e 浏览器事件对象\r\n * @return {void}\r\n */\r\n\r\nexport type IDispatchEvent = <T extends Document>(ele: T, data: any) => void\r\n\r\n##### request\r\n\r\nexport type IRequestParams<T> = T | IObject<any> | null\r\n\r\n// 请求路径\r\n\r\nexport type IUrl = string\r\n\r\n// 环境判断\r\n\r\nexport type IEnv = 'Window' | 'Node'\r\n\r\n// fetch返回取值方式\r\n\r\nexport type IDataType = \"text\" | \"json\" | \"blob\" | \"formData\" | \"arrayBuffer\"\r\n\r\n// 请求方式\r\n\r\nexport type IRequestMethods = \"GET\" | \"POST\" | \"DELETE\" | \"PUT\" | \"OPTION\"\r\n\r\n// body结构\r\n\r\nexport type IRequestBody = IRequestParams<BodyInit>\r\n\r\n// heads结构\r\n\r\nexport type IRequestHeaders = IRequestParams<HeadersInit>\r\n\r\n// 请求基础函数\r\n\r\nexport type IRequestBaseFn = (url: IUrl, opts: IRequestOptions) => Promise<any>\r\n\r\n// 请求函数体\r\n\r\nexport type IRequestFn = (url?: IUrl, query?: IObject<any>, body?: IRequestBody, opts?: IRequestOptions) => Promise<any>\r\n\r\n// 请求参数\r\n\r\nexport type IRequestOptions = {\r\n method?: IRequestMethods\r\n query?: IRequestParams<IObject<any>>\r\n body?: IRequestBody\r\n headers?: IRequestHeaders\r\n controller?: AbortController\r\n timeout?: number\r\n timer?: number | unknown | null\r\n [key: string]: any\r\n}\r\n\r\n// 拦截器\r\n\r\nexport type IInterceptors = {\r\n use(type: \"request\" | \"response\" | \"error\", fn: Function): void\r\n get reqFn(): Function\r\n get resFn(): Function\r\n get errFn(): Function\r\n}\r\n\r\n// 公共函数\r\n\r\nexport type IRequestBase = {\r\n readonly origin: string\r\n chackUrl: (url: IUrl) => boolean\r\n envDesc: () => IEnv\r\n errorFn: <Err = any, R = Function>(reject: R) => (err: Err) => R\r\n clearTimer: (opts: IRequestOptions) => void\r\n initAbort: <T = IRequestOptions>(opts: T) => T\r\n requestType: () => IRequestBaseFn\r\n fixOrigin: (fixStr: string) => string\r\n fetch: IRequestBaseFn\r\n http: IRequestBaseFn\r\n getDataByType: (type: IDataType, response: Response) => Promise<any>\r\n}\r\n\r\n// 初始化参数\r\n\r\nexport type IRequestInit = {\r\n initDefaultParams: (url: IUrl, opts: IRequestOptions) => any\r\n initFetchParams: (url: IUrl, opts: IRequestOptions) => any\r\n initHttpParams: (url: IUrl, opts: IRequestOptions) => any\r\n}\r\n\r\n// 请求主体类\r\n\r\nexport type IRequest = {\r\n GET: IRequestFn\r\n POST: IRequestFn\r\n DELETE: IRequestFn\r\n PUT: IRequestFn\r\n OPTIONS: IRequestFn\r\n HEAD: IRequestFn\r\n PATCH: IRequestFn\r\n} & IRequestBase\r\n"
43
44
  }