@snack-kit/lib 0.2.0 → 0.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.
- package/dist/cjs/chunk-U6KYHCBL.cjs +264 -0
- package/dist/cjs/chunk-U6KYHCBL.cjs.map +1 -0
- package/dist/cjs/index.cjs +138 -1
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/utils.cjs +144 -0
- package/dist/cjs/utils.cjs.map +1 -0
- package/dist/es/chunk-WJX5Q3WL.js +229 -0
- package/dist/es/chunk-WJX5Q3WL.js.map +1 -0
- package/dist/es/index.js +2 -1
- package/dist/es/index.js.map +1 -1
- package/dist/es/utils.js +3 -0
- package/dist/es/utils.js.map +1 -0
- package/dist/types/index.d.ts +2 -1
- package/dist/types/utils.d.ts +410 -0
- package/dist/umd/index.global.js +261 -1
- package/dist/umd/index.global.js.map +1 -1
- package/dist/umd/utils.global.js +269 -0
- package/dist/umd/utils.global.js.map +1 -0
- package/package.json +6 -1
|
@@ -0,0 +1,410 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 类型检测工具
|
|
3
|
+
*
|
|
4
|
+
* 统一使用 Object.prototype.toString 确保跨环境准确性,
|
|
5
|
+
* 规避 typeof null === 'object' 等已知陷阱。
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* 是否为数字(含可转换的字符串数字,如 '42'、'3.14')
|
|
9
|
+
* @example IsNumber(42) // true
|
|
10
|
+
* @example IsNumber('42') // true
|
|
11
|
+
* @example IsNumber('abc') // false
|
|
12
|
+
* @remarks
|
|
13
|
+
* 浏览器兼容性:Chrome 5+ · Firefox 3+ · Safari 4+ · Node.js 0.10+
|
|
14
|
+
*/
|
|
15
|
+
declare const IsNumber: (val: unknown) => boolean;
|
|
16
|
+
/**
|
|
17
|
+
* 是否为严格数字类型(不含字符串数字)
|
|
18
|
+
* @example IsRealNumber(42) // true
|
|
19
|
+
* @example IsRealNumber('42') // false
|
|
20
|
+
* @remarks
|
|
21
|
+
* 浏览器兼容性:Chrome 5+ · Firefox 3+ · Safari 4+ · Node.js 0.10+
|
|
22
|
+
*/
|
|
23
|
+
declare const IsRealNumber: (val: unknown) => val is number;
|
|
24
|
+
/**
|
|
25
|
+
* 是否为字符串
|
|
26
|
+
* @example IsString('hello') // true
|
|
27
|
+
* @example IsString(new String('hello')) // true
|
|
28
|
+
* @remarks
|
|
29
|
+
* 浏览器兼容性:Chrome 5+ · Firefox 3+ · Safari 4+ · Node.js 0.10+
|
|
30
|
+
*/
|
|
31
|
+
declare const IsString: (val: unknown) => val is string;
|
|
32
|
+
/**
|
|
33
|
+
* 是否为布尔值
|
|
34
|
+
* @example IsBoolean(true) // true
|
|
35
|
+
* @example IsBoolean(1) // false
|
|
36
|
+
* @remarks
|
|
37
|
+
* 浏览器兼容性:Chrome 5+ · Firefox 3+ · Safari 4+ · Node.js 0.10+
|
|
38
|
+
*/
|
|
39
|
+
declare const IsBoolean: (val: unknown) => val is boolean;
|
|
40
|
+
/**
|
|
41
|
+
* 是否为数组
|
|
42
|
+
* @example IsArray([1, 2]) // true
|
|
43
|
+
* @example IsArray({}) // false
|
|
44
|
+
* @remarks
|
|
45
|
+
* 浏览器兼容性:Chrome 5+ · Firefox 3.5+ · Safari 4+ · Node.js 0.10+
|
|
46
|
+
*/
|
|
47
|
+
declare const IsArray: (val: unknown) => val is unknown[];
|
|
48
|
+
/**
|
|
49
|
+
* 是否为函数
|
|
50
|
+
* @example IsFunction(() => {}) // true
|
|
51
|
+
* @remarks
|
|
52
|
+
* 浏览器兼容性:Chrome 5+ · Firefox 3+ · Safari 4+ · Node.js 0.10+
|
|
53
|
+
*/
|
|
54
|
+
declare const IsFunction: (val: unknown) => val is (...args: unknown[]) => unknown;
|
|
55
|
+
/**
|
|
56
|
+
* 是否为普通对象(排除 null 和数组)
|
|
57
|
+
* @example IsObject({}) // true
|
|
58
|
+
* @example IsObject([]) // false
|
|
59
|
+
* @example IsObject(null) // false
|
|
60
|
+
* @remarks
|
|
61
|
+
* 浏览器兼容性:Chrome 5+ · Firefox 3+ · Safari 4+ · Node.js 0.10+
|
|
62
|
+
*/
|
|
63
|
+
declare const IsObject: (val: unknown) => val is Record<string, unknown>;
|
|
64
|
+
/**
|
|
65
|
+
* 是否为空值(null / undefined / 空字符串)
|
|
66
|
+
* @example IsNull(null) // true
|
|
67
|
+
* @example IsNull(undefined) // true
|
|
68
|
+
* @example IsNull('') // true
|
|
69
|
+
* @example IsNull(0) // false
|
|
70
|
+
* @remarks
|
|
71
|
+
* 浏览器兼容性:Chrome 5+ · Firefox 3+ · Safari 4+ · Node.js 0.10+
|
|
72
|
+
*/
|
|
73
|
+
declare const IsNull: (val: unknown) => boolean;
|
|
74
|
+
/**
|
|
75
|
+
* 深度相等比较,支持对象和数组的递归比较。
|
|
76
|
+
*
|
|
77
|
+
* 注意:仅比较自身可枚举属性,不比较原型链上的属性。
|
|
78
|
+
* 不支持 Map、Set、Date、RegExp 等特殊对象的深度比较。
|
|
79
|
+
* @example IsEqual({ a: 1 }, { a: 1 }) // true
|
|
80
|
+
* @example IsEqual([1, 2], [1, 2]) // true
|
|
81
|
+
* @example IsEqual({ a: 1 }, { a: 2 }) // false
|
|
82
|
+
* @remarks
|
|
83
|
+
* 浏览器兼容性:Chrome 5+ · Firefox 3+ · Safari 4+ · Node.js 0.10+
|
|
84
|
+
*/
|
|
85
|
+
declare const IsEqual: (a: unknown, b: unknown) => boolean;
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* 输入校验工具 + 常用正则常量
|
|
89
|
+
*/
|
|
90
|
+
/**
|
|
91
|
+
* 常用正则规则常量,来源参考 any-rule(https://github.com/any86/any-rule)
|
|
92
|
+
* @remarks
|
|
93
|
+
* 浏览器兼容性:Chrome 5+ · Firefox 3+ · Safari 4+ · Node.js 0.10+
|
|
94
|
+
*/
|
|
95
|
+
declare const REGEX: {
|
|
96
|
+
/** 手机号(宽松:13-19 开头) */
|
|
97
|
+
readonly phone: RegExp;
|
|
98
|
+
/** 手机号(严谨,基于工信部 2019 年最新公布号段) */
|
|
99
|
+
readonly strictPhone: RegExp;
|
|
100
|
+
/** 邮箱(符合 RFC 标准) */
|
|
101
|
+
readonly email: RegExp;
|
|
102
|
+
/** URL(支持 http / https / ftp) */
|
|
103
|
+
readonly url: RegExp;
|
|
104
|
+
/** IPv4 地址 */
|
|
105
|
+
readonly ipv4: RegExp;
|
|
106
|
+
/** 整数(含负数和零) */
|
|
107
|
+
readonly integer: RegExp;
|
|
108
|
+
/** 正整数(不含零) */
|
|
109
|
+
readonly positiveInteger: RegExp;
|
|
110
|
+
/** 二代身份证 */
|
|
111
|
+
readonly idcard: RegExp;
|
|
112
|
+
/** 日期(宽松,如 2022-9-1) */
|
|
113
|
+
readonly date: RegExp;
|
|
114
|
+
/** 16 进制颜色值 */
|
|
115
|
+
readonly color: RegExp;
|
|
116
|
+
};
|
|
117
|
+
/**
|
|
118
|
+
* 是否为合法邮箱地址
|
|
119
|
+
* @example IsEmail('user@example.com') // true
|
|
120
|
+
* @example IsEmail('invalid') // false
|
|
121
|
+
* @remarks
|
|
122
|
+
* 浏览器兼容性:Chrome 5+ · Firefox 3+ · Safari 4+ · Node.js 0.10+
|
|
123
|
+
*/
|
|
124
|
+
declare const IsEmail: (val: string) => boolean;
|
|
125
|
+
/**
|
|
126
|
+
* 是否为中国大陆手机号(宽松校验)
|
|
127
|
+
* @example IsPhone('13812345678') // true
|
|
128
|
+
* @example IsPhone('12345678901') // false
|
|
129
|
+
* @remarks
|
|
130
|
+
* 浏览器兼容性:Chrome 5+ · Firefox 3+ · Safari 4+ · Node.js 0.10+
|
|
131
|
+
*/
|
|
132
|
+
declare const IsPhone: (val: string) => boolean;
|
|
133
|
+
/**
|
|
134
|
+
* 是否为合法 URL
|
|
135
|
+
* @example IsUrl('https://example.com') // true
|
|
136
|
+
* @example IsUrl('not-a-url') // false
|
|
137
|
+
* @remarks
|
|
138
|
+
* 浏览器兼容性:Chrome 5+ · Firefox 3+ · Safari 4+ · Node.js 0.10+
|
|
139
|
+
*/
|
|
140
|
+
declare const IsUrl: (val: string) => boolean;
|
|
141
|
+
/**
|
|
142
|
+
* 是否为合法 IPv4 地址
|
|
143
|
+
* @example IsIpv4('192.168.1.1') // true
|
|
144
|
+
* @example IsIpv4('999.0.0.1') // false
|
|
145
|
+
* @remarks
|
|
146
|
+
* 浏览器兼容性:Chrome 5+ · Firefox 3+ · Safari 4+ · Node.js 0.10+
|
|
147
|
+
*/
|
|
148
|
+
declare const IsIpv4: (val: string) => boolean;
|
|
149
|
+
/**
|
|
150
|
+
* 是否为整数(含负数和零)
|
|
151
|
+
* @example IsInteger('42') // true
|
|
152
|
+
* @example IsInteger('-10') // true
|
|
153
|
+
* @example IsInteger('3.14') // false
|
|
154
|
+
* @remarks
|
|
155
|
+
* 浏览器兼容性:Chrome 5+ · Firefox 3+ · Safari 4+ · Node.js 0.10+
|
|
156
|
+
*/
|
|
157
|
+
declare const IsInteger: (val: string) => boolean;
|
|
158
|
+
/**
|
|
159
|
+
* 是否为正整数(不含零)
|
|
160
|
+
* @example IsPositiveInteger('1') // true
|
|
161
|
+
* @example IsPositiveInteger('0') // false
|
|
162
|
+
* @example IsPositiveInteger('-1') // false
|
|
163
|
+
* @remarks
|
|
164
|
+
* 浏览器兼容性:Chrome 5+ · Firefox 3+ · Safari 4+ · Node.js 0.10+
|
|
165
|
+
*/
|
|
166
|
+
declare const IsPositiveInteger: (val: string) => boolean;
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* 数组工具
|
|
170
|
+
*/
|
|
171
|
+
/**
|
|
172
|
+
* 数组去重(基于 Set,时间复杂度 O(n))
|
|
173
|
+
* @example Unique([1, 2, 1, 3]) // [1, 2, 3]
|
|
174
|
+
* @remarks
|
|
175
|
+
* 浏览器兼容性:Chrome 38+ · Firefox 13+ · Safari 7.1+ · Node.js 0.12+
|
|
176
|
+
*
|
|
177
|
+
* 依赖 `Set`(ES2015)。
|
|
178
|
+
*/
|
|
179
|
+
declare const Unique: <T>(arr: T[]) => T[];
|
|
180
|
+
/**
|
|
181
|
+
* 按对象指定 key 的值去重,保留首次出现的元素
|
|
182
|
+
* @param arr 待去重数组
|
|
183
|
+
* @param key 用于去重的对象属性名
|
|
184
|
+
* @example
|
|
185
|
+
* UniqueByKey([{ id: 1, name: 'a' }, { id: 1, name: 'b' }], 'id')
|
|
186
|
+
* // [{ id: 1, name: 'a' }]
|
|
187
|
+
* @remarks
|
|
188
|
+
* 浏览器兼容性:Chrome 38+ · Firefox 13+ · Safari 7.1+ · Node.js 0.12+
|
|
189
|
+
*
|
|
190
|
+
* 依赖 `Set`(ES2015)。
|
|
191
|
+
*/
|
|
192
|
+
declare const UniqueByKey: <T extends object>(arr: T[], key: keyof T) => T[];
|
|
193
|
+
/**
|
|
194
|
+
* 差集:返回只在 arr 中存在、不在 other 中存在的元素(基于 Set,时间复杂度 O(n))
|
|
195
|
+
* @param arr 基础数组
|
|
196
|
+
* @param other 比较数组
|
|
197
|
+
* @example Minus([1, 2, 3], [2, 3, 4]) // [1]
|
|
198
|
+
* @remarks
|
|
199
|
+
* 浏览器兼容性:Chrome 38+ · Firefox 13+ · Safari 7.1+ · Node.js 0.12+
|
|
200
|
+
*
|
|
201
|
+
* 依赖 `Set`(ES2015)。
|
|
202
|
+
*/
|
|
203
|
+
declare const Minus: <T>(arr: T[], other: T[]) => T[];
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* 对象工具
|
|
207
|
+
*/
|
|
208
|
+
/**
|
|
209
|
+
* 深拷贝对象或数组。
|
|
210
|
+
*
|
|
211
|
+
* 优先使用原生 `structuredClone`(支持 Date、RegExp、Map、Set、循环引用等),
|
|
212
|
+
* 降级为 JSON 序列化(不支持 undefined、函数、循环引用)。
|
|
213
|
+
* @example DeepClone({ a: { b: 1 } }) // { a: { b: 1 } }(新对象)
|
|
214
|
+
* @remarks
|
|
215
|
+
* 浏览器兼容性:
|
|
216
|
+
* - `structuredClone`(优先):Chrome 98+ · Firefox 94+ · Safari 15.4+ · Node.js 17.0+
|
|
217
|
+
* - JSON 降级(自动回退):Chrome 5+ · Firefox 3+ · Safari 4+ · Node.js 0.10+
|
|
218
|
+
*
|
|
219
|
+
* 在不支持 `structuredClone` 的环境中自动降级为 `JSON.parse(JSON.stringify(obj))`,
|
|
220
|
+
* 降级时不支持 `undefined`、函数、`Date`、`RegExp`、循环引用。
|
|
221
|
+
*/
|
|
222
|
+
declare const DeepClone: <T>(obj: T) => T;
|
|
223
|
+
/**
|
|
224
|
+
* 删除对象中值为 null 或 undefined 的属性,返回新对象(不修改原对象)。
|
|
225
|
+
*
|
|
226
|
+
* 注意:空字符串、false、0 等假值不会被删除。
|
|
227
|
+
* @example CleanObject({ a: 1, b: null, c: undefined }) // { a: 1 }
|
|
228
|
+
* @remarks
|
|
229
|
+
* 浏览器兼容性:Chrome 5+ · Firefox 3+ · Safari 4+ · Node.js 0.10+
|
|
230
|
+
*/
|
|
231
|
+
declare const CleanObject: <T extends object>(obj: T) => Partial<T>;
|
|
232
|
+
/**
|
|
233
|
+
* 按 key 列表提取对象子集,返回新对象(类型安全)。
|
|
234
|
+
* @example Pick({ a: 1, b: 2, c: 3 }, ['a', 'c']) // { a: 1, c: 3 }
|
|
235
|
+
* @remarks
|
|
236
|
+
* 浏览器兼容性:Chrome 5+ · Firefox 3+ · Safari 4+ · Node.js 0.10+
|
|
237
|
+
*/
|
|
238
|
+
declare const Pick: <T extends object, K extends keyof T>(obj: T, keys: K[]) => Pick<T, K>;
|
|
239
|
+
/**
|
|
240
|
+
* 排除指定 key 后返回新对象(类型安全)。
|
|
241
|
+
* @example Omit({ a: 1, b: 2, c: 3 }, ['b']) // { a: 1, c: 3 }
|
|
242
|
+
* @remarks
|
|
243
|
+
* 浏览器兼容性:Chrome 38+ · Firefox 13+ · Safari 7.1+ · Node.js 0.12+
|
|
244
|
+
*
|
|
245
|
+
* 依赖 `Set`(ES2015)。
|
|
246
|
+
*/
|
|
247
|
+
declare const Omit: <T extends object, K extends keyof T>(obj: T, keys: K[]) => Omit<T, K>;
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* 函数工具
|
|
251
|
+
*/
|
|
252
|
+
/**
|
|
253
|
+
* 防抖:在最后一次调用后等待 delay 毫秒再执行(trailing edge)。
|
|
254
|
+
*
|
|
255
|
+
* 适合搜索输入、窗口 resize、表单提交等场景。
|
|
256
|
+
* @param fn 要防抖的函数
|
|
257
|
+
* @param delay 延迟毫秒数,默认 300ms
|
|
258
|
+
* @example
|
|
259
|
+
* const handleInput = Debounce((val: string) => search(val), 500)
|
|
260
|
+
* @remarks
|
|
261
|
+
* 浏览器兼容性:Chrome 5+ · Firefox 3+ · Safari 4+ · Node.js 0.10+
|
|
262
|
+
*
|
|
263
|
+
* 依赖 `setTimeout` / `clearTimeout`,全平台支持。
|
|
264
|
+
*/
|
|
265
|
+
declare const Debounce: <T extends (...args: Parameters<T>) => ReturnType<T>>(fn: T, delay?: number) => ((...args: Parameters<T>) => void);
|
|
266
|
+
/**
|
|
267
|
+
* 节流:在 delay 毫秒内最多执行一次(leading edge,时间戳实现)。
|
|
268
|
+
*
|
|
269
|
+
* 适合滚动监听、mousemove、高频点击等场景。
|
|
270
|
+
* @param fn 要节流的函数
|
|
271
|
+
* @param delay 节流间隔毫秒数,默认 50ms
|
|
272
|
+
* @example
|
|
273
|
+
* const handleScroll = Throttle(() => updatePosition(), 100)
|
|
274
|
+
* @remarks
|
|
275
|
+
* 浏览器兼容性:Chrome 5+ · Firefox 3+ · Safari 4+ · Node.js 0.10+
|
|
276
|
+
*
|
|
277
|
+
* 依赖 `Date.now()`,无 `setTimeout` 悬空回调风险。
|
|
278
|
+
*/
|
|
279
|
+
declare const Throttle: <T extends (...args: Parameters<T>) => ReturnType<T>>(fn: T, delay?: number) => ((...args: Parameters<T>) => void);
|
|
280
|
+
/**
|
|
281
|
+
* Promise 延迟:等待指定毫秒数后 resolve。
|
|
282
|
+
* @param ms 延迟毫秒数
|
|
283
|
+
* @example await Delay(1000) // 等待 1 秒
|
|
284
|
+
* @remarks
|
|
285
|
+
* 浏览器兼容性:Chrome 32+ · Firefox 29+ · Safari 8+ · Node.js 0.12+
|
|
286
|
+
*
|
|
287
|
+
* 依赖 `Promise`(ES2015)和 `setTimeout`。
|
|
288
|
+
*/
|
|
289
|
+
declare const Delay: (ms: number) => Promise<void>;
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* 字符串 / UUID / URL 工具
|
|
293
|
+
*/
|
|
294
|
+
/**
|
|
295
|
+
* 生成标准 RFC 4122 v4 UUID。
|
|
296
|
+
*
|
|
297
|
+
* 优先使用 `crypto.randomUUID()`(现代浏览器 / Node.js 19+),
|
|
298
|
+
* 降级为基于 `crypto.getRandomValues` 的手动实现。
|
|
299
|
+
* @example UUID() // 'f47ac10b-58cc-4372-a567-0e02b2c3d479'
|
|
300
|
+
* @remarks
|
|
301
|
+
* 浏览器兼容性:
|
|
302
|
+
* - `crypto.randomUUID()`(优先):Chrome 92+ · Firefox 95+ · Safari 15.4+ · Node.js 19.0+
|
|
303
|
+
* - `crypto.getRandomValues` 降级:Chrome 11+ · Firefox 21+ · Safari 6.1+ · Node.js 15.0+
|
|
304
|
+
*
|
|
305
|
+
* Node.js 14.17+ 可通过 `import { randomUUID } from 'crypto'` 单独使用,
|
|
306
|
+
* 但全局 `crypto` 对象需 Node.js 15.0+ 才可用。
|
|
307
|
+
*/
|
|
308
|
+
declare const UUID: () => string;
|
|
309
|
+
/**
|
|
310
|
+
* 从 URL 中获取指定查询参数的值,不存在则返回空字符串。
|
|
311
|
+
* @param name 参数名
|
|
312
|
+
* @param url 目标 URL,默认取 `window.location.href`
|
|
313
|
+
* @example GetURLParam('page', 'https://example.com?page=2&size=10') // '2'
|
|
314
|
+
* @remarks
|
|
315
|
+
* 浏览器兼容性:Chrome 49+ · Firefox 44+ · Safari 10.1+ · Node.js 10.0+
|
|
316
|
+
*
|
|
317
|
+
* 依赖 `URL` API 和 `URLSearchParams`(WHATWG URL Standard)。
|
|
318
|
+
* 若 URL 格式非法会静默返回空字符串。
|
|
319
|
+
*/
|
|
320
|
+
declare const GetURLParam: (name: string, url?: string) => string;
|
|
321
|
+
/**
|
|
322
|
+
* 获取 URL 中所有查询参数,以键值对对象返回。
|
|
323
|
+
*
|
|
324
|
+
* 若同名参数出现多次,取最后一个值。
|
|
325
|
+
* @param url 目标 URL,默认取 `window.location.href`
|
|
326
|
+
* @example GetURLParams('https://example.com?a=1&b=2') // { a: '1', b: '2' }
|
|
327
|
+
* @remarks
|
|
328
|
+
* 浏览器兼容性:Chrome 49+ · Firefox 44+ · Safari 10.1+ · Node.js 10.0+
|
|
329
|
+
*
|
|
330
|
+
* 依赖 `URL` API 和 `URLSearchParams`(WHATWG URL Standard)。
|
|
331
|
+
* 若 URL 格式非法会静默返回空对象。
|
|
332
|
+
*/
|
|
333
|
+
declare const GetURLParams: (url?: string) => Record<string, string>;
|
|
334
|
+
/**
|
|
335
|
+
* 将对象转换为 URL 查询字符串(以 `?` 开头)。
|
|
336
|
+
*
|
|
337
|
+
* 值为 null / undefined 的属性会被跳过,特殊字符自动编码。
|
|
338
|
+
* @example ObjectToQuery({ page: 1, size: 10 }) // '?page=1&size=10'
|
|
339
|
+
* @example ObjectToQuery({}) // ''
|
|
340
|
+
* @remarks
|
|
341
|
+
* 浏览器兼容性:Chrome 49+ · Firefox 44+ · Safari 10.1+ · Node.js 10.0+
|
|
342
|
+
*
|
|
343
|
+
* 依赖 `URLSearchParams`(WHATWG URL Standard)。
|
|
344
|
+
* 空格编码为 `+`(application/x-www-form-urlencoded 规范),
|
|
345
|
+
* 与 `encodeURIComponent` 的 `%20` 编码略有差异。
|
|
346
|
+
*/
|
|
347
|
+
declare const ObjectToQuery: (obj: Record<string, unknown>) => string;
|
|
348
|
+
/**
|
|
349
|
+
* 将查询字符串解析为键值对对象。
|
|
350
|
+
*
|
|
351
|
+
* 支持以 `?` 开头或不以 `?` 开头的格式,自动解码编码字符。
|
|
352
|
+
* @example QueryToObject('?a=1&b=hello%20world') // { a: '1', b: 'hello world' }
|
|
353
|
+
* @remarks
|
|
354
|
+
* 浏览器兼容性:Chrome 49+ · Firefox 44+ · Safari 10.1+ · Node.js 10.0+
|
|
355
|
+
*
|
|
356
|
+
* 依赖 `URLSearchParams`(WHATWG URL Standard)。
|
|
357
|
+
*/
|
|
358
|
+
declare const QueryToObject: (query: string) => Record<string, string>;
|
|
359
|
+
|
|
360
|
+
/**
|
|
361
|
+
* 时间日期工具
|
|
362
|
+
*/
|
|
363
|
+
/**
|
|
364
|
+
* 格式化日期时间。
|
|
365
|
+
*
|
|
366
|
+
* 支持占位符:`yyyy`(年)、`MM`(月)、`dd`(日)、`HH`(24 小时制小时)、`mm`(分钟)、`ss`(秒)。
|
|
367
|
+
* @param date 日期,默认当前时间,支持 Date 对象、日期字符串、时间戳
|
|
368
|
+
* @param format 格式字符串,默认 `'yyyy-MM-dd HH:mm:ss'`
|
|
369
|
+
* @example FormatDate(new Date('2024-01-05 09:05:03')) // '2024-01-05 09:05:03'
|
|
370
|
+
* @example FormatDate(new Date('2024-01-05'), 'yyyy/MM/dd') // '2024/01/05'
|
|
371
|
+
* @example FormatDate(new Date('2024-01-05'), 'yy-MM-dd') // '24-01-05'
|
|
372
|
+
* @remarks
|
|
373
|
+
* 浏览器兼容性:Chrome 5+ · Firefox 3+ · Safari 4+ · Node.js 0.10+
|
|
374
|
+
*
|
|
375
|
+
* 依赖原生 `Date` API,全平台支持。
|
|
376
|
+
* 传入非法日期字符串时返回空字符串。
|
|
377
|
+
*/
|
|
378
|
+
declare const FormatDate: (date?: Date | string | number, format?: string) => string;
|
|
379
|
+
/**
|
|
380
|
+
* 获取指定日期偏移 N 天后的日期字符串。
|
|
381
|
+
*
|
|
382
|
+
* 正数表示往后,负数表示往前。不修改传入的 Date 对象。
|
|
383
|
+
* @param date 基准日期,默认当前时间
|
|
384
|
+
* @param days 偏移天数(正数往后,负数往前)
|
|
385
|
+
* @param format 格式字符串,默认 `'yyyy-MM-dd HH:mm:ss'`
|
|
386
|
+
* @example GetDateOffset(new Date('2024-01-10'), 3, 'yyyy-MM-dd') // '2024-01-13'
|
|
387
|
+
* @example GetDateOffset(new Date('2024-01-10'), -3, 'yyyy-MM-dd') // '2024-01-07'
|
|
388
|
+
* @remarks
|
|
389
|
+
* 浏览器兼容性:Chrome 5+ · Firefox 3+ · Safari 4+ · Node.js 0.10+
|
|
390
|
+
*
|
|
391
|
+
* 依赖原生 `Date` API,全平台支持。
|
|
392
|
+
*/
|
|
393
|
+
declare const GetDateOffset: (date?: Date, days?: number, format?: string) => string;
|
|
394
|
+
/**
|
|
395
|
+
* 获取指定日期当天的开始时间戳(00:00:00.000)和结束时间戳(23:59:59.999)。
|
|
396
|
+
* @param date 指定日期,默认当前时间
|
|
397
|
+
* @example GetDayRange(new Date('2024-01-05'))
|
|
398
|
+
* // { start: 1704384000000, end: 1704470399999 }
|
|
399
|
+
* @remarks
|
|
400
|
+
* 浏览器兼容性:Chrome 5+ · Firefox 3+ · Safari 4+ · Node.js 0.10+
|
|
401
|
+
*
|
|
402
|
+
* 依赖原生 `Date` API,全平台支持。
|
|
403
|
+
* 时间戳基于本地时区计算。
|
|
404
|
+
*/
|
|
405
|
+
declare const GetDayRange: (date?: Date) => {
|
|
406
|
+
start: number;
|
|
407
|
+
end: number;
|
|
408
|
+
};
|
|
409
|
+
|
|
410
|
+
export { CleanObject, Debounce, DeepClone, Delay, FormatDate, GetDateOffset, GetDayRange, GetURLParam, GetURLParams, IsArray, IsBoolean, IsEmail, IsEqual, IsFunction, IsInteger, IsIpv4, IsNull, IsNumber, IsObject, IsPhone, IsPositiveInteger, IsRealNumber, IsString, IsUrl, Minus, ObjectToQuery, Omit, Pick, QueryToObject, REGEX, Throttle, UUID, Unique, UniqueByKey };
|
package/dist/umd/index.global.js
CHANGED
|
@@ -11625,7 +11625,7 @@ var SnackKit = (function (exports, crypto2, url, http, https, http2, util2, zlib
|
|
|
11625
11625
|
});
|
|
11626
11626
|
|
|
11627
11627
|
// package.json
|
|
11628
|
-
var version = "0.
|
|
11628
|
+
var version = "0.3.0";
|
|
11629
11629
|
|
|
11630
11630
|
// node_modules/axios/lib/helpers/bind.js
|
|
11631
11631
|
function bind(fn, thisArg) {
|
|
@@ -16429,6 +16429,232 @@ var SnackKit = (function (exports, crypto2, url, http, https, http2, util2, zlib
|
|
|
16429
16429
|
this.footerEl.className = `dbg-footer${cls ? ` ${cls}` : ""}`;
|
|
16430
16430
|
}
|
|
16431
16431
|
};
|
|
16432
|
+
|
|
16433
|
+
// src/utils/detect.ts
|
|
16434
|
+
var _toString = (val) => Object.prototype.toString.call(val);
|
|
16435
|
+
var IsNumber = (val) => !isNaN(parseFloat(val)) && isFinite(val);
|
|
16436
|
+
var IsRealNumber = (val) => typeof val === "number" && !isNaN(val);
|
|
16437
|
+
var IsString = (val) => _toString(val) === "[object String]";
|
|
16438
|
+
var IsBoolean = (val) => _toString(val) === "[object Boolean]";
|
|
16439
|
+
var IsArray = (val) => Array.isArray(val);
|
|
16440
|
+
var IsFunction = (val) => _toString(val) === "[object Function]";
|
|
16441
|
+
var IsObject = (val) => _toString(val) === "[object Object]";
|
|
16442
|
+
var IsNull = (val) => val === null || val === void 0 || val === "";
|
|
16443
|
+
var IsEqual = (a, b) => {
|
|
16444
|
+
if (a === b) return true;
|
|
16445
|
+
if (typeof a !== typeof b) return false;
|
|
16446
|
+
if (a === null || b === null) return false;
|
|
16447
|
+
if (typeof a !== "object") return false;
|
|
16448
|
+
if (Array.isArray(a) !== Array.isArray(b)) return false;
|
|
16449
|
+
const keysA = Object.keys(a);
|
|
16450
|
+
const keysB = Object.keys(b);
|
|
16451
|
+
if (keysA.length !== keysB.length) return false;
|
|
16452
|
+
for (const key of keysA) {
|
|
16453
|
+
if (!IsEqual(a[key], b[key])) {
|
|
16454
|
+
return false;
|
|
16455
|
+
}
|
|
16456
|
+
}
|
|
16457
|
+
return true;
|
|
16458
|
+
};
|
|
16459
|
+
|
|
16460
|
+
// src/utils/validate.ts
|
|
16461
|
+
var REGEX = {
|
|
16462
|
+
/** 手机号(宽松:13-19 开头) */
|
|
16463
|
+
phone: /^(?:(?:\+|00)86)?1[3-9]\d{9}$/,
|
|
16464
|
+
/** 手机号(严谨,基于工信部 2019 年最新公布号段) */
|
|
16465
|
+
strictPhone: /^(?:(?:\+|00)86)?1(?:(?:3\d)|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8\d)|(?:9[189]))\d{8}$/,
|
|
16466
|
+
/** 邮箱(符合 RFC 标准) */
|
|
16467
|
+
email: /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-_0-9]+\.)+[a-zA-Z]{2,}))$/,
|
|
16468
|
+
/** URL(支持 http / https / ftp) */
|
|
16469
|
+
url: /^(https?|ftp):\/\/[\w\-]+(\.[\w\-]+)+([\w\-.,@?^=%&:/~+#]*[\w\-@?^=%&/~+#])?/,
|
|
16470
|
+
/** IPv4 地址 */
|
|
16471
|
+
ipv4: /^((25[0-5]|2[0-4]\d|1\d{2}|[1-9]\d|\d)\.){3}(25[0-5]|2[0-4]\d|1\d{2}|[1-9]\d|\d)$/,
|
|
16472
|
+
/** 整数(含负数和零) */
|
|
16473
|
+
integer: /^[+-]?\d+$/,
|
|
16474
|
+
/** 正整数(不含零) */
|
|
16475
|
+
positiveInteger: /^[1-9]\d*$/,
|
|
16476
|
+
/** 二代身份证 */
|
|
16477
|
+
idcard: /^[1-9]\d{5}(?:18|19|20)\d{2}(?:0[1-9]|10|11|12)(?:0[1-9]|[1-2]\d|30|31)\d{3}[\dXx]$/,
|
|
16478
|
+
/** 日期(宽松,如 2022-9-1) */
|
|
16479
|
+
date: /^\d{1,4}-(1[0-2]|0?[1-9])-(0?[1-9]|[1-2]\d|30|31)$/,
|
|
16480
|
+
/** 16 进制颜色值 */
|
|
16481
|
+
color: /^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/
|
|
16482
|
+
};
|
|
16483
|
+
var IsEmail = (val) => REGEX.email.test(val);
|
|
16484
|
+
var IsPhone = (val) => REGEX.phone.test(val);
|
|
16485
|
+
var IsUrl = (val) => REGEX.url.test(val);
|
|
16486
|
+
var IsIpv4 = (val) => REGEX.ipv4.test(val);
|
|
16487
|
+
var IsInteger = (val) => REGEX.integer.test(val);
|
|
16488
|
+
var IsPositiveInteger = (val) => REGEX.positiveInteger.test(val);
|
|
16489
|
+
|
|
16490
|
+
// src/utils/array.ts
|
|
16491
|
+
var Unique = (arr) => [...new Set(arr)];
|
|
16492
|
+
var UniqueByKey = (arr, key) => {
|
|
16493
|
+
const seen = /* @__PURE__ */ new Set();
|
|
16494
|
+
const result = [];
|
|
16495
|
+
for (const item of arr) {
|
|
16496
|
+
const k = item[key];
|
|
16497
|
+
if (!seen.has(k)) {
|
|
16498
|
+
seen.add(k);
|
|
16499
|
+
result.push(item);
|
|
16500
|
+
}
|
|
16501
|
+
}
|
|
16502
|
+
return result;
|
|
16503
|
+
};
|
|
16504
|
+
var Minus = (arr, other) => {
|
|
16505
|
+
const otherSet = new Set(other);
|
|
16506
|
+
return arr.filter((item) => !otherSet.has(item));
|
|
16507
|
+
};
|
|
16508
|
+
|
|
16509
|
+
// src/utils/object.ts
|
|
16510
|
+
var DeepClone = (obj) => {
|
|
16511
|
+
if (typeof structuredClone === "function") {
|
|
16512
|
+
return structuredClone(obj);
|
|
16513
|
+
}
|
|
16514
|
+
return JSON.parse(JSON.stringify(obj));
|
|
16515
|
+
};
|
|
16516
|
+
var CleanObject = (obj) => {
|
|
16517
|
+
const result = {};
|
|
16518
|
+
for (const key in obj) {
|
|
16519
|
+
if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
16520
|
+
const val = obj[key];
|
|
16521
|
+
if (val !== null && val !== void 0) {
|
|
16522
|
+
result[key] = val;
|
|
16523
|
+
}
|
|
16524
|
+
}
|
|
16525
|
+
}
|
|
16526
|
+
return result;
|
|
16527
|
+
};
|
|
16528
|
+
var Pick = (obj, keys) => {
|
|
16529
|
+
const result = {};
|
|
16530
|
+
for (const key of keys) {
|
|
16531
|
+
if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
16532
|
+
result[key] = obj[key];
|
|
16533
|
+
}
|
|
16534
|
+
}
|
|
16535
|
+
return result;
|
|
16536
|
+
};
|
|
16537
|
+
var Omit = (obj, keys) => {
|
|
16538
|
+
const excludeSet = new Set(keys);
|
|
16539
|
+
const result = {};
|
|
16540
|
+
for (const key in obj) {
|
|
16541
|
+
if (Object.prototype.hasOwnProperty.call(obj, key) && !excludeSet.has(key)) {
|
|
16542
|
+
result[key] = obj[key];
|
|
16543
|
+
}
|
|
16544
|
+
}
|
|
16545
|
+
return result;
|
|
16546
|
+
};
|
|
16547
|
+
|
|
16548
|
+
// src/utils/func.ts
|
|
16549
|
+
var Debounce = (fn, delay = 300) => {
|
|
16550
|
+
let timer = null;
|
|
16551
|
+
return function(...args) {
|
|
16552
|
+
if (timer !== null) clearTimeout(timer);
|
|
16553
|
+
timer = setTimeout(() => {
|
|
16554
|
+
fn.apply(this, args);
|
|
16555
|
+
timer = null;
|
|
16556
|
+
}, delay);
|
|
16557
|
+
};
|
|
16558
|
+
};
|
|
16559
|
+
var Throttle = (fn, delay = 50) => {
|
|
16560
|
+
let lastTime = 0;
|
|
16561
|
+
return function(...args) {
|
|
16562
|
+
const now = Date.now();
|
|
16563
|
+
if (now - lastTime >= delay) {
|
|
16564
|
+
lastTime = now;
|
|
16565
|
+
fn.apply(this, args);
|
|
16566
|
+
}
|
|
16567
|
+
};
|
|
16568
|
+
};
|
|
16569
|
+
var Delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
16570
|
+
|
|
16571
|
+
// src/utils/string.ts
|
|
16572
|
+
var UUID = () => {
|
|
16573
|
+
if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
|
|
16574
|
+
return crypto.randomUUID();
|
|
16575
|
+
}
|
|
16576
|
+
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
|
|
16577
|
+
const arr = new Uint8Array(1);
|
|
16578
|
+
crypto.getRandomValues(arr);
|
|
16579
|
+
const r = arr[0] & 15;
|
|
16580
|
+
const v = c === "x" ? r : r & 3 | 8;
|
|
16581
|
+
return v.toString(16);
|
|
16582
|
+
});
|
|
16583
|
+
};
|
|
16584
|
+
var GetURLParam = (name, url2 = typeof window !== "undefined" ? window.location.href : "") => {
|
|
16585
|
+
try {
|
|
16586
|
+
return new URL(url2).searchParams.get(name) ?? "";
|
|
16587
|
+
} catch {
|
|
16588
|
+
return "";
|
|
16589
|
+
}
|
|
16590
|
+
};
|
|
16591
|
+
var GetURLParams = (url2 = typeof window !== "undefined" ? window.location.href : "") => {
|
|
16592
|
+
try {
|
|
16593
|
+
const result = {};
|
|
16594
|
+
new URL(url2).searchParams.forEach((val, key) => {
|
|
16595
|
+
result[key] = val;
|
|
16596
|
+
});
|
|
16597
|
+
return result;
|
|
16598
|
+
} catch {
|
|
16599
|
+
return {};
|
|
16600
|
+
}
|
|
16601
|
+
};
|
|
16602
|
+
var ObjectToQuery = (obj) => {
|
|
16603
|
+
const params = new URLSearchParams();
|
|
16604
|
+
for (const [key, val] of Object.entries(obj)) {
|
|
16605
|
+
if (val !== null && val !== void 0) {
|
|
16606
|
+
params.append(key, String(val));
|
|
16607
|
+
}
|
|
16608
|
+
}
|
|
16609
|
+
const str = params.toString();
|
|
16610
|
+
return str ? `?${str}` : "";
|
|
16611
|
+
};
|
|
16612
|
+
var QueryToObject = (query) => {
|
|
16613
|
+
const str = query.startsWith("?") ? query.slice(1) : query;
|
|
16614
|
+
const result = {};
|
|
16615
|
+
new URLSearchParams(str).forEach((val, key) => {
|
|
16616
|
+
result[key] = val;
|
|
16617
|
+
});
|
|
16618
|
+
return result;
|
|
16619
|
+
};
|
|
16620
|
+
|
|
16621
|
+
// src/utils/time.ts
|
|
16622
|
+
var FormatDate = (date = /* @__PURE__ */ new Date(), format = "yyyy-MM-dd HH:mm:ss") => {
|
|
16623
|
+
const d = date instanceof Date ? date : new Date(date);
|
|
16624
|
+
if (isNaN(d.getTime())) return "";
|
|
16625
|
+
const map = {
|
|
16626
|
+
"M+": d.getMonth() + 1,
|
|
16627
|
+
"d+": d.getDate(),
|
|
16628
|
+
"H+": d.getHours(),
|
|
16629
|
+
"m+": d.getMinutes(),
|
|
16630
|
+
"s+": d.getSeconds()
|
|
16631
|
+
};
|
|
16632
|
+
let result = format.replace(
|
|
16633
|
+
/(y+)/,
|
|
16634
|
+
(_, p) => String(d.getFullYear()).slice(4 - p.length)
|
|
16635
|
+
);
|
|
16636
|
+
for (const [pattern, value] of Object.entries(map)) {
|
|
16637
|
+
result = result.replace(
|
|
16638
|
+
new RegExp(`(${pattern})`),
|
|
16639
|
+
(_, p) => String(value).padStart(p.length, "0")
|
|
16640
|
+
);
|
|
16641
|
+
}
|
|
16642
|
+
return result;
|
|
16643
|
+
};
|
|
16644
|
+
var GetDateOffset = (date = /* @__PURE__ */ new Date(), days = 0, format = "yyyy-MM-dd HH:mm:ss") => {
|
|
16645
|
+
const d = new Date(date.getTime());
|
|
16646
|
+
d.setDate(d.getDate() + days);
|
|
16647
|
+
return FormatDate(d, format);
|
|
16648
|
+
};
|
|
16649
|
+
var GetDayRange = (date = /* @__PURE__ */ new Date()) => {
|
|
16650
|
+
const y = date.getFullYear();
|
|
16651
|
+
const m = date.getMonth();
|
|
16652
|
+
const d = date.getDate();
|
|
16653
|
+
return {
|
|
16654
|
+
start: new Date(y, m, d, 0, 0, 0, 0).getTime(),
|
|
16655
|
+
end: new Date(y, m, d, 23, 59, 59, 999).getTime()
|
|
16656
|
+
};
|
|
16657
|
+
};
|
|
16432
16658
|
/*! Bundled license information:
|
|
16433
16659
|
|
|
16434
16660
|
mime-db/index.js:
|
|
@@ -16450,17 +16676,51 @@ var SnackKit = (function (exports, crypto2, url, http, https, http2, util2, zlib
|
|
|
16450
16676
|
|
|
16451
16677
|
exports.Cancel = Cancel2;
|
|
16452
16678
|
exports.CancelAll = CancelAll;
|
|
16679
|
+
exports.CleanObject = CleanObject;
|
|
16453
16680
|
exports.Context = Context;
|
|
16454
16681
|
exports.Ctx = Ctx;
|
|
16682
|
+
exports.Debounce = Debounce;
|
|
16455
16683
|
exports.Debugger = Debugger;
|
|
16684
|
+
exports.DeepClone = DeepClone;
|
|
16456
16685
|
exports.Del = Del;
|
|
16686
|
+
exports.Delay = Delay;
|
|
16687
|
+
exports.FormatDate = FormatDate;
|
|
16457
16688
|
exports.Get = Get;
|
|
16689
|
+
exports.GetDateOffset = GetDateOffset;
|
|
16690
|
+
exports.GetDayRange = GetDayRange;
|
|
16691
|
+
exports.GetURLParam = GetURLParam;
|
|
16692
|
+
exports.GetURLParams = GetURLParams;
|
|
16458
16693
|
exports.HttpContext = HttpContext;
|
|
16694
|
+
exports.IsArray = IsArray;
|
|
16695
|
+
exports.IsBoolean = IsBoolean;
|
|
16696
|
+
exports.IsEmail = IsEmail;
|
|
16697
|
+
exports.IsEqual = IsEqual;
|
|
16698
|
+
exports.IsFunction = IsFunction;
|
|
16699
|
+
exports.IsInteger = IsInteger;
|
|
16700
|
+
exports.IsIpv4 = IsIpv4;
|
|
16701
|
+
exports.IsNull = IsNull;
|
|
16702
|
+
exports.IsNumber = IsNumber;
|
|
16703
|
+
exports.IsObject = IsObject;
|
|
16704
|
+
exports.IsPhone = IsPhone;
|
|
16705
|
+
exports.IsPositiveInteger = IsPositiveInteger;
|
|
16706
|
+
exports.IsRealNumber = IsRealNumber;
|
|
16707
|
+
exports.IsString = IsString;
|
|
16708
|
+
exports.IsUrl = IsUrl;
|
|
16709
|
+
exports.Minus = Minus;
|
|
16710
|
+
exports.ObjectToQuery = ObjectToQuery;
|
|
16711
|
+
exports.Omit = Omit;
|
|
16459
16712
|
exports.Origin = Origin;
|
|
16460
16713
|
exports.Patch = Patch;
|
|
16714
|
+
exports.Pick = Pick;
|
|
16461
16715
|
exports.Post = Post;
|
|
16462
16716
|
exports.Put = Put;
|
|
16717
|
+
exports.QueryToObject = QueryToObject;
|
|
16718
|
+
exports.REGEX = REGEX;
|
|
16463
16719
|
exports.Request = Request;
|
|
16720
|
+
exports.Throttle = Throttle;
|
|
16721
|
+
exports.UUID = UUID;
|
|
16722
|
+
exports.Unique = Unique;
|
|
16723
|
+
exports.UniqueByKey = UniqueByKey;
|
|
16464
16724
|
exports.VERSION = version;
|
|
16465
16725
|
|
|
16466
16726
|
return exports;
|