utils-lib-js 1.2.1 → 1.3.0

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.
@@ -26,4 +26,6 @@ __exportStar(require("./static.js"), exports);
26
26
  __exportStar(require("./types.js"), exports);
27
27
  __exportStar(require("./request.js"), exports);
28
28
  __exportStar(require("./event.js"), exports);
29
- __exportStar(require("./storage.js"), exports);
29
+ __exportStar(require("./storage.js"), exports);
30
+ console.log(require, typeof Window !== "undefined");
31
+ typeof require === "undefined" && typeof Window !== "undefined" && (window.require = function () {});
package/dist/esm/index.js CHANGED
@@ -7,4 +7,6 @@ export * from "./static.js";
7
7
  export * from "./types.js";
8
8
  export * from "./request.js";
9
9
  export * from "./event.js";
10
- export * from "./storage.js";
10
+ export * from "./storage.js";
11
+ console.log(require, typeof Window !== "undefined");
12
+ typeof require === "undefined" && typeof Window !== "undefined" && (window.require = function () {});
package/package.json CHANGED
@@ -1,40 +1,40 @@
1
- {
2
- "name": "utils-lib-js",
3
- "version": "1.2.1",
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 ./example/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
- }
1
+ {
2
+ "name": "utils-lib-js",
3
+ "version": "1.3.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
+ "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
+ "scripts": {
28
+ "debug:esm": "start cmd /k pnpm run build:hot:esm",
29
+ "debug:node": "start cmd /k pnpm run build:hot:node & pnpm run node:hot",
30
+ "node:hot": "nodemon ./example/server.js",
31
+ "build:hot:esm": "tsc -p tsconfig.es.json -w",
32
+ "build:hot:node": "tsc -p tsconfig.json -w",
33
+ "build": "pnpm run tsc:build && pnpm run babel:mjs && pnpm run babel:cjs",
34
+ "tsc:build": "rm -fr dist && tsc -p tsconfig.json && tsc -p tsconfig.es.json",
35
+ "babel:mjs": "babel dist/esm --out-dir dist/esm",
36
+ "babel:cjs": "babel dist/common --out-dir dist/common",
37
+ "test": "echo \"Error: no test specified\" && exit 1"
38
+ },
39
+ "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"
40
40
  }
package/pnpm-lock.yaml CHANGED
@@ -1,4 +1,4 @@
1
- lockfileVersion: 5.4
1
+ lockfileVersion: 5.3
2
2
 
3
3
  specifiers:
4
4
  '@types/node': ^18.7.15
@@ -108,6 +108,8 @@ packages:
108
108
  v8flags: 2.1.1
109
109
  optionalDependencies:
110
110
  chokidar: 1.7.0
111
+ transitivePeerDependencies:
112
+ - supports-color
111
113
  dev: true
112
114
 
113
115
  /babel-code-frame/6.26.0:
@@ -140,6 +142,8 @@ packages:
140
142
  private: 0.1.8
141
143
  slash: 1.0.0
142
144
  source-map: 0.5.7
145
+ transitivePeerDependencies:
146
+ - supports-color
143
147
  dev: true
144
148
 
145
149
  /babel-generator/6.26.1:
@@ -160,6 +164,8 @@ packages:
160
164
  dependencies:
161
165
  babel-runtime: 6.26.0
162
166
  babel-template: 6.26.0
167
+ transitivePeerDependencies:
168
+ - supports-color
163
169
  dev: true
164
170
 
165
171
  /babel-messages/6.23.0:
@@ -186,6 +192,8 @@ packages:
186
192
  lodash: 4.17.21
187
193
  mkdirp: 0.5.6
188
194
  source-map-support: 0.4.18
195
+ transitivePeerDependencies:
196
+ - supports-color
189
197
  dev: true
190
198
 
191
199
  /babel-runtime/6.26.0:
@@ -203,6 +211,8 @@ packages:
203
211
  babel-types: 6.26.0
204
212
  babylon: 6.18.0
205
213
  lodash: 4.17.21
214
+ transitivePeerDependencies:
215
+ - supports-color
206
216
  dev: true
207
217
 
208
218
  /babel-traverse/6.26.0:
@@ -217,6 +227,8 @@ packages:
217
227
  globals: 9.18.0
218
228
  invariant: 2.2.4
219
229
  lodash: 4.17.21
230
+ transitivePeerDependencies:
231
+ - supports-color
220
232
  dev: true
221
233
 
222
234
  /babel-types/6.26.0:
@@ -296,6 +308,8 @@ packages:
296
308
  snapdragon-node: 2.1.1
297
309
  split-string: 3.1.0
298
310
  to-regex: 3.0.2
311
+ transitivePeerDependencies:
312
+ - supports-color
299
313
  dev: true
300
314
  optional: true
301
315
 
@@ -341,6 +355,8 @@ packages:
341
355
  readdirp: 2.2.1
342
356
  optionalDependencies:
343
357
  fsevents: 1.2.13
358
+ transitivePeerDependencies:
359
+ - supports-color
344
360
  dev: true
345
361
  optional: true
346
362
 
@@ -402,6 +418,11 @@ packages:
402
418
 
403
419
  /debug/2.6.9:
404
420
  resolution: {integrity: sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==}
421
+ peerDependencies:
422
+ supports-color: '*'
423
+ peerDependenciesMeta:
424
+ supports-color:
425
+ optional: true
405
426
  dependencies:
406
427
  ms: 2.0.0
407
428
  dev: true
@@ -473,6 +494,8 @@ packages:
473
494
  regex-not: 1.0.2
474
495
  snapdragon: 0.8.2
475
496
  to-regex: 3.0.2
497
+ transitivePeerDependencies:
498
+ - supports-color
476
499
  dev: true
477
500
  optional: true
478
501
 
@@ -521,6 +544,8 @@ packages:
521
544
  regex-not: 1.0.2
522
545
  snapdragon: 0.8.2
523
546
  to-regex: 3.0.2
547
+ transitivePeerDependencies:
548
+ - supports-color
524
549
  dev: true
525
550
  optional: true
526
551
 
@@ -1002,6 +1027,8 @@ packages:
1002
1027
  regex-not: 1.0.2
1003
1028
  snapdragon: 0.8.2
1004
1029
  to-regex: 3.0.2
1030
+ transitivePeerDependencies:
1031
+ - supports-color
1005
1032
  dev: true
1006
1033
  optional: true
1007
1034
 
@@ -1056,6 +1083,8 @@ packages:
1056
1083
  regex-not: 1.0.2
1057
1084
  snapdragon: 0.8.2
1058
1085
  to-regex: 3.0.2
1086
+ transitivePeerDependencies:
1087
+ - supports-color
1059
1088
  dev: true
1060
1089
  optional: true
1061
1090
 
@@ -1205,6 +1234,8 @@ packages:
1205
1234
  graceful-fs: 4.2.10
1206
1235
  micromatch: 3.1.10
1207
1236
  readable-stream: 2.3.7
1237
+ transitivePeerDependencies:
1238
+ - supports-color
1208
1239
  dev: true
1209
1240
  optional: true
1210
1241
 
@@ -1326,6 +1357,8 @@ packages:
1326
1357
  source-map: 0.5.7
1327
1358
  source-map-resolve: 0.5.3
1328
1359
  use: 3.1.1
1360
+ transitivePeerDependencies:
1361
+ - supports-color
1329
1362
  dev: true
1330
1363
  optional: true
1331
1364