@ajaxjs/util 1.1.2 → 1.2.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.
package/package.json CHANGED
@@ -1,39 +1,41 @@
1
- {
2
- "name": "@ajaxjs/util",
3
- "version": "1.1.2",
4
- "description": "Core JS Utils",
5
- "scripts": {
6
- "dev": "rollup -w -c",
7
- "build": "rollup -c"
8
- },
9
- "main": "dist/index.umd.js",
10
- "module": "dist/index.esm.js",
11
- "types": "dist/index.d.ts",
12
- "type": "module",
13
- "files": [
14
- "src/",
15
- "dist/"
16
- ],
17
- "repository": {
18
- "type": "git",
19
- "url": "https://github.com/lightweight-component/js"
20
- },
21
- "keywords": [
22
- "ajaxjs",
23
- "util"
24
- ],
25
- "author": "Frank Cheung <frank@ajaxjs.com>",
26
- "license": "Apache License 2.0",
27
- "devDependencies": {
28
- "@eslint/js": "^9.9.1",
29
- "@rollup/plugin-commonjs": "^26.0.1",
30
- "@rollup/plugin-node-resolve": "^15.2.3",
31
- "@rollup/plugin-typescript": "^11.1.6",
32
- "eslint": "^9.9.1",
33
- "globals": "^15.9.0",
34
- "rollup": "^4.21.2",
35
- "tslib": "^2.7.0",
36
- "typescript": "^5.5.4",
37
- "typescript-eslint": "^8.4.0"
38
- }
39
- }
1
+ {
2
+ "name": "@ajaxjs/util",
3
+ "version": "1.2.0",
4
+ "description": "Core JS Utils",
5
+ "scripts": {
6
+ "dev": "rollup -w -c",
7
+ "build": "rollup -c"
8
+ },
9
+ "main": "dist/index.umd.js",
10
+ "module": "dist/index.esm.js",
11
+ "types": "dist/src/index.d.ts",
12
+ "type": "module",
13
+ "files": [
14
+ "dist/"
15
+ ],
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "https://github.com/lightweight-component/js/tree/main/util"
19
+ },
20
+ "bugs": {
21
+ "url": "https://github.com/lightweight-component/js/issues"
22
+ },
23
+ "keywords": [
24
+ "ajaxjs",
25
+ "util"
26
+ ],
27
+ "author": "Frank Cheung <frank@ajaxjs.com>",
28
+ "license": "Apache License 2.0",
29
+ "devDependencies": {
30
+ "@eslint/js": "^9.9.1",
31
+ "@rollup/plugin-commonjs": "^26.0.1",
32
+ "@rollup/plugin-node-resolve": "^15.2.3",
33
+ "@rollup/plugin-typescript": "^11.1.6",
34
+ "eslint": "^9.9.1",
35
+ "globals": "^15.9.0",
36
+ "rollup": "^4.21.2",
37
+ "tslib": "^2.7.0",
38
+ "typescript": "^5.5.4",
39
+ "typescript-eslint": "^8.4.0"
40
+ }
41
+ }
@@ -1,18 +0,0 @@
1
- /**
2
- * 获取某个 Cookie
3
- *
4
- * @param name 键名称
5
- * @returns 值
6
- */
7
- export declare function getCookie(name: string): string | null;
8
- /**
9
- * 设置某个 Cookie
10
- *
11
- * @param name 键名称
12
- * @param value 值
13
- */
14
- export declare function setCookie(name: string, value: string): void;
15
- /**
16
- * 清空 Cookie
17
- */
18
- export declare function delCookie(): void;
@@ -1,17 +0,0 @@
1
- /**
2
- * 向父级元素递归搜索
3
- *
4
- * @param _el 当前所在元素
5
- * @param tagName 目标标签名称
6
- * @param className 目标元素样式类
7
- * @returns 目标元素,找不到为 null
8
- */
9
- export declare function up(_el: Element, tagName: string, className: string): Element | null;
10
- /**
11
- * 加载脚本
12
- *
13
- * @param url 脚本地址
14
- * @param id 脚本元素 id,可选的
15
- * @param cb 回调函数,可选的
16
- */
17
- export declare function loadScript(url: string, id?: string, cb?: (ev: Event) => any): void;
@@ -1,51 +0,0 @@
1
- /**
2
- * 通用工具类
3
- */
4
- /**
5
- * 是否调试模式中
6
- *
7
- * 打包成组件之后不能用
8
- *
9
- * @returns
10
- */
11
- export declare function isDebug(): boolean;
12
- export declare function isDev(): boolean;
13
- /**
14
- * 日期格式化。详见博客文章:http://blog.csdn.net/zhangxin09/archive/2011/01/01/6111294.aspx
15
- * e.g: new Date().format("yyyy-MM-dd hh:mm:ss")
16
- *
17
- * @param {String} format
18
- * @return {String}
19
- */
20
- export declare function dateFormat(this: Date, format: string): string;
21
- /**
22
- * 日期格式化
23
- * @author meizz
24
- * @param date 日期,必须为 Date 类型
25
- * @param fmt 格式模板
26
- * @returns 格式化后的字符串
27
- */
28
- export declare function dateFormat2(date: Date, fmt: string): string;
29
- /**
30
- * 并行和串行任务
31
- *
32
- * @author https://segmentfault.com/a/1190000013265925
33
- * @param arr
34
- * @param finnaly
35
- */
36
- export declare function parallel(arr: [], _finally: Function): void;
37
- /**
38
- * 函数节流
39
- *
40
- * @author https://www.cnblogs.com/moqiutao/p/6875955.html
41
- * @param fn
42
- * @param delay
43
- * @param mustRunDelay
44
- */
45
- export declare function throttle(fn: Function, delay: number, mustRunDelay: number): Function;
46
- /**
47
- * 复制文字到剪切板
48
- *
49
- * @param {string} text
50
- */
51
- export declare function copyToClipboard(text: string): void;
@@ -1,22 +0,0 @@
1
- /**
2
- * XHR 请求配置
3
- */
4
- export interface XhrConfig {
5
- /**
6
- * 请求头里面的 Content-Type 字段
7
- */
8
- contentType?: string;
9
- /**
10
- * XHR 响应结果的类型
11
- */
12
- parseContentType?: 'text' | 'xml' | 'json';
13
- /**
14
- * 超时时间,单位是毫秒
15
- * 设为 0 适合不控制超时
16
- */
17
- timeout?: number;
18
- /**
19
- * 是否跨域
20
- */
21
- withCredentials?: boolean;
22
- }
@@ -1,71 +0,0 @@
1
- import { XhrConfig } from './xhr-config';
2
- /**
3
- * 处理响应的回调函数
4
- */
5
- type XhrCallback = (json: {}, text: string) => void;
6
- /**
7
- * 设置全局请求的 head 参数
8
- *
9
- * @param param
10
- */
11
- export declare function setBaseHeadParams(params: any): void;
12
- /**
13
- *
14
- * @param url
15
- * @param cb
16
- * @param params
17
- * @param cfg
18
- */
19
- export declare function xhr_post_upload(url: string, cb: XhrCallback, params: Document | XMLHttpRequestBodyInit, cfg?: XhrConfig): void;
20
- /**
21
- * XHR GET 请求
22
- *
23
- * @param url 请求地址
24
- * @param cb 回调函数 @example (json: {}, text: string) => void;
25
- * @param params 参数,必填,如无填空字符串 "";参数类型是json;参数值会进行 URL 编码,最后附加到 QueryString 中
26
- * @param cfg 配置,可选的
27
- */
28
- export declare function xhr_get(url: string, cb: XhrCallback, params?: {}, cfg?: XhrConfig): void;
29
- /**
30
- * XHR DELETE 请求
31
- *
32
- * @param url 请求地址
33
- * @param cb 回调函数 @example (json: {}, text: string) => void;
34
- * @param params 参数,必填,如无填空字符串 "";参数类型是json;参数值会进行 URL 编码,最后附加到 QueryString 中
35
- * @param cfg 配置,可选的
36
- */
37
- export declare function xhr_del(url: string, cb: XhrCallback, params?: {}, cfg?: XhrConfig): void;
38
- /**
39
- * XHR POST 请求
40
- *
41
- * @param url 请求地址
42
- * @param cb 回调函数 @example (json: {}, text: string) => void;
43
- * @param params 参数,必填,如无填空字符串 "";参数类型可以是字符串或 json;参数值会进行 URL 编码
44
- * @param cfg 配置,可选的
45
- */
46
- export declare function xhr_post(url: string, cb: XhrCallback, params: string | {}, cfg?: XhrConfig): void;
47
- /**
48
- * XHR PUT 请求
49
- *
50
- * @param url 请求地址
51
- * @param cb 回调函数 @example (json: {}, text: string) => void;
52
- * @param params 参数,必填,如无填空字符串 "";参数类型可以是字符串或 json;参数值会进行 URL 编码
53
- * @param cfg 配置,可选的
54
- */
55
- export declare function xhr_put(url: string, cb: XhrCallback, params: string | {}, cfg?: XhrConfig): void;
56
- /**
57
- * 对象转换为 URL 参数列表,用 & 分隔
58
- *
59
- * @param {Object} param JSON 对象
60
- * @returns URL 参数列表
61
- */
62
- export declare function toParams(param: any): string;
63
- /**
64
- * 获取 QueryString 的某个参数
65
- *
66
- * @param val
67
- * @returns
68
- */
69
- export declare function getQuery(val: string): string;
70
- export declare function getPageList(self: any, listArray: any, callback?: Function): XhrCallback;
71
- export {};
@@ -1,43 +0,0 @@
1
- /**
2
- * 获取某个 Cookie
3
- *
4
- * @param name 键名称
5
- * @returns 值
6
- */
7
- export function getCookie(name: string): string | null {
8
- let arr: RegExpMatchArray, reg: RegExp = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
9
- // @ts-ignore
10
- if (arr = document.cookie.match(reg))
11
- return unescape(arr[2]);
12
- else
13
- return null;
14
- }
15
-
16
- /**
17
- * 设置某个 Cookie
18
- *
19
- * @param name 键名称
20
- * @param value 值
21
- */
22
- export function setCookie(name: string, value: string): void {
23
- let days: number = 2, exp: Date = new Date();
24
- exp.setTime(exp.getTime() + days * 24 * 60 * 60 * 1000);
25
- // @ts-ignore
26
- document.cookie = name + "=" + escape(value) + ";path=/;expires=" + exp.toGMTString();
27
- }
28
-
29
- /**
30
- * 清空 Cookie
31
- */
32
- export function delCookie(): void {
33
- let keys = document.cookie.match(/[^ =;]+(?==)/g);
34
-
35
- if (keys) {
36
- let date: string = new Date(0).toUTCString();
37
- for (let i = keys.length; i--;) {
38
- document.cookie = keys[i] + '=0;path=/;expires=' + date; // 清除当前域名下的,例如:m.ratingdog.cn
39
- document.cookie = keys[i] + '=0;path=/;domain=' + document.domain + ';expires=' + date; // 清除当前域名下的,例如 .m.ratingdog.cn
40
- document.cookie = keys[i] + '=0;path=/;domain=' + location.host + ';expires=' + date; // 清除一级域名下的或指定的,例如 .ratingdog.cn
41
- }
42
- }
43
- }
package/src/core/dom.ts DELETED
@@ -1,47 +0,0 @@
1
- /**
2
- * 向父级元素递归搜索
3
- *
4
- * @param _el 当前所在元素
5
- * @param tagName 目标标签名称
6
- * @param className 目标元素样式类
7
- * @returns 目标元素,找不到为 null
8
- */
9
- export function up(_el: Element, tagName: string, className: string): Element | null {
10
- if (tagName && className)
11
- throw '只能任选一种参数,不能同时传';
12
-
13
- let el: Element = _el.parentNode as Element;
14
- tagName = tagName && tagName.toUpperCase();
15
-
16
- while (el) {
17
- if (tagName && el.tagName == tagName)
18
- return el;
19
-
20
- if (className && el.className && ~el.className.indexOf(className))
21
- return el;
22
-
23
- el = <Element>el.parentNode;
24
- }
25
-
26
- return null;
27
- }
28
-
29
- /**
30
- * 加载脚本
31
- *
32
- * @param url 脚本地址
33
- * @param id 脚本元素 id,可选的
34
- * @param cb 回调函数,可选的
35
- */
36
- export function loadScript(url: string, id?: string, cb?: (ev: Event) => any): void {
37
- let script: HTMLScriptElement = document.createElement("script");
38
- script.src = url;
39
-
40
- if (cb)
41
- script.onload = cb;
42
-
43
- if (id)
44
- script.id = id;
45
-
46
- document.getElementsByTagName("head")[0].appendChild(script);
47
- }
package/src/core/utils.ts DELETED
@@ -1,179 +0,0 @@
1
- /**
2
- * 通用工具类
3
- */
4
-
5
-
6
-
7
- /**
8
- * 是否调试模式中
9
- *
10
- * 打包成组件之后不能用
11
- *
12
- * @returns
13
- */
14
- export function isDebug(): boolean {
15
- // @ts-ignore
16
- return process.env.NODE_ENV === 'development';
17
- }
18
-
19
- export function isDev(): boolean {
20
- let currentHostname: string = window.location.hostname;
21
-
22
- // 判断主机名是否是内网地址
23
- return (currentHostname.startsWith('192.168.') || currentHostname.startsWith('10.') || currentHostname === 'localhost');
24
- }
25
-
26
- /**
27
- * 日期格式化。详见博客文章:http://blog.csdn.net/zhangxin09/archive/2011/01/01/6111294.aspx
28
- * e.g: new Date().format("yyyy-MM-dd hh:mm:ss")
29
- *
30
- * @param {String} format
31
- * @return {String}
32
- */
33
- export function dateFormat(this: Date, format: string): string {
34
- let $1, o: any = {
35
- "M+": this.getMonth() + 1, // 月份,从0开始算
36
- "d+": this.getDate(), // 日期
37
- "h+": this.getHours(), // 小时
38
- "m+": this.getMinutes(), // 分钟
39
- "s+": this.getSeconds(), // 秒钟
40
- // 季度 quarter
41
- "q+": Math.floor((this.getMonth() + 3) / 3),
42
- "S": this.getMilliseconds() // 千秒
43
- };
44
-
45
- if (/(y+)/.test(format))
46
- // @ts-ignore
47
- $1 = RegExp.$1, format = format.replace($1, String(this.getFullYear()).substr(4 - $1));
48
-
49
- let key: string, value: string;
50
- for (key in o) { // 如果没有指定该参数,则子字符串将延续到 stringvar 的最后。
51
- if (new RegExp("(" + key + ")").test(format)) {
52
- $1 = RegExp.$1,
53
- value = String(o[key]),
54
- value = $1.length == 1 ? value : ("00" + value).substr(value.length),
55
- format = format.replace($1, value);
56
- }
57
- }
58
-
59
- return format;
60
- }
61
-
62
- /**
63
- * 日期格式化
64
- * @author meizz
65
- * @param date 日期,必须为 Date 类型
66
- * @param fmt 格式模板
67
- * @returns 格式化后的字符串
68
- */
69
- export function dateFormat2(date: Date, fmt: string): string {
70
- let o: { [key: string]: number } = {
71
- "M+": date.getMonth() + 1, // 月份
72
- "d+": date.getDate(), // 日
73
- "h+": date.getHours(), // 小时
74
- "m+": date.getMinutes(), // 分
75
- "s+": date.getSeconds(), // 秒
76
- "q+": Math.floor((date.getMonth() + 3) / 3),// 季度
77
- "S": date.getMilliseconds() // 毫秒
78
- };
79
-
80
- if (/(y+)/.test(fmt))
81
- fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
82
-
83
- for (var k in o)
84
- if (new RegExp("(" + k + ")").test(fmt)) {
85
- let obj = (RegExp.$1.length == 1) ? o[k] : ("00" + o[k]).substr(("" + o[k]).length);
86
- // @ts-ignore
87
- fmt = fmt.replace(RegExp.$1, obj);
88
- }
89
-
90
- return fmt;
91
- }
92
-
93
- /**
94
- * 并行和串行任务
95
- *
96
- * @author https://segmentfault.com/a/1190000013265925
97
- * @param arr
98
- * @param finnaly
99
- */
100
- export function parallel(arr: [], _finally: Function) {
101
- let fn: Function, index: number = 0;
102
- // @ts-ignore
103
- let statusArr = Array(arr.length).fill().map(() => ({ isActive: false, data: null }));
104
-
105
- let isFinished = function () {
106
- return statusArr.every((item: any) => item.isActive === true);
107
- }
108
-
109
- let resolve = function (index: number): Function {
110
- return function (data: any) {
111
- statusArr[index].data = data;
112
- statusArr[index].isActive = true;
113
- let isFinish = isFinished();
114
-
115
- if (isFinish) {
116
- let datas = statusArr.map((item: any) => item.data);
117
-
118
- _finally(datas);
119
- }
120
- };
121
- };
122
-
123
- // @ts-ignore
124
- while ((fn = arr.shift())) {
125
- fn(resolve(index));// 给 resolve 函数追加参数,可以使用 bind 函数实现,这里使用了柯里化
126
- index++;
127
- }
128
- }
129
-
130
- /**
131
- * 函数节流
132
- *
133
- * @author https://www.cnblogs.com/moqiutao/p/6875955.html
134
- * @param fn
135
- * @param delay
136
- * @param mustRunDelay
137
- */
138
- export function throttle(fn: Function, delay: number, mustRunDelay: number): Function {
139
- var timer: number, t_start: number;
140
-
141
- return function () {
142
- var t_curr = +new Date();
143
- window.clearTimeout(timer);
144
-
145
- if (!t_start)
146
- t_start = t_curr;
147
-
148
- if (t_curr - t_start >= mustRunDelay) {
149
- // @ts-ignore
150
- fn.apply(this, arguments);
151
- t_start = t_curr;
152
- } else {
153
- var args = arguments;
154
- // @ts-ignore
155
- timer = window.setTimeout(() => fn.apply(this, args), delay);
156
- }
157
- };
158
- }
159
-
160
- /**
161
- * 复制文字到剪切板
162
- *
163
- * @param {string} text
164
- */
165
- export function copyToClipboard(text: string): void {
166
- if (navigator.clipboard)
167
- navigator.clipboard.writeText(text); // clipboard api 复制
168
- else {
169
- let textarea = document.createElement('textarea');
170
- document.body.appendChild(textarea); // 隐藏此输入框
171
- textarea.style.position = 'fixed';
172
- textarea.style.clip = 'rect(0 0 0 0)';
173
- textarea.style.top = '10px';
174
- textarea.value = text; // 赋值
175
- textarea.select(); // 选中
176
- document.execCommand('copy', true); // 复制
177
- document.body.removeChild(textarea); // 移除输入框
178
- }
179
- }
@@ -1,25 +0,0 @@
1
- /**
2
- * XHR 请求配置
3
- */
4
- export interface XhrConfig {
5
- /**
6
- * 请求头里面的 Content-Type 字段
7
- */
8
- contentType?: string;
9
-
10
- /**
11
- * XHR 响应结果的类型
12
- */
13
- parseContentType?: 'text' | 'xml' | 'json';
14
-
15
- /**
16
- * 超时时间,单位是毫秒
17
- * 设为 0 适合不控制超时
18
- */
19
- timeout?: number;
20
-
21
- /**
22
- * 是否跨域
23
- */
24
- withCredentials?: boolean;
25
- }