chen.lin
1 天以前 79edfec1f6e6789d3f6cc57db3cb0cfdffd64c32
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
 
/** 库存/数量显示:保留最多6位小数,去掉末尾多余的0(不强制补零) */
export const formatQuantity = (value) => {
    if (value == null || value === '') return '0';
    const n = Number(value);
    if (Number.isNaN(n)) return '0';
    if (n < 0) return '0';
    return n % 1 === 0 ? String(n) : n.toFixed(6).replace(/\.?0+$/, '');
};
 
/** 校验最多 N 位小数,用于数量类字段;超过时返回错误信息并阻止提交 */
export const maxDecimalPlaces = (maxDecimals, message) => {
    const factor = Math.pow(10, maxDecimals);
    const msg = message || `最多${maxDecimals}位小数`;
    return (value) => {
        if (value == null || value === '') return undefined;
        const n = Number(value);
        if (Number.isNaN(n)) return undefined;
        const rounded = Math.round(n * factor) / factor;
        if (Math.abs(n - rounded) > 1e-10) return msg;
        return undefined;
    };
};
 
/** 判断数值是否超过 6 位小数(用于提交前校验) */
export const hasMoreThan6Decimals = (value) => {
    if (value == null || value === '') return false;
    const n = Number(value);
    if (Number.isNaN(n)) return false;
    const rounded = Math.round(n * 1e6) / 1e6;
    return Math.abs(n - rounded) > 1e-10;
};
 
export const extractNavMenus = (data) => {
    if (!data) {
        return;
    }
    const navMenus = [];
    const traverse = (nodes) => {
        nodes.forEach((node) => {
            if (!node.children) {
                navMenus.push(node);
            } else {
                traverse(node.children);
            }
        });
    };
    traverse(data);
    return navMenus;
};
 
export const integrateParams = (_params) => {
    const { pagination, sort, filter, ...other } = _params;
    return {
        current: pagination?.page,
        pageSize: pagination?.perPage,
        orderBy: sort?.field + ' ' + sort?.order,
        ...filter,
        ...other
    }
}
 
export const camelToPascalWithSpaces = (camelCaseString) => {
    if (typeof camelCaseString !== 'string') {
        return '';
    }
    if (camelCaseString.trim() === '') {
        return '';
    }
    return camelCaseString
        .replace(/([a-z0-9])([A-Z])/g, '$1 $2')
        .replace(/^\w/, (c) => c.toUpperCase());
}
 
export const flattenTree = (nodes, depth = 0) => {
    let result = [];
    nodes.forEach(node => {
        result.push({ ...node, depth });
        if (node.children && node.children.length > 0) {
            result = result.concat(flattenTree(node.children, depth + 1));
        }
    });
    return result;
};
 
export const haveChildren = (item) => {
    if (Array.isArray(item)) {
        return item.map((k) => haveChildren(k));
    }
 
    if (item && typeof item === 'object') {
        if (item.id !== undefined) {
            item.id = item.id.toString();
        }
 
        if (item.children && Array.isArray(item.children)) {
            item.children = haveChildren(item.children);
        }
    }
 
    return item;
};