@xn-lib/base 0.0.31 → 0.1.2

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,212 @@
1
+ type CommonOption = {
2
+ label: string;
3
+ value: any;
4
+ disabled?: boolean;
5
+ [key: string]: any;
6
+ };
7
+ /**
8
+ * 两个数相乘
9
+ * @param a 数字a
10
+ * @param b 数字b
11
+ * @param [fractionDigits] 截取的小数位数
12
+ * */
13
+ export declare function abMultiply(a: number, b: number, fractionDigits?: number | false): string | number;
14
+ /**
15
+ * 两个数相除
16
+ * @param a 分子
17
+ * @param b 分母
18
+ * @param percentage 是否为百分比的数值,是则乘以100
19
+ * @param fractionDigits 截取的小数位数,默认2,为false时不截取,为true时截取1位
20
+ * */
21
+ export declare function abDivide(a: number, b: number, percentage?: boolean, fractionDigits?: number | false): string | number;
22
+ /**
23
+ * 金额转元,需要除以 ÷ 1000
24
+ * */
25
+ export declare function amountToYuan(price: string | number): number;
26
+ /**
27
+ * 金额转元,并且格式化(用于展示)。
28
+ * @param price 原始金额(厘)
29
+ * @param fractionDigits 保留小数的位数,默认2
30
+ * */
31
+ export declare function formatAmountToYuan(price: number, fractionDigits?: number): string;
32
+ /**
33
+ * 元转金额,需要乘以 × 1000
34
+ * */
35
+ export declare function yuanToAmount(price: string | number): number;
36
+ /**
37
+ * 百分比率转数字(× 100,用于整数值的存储)例如:0.1 -> 100
38
+ * */
39
+ export declare function rateToNumber(rate: string | number): string | number;
40
+ /**
41
+ * 数字转百分比率(÷ 100,用于展示),例如:100 -> 0.1
42
+ * */
43
+ export declare function numberToRate(number: string | number): number;
44
+ /**
45
+ * 比率转百分比(× 100,用于“新”比率的展示、编辑,默认保留 4 位小数)例如:0.010086 -> 1.0086
46
+ * */
47
+ export declare function rateToPercentage(rate: string | number, fractionDigits?: number): number;
48
+ /**
49
+ * 百分比转比率(÷ 100,用于“新”比率的存储)例如:1.0086 -> 0.010086
50
+ * */
51
+ export declare function percentageToRate(rate: string | number): number;
52
+ /**
53
+ * 百分比率格式化,(× 100,保留2位小数,加%,用于展示)例如:0.01 -> 1.00%
54
+ * */
55
+ export declare function formatRate(rate: string | number): string;
56
+ /**
57
+ * 替换字符串文本的值
58
+ * @param content - 原始内容,如果是数字类型则先转为字符串
59
+ * @param searchValue - 被替换的内容,可以是正则
60
+ * @param replaceValue - 新的内容
61
+ */
62
+ export declare function replaceText(content: any, searchValue?: string | RegExp, replaceValue?: string): string;
63
+ /**
64
+ * 转换文件大小
65
+ * -- 按单位转换,例如:字节数(bytes) / 1024 返回为 KB
66
+ * @param size 原始数值,例如字节
67
+ * @param units 单位
68
+ * @param fractionDigits 截取位数
69
+ * @param emptyValue 默认值
70
+ * */
71
+ export declare function transformFileSize(size?: number, units?: number, fractionDigits?: number, emptyValue?: string): string;
72
+ /**
73
+ * 驼峰字符串转换为下划线
74
+ * @param {string} str 原始字符串
75
+ * @return {string}
76
+ * */
77
+ export declare function convertToLowerCase(str: string): string;
78
+ /**
79
+ * 下划线字符串转为换驼峰
80
+ * @param {string} str 原始字符串
81
+ * @return {string}
82
+ * */
83
+ export declare function convertToUpperCase(str: string): string;
84
+ /**
85
+ * 获取某个对象属性的值,支持多级查找
86
+ * @param obj 被查找的对象
87
+ * @param _keys 要查找的属性名称,列如:1, 'a.b', 'b.0.a', ['a', 'b']
88
+ * @param defaultValue 找不属性时的默认值
89
+ * @return {any}
90
+ * */
91
+ export declare function getValue(obj: Record<string, any>, _keys: number | string | string[], defaultValue?: any): any;
92
+ /**
93
+ * 根据已有的值,查找所有父级的值列表
94
+ * @param data 数据列表
95
+ * @param value 当前值
96
+ * @param key 值的属性名,默认`id`
97
+ * @param parentKey 父级的属性名,默认`parentId`
98
+ * @return V[] 返回值列表
99
+ * */
100
+ export declare function getValuesForTree<T extends Record<any, any>, V>(data: T[], value: V, key?: string, parentKey?: string): V[];
101
+ /**
102
+ * 查找对象列表中对应的某一项的值
103
+ * @param list 对象列表
104
+ * @param key 匹配的属性名
105
+ * @param value 当前值
106
+ * @param useKey 读取指定的属性(成功匹配后,直接返回其属性值)
107
+ * @return 匹配项的值 或 原始值
108
+ * */
109
+ export declare function findItemValue<T extends Record<any, any>>(list: T[], key: string, value: any, useKey?: string): any;
110
+ /**
111
+ * 移除对象列表中的某一项(匹配的属性值)
112
+ * @param list 对象列表
113
+ * @param key 匹配的属性名
114
+ * @param value 当前值
115
+ * */
116
+ export declare function removeItem<T extends Record<any, any>>(list: T[], key: string, value: any): void;
117
+ /**
118
+ * 移除列表中的某个值
119
+ * @param list 值列表
120
+ * @param value 当前值
121
+ * */
122
+ export declare function removeValue<T>(list: T[], value: T): void;
123
+ /**
124
+ * 获取静态字典列表中某项的标题
125
+ * @param list 数据列表
126
+ * @param value 当前值
127
+ * @param defaultValue 默认值
128
+ * */
129
+ export declare function getDictLabel(list: Record<any, any>[], value: any, defaultValue?: string): string;
130
+ /**
131
+ * 根据属性名查找数组中所有重复项
132
+ * @param list 原始数组
133
+ * @param props 需要要校验的属性,可以是多个(值的匹配方式为"且")
134
+ * @return {[]} 重复项的数组
135
+ * */
136
+ export declare function findRepeatedItems<T extends Record<string, any>>(list: T[], props: string | string[]): T[];
137
+ export declare function parseJsonStr(text: any): any;
138
+ export declare function toJsonString(data: any, notEmpty?: boolean): string;
139
+ /**
140
+ * 修剪字符串类型的数据,清除两端空格
141
+ * 如果是 Object,其所有属性 JSON 类型的字符串也会被修剪
142
+ * @param {any} data 原始数据
143
+ * @return any
144
+ * */
145
+ export declare function trimDataValues(data: any): any;
146
+ export declare function removeEmptyString(value: string): string;
147
+ /**
148
+ * 针对 GET 请求时,过滤空值的条件
149
+ * @param params 原始对象
150
+ * @param isTrim 是否需要剔除`字符串类型的值`两端的空格
151
+ * */
152
+ export declare function removeEmptyProps(params: any, isTrim?: boolean): any;
153
+ /**
154
+ * 数字四舍五入
155
+ * @param number 原始数值
156
+ * @param precision 精度,默认0
157
+ * */
158
+ export declare function round(number: number, precision?: number): number;
159
+ /**
160
+ * 截取浮点数字的小数
161
+ * @param number 原始数值
162
+ * @param fractionDigits 截取的位数,默认2(普通的四舍五入)
163
+ * */
164
+ export declare function toFixed(number: number, fractionDigits?: number): string;
165
+ /**
166
+ * 数字格式化
167
+ * 例如:888888.88 -> 888,888.88
168
+ * @param value 数值
169
+ * @param p 分隔开的位数,默认3
170
+ * */
171
+ export declare function formatNumber(value: number | string, p?: number): string;
172
+ /**
173
+ * 拆分字符串为数组
174
+ * @param str 原始字符串
175
+ * @param separator 分隔符
176
+ * */
177
+ export declare function splitString(str: string, separator: string | RegExp): string[];
178
+ /**
179
+ * 拆分字符串形式的数据为数组
180
+ * @param str 原始字符串
181
+ * */
182
+ export declare function splitStringData(str: string): string[];
183
+ /**
184
+ * 补充24个小时段的数据
185
+ * @param hoursData 已存在的小时段数据
186
+ * @param padData 填充的数据
187
+ * @param needPadAfter 是否需要补充当前小时段之后的数据(例如:今日数据的当前时段)
188
+ * */
189
+ export declare function paddingHoursData<T extends Record<any, any>>(hoursData: T[], padData: T, needPadAfter?: boolean): T[];
190
+ export declare function getNumberValue(value: any, defaultValue?: number): number | undefined;
191
+ /**
192
+ * 格式化字符串列表
193
+ * @param str 原始字符串
194
+ * @description 将中文逗号转为英文逗号,空格转为英文逗号,过滤空值,最后用英文逗号连接
195
+ */
196
+ export declare function formatStringList(str: string): string;
197
+ /**
198
+ * 格式化金额显示
199
+ * @param value 金额值
200
+ * @param defaultValue 默认值
201
+ */
202
+ export declare function formatAmount(value: number | string, defaultValue?: string): string;
203
+ export declare function thousandMark(value: number): string;
204
+ export declare function percentage(value: number): string;
205
+ export declare function objectToOptions(obj: Record<string, string>): {
206
+ value: string;
207
+ label: string;
208
+ }[];
209
+ export declare function listToOptions<T extends object>(list: T[], labelKey: keyof T, valueKey: keyof T, options?: {
210
+ mapFn?: (item: T, index: number) => Record<string, any>;
211
+ }): CommonOption[] & Record<string, any>;
212
+ export {};
@@ -0,0 +1,157 @@
1
+ import { Dayjs } from 'dayjs';
2
+ export type DateParam = number | string | Date | null;
3
+ export type DateInput = number | string | Date | Dayjs | null | undefined;
4
+ /**
5
+ * 路由查询参数类型
6
+ */
7
+ export interface LocationQuery {
8
+ startDate?: string | string[];
9
+ endDate?: string | string[];
10
+ [key: string]: string | string[] | undefined;
11
+ }
12
+ /**
13
+ * 请求参数类型
14
+ */
15
+ export interface RequestParams {
16
+ rptDtBegin?: string;
17
+ rptDtEnd?: string;
18
+ [key: string]: any;
19
+ }
20
+ /**
21
+ * 日期快捷选项类型
22
+ */
23
+ export interface DatePickerShortcut {
24
+ text: string;
25
+ value: () => [string, string];
26
+ }
27
+ /**
28
+ * 创建一个日期对象
29
+ * @param time 可用的日期参数
30
+ * @returns Date 对象
31
+ */
32
+ export declare function newDate(time: DateParam): Date;
33
+ /**
34
+ * 获取当前页面的默认`范围日期`
35
+ * - 首先,尝试读取路由中的参数
36
+ * - 其次,创建当前日期
37
+ * @param query 路由 query 对象。为 null 时表示使用当前时间
38
+ * @param fmt 自定义格式。传入字符串格式时作用于开始和结束、数组时对应开始和结束、固定格式类型(例如需要查询 `daily`每日 或 `hour`小时 的数据)
39
+ * @returns 日期范围数组 [开始日期, 结束日期]
40
+ */
41
+ export declare function getDefaultDates(query: LocationQuery | null, fmt?: 'daily' | 'hour' | string | string[]): string[];
42
+ /**
43
+ * 设置接口请求的范围日期
44
+ * - 为结束日期增加 1个月 或 1天 或 1小时
45
+ * @param params 请求参数的对象
46
+ * @param type 增加的类型
47
+ * @param fmt 处理后的日期格式
48
+ * @returns 处理后的请求参数对象
49
+ */
50
+ export declare function setRequestDates(params: RequestParams, type: 'month' | 'daily' | 'hour', fmt?: string): RequestParams;
51
+ /**
52
+ * 判断两个日期是否为同一天
53
+ * @param start 开始日期
54
+ * @param end 结束日期
55
+ * @returns boolean
56
+ */
57
+ export declare function isSameDate(start: DateParam, end: DateParam): boolean;
58
+ /**
59
+ * 获取两个日期的间隔时间(天数)
60
+ * @param start 开始日期
61
+ * @param end 结束日期
62
+ * @returns 间隔天数
63
+ */
64
+ export declare function getRangeDays(start: DateParam, end: DateParam): number;
65
+ /**
66
+ * 格式化日期为 YYYY-MM-DD 格式
67
+ * @param time 可用的日期
68
+ * @returns 格式化后的日期字符串
69
+ */
70
+ export declare function formatDateToYMD(time?: DateParam): string;
71
+ /**
72
+ * 格式化日期为 YYYY-MM 格式
73
+ * @param time 可用的日期
74
+ * @returns 格式化后的日期字符串
75
+ */
76
+ export declare function formatDateToYM(time?: DateParam): string;
77
+ /**
78
+ * 格式化日期
79
+ * @param time 可用的日期
80
+ * @param fmt 格式,默认 YYYY-MM-DD HH:mm:ss
81
+ * @param padZero 是否`月日时分秒`小于10时填充0,默认填充(可传入字符串指定)。例如 06-01 09:02
82
+ * @returns 格式化后的日期字符串
83
+ */
84
+ export declare function formatDate(time: DateParam, fmt?: string, padZero?: string | boolean): string;
85
+ /**
86
+ * 设置某个日期的月份
87
+ * @param time 可用的日期
88
+ * @param months 需要增加的月数,默认0(支持负数,例如:上个月`-1`)
89
+ * @param fmt 格式,请查看 formatDate()
90
+ * @param dayValue 从 1 到 31 之间的整数,月份中的第几天(JavaScript 1.3 版本之前),默认 1
91
+ * @returns 格式化后的日期字符串
92
+ */
93
+ export declare function setMonths(time: DateParam, months?: number, fmt?: string, dayValue?: number): string;
94
+ /**
95
+ * 为某个日期增加 n 天
96
+ * @param time 可用的日期
97
+ * @param days 需要增加的天数,默认1
98
+ * @param fmt 格式,请查看 formatDate()
99
+ * @returns 格式化后的日期字符串或 Date 对象
100
+ */
101
+ export declare function addDays(time: DateParam, days?: number, fmt?: string): string | Date;
102
+ /**
103
+ * 为某个日期增加 n 小时 -- 适用于精确到小时的范围日期
104
+ * @param time 可用的日期
105
+ * @param hours 需要增加的小时,默认1
106
+ * @param fmt 格式,请查看 formatDate()
107
+ * @returns 格式化后的日期字符串或 Date 对象
108
+ */
109
+ export declare function addHours(time: DateParam, hours?: number, fmt?: string): string | Date;
110
+ /**
111
+ * 为某个日期增加秒数
112
+ * @param time 可用的日期
113
+ * @param seconds 需要增加的秒数
114
+ * @param fmt 格式,请查看 formatDate()
115
+ * @returns 格式化后的日期字符串或 Date 对象
116
+ */
117
+ export declare function addSeconds(time: DateParam, seconds: number, fmt?: string): string | Date;
118
+ /**
119
+ * 获取范围日期的快捷选取的配置
120
+ * @param configs 快捷选项配置数组,每个元素为 [天数, 文本] 的元组
121
+ * @returns 日期快捷选项数组
122
+ */
123
+ export declare function getPickerShortcuts(configs?: [number, string][]): DatePickerShortcut[];
124
+ /**
125
+ * 校验两个日期的天数是否超出范围(默认最大 93 天)
126
+ * @param begin 开始日期
127
+ * @param end 结束日期
128
+ * @param maxDays 最大天数,默认 93
129
+ * @returns 错误信息字符串,如果未超出范围则返回 undefined
130
+ */
131
+ export declare function validateDatesRange(begin: string, end: string, maxDays?: number): string | undefined;
132
+ /**
133
+ * 判断是否为 10 位数字的秒级 unix 时间戳
134
+ * @param v 日期输入
135
+ * @returns boolean
136
+ */
137
+ export declare function isUnix(v: DateInput): boolean;
138
+ /**
139
+ * 将任意输入标准化为 Dayjs 对象
140
+ * @param v 日期输入
141
+ * @returns Dayjs 对象
142
+ */
143
+ export declare const standardTime: (v: DateInput) => Dayjs;
144
+ /**
145
+ * 格式化时间,默认 'YYYY-MM-DD HH:mm:ss'
146
+ * 返回 undefined/null 时原样返回
147
+ * @param v 日期输入
148
+ * @param format 日期格式,默认 'YYYY-MM-DD HH:mm:ss'
149
+ * @returns 格式化后的日期字符串,或 undefined/null
150
+ */
151
+ export declare const formatTime: (v: DateInput, format?: string) => string | undefined | null;
152
+ /**
153
+ * 转为 unix 时间戳(秒)
154
+ * @param date 日期输入
155
+ * @returns Unix 时间戳(秒)
156
+ */
157
+ export declare function toUnix(date: DateInput): number;
@@ -0,0 +1,6 @@
1
+ export * from './md5.utils';
2
+ export * from './data.utils';
3
+ export * from './validate.utils';
4
+ export * from './base.utils';
5
+ export * from './tree.utils';
6
+ export * from './date.utils';
@@ -0,0 +1,2 @@
1
+ export declare function createJsonMd5(json?: {}): string | null;
2
+ export declare function createMd5(content?: string): string;
@@ -0,0 +1,66 @@
1
+ /**
2
+ * 递归遍历树形数据的选项
3
+ */
4
+ export interface RecursionTreeOptions<T = any> {
5
+ /** 子节点的键名,默认为 'children' */
6
+ childrenKey?: string;
7
+ /** 访问每个节点时的回调函数 */
8
+ visit?: (item: T, parent: T | null, index: number, path: T[]) => void;
9
+ /** 停止遍历的条件函数,返回 true 时停止遍历 */
10
+ stop?: (item: T) => boolean;
11
+ }
12
+ /**
13
+ * 递归遍历树形数据
14
+ * @param data 树形数据,可以是数组或单个对象
15
+ * @param options 遍历选项
16
+ * @returns void
17
+ */
18
+ export declare const recursionTree: <T extends Record<string, any> = any>(data?: T | T[], options?: RecursionTreeOptions<T>) => void;
19
+ /**
20
+ * 数组转树形结构的选项
21
+ */
22
+ export interface ArrayToTreeOptions<T = any> {
23
+ /** 父节点的键名,默认为 'parentId' */
24
+ parentKey?: string;
25
+ /** 子节点的键名,默认为 'children' */
26
+ childrenKey?: string;
27
+ /** 格式化每个节点的函数 */
28
+ formatItem?: (item: T) => T;
29
+ }
30
+ /**
31
+ * 将扁平数组转换为树形结构
32
+ * @param list 扁平数组
33
+ * @param options 转换选项
34
+ * @returns 树形结构的根节点数组
35
+ */
36
+ export declare const arrayToTree: <T extends Record<string, any> = any>(list?: T[], options?: ArrayToTreeOptions<T>) => T[];
37
+ /**
38
+ * 过滤树形数据的选项
39
+ */
40
+ export interface FilterTreeOptions {
41
+ /** 子节点的键名,默认为 'children' */
42
+ childrenKey?: string;
43
+ }
44
+ /**
45
+ * 过滤树形数据
46
+ * @param tree 树形数据数组
47
+ * @param fn 过滤函数,返回 true 保留该节点
48
+ * @param options 过滤选项
49
+ * @returns 过滤后的树形数据
50
+ */
51
+ export declare const filterTree: <T extends Record<string, any> = any>(tree: T[], fn: (item: T, index: number) => boolean, options?: FilterTreeOptions) => T[];
52
+ /**
53
+ * 映射树形数据的选项
54
+ */
55
+ export interface MapTreeOptions {
56
+ /** 子节点的键名,默认为 'children' */
57
+ childrenKey?: string;
58
+ }
59
+ /**
60
+ * 映射树形数据(转换每个节点)
61
+ * @param tree 树形数据数组
62
+ * @param fn 映射函数,返回转换后的节点
63
+ * @param options 映射选项
64
+ * @returns 映射后的树形数据
65
+ */
66
+ export declare const mapTree: <T extends Record<string, any> = any, R = T>(tree: T[], fn: (item: T, index: number) => R, options?: MapTreeOptions) => R[];
@@ -0,0 +1,94 @@
1
+ /**
2
+ * 各类数据及类型的校验
3
+ * */
4
+ /**
5
+ * 校验是否为有效的url
6
+ * 仅格式校验,不做严各的真实性校验
7
+ * @param {string} content url内容
8
+ * @return {boolean}
9
+ * */
10
+ export declare function isURL(content: string): boolean;
11
+ /**
12
+ * 是否为数字
13
+ * 范围:正负数、浮点数
14
+ * @param value 数值
15
+ * @param floatLength 小数位数,默认不限
16
+ * */
17
+ export declare function isNumber(value: any, floatLength?: number): boolean;
18
+ /**
19
+ * 是否为整数
20
+ * 范围:正负数
21
+ * @param {any} value 数值
22
+ * @return {boolean}
23
+ */
24
+ export declare function isInteger(value: any): boolean;
25
+ /**
26
+ * 是否为非0的有效数字
27
+ * @param value 数值
28
+ */
29
+ export declare function isNumberNot0(value: number | string): boolean;
30
+ /**
31
+ * 是否为浮点数字
32
+ * @description 范围:正整数、浮点正数(无符号)
33
+ * @param value 必选,被校验的值 (.11 这样的数字在校验时会自动转为0.11)
34
+ * @param length 可选,浮点位数,默认 2
35
+ * */
36
+ export declare function isFloat(value: string, length?: number): boolean;
37
+ /**
38
+ * 是否为正整数
39
+ * @description 范围:正整数
40
+ * @param value 必选,被校验的值
41
+ * */
42
+ export declare function isPositiveFloat(value: string): boolean;
43
+ /**
44
+ * 是否为空
45
+ * @param target 目标对象
46
+ * @return {boolean}
47
+ */
48
+ export declare function isEmpty(target: any): boolean;
49
+ /**
50
+ * 是否为Object对象
51
+ * @param {any} target 需要校验的目标
52
+ * @param {Boolean} [isEmpty] 是否允许为空对象
53
+ * @return {boolean}
54
+ * */
55
+ export declare function isObject(target: any, isEmpty?: boolean): boolean;
56
+ /**
57
+ * 是否为空Object对象
58
+ * @param {any} target 需要校验的目标
59
+ * @return {boolean}
60
+ * */
61
+ export declare function isEmptyObject(target: any): boolean;
62
+ /**
63
+ * 判断空值,'undefined'、null、''
64
+ * @param {any} value 任意形式的值
65
+ * */
66
+ export declare function isEmptyValue(value: any): boolean;
67
+ export declare function isFunc(func: any): boolean;
68
+ export declare function isUndefined(value: any): value is undefined;
69
+ export declare function isString(value: any): value is string;
70
+ export declare function isMobilePhone(value: any): boolean;
71
+ export declare function isEmail(value: any): boolean;
72
+ export declare function isChinese(string: any): RegExpExecArray | null;
73
+ export declare function isPromise(target: any): boolean;
74
+ export declare function isBlob(value: any): boolean;
75
+ export declare function isColor(value: string): boolean;
76
+ export declare const compareNumberAb: (a?: string, b?: string, label?: string, isAmount?: boolean) => string;
77
+ export declare const replaceSpaceAndChineseComma: (value: string) => string;
78
+ export declare const Regs: {
79
+ ios: RegExp;
80
+ android: RegExp;
81
+ harmony: RegExp;
82
+ wechat: RegExp;
83
+ alipay: RegExp;
84
+ chrome: RegExp;
85
+ phone: RegExp;
86
+ };
87
+ export declare function getDeviceInfo(): {
88
+ os: Record<any, any>;
89
+ browser: Record<any, any>;
90
+ };
91
+ export declare function detect(ua: string): {
92
+ os: Record<any, any>;
93
+ browser: Record<any, any>;
94
+ };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "private": false,
3
3
  "name": "@xn-lib/base",
4
- "version": "0.0.31",
4
+ "version": "0.1.2",
5
5
  "description": "",
6
6
  "main": "./dist/index.cjs",
7
7
  "module": "./dist/index.mjs",