@lntvow/utils 3.0.2 → 3.0.6

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,431 @@
1
+ /**
2
+ * @description 将非数组转换为数组 如果是目标本就是数组则直接返回
3
+ * @param target 目标
4
+ * @example castArray(1) => [1]
5
+ */
6
+ declare function castArray<T>(target: T | T[]): T[];
7
+
8
+ /**
9
+ * @description compose
10
+ * @param fns 任意函数
11
+ */
12
+ declare function compose(...fns: ((...args: any[]) => any)[]): (...args: any[]) => any;
13
+ /**
14
+ * @description compose 从右到左
15
+ * @param fns 任意函数
16
+ */
17
+ declare function composeRight(...fns: ((...args: any[]) => any)[]): (...args: any[]) => any;
18
+
19
+ /**
20
+ * @description 防抖函数
21
+ * @param target 目标函数
22
+ * @param wait 等待时间 默认 500ms
23
+ * @example debounce(() => console.log('debounce'), 500)
24
+ */
25
+ declare function debounce(target: Function, wait?: number): (...args: any[]) => void;
26
+
27
+ /**
28
+ * @description 深拷贝
29
+ * @param target 拷贝对象
30
+ * @example deepClone({ name: 1, obj: { name: 1 }, arr: [1, 2, 3] }) => { name: 1, obj: { name: 1 }, arr: [1, 2, 3] }
31
+ */
32
+ declare function deepClone<T>(target: T): T;
33
+
34
+ type AnyObject = Record<string, any>;
35
+ type ArrayItem<T> = T extends (infer U)[] ? U : never;
36
+ declare const objectToString: () => string;
37
+ declare const toTypeString: (value: unknown) => string;
38
+ declare function initLog(cb: Function): void;
39
+ declare const log: (name: unknown, ...arg: unknown[]) => void;
40
+ declare const warn: (...arg: unknown[]) => void;
41
+ declare const error: (...arg: unknown[]) => void;
42
+ declare const isMap: (val: unknown) => val is Map<any, any>;
43
+ declare const isSet: (val: unknown) => val is Set<any>;
44
+ declare const isDate: (val: unknown) => val is Date;
45
+ declare const isRegExp: (val: unknown) => val is RegExp;
46
+ declare const isFunction: (val: unknown) => val is Function;
47
+ declare const isNumber: (val: unknown) => val is number;
48
+ declare const isString: (val: unknown) => val is string;
49
+ declare const isSymbol: (val: unknown) => val is symbol;
50
+ declare const isObject: (val: unknown) => val is Record<any, any>;
51
+ declare const isPromise: <T = any>(val: unknown) => val is Promise<T>;
52
+ declare const isUndef: (val: unknown) => val is null | undefined;
53
+ declare const isUndefined: (val: unknown) => val is undefined;
54
+ declare const isNull: (val: unknown) => val is null;
55
+ declare const isDef: <T>(val: T) => val is NonNullable<T>;
56
+ declare const isBoolean: (val: unknown) => val is boolean;
57
+ declare const isArray: (arg: any) => arg is any[];
58
+ declare const hasOwn: <T extends object, U extends keyof T>(target: T, key: U) => boolean;
59
+ declare const hasChanged: (oldValue: unknown, newValue: unknown) => boolean;
60
+ type Arrayable<T> = T | T[];
61
+
62
+ interface DeepMergeOptions {
63
+ /**
64
+ * @description 是否开启深拷贝 默认开启
65
+ * @default true
66
+ */
67
+ deepClone: boolean;
68
+ }
69
+ /**
70
+ * @description 深度合并两对象
71
+ * @param template 模板对象 就是默认值
72
+ * @param source 源对象 就是用户传入的值
73
+ * @param options 配置对象
74
+ * @example deepMerge({ name: 1, obj: { name: '默认' , text: '旧值' }, arr: [1, 2, 3] }, { name: 1, obj: { name: '修改', age: '新增' }, test: 'test' }) => { name: 1, obj: { name: '修改', text: '旧值', age: '新增' }, arr: [1, 2, 3], test: 'test' }
75
+ */
76
+ declare function deepMerge(template: AnyObject, source: AnyObject, options?: DeepMergeOptions): any;
77
+
78
+ declare function throwError(scope: string, message: string): void;
79
+ declare function debugWarn(err: Error): void;
80
+ declare function debugWarn(scope: string, message: string): void;
81
+ declare const deprecated: ({ from, replacement, version, type, }: {
82
+ from: string;
83
+ replacement: string;
84
+ version: string;
85
+ type?: 'API';
86
+ }) => void;
87
+
88
+ /**
89
+ * @description 解析查询字符串
90
+ * @param str 转换的字符串
91
+ * @return 转换后的对象
92
+ * @example
93
+ * parse('a=1&b=2') => { a: '1', b: '2' }
94
+ * parse('?a=1&b=2') => { a: '1', b: '2' }
95
+ * parse('&a=1&b=2') => { a: '1', b: '2' }
96
+ * parse('from=%E4%B8%AD%E5%9B%BD') => { from: '中国' }
97
+ * parse('from=%E4%B8%AD%E5%9B%BD', { decode: false }) => { from: '%E4%B8%AD%E5%9B%BD' }
98
+ */
99
+ declare function parse(str: string, options?: ParseOptions): Record<string, string>;
100
+ /**
101
+ *@description 转换对象为查询字符串
102
+ * @param obj 转换的对象
103
+ * @return 转换后的字符串
104
+ * @example
105
+ * stringify({ a: '1', b: '2' }) => 'a=1&b=2'
106
+ * stringify({ from: '中国' }) => 'from=%E4%B8%AD%E5%9B%BD'
107
+ * stringify({ from: '中国' }, { encode: false }) => 'from=中国'
108
+ */
109
+ declare function stringify(obj: Record<string, any>, options?: StringifyOptions): string;
110
+ /**
111
+ * @description url追加查询字符串
112
+ * @param url url
113
+ * @param obj 查询字符串对象
114
+ * @return 追加后的url
115
+ * @example
116
+ * appendQueryString('https://www.baidu.com', { a: '1', b: '2' }) => 'https://www.baidu.com?a=1&b=2'
117
+ * appendQueryString('/pages/index?id=10', { test:'23' }) => '/pages/index?id=10&test=23'
118
+ */
119
+ declare function appendQueryString(url: string, obj: AnyObject, options?: StringifyOptions): string;
120
+ declare const qs: {
121
+ parse: typeof parse;
122
+ stringify: typeof stringify;
123
+ appendQueryString: typeof appendQueryString;
124
+ };
125
+ interface ParseOptions {
126
+ /**
127
+ * @description 是否解码
128
+ * @default true
129
+ */
130
+ decode?: boolean;
131
+ }
132
+ interface StringifyOptions {
133
+ /**
134
+ * @description 是否编码
135
+ * @default true
136
+ */
137
+ encode?: boolean;
138
+ }
139
+
140
+ /**
141
+ * @description 节流函数
142
+ * @param target 目标函数
143
+ * @param wait 等待时间 默认 500ms
144
+ * @example throttle(() => console.log('hello world'), 500)
145
+ */
146
+ declare function throttle(target: Function, wait?: number): (...args: any[]) => any;
147
+
148
+ /**
149
+ * @description 校验经纬度
150
+ */
151
+ declare function validatorLatitudeOrLongitude(value: string): boolean;
152
+ /**
153
+ * @description 校验中文、字母、数字
154
+ */
155
+ declare function validatorChineseOrEnglishOrNumber(value: string): boolean;
156
+ /**
157
+ * @description 校验中英文
158
+ */
159
+ declare function validatorChineseOrEnglish(value: string): boolean;
160
+ /**
161
+ * @description 校验大写字母、数字、特殊字符 特殊字符包括 !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
162
+ */
163
+ declare function validatorUppercaseOrNumbersOrSpecial(value: string): boolean;
164
+ /**
165
+ * @description 校验大写字母、数字、_
166
+ */
167
+ declare function validatorUppercaseOrNumbersOrUnderline(value: string): boolean;
168
+ /**
169
+ * @description 校验是否为邮箱
170
+ * @deprecated 使用 isEmail 代替
171
+ */
172
+ declare function validatorEmail(value: string): boolean;
173
+
174
+ declare function compareProperties(target: AnyObject, source: AnyObject): void | AnyObject;
175
+
176
+ /**
177
+ * @description 排除对象中的某些属性
178
+ * @param target 目标对象
179
+ * @param properties 要排除的属性
180
+ * @example excludeProperties({ a: 1, b: 2, c: 3 }, ['a', 'b']) => { c: 3 }
181
+ */
182
+ declare function excludeProperties<T extends AnyObject, K extends keyof T>(target: T, properties: Arrayable<K>): Omit<T, K>;
183
+ /**
184
+ * @description 提取对象中的某些属性
185
+ * @param target 目标对象
186
+ * @param properties 要提取的属性
187
+ * @example extractProperties({ a: 1, b: 2, c: 3 }, ['a', 'b']) => { a: 1, b: 2 }
188
+ */
189
+ declare function extractProperties<T extends AnyObject, K extends keyof T>(target: T, properties: Arrayable<K>): Pick<T, K>;
190
+
191
+ /**
192
+ * @description 生成随机数组
193
+ * @example generateRandomArray(4, () => 1) => [1, 1, 1, 1]
194
+ * */
195
+ declare function generateRandomArray<T>(num: number, cb: (item?: unknown, index?: number) => T): T[];
196
+
197
+ /**
198
+ * @description 生成随机颜色
199
+ * @example generateRandomColor() => '#f36a38'
200
+ * */
201
+ declare function generateRandomColor(): string;
202
+
203
+ /**
204
+ * @description 生成随机时间
205
+ * @example generateRandomDate() => '2021-07-01 08:51:34'
206
+ * */
207
+ declare function generateRandomDate(options?: GenerateRandomDate): string;
208
+ interface GenerateRandomDate {
209
+ /**
210
+ * @description 开始时间
211
+ * @default 1800-01-01 00:00:00
212
+ */
213
+ start?: string | Date;
214
+ /**
215
+ * @description 结束时间
216
+ * @default 当前时间
217
+ */
218
+ end?: string | Date;
219
+ /**
220
+ * @description 时间格式
221
+ * @default YYYY-MM-DD HH:mm:ss
222
+ */
223
+ format?: string;
224
+ }
225
+
226
+ /**
227
+ * @description 生成随机邮箱
228
+ * @example generateRandomEmail() => lZtJqMl0dyTO3WDGzFDO@example.com
229
+ */
230
+ declare function generateRandomEmail(): string;
231
+
232
+ /**
233
+ * @description 生成范围内的随机浮点数
234
+ * @example generateRandomFloat() => 0.35
235
+ * */
236
+ declare function generateRandomFloat(options?: GenerateRandomFloat): number;
237
+ type GenerateRandomFloat = number | {
238
+ /**
239
+ * @description 最大值
240
+ * @default 1.00
241
+ */
242
+ max?: number;
243
+ /**
244
+ * @description 最小值
245
+ * @default 0.00
246
+ */
247
+ min?: number;
248
+ /**
249
+ * @description 小数位数
250
+ * @default 2
251
+ */
252
+ fractionDigits?: number;
253
+ };
254
+
255
+ /**
256
+ * @description 生成随机二代身份证号
257
+ * @example generateRandomIdCard() => '410304200210165258'
258
+ */
259
+ declare function generateRandomIdCard(): string;
260
+
261
+ /**
262
+ * @description 生成范围内的随机整数
263
+ * @example generateRandomInteger() => 217342
264
+ * */
265
+ declare function generateRandomInteger(options?: GenerateRandomIntegerOptions): number;
266
+ type GenerateRandomIntegerOptions = number | {
267
+ /**
268
+ * @description 最大值
269
+ * @default Number.MAX_SAFE_INTEGER
270
+ */
271
+ max?: number;
272
+ /**
273
+ * @description 最小值
274
+ * @default 0
275
+ */
276
+ min?: number;
277
+ };
278
+
279
+ /**
280
+ * @description 生成随机手机号码
281
+ * @example generateRandomMobilePhone() => 13012345678
282
+ */
283
+ declare function generateRandomMobilePhone(): string;
284
+
285
+ /**
286
+ * @description 生成随机字符串 大小写字母和数字
287
+ * @param length 返回字符串的长度
288
+ * @example generateRandomString(20) => 'lZtJqMl0dyTO3WDGzFDO'
289
+ * */
290
+ declare function generateRandomString(length: number, options?: GenerateRandomStringOptions): string;
291
+ interface GenerateRandomStringOptions {
292
+ /**
293
+ * @description 是否包含小写字母
294
+ * @default true
295
+ */
296
+ lowerCase?: boolean;
297
+ /**
298
+ * @description 是否包含大写字母
299
+ * @default true
300
+ */
301
+ upperCase?: boolean;
302
+ /**
303
+ * @description 是否包含数字
304
+ * @default true
305
+ */
306
+ number?: boolean;
307
+ }
308
+
309
+ /**
310
+ * @description 生成随机数据从数据源中
311
+ * @param num 随机获取数据源子项次数
312
+ * @example generateRandomStringFromSource(4, ['1', 'b', 'c', 'd', 'e']) => 'dea1'
313
+ * */
314
+ declare function generateRandomStringFromSource(num: number, source: (number | string)[]): string;
315
+
316
+ /**
317
+ * @description 获取数组中的随机元素
318
+ * @example getRandomItem([0, 1, 2, 3, 4]) => 2
319
+ */
320
+ declare function getRandomItem<T>(list: T[]): T;
321
+
322
+ /**
323
+ * @description 校验是否为数字
324
+ */
325
+ declare function isNumberOrNumberString(val: string): boolean;
326
+ /**
327
+ * @description 校验是否为中文
328
+ */
329
+ declare function isChinese(val: string): boolean;
330
+ /**
331
+ * @description 校验是否为英文字母
332
+ */
333
+ declare function isEnglishAphabet(val: string): boolean;
334
+ /**
335
+ * @description 校验是否为大写字母
336
+ */
337
+ declare function isUpperCase(val: string): boolean;
338
+ /**
339
+ * @description 校验是否为大写字母和数字
340
+ */
341
+ declare function isUpperCaseAndNumber(val: string): boolean;
342
+ /**
343
+ * @description 校验是否为大写字母数字和中文
344
+ */
345
+ declare function isUpperCaseAndNumberAndChinese(val: string): boolean;
346
+ /**
347
+ * @description 校验是否为小写字母
348
+ */
349
+ declare function isLowerCase(val: string): boolean;
350
+ /**
351
+ * @description 校验是否为小写字母和数字
352
+ */
353
+ declare function isLowerCaseAndNumber(val: string): boolean;
354
+ /**
355
+ * @description 校验是否为小写字母数字和中文
356
+ */
357
+ declare function isLowerCaseAndNumberAndChinese(val: string): boolean;
358
+
359
+ /**
360
+ * @description 校验是否为浮点数
361
+ */
362
+ declare function isFloat(val: string, floatOptions?: FloatOptions): boolean;
363
+ /**
364
+ * @description 校验是否为大于零的浮点数
365
+ */
366
+ declare function isPositiveFloat(val: string, floatOptions?: FloatOptions): boolean;
367
+ /**
368
+ * @description 校验是否为小于零的浮点数
369
+ */
370
+ declare function isNegativeFloat(val: string, floatOptions?: FloatOptions): boolean;
371
+ /**
372
+ * @description 校验是否为大于等于零的浮点数
373
+ */
374
+ declare function isPositiveFloatOrZero(val: string, floatOptions?: FloatOptions): boolean;
375
+ /**
376
+ * @description 校验是否为小于等于零的浮点数
377
+ */
378
+ declare function isNegativeFloatOrZero(val: string, floatOptions?: FloatOptions): boolean;
379
+ interface FloatOptions {
380
+ /**
381
+ * @description 是否包括整数
382
+ * @default true
383
+ */
384
+ integer?: boolean;
385
+ }
386
+
387
+ /**
388
+ * @description 校验是否为整数
389
+ */
390
+ declare function isInteger(val: string): boolean;
391
+ /**
392
+ * @description 校验否为正整数
393
+ */
394
+ declare function isPositiveInteger(val: string): boolean;
395
+ /**
396
+ * @description 校验是否为大于等于零的整数
397
+ */
398
+ declare function isPositiveIntegerOrZero(val: string): boolean;
399
+ /**
400
+ * @description 校验是否为负整数
401
+ */
402
+ declare function isNegativeInteger(val: string): boolean;
403
+ /**
404
+ * @description 校验是否为小于等于零的整数
405
+ */
406
+ declare function isNegativeIntegerOrZero(val: string): boolean;
407
+
408
+ /**
409
+ * @description 校验是否为手机号
410
+ */
411
+ declare function isMobilePhone(val: string): boolean;
412
+ /**
413
+ * @description 校验是否为邮箱
414
+ */
415
+ declare function isEmail(val: string): boolean;
416
+ /**
417
+ * @description 校验是否为空字符串
418
+ */
419
+ declare function isEmptyString(val: unknown): boolean;
420
+ /**
421
+ * @description 校验是否为合法时间格式
422
+ * @param format 时间格式 默认 YYYY-MM-DD HH:mm:ss
423
+ * @link https://day.js.org/docs/zh-CN/parse/string-format
424
+ */
425
+ declare function isDateString(val: string, format?: string): boolean;
426
+ /**
427
+ * @description 校验是否为二代身份证号
428
+ */
429
+ declare function isIdCard(val: string): boolean;
430
+
431
+ export { type AnyObject, type ArrayItem, type Arrayable, type FloatOptions, type GenerateRandomDate, type GenerateRandomFloat, type GenerateRandomIntegerOptions, type GenerateRandomStringOptions, type ParseOptions, type StringifyOptions, castArray, compareProperties, compose, composeRight, debounce, debugWarn, deepClone, deepMerge, deprecated, error, excludeProperties, extractProperties, generateRandomArray, generateRandomColor, generateRandomDate, generateRandomEmail, generateRandomFloat, generateRandomIdCard, generateRandomInteger, generateRandomMobilePhone, generateRandomString, generateRandomStringFromSource, getRandomItem, hasChanged, hasOwn, initLog, isArray, isBoolean, isChinese, isDate, isDateString, isDef, isEmail, isEmptyString, isEnglishAphabet, isFloat, isFunction, isIdCard, isInteger, isLowerCase, isLowerCaseAndNumber, isLowerCaseAndNumberAndChinese, isMap, isMobilePhone, isNegativeFloat, isNegativeFloatOrZero, isNegativeInteger, isNegativeIntegerOrZero, isNull, isNumber, isNumberOrNumberString, isObject, isPositiveFloat, isPositiveFloatOrZero, isPositiveInteger, isPositiveIntegerOrZero, isPromise, isRegExp, isSet, isString, isSymbol, isUndef, isUndefined, isUpperCase, isUpperCaseAndNumber, isUpperCaseAndNumberAndChinese, log, objectToString, qs, throttle, throwError, toTypeString, validatorChineseOrEnglish, validatorChineseOrEnglishOrNumber, validatorEmail, validatorLatitudeOrLongitude, validatorUppercaseOrNumbersOrSpecial, validatorUppercaseOrNumbersOrUnderline, warn };