@snack-kit/lib 0.1.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.
@@ -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 };