@sybz-components/utils 0.0.12 → 1.0.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/day.d.mts DELETED
@@ -1,81 +0,0 @@
1
- import { ConfigType, QUnitType, OpUnitType } from 'dayjs';
2
-
3
- type DayUnit = QUnitType | OpUnitType;
4
- /**
5
- * 使用 `dayjs` 格式化日期。
6
- *
7
- * @param date 要格式化的日期值。为空时默认使用当前时间。
8
- * @param format 输出格式,默认 `YYYY-MM-DD HH:mm:ss`。
9
- * @returns 格式化后的日期字符串。
10
- *
11
- * @example
12
- * formatDate('2026-04-24 12:30:45')
13
- * // => '2026-04-24 12:30:45'
14
- *
15
- * @example
16
- * formatDate('2026-04-24 12:30:45', 'YYYY/MM/DD')
17
- * // => '2026/04/24'
18
- */
19
- declare function formatDate(date?: ConfigType, format?: string): string;
20
- /**
21
- * 将日期格式化为 `YYYY-MM-DD`。
22
- *
23
- * @param date 要格式化的日期值。
24
- * @returns 形如 `2026-04-24` 的字符串。
25
- *
26
- * @example
27
- * formatDateToDay(new Date())
28
- *
29
- * @example
30
- * formatDateToDay('2026-04-24 12:30:45')
31
- * // => '2026-04-24'
32
- */
33
- declare const formatDateToDay: (date: ConfigType) => string;
34
- /**
35
- * 将日期格式化为 `YYYY-MM-DD HH:mm`。
36
- *
37
- * @param date 要格式化的日期值。
38
- * @returns 形如 `2026-04-24 12:30` 的字符串。
39
- *
40
- * @example
41
- * formatDateToMinute(new Date())
42
- *
43
- * @example
44
- * formatDateToMinute('2026-04-24 12:30:45')
45
- * // => '2026-04-24 12:30'
46
- */
47
- declare const formatDateToMinute: (date: ConfigType) => string;
48
- /**
49
- * 计算两个日期之间的差值。
50
- *
51
- * @param date1 起始时间。未传时返回 `undefined`。
52
- * @param date2 对比时间,默认取当前时间。
53
- * @param format 差值单位,默认 `second`。
54
- * @returns 两个时间的差值。
55
- *
56
- * @example
57
- * diffDate('2026-04-24 12:00:00', '2026-04-24 11:59:00')
58
- * // => 60
59
- *
60
- * @example
61
- * diffDate('2026-04-24 12:00:00', '2026-04-24 11:30:00', 'minute')
62
- * // => 30
63
- */
64
- declare function diffDate(date1: ConfigType, date2?: ConfigType, format?: DayUnit): number | undefined;
65
- /**
66
- * 将秒数转换成“几秒前 / 几分钟前 / 几小时前 / 几天前”这类相对时间文案。
67
- *
68
- * @param second 距离当前时间的秒数。
69
- * @returns 中文相对时间描述。
70
- *
71
- * @example
72
- * diffDateFromCurrent(45)
73
- * // => '45秒前'
74
- *
75
- * @example
76
- * diffDateFromCurrent(3600)
77
- * // => '1小时前'
78
- */
79
- declare function diffDateFromCurrent(second: number): string;
80
-
81
- export { diffDate, diffDateFromCurrent, formatDate, formatDateToDay, formatDateToMinute };
package/dist/day.d.ts DELETED
@@ -1,81 +0,0 @@
1
- import { ConfigType, QUnitType, OpUnitType } from 'dayjs';
2
-
3
- type DayUnit = QUnitType | OpUnitType;
4
- /**
5
- * 使用 `dayjs` 格式化日期。
6
- *
7
- * @param date 要格式化的日期值。为空时默认使用当前时间。
8
- * @param format 输出格式,默认 `YYYY-MM-DD HH:mm:ss`。
9
- * @returns 格式化后的日期字符串。
10
- *
11
- * @example
12
- * formatDate('2026-04-24 12:30:45')
13
- * // => '2026-04-24 12:30:45'
14
- *
15
- * @example
16
- * formatDate('2026-04-24 12:30:45', 'YYYY/MM/DD')
17
- * // => '2026/04/24'
18
- */
19
- declare function formatDate(date?: ConfigType, format?: string): string;
20
- /**
21
- * 将日期格式化为 `YYYY-MM-DD`。
22
- *
23
- * @param date 要格式化的日期值。
24
- * @returns 形如 `2026-04-24` 的字符串。
25
- *
26
- * @example
27
- * formatDateToDay(new Date())
28
- *
29
- * @example
30
- * formatDateToDay('2026-04-24 12:30:45')
31
- * // => '2026-04-24'
32
- */
33
- declare const formatDateToDay: (date: ConfigType) => string;
34
- /**
35
- * 将日期格式化为 `YYYY-MM-DD HH:mm`。
36
- *
37
- * @param date 要格式化的日期值。
38
- * @returns 形如 `2026-04-24 12:30` 的字符串。
39
- *
40
- * @example
41
- * formatDateToMinute(new Date())
42
- *
43
- * @example
44
- * formatDateToMinute('2026-04-24 12:30:45')
45
- * // => '2026-04-24 12:30'
46
- */
47
- declare const formatDateToMinute: (date: ConfigType) => string;
48
- /**
49
- * 计算两个日期之间的差值。
50
- *
51
- * @param date1 起始时间。未传时返回 `undefined`。
52
- * @param date2 对比时间,默认取当前时间。
53
- * @param format 差值单位,默认 `second`。
54
- * @returns 两个时间的差值。
55
- *
56
- * @example
57
- * diffDate('2026-04-24 12:00:00', '2026-04-24 11:59:00')
58
- * // => 60
59
- *
60
- * @example
61
- * diffDate('2026-04-24 12:00:00', '2026-04-24 11:30:00', 'minute')
62
- * // => 30
63
- */
64
- declare function diffDate(date1: ConfigType, date2?: ConfigType, format?: DayUnit): number | undefined;
65
- /**
66
- * 将秒数转换成“几秒前 / 几分钟前 / 几小时前 / 几天前”这类相对时间文案。
67
- *
68
- * @param second 距离当前时间的秒数。
69
- * @returns 中文相对时间描述。
70
- *
71
- * @example
72
- * diffDateFromCurrent(45)
73
- * // => '45秒前'
74
- *
75
- * @example
76
- * diffDateFromCurrent(3600)
77
- * // => '1小时前'
78
- */
79
- declare function diffDateFromCurrent(second: number): string;
80
-
81
- export { diffDate, diffDateFromCurrent, formatDate, formatDateToDay, formatDateToMinute };
package/dist/day.mjs DELETED
@@ -1,31 +0,0 @@
1
- import dayjs from 'dayjs';
2
-
3
- function formatDate(date, format = "YYYY-MM-DD HH:mm:ss") {
4
- return dayjs(date || /* @__PURE__ */ new Date()).format(format);
5
- }
6
- const formatDateToDay = (date) => formatDate(date, "YYYY-MM-DD");
7
- const formatDateToMinute = (date) => formatDate(date, "YYYY-MM-DD HH:mm");
8
- function diffDate(date1, date2 = dayjs(), format = "second") {
9
- if (!date1) return;
10
- return dayjs(date1).diff(dayjs(date2), format);
11
- }
12
- function diffDateFromCurrent(second) {
13
- if (second >= 60 * 60 * 24 * 365) {
14
- return `${parseInt(String(second / (60 * 60 * 24 * 365)))}\u5E74\u524D`;
15
- }
16
- if (second >= 60 * 60 * 24 * 30) {
17
- return `${parseInt(String(second / (60 * 60 * 24 * 30)))}\u6708\u524D`;
18
- }
19
- if (second >= 60 * 60 * 24) {
20
- return `${parseInt(String(second / (60 * 60 * 24)))}\u5929\u524D`;
21
- }
22
- if (second >= 60 * 60) {
23
- return `${parseInt(String(second / (60 * 60)))}\u5C0F\u65F6\u524D`;
24
- }
25
- if (second >= 60) {
26
- return `${parseInt(String(second / 60))}\u5206\u949F\u524D`;
27
- }
28
- return `${parseInt(String(second))}\u79D2\u524D`;
29
- }
30
-
31
- export { diffDate, diffDateFromCurrent, formatDate, formatDateToDay, formatDateToMinute };
package/dist/ws.cjs DELETED
@@ -1,175 +0,0 @@
1
- 'use strict';
2
-
3
- const qs = require('qs');
4
-
5
- function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
6
-
7
- const qs__default = /*#__PURE__*/_interopDefaultCompat(qs);
8
-
9
- const reconnectMaxCount = 3;
10
- const message = "ping";
11
- const interval = 3e3;
12
- const timeout = 1e3;
13
- class WS {
14
- url;
15
- socket = null;
16
- reconnectCount = 0;
17
- delay = null;
18
- timer = null;
19
- autoReconnect;
20
- heartbeat;
21
- query;
22
- /**
23
- * 创建并立即连接一个 WebSocket 实例。
24
- *
25
- * @param url WebSocket 地址,例如 `wss://example.com/ws`。
26
- * @param options 连接配置,支持重连、心跳和 query 参数。
27
- *
28
- * @example
29
- * const ws = new WS('wss://example.com/ws', {
30
- * autoReconnect: { reconnectMaxCount: 5 },
31
- * heartbeat: { message: 'ping', interval: 5000 },
32
- * query: { token: 'abc123' },
33
- * })
34
- */
35
- constructor(url, options) {
36
- const { autoReconnect = true, query = {}, heartbeat = false } = options || {};
37
- this.autoReconnect = autoReconnect;
38
- this.heartbeat = heartbeat;
39
- this.query = query;
40
- this.url = `${url}` + qs__default.stringify({ ...this.query }, { addQueryPrefix: true });
41
- this.connect();
42
- }
43
- /**
44
- * 主动发起连接。
45
- *
46
- * 调用时会先关闭旧连接,再创建新的 WebSocket 实例。
47
- *
48
- * @example
49
- * const ws = new WS('wss://example.com/ws')
50
- * ws.connect()
51
- */
52
- connect() {
53
- this.close();
54
- this.socket = new WebSocket(this.url);
55
- this.onError();
56
- this.onOpen();
57
- }
58
- /**
59
- * 注册连接成功后的处理逻辑,并在需要时开启心跳。
60
- *
61
- * @example
62
- * const ws = new WS('wss://example.com/ws')
63
- * ws.onOpen()
64
- */
65
- onOpen() {
66
- if (this.socket) {
67
- this.socket.onopen = () => {
68
- this.send("ping");
69
- this.heartbeat && this.startHeartbeat();
70
- };
71
- }
72
- }
73
- /**
74
- * 开启心跳发送。
75
- *
76
- * @example
77
- * const ws = new WS('wss://example.com/ws', {
78
- * heartbeat: { message: 'ping', interval: 3000 },
79
- * })
80
- * ws.startHeartbeat()
81
- */
82
- startHeartbeat() {
83
- const msg = this.heartbeat?.message || message;
84
- const int = this.heartbeat?.interval || interval;
85
- this.timer = setInterval(() => {
86
- this.send(msg);
87
- }, int);
88
- }
89
- /**
90
- * 注册错误处理和自动重连逻辑。
91
- *
92
- * @example
93
- * const ws = new WS('wss://example.com/ws', { autoReconnect: true })
94
- * ws.onError()
95
- */
96
- onError() {
97
- if (this.socket) {
98
- this.socket.onerror = () => {
99
- const count = this.autoReconnect?.reconnectMaxCount || reconnectMaxCount;
100
- if (this.autoReconnect && this.reconnectCount < count) {
101
- this.reconnectCount++;
102
- this.connect();
103
- }
104
- };
105
- }
106
- }
107
- /**
108
- * 关闭连接并清理相关定时器。
109
- *
110
- * @example
111
- * ws.close()
112
- */
113
- close() {
114
- this.socket && this.socket.close();
115
- this.delay && clearTimeout(this.delay);
116
- this.timer && clearInterval(this.timer);
117
- this.socket = null;
118
- }
119
- /**
120
- * 监听服务端消息。
121
- *
122
- * 内部会优先尝试将消息解析为 JSON;解析失败时会回传原始 `MessageEvent`。
123
- *
124
- * @param callback 接收到消息时触发的回调。
125
- *
126
- * @example
127
- * ws.onMessage((message) => {
128
- * console.log(message)
129
- * })
130
- */
131
- onMessage(callback) {
132
- if (this.socket) {
133
- this.socket.onmessage = (data) => {
134
- try {
135
- const res = JSON.parse(data.data);
136
- callback(res);
137
- } catch {
138
- callback(data);
139
- }
140
- };
141
- }
142
- }
143
- /**
144
- * 发送消息。
145
- *
146
- * - 如果连接已就绪,会立即发送
147
- * - 如果正在连接,会延迟发送
148
- * - 如果连接已关闭,会先重新连接再发送
149
- *
150
- * @param data 需要发送的消息内容。
151
- *
152
- * @example
153
- * ws.send('hello')
154
- *
155
- * @example
156
- * ws.send(JSON.stringify({ type: 'ping' }))
157
- */
158
- send(data) {
159
- if (!this.socket) return;
160
- if (this.socket.readyState === this.socket.OPEN) {
161
- this.socket.send(JSON.stringify(data));
162
- } else if (this.socket.readyState === this.socket.CONNECTING) {
163
- this.delay = setTimeout(() => {
164
- this.socket?.send(data);
165
- }, timeout);
166
- } else {
167
- this.connect();
168
- this.delay = setTimeout(() => {
169
- this.socket?.send(data);
170
- }, timeout);
171
- }
172
- }
173
- }
174
-
175
- exports.WS = WS;
package/dist/ws.d.cts DELETED
@@ -1,165 +0,0 @@
1
- type Timeout = ReturnType<typeof setTimeout>;
2
- type Interval = ReturnType<typeof setInterval>;
3
- type Nullable<T> = T | null;
4
- /**
5
- * WebSocket 自动重连配置。
6
- */
7
- interface WSAutoReconnectOptions {
8
- /**
9
- * 最大重连次数,默认 `3`。
10
- */
11
- reconnectMaxCount?: number;
12
- }
13
- /**
14
- * WebSocket 心跳配置。
15
- */
16
- interface WSHeartbeatOptions {
17
- /**
18
- * 心跳消息内容,默认 `ping`。
19
- */
20
- message: string;
21
- /**
22
- * 心跳间隔,单位毫秒,默认 `3000`。
23
- */
24
- interval: number;
25
- }
26
- /**
27
- * `WS` 构造函数可选配置。
28
- */
29
- interface WSOptions {
30
- /**
31
- * 是否自动重连。
32
- *
33
- * - `true`: 使用默认重连策略
34
- * - `false`: 不自动重连
35
- * - 对象: 自定义重连次数
36
- */
37
- autoReconnect: boolean | WSAutoReconnectOptions;
38
- /**
39
- * 是否开启心跳。
40
- *
41
- * - `false`: 不发送心跳
42
- * - `true`: 使用默认心跳策略
43
- * - 对象: 自定义心跳内容和间隔
44
- */
45
- heartbeat: boolean | WSHeartbeatOptions;
46
- /**
47
- * 需要拼接到 url 上的查询参数。
48
- */
49
- query: Record<string, string>;
50
- }
51
- /**
52
- * WebSocket 轻量封装,支持自动连接、自动重连、心跳和 query 参数。
53
- *
54
- * @example
55
- * const ws = new WS('wss://example.com/ws', {
56
- * autoReconnect: true,
57
- * heartbeat: { message: 'ping', interval: 3000 },
58
- * query: { token: 'demo-token' },
59
- * })
60
- *
61
- * ws.onMessage((message) => {
62
- * console.log(message)
63
- * })
64
- *
65
- * ws.send('hello')
66
- */
67
- declare class WS {
68
- url: string;
69
- socket: WebSocket | null;
70
- reconnectCount: number;
71
- delay: Nullable<Timeout>;
72
- timer: Nullable<Interval>;
73
- autoReconnect: WSOptions['autoReconnect'];
74
- heartbeat: WSOptions['heartbeat'];
75
- query: WSOptions['query'];
76
- /**
77
- * 创建并立即连接一个 WebSocket 实例。
78
- *
79
- * @param url WebSocket 地址,例如 `wss://example.com/ws`。
80
- * @param options 连接配置,支持重连、心跳和 query 参数。
81
- *
82
- * @example
83
- * const ws = new WS('wss://example.com/ws', {
84
- * autoReconnect: { reconnectMaxCount: 5 },
85
- * heartbeat: { message: 'ping', interval: 5000 },
86
- * query: { token: 'abc123' },
87
- * })
88
- */
89
- constructor(url?: string, options?: WSOptions);
90
- /**
91
- * 主动发起连接。
92
- *
93
- * 调用时会先关闭旧连接,再创建新的 WebSocket 实例。
94
- *
95
- * @example
96
- * const ws = new WS('wss://example.com/ws')
97
- * ws.connect()
98
- */
99
- connect(): void;
100
- /**
101
- * 注册连接成功后的处理逻辑,并在需要时开启心跳。
102
- *
103
- * @example
104
- * const ws = new WS('wss://example.com/ws')
105
- * ws.onOpen()
106
- */
107
- onOpen(): void;
108
- /**
109
- * 开启心跳发送。
110
- *
111
- * @example
112
- * const ws = new WS('wss://example.com/ws', {
113
- * heartbeat: { message: 'ping', interval: 3000 },
114
- * })
115
- * ws.startHeartbeat()
116
- */
117
- startHeartbeat(): void;
118
- /**
119
- * 注册错误处理和自动重连逻辑。
120
- *
121
- * @example
122
- * const ws = new WS('wss://example.com/ws', { autoReconnect: true })
123
- * ws.onError()
124
- */
125
- onError(): void;
126
- /**
127
- * 关闭连接并清理相关定时器。
128
- *
129
- * @example
130
- * ws.close()
131
- */
132
- close(): void;
133
- /**
134
- * 监听服务端消息。
135
- *
136
- * 内部会优先尝试将消息解析为 JSON;解析失败时会回传原始 `MessageEvent`。
137
- *
138
- * @param callback 接收到消息时触发的回调。
139
- *
140
- * @example
141
- * ws.onMessage((message) => {
142
- * console.log(message)
143
- * })
144
- */
145
- onMessage(callback: (data: unknown) => any): void;
146
- /**
147
- * 发送消息。
148
- *
149
- * - 如果连接已就绪,会立即发送
150
- * - 如果正在连接,会延迟发送
151
- * - 如果连接已关闭,会先重新连接再发送
152
- *
153
- * @param data 需要发送的消息内容。
154
- *
155
- * @example
156
- * ws.send('hello')
157
- *
158
- * @example
159
- * ws.send(JSON.stringify({ type: 'ping' }))
160
- */
161
- send(data: string | ArrayBufferLike | Blob | ArrayBufferView): void;
162
- }
163
-
164
- export { WS };
165
- export type { WSAutoReconnectOptions, WSHeartbeatOptions, WSOptions };
package/dist/ws.d.mts DELETED
@@ -1,165 +0,0 @@
1
- type Timeout = ReturnType<typeof setTimeout>;
2
- type Interval = ReturnType<typeof setInterval>;
3
- type Nullable<T> = T | null;
4
- /**
5
- * WebSocket 自动重连配置。
6
- */
7
- interface WSAutoReconnectOptions {
8
- /**
9
- * 最大重连次数,默认 `3`。
10
- */
11
- reconnectMaxCount?: number;
12
- }
13
- /**
14
- * WebSocket 心跳配置。
15
- */
16
- interface WSHeartbeatOptions {
17
- /**
18
- * 心跳消息内容,默认 `ping`。
19
- */
20
- message: string;
21
- /**
22
- * 心跳间隔,单位毫秒,默认 `3000`。
23
- */
24
- interval: number;
25
- }
26
- /**
27
- * `WS` 构造函数可选配置。
28
- */
29
- interface WSOptions {
30
- /**
31
- * 是否自动重连。
32
- *
33
- * - `true`: 使用默认重连策略
34
- * - `false`: 不自动重连
35
- * - 对象: 自定义重连次数
36
- */
37
- autoReconnect: boolean | WSAutoReconnectOptions;
38
- /**
39
- * 是否开启心跳。
40
- *
41
- * - `false`: 不发送心跳
42
- * - `true`: 使用默认心跳策略
43
- * - 对象: 自定义心跳内容和间隔
44
- */
45
- heartbeat: boolean | WSHeartbeatOptions;
46
- /**
47
- * 需要拼接到 url 上的查询参数。
48
- */
49
- query: Record<string, string>;
50
- }
51
- /**
52
- * WebSocket 轻量封装,支持自动连接、自动重连、心跳和 query 参数。
53
- *
54
- * @example
55
- * const ws = new WS('wss://example.com/ws', {
56
- * autoReconnect: true,
57
- * heartbeat: { message: 'ping', interval: 3000 },
58
- * query: { token: 'demo-token' },
59
- * })
60
- *
61
- * ws.onMessage((message) => {
62
- * console.log(message)
63
- * })
64
- *
65
- * ws.send('hello')
66
- */
67
- declare class WS {
68
- url: string;
69
- socket: WebSocket | null;
70
- reconnectCount: number;
71
- delay: Nullable<Timeout>;
72
- timer: Nullable<Interval>;
73
- autoReconnect: WSOptions['autoReconnect'];
74
- heartbeat: WSOptions['heartbeat'];
75
- query: WSOptions['query'];
76
- /**
77
- * 创建并立即连接一个 WebSocket 实例。
78
- *
79
- * @param url WebSocket 地址,例如 `wss://example.com/ws`。
80
- * @param options 连接配置,支持重连、心跳和 query 参数。
81
- *
82
- * @example
83
- * const ws = new WS('wss://example.com/ws', {
84
- * autoReconnect: { reconnectMaxCount: 5 },
85
- * heartbeat: { message: 'ping', interval: 5000 },
86
- * query: { token: 'abc123' },
87
- * })
88
- */
89
- constructor(url?: string, options?: WSOptions);
90
- /**
91
- * 主动发起连接。
92
- *
93
- * 调用时会先关闭旧连接,再创建新的 WebSocket 实例。
94
- *
95
- * @example
96
- * const ws = new WS('wss://example.com/ws')
97
- * ws.connect()
98
- */
99
- connect(): void;
100
- /**
101
- * 注册连接成功后的处理逻辑,并在需要时开启心跳。
102
- *
103
- * @example
104
- * const ws = new WS('wss://example.com/ws')
105
- * ws.onOpen()
106
- */
107
- onOpen(): void;
108
- /**
109
- * 开启心跳发送。
110
- *
111
- * @example
112
- * const ws = new WS('wss://example.com/ws', {
113
- * heartbeat: { message: 'ping', interval: 3000 },
114
- * })
115
- * ws.startHeartbeat()
116
- */
117
- startHeartbeat(): void;
118
- /**
119
- * 注册错误处理和自动重连逻辑。
120
- *
121
- * @example
122
- * const ws = new WS('wss://example.com/ws', { autoReconnect: true })
123
- * ws.onError()
124
- */
125
- onError(): void;
126
- /**
127
- * 关闭连接并清理相关定时器。
128
- *
129
- * @example
130
- * ws.close()
131
- */
132
- close(): void;
133
- /**
134
- * 监听服务端消息。
135
- *
136
- * 内部会优先尝试将消息解析为 JSON;解析失败时会回传原始 `MessageEvent`。
137
- *
138
- * @param callback 接收到消息时触发的回调。
139
- *
140
- * @example
141
- * ws.onMessage((message) => {
142
- * console.log(message)
143
- * })
144
- */
145
- onMessage(callback: (data: unknown) => any): void;
146
- /**
147
- * 发送消息。
148
- *
149
- * - 如果连接已就绪,会立即发送
150
- * - 如果正在连接,会延迟发送
151
- * - 如果连接已关闭,会先重新连接再发送
152
- *
153
- * @param data 需要发送的消息内容。
154
- *
155
- * @example
156
- * ws.send('hello')
157
- *
158
- * @example
159
- * ws.send(JSON.stringify({ type: 'ping' }))
160
- */
161
- send(data: string | ArrayBufferLike | Blob | ArrayBufferView): void;
162
- }
163
-
164
- export { WS };
165
- export type { WSAutoReconnectOptions, WSHeartbeatOptions, WSOptions };