util-helpers 4.15.0 → 4.15.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.
- package/dist/util-helpers.js +82 -72
- package/dist/util-helpers.js.map +1 -1
- package/dist/util-helpers.min.js +1 -1
- package/dist/util-helpers.min.js.map +1 -1
- package/esm/bytesToSize.js +5 -2
- package/esm/filterTree.js +3 -4
- package/esm/findTreeNode.js +3 -0
- package/esm/findTreeNodes.js +3 -0
- package/esm/findTreeSelect.js +3 -4
- package/esm/formatMoney.js +11 -9
- package/esm/listToTree.js +34 -30
- package/esm/parseIdCard.js +10 -13
- package/esm/randomString.js +3 -5
- package/esm/transformFieldNames.js +1 -2
- package/esm/treeToList.js +4 -1
- package/esm/utils/config.js +1 -1
- package/lib/bytesToSize.js +5 -2
- package/lib/filterTree.js +3 -4
- package/lib/findTreeNode.js +3 -0
- package/lib/findTreeNodes.js +3 -0
- package/lib/findTreeSelect.js +3 -4
- package/lib/formatMoney.js +11 -9
- package/lib/listToTree.js +34 -30
- package/lib/parseIdCard.js +10 -13
- package/lib/randomString.js +3 -5
- package/lib/transformFieldNames.js +1 -2
- package/lib/treeToList.js +4 -1
- package/lib/utils/config.js +1 -1
- package/package.json +2 -1
- package/types/bytesToSize.d.ts +2 -0
- package/types/formatMoney.d.ts +12 -10
- package/types/listToTree.d.ts +2 -2
package/dist/util-helpers.js
CHANGED
|
@@ -537,7 +537,7 @@
|
|
|
537
537
|
}
|
|
538
538
|
|
|
539
539
|
// eslint-disable-next-line no-undef
|
|
540
|
-
var version = "4.15.
|
|
540
|
+
var version = "4.15.2";
|
|
541
541
|
|
|
542
542
|
/**
|
|
543
543
|
* 打印警告信息
|
|
@@ -1897,25 +1897,25 @@
|
|
|
1897
1897
|
* @alias module:Processor.formatMoney
|
|
1898
1898
|
* @since 1.1.0
|
|
1899
1899
|
* @param {string | number} num 需转换金额 (最大:9007199254740991 最小: -9007199254740991)
|
|
1900
|
-
* @param {Object} [options]
|
|
1901
|
-
* @param {
|
|
1902
|
-
* @param {string} [options.symbol]
|
|
1903
|
-
* @param {string} [options.thousand=","]
|
|
1904
|
-
* @param {string} [options.decimal="."]
|
|
1900
|
+
* @param {Object} [options] 金额格式化配置
|
|
1901
|
+
* @param {number} [options.precision=2] 保留位数 (最高:10位)
|
|
1902
|
+
* @param {string} [options.symbol] 货币符号
|
|
1903
|
+
* @param {string} [options.thousand=","] 千分位符号
|
|
1904
|
+
* @param {string} [options.decimal="."] 小数位符号
|
|
1905
1905
|
* @returns {string} 格式化的金额
|
|
1906
1906
|
* @example
|
|
1907
1907
|
*
|
|
1908
1908
|
* // 整数
|
|
1909
|
-
* formatMoney(
|
|
1909
|
+
* formatMoney(1000); // 1,000.00
|
|
1910
1910
|
*
|
|
1911
1911
|
* // 小数(默认保留2位小数)
|
|
1912
|
-
* formatMoney(
|
|
1912
|
+
* formatMoney(3000.03); // 3,000.03
|
|
1913
1913
|
*
|
|
1914
1914
|
* // 保留4位小数
|
|
1915
|
-
* formatMoney(
|
|
1915
|
+
* formatMoney(3000.03, { precision: 4 }); // 3,000.0300
|
|
1916
1916
|
*
|
|
1917
1917
|
* // 保留10位小数
|
|
1918
|
-
* formatMoney(
|
|
1918
|
+
* formatMoney(1500.2, { precision: 10 }); // 1,500.2000000000
|
|
1919
1919
|
*
|
|
1920
1920
|
* // 自定义单位符号
|
|
1921
1921
|
* formatMoney(1000.00, { symbol: '$' }); // $1,000.00
|
|
@@ -1926,6 +1926,8 @@
|
|
|
1926
1926
|
* // 自定义小数位分割符(默认'.')
|
|
1927
1927
|
* formatMoney(1000.00, { decimal: '&' }); // 1,000&00
|
|
1928
1928
|
*
|
|
1929
|
+
* // 字符串数字
|
|
1930
|
+
* formatMoney('3000.03', { precision: 4 }); // 3,000.0300
|
|
1929
1931
|
*/
|
|
1930
1932
|
var formatMoney = function formatMoney(num) {
|
|
1931
1933
|
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
@@ -2319,6 +2321,7 @@
|
|
|
2319
2321
|
* @param {number} bytes 字节大小
|
|
2320
2322
|
* @param {Object} [options] 配置项
|
|
2321
2323
|
* @param {string} [options.spaceMark=' '] 间隔字符
|
|
2324
|
+
* @param {number} [options.precision=2] 精度
|
|
2322
2325
|
* @returns {string} 存储单位值
|
|
2323
2326
|
* @example
|
|
2324
2327
|
*
|
|
@@ -2335,14 +2338,16 @@
|
|
|
2335
2338
|
function bytesToSize(bytes) {
|
|
2336
2339
|
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
2337
2340
|
var _options$spaceMark = options.spaceMark,
|
|
2338
|
-
spaceMark = _options$spaceMark === void 0 ? ' ' : _options$spaceMark
|
|
2341
|
+
spaceMark = _options$spaceMark === void 0 ? ' ' : _options$spaceMark,
|
|
2342
|
+
_options$precision = options.precision,
|
|
2343
|
+
precision = _options$precision === void 0 ? 2 : _options$precision;
|
|
2339
2344
|
var numBytes = typeof bytes !== 'number' ? Number(bytes) : bytes;
|
|
2340
2345
|
if (numBytes === 0 || isNaN(numBytes)) return "0".concat(spaceMark, "B");
|
|
2341
2346
|
var k = 1024;
|
|
2342
2347
|
// 存储单位
|
|
2343
2348
|
var sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
|
|
2344
2349
|
var i = Math.floor(Math.log(numBytes) / Math.log(k));
|
|
2345
|
-
return sizes[i] ? "".concat(Number((numBytes / Math.pow(k, i)).toFixed(
|
|
2350
|
+
return sizes[i] ? "".concat(Number((numBytes / Math.pow(k, i)).toFixed(precision))).concat(spaceMark).concat(sizes[i]) : numBytes + '';
|
|
2346
2351
|
}
|
|
2347
2352
|
|
|
2348
2353
|
var regIdCard = /*#__PURE__*/_wrapRegExp(/^(\d{2})(\d{2})(\d{2})((?:\d{2})?\d{2})(\d{2})(\d{2})\d{2}(\d)(?:\d|X)?$/i, {
|
|
@@ -2441,24 +2446,21 @@
|
|
|
2441
2446
|
*
|
|
2442
2447
|
*/
|
|
2443
2448
|
function parseIdCard(id) {
|
|
2444
|
-
|
|
2449
|
+
var match = regIdCard.exec(id);
|
|
2450
|
+
if (!match) {
|
|
2445
2451
|
return null;
|
|
2446
2452
|
}
|
|
2447
2453
|
|
|
2448
|
-
/** @type {RegExpExecArray} */
|
|
2449
|
-
// @ts-ignore
|
|
2450
|
-
var info = regIdCard.exec(id);
|
|
2451
|
-
|
|
2452
2454
|
/** @type {{ province: string, city: string, area: string, year: string, month: string, day: string, gender: string }} */
|
|
2453
2455
|
// @ts-ignore
|
|
2454
|
-
var origin =
|
|
2455
|
-
province:
|
|
2456
|
-
city:
|
|
2457
|
-
area:
|
|
2458
|
-
year:
|
|
2459
|
-
month:
|
|
2460
|
-
day:
|
|
2461
|
-
gender:
|
|
2456
|
+
var origin = match.groups || {
|
|
2457
|
+
province: match[1],
|
|
2458
|
+
city: match[2],
|
|
2459
|
+
area: match[3],
|
|
2460
|
+
year: match[4],
|
|
2461
|
+
month: match[5],
|
|
2462
|
+
day: match[6],
|
|
2463
|
+
gender: match[7]
|
|
2462
2464
|
};
|
|
2463
2465
|
var province = Provinces.find(function (item) {
|
|
2464
2466
|
return item[0] === origin.province;
|
|
@@ -2744,8 +2746,7 @@
|
|
|
2744
2746
|
* @param {Array.<object>} arr 列表数据
|
|
2745
2747
|
* @returns {*}
|
|
2746
2748
|
*/
|
|
2747
|
-
function recusion() {
|
|
2748
|
-
var arr = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
|
|
2749
|
+
function recusion(arr) {
|
|
2749
2750
|
return arr.map(function (item) {
|
|
2750
2751
|
if (!isObject(item)) {
|
|
2751
2752
|
return item;
|
|
@@ -2793,29 +2794,30 @@
|
|
|
2793
2794
|
* @private
|
|
2794
2795
|
* @template {Record<string,any>} [T=Record<string,any>]
|
|
2795
2796
|
* @param {T[]} arr 列表数据
|
|
2796
|
-
* @param {object} options 配置项
|
|
2797
|
+
* @param {object} [options] 配置项
|
|
2797
2798
|
* @param {string} [options.childrenField='children'] 子级字段名称
|
|
2798
|
-
* @param {'none'|'null'
|
|
2799
|
-
*/
|
|
2800
|
-
function processEmptyChildren(arr) {
|
|
2801
|
-
var
|
|
2802
|
-
|
|
2803
|
-
childrenField =
|
|
2804
|
-
|
|
2805
|
-
emptyChildrenValue =
|
|
2799
|
+
* @param {'none'|'null'} [options.emptyChildrenValue='none'] 子级为空时的值,none表示删除该子级,null表示为null,array表示为[]。
|
|
2800
|
+
*/
|
|
2801
|
+
function processEmptyChildren(arr, options) {
|
|
2802
|
+
var _ref = options || {},
|
|
2803
|
+
_ref$childrenField = _ref.childrenField,
|
|
2804
|
+
childrenField = _ref$childrenField === void 0 ? 'children' : _ref$childrenField,
|
|
2805
|
+
_ref$emptyChildrenVal = _ref.emptyChildrenValue,
|
|
2806
|
+
emptyChildrenValue = _ref$emptyChildrenVal === void 0 ? 'none' : _ref$emptyChildrenVal;
|
|
2806
2807
|
arr.forEach(function (item) {
|
|
2807
|
-
if (isObject(item) && Array.isArray(item[childrenField])) {
|
|
2808
|
-
|
|
2809
|
-
|
|
2810
|
-
|
|
2811
|
-
|
|
2812
|
-
} else if (emptyChildrenValue === 'none') {
|
|
2813
|
-
delete item[childrenField];
|
|
2814
|
-
}
|
|
2808
|
+
// if (isObject(item) && Array.isArray(item[childrenField])) {
|
|
2809
|
+
if (item[childrenField].length <= 0) {
|
|
2810
|
+
if (emptyChildrenValue === 'null') {
|
|
2811
|
+
// @ts-ignore
|
|
2812
|
+
item[childrenField] = null;
|
|
2813
|
+
// } else if (emptyChildrenValue === 'none') { // emptyChildrenValue='array' 不会执行该内部方法
|
|
2815
2814
|
} else {
|
|
2816
|
-
|
|
2815
|
+
delete item[childrenField];
|
|
2817
2816
|
}
|
|
2817
|
+
} else {
|
|
2818
|
+
processEmptyChildren(item[childrenField], options);
|
|
2818
2819
|
}
|
|
2820
|
+
// }
|
|
2819
2821
|
});
|
|
2820
2822
|
}
|
|
2821
2823
|
|
|
@@ -2828,7 +2830,7 @@
|
|
|
2828
2830
|
* @template {Record<string,any>} [T=Record<string,any>]
|
|
2829
2831
|
* @template {*} [R=T&Record<string,any>]
|
|
2830
2832
|
* @param {T[]} list 列表数据
|
|
2831
|
-
* @param {object} options 配置项
|
|
2833
|
+
* @param {object} [options] 配置项
|
|
2832
2834
|
* @param {string} [options.keyField='id'] 当前数据的键值字段名称
|
|
2833
2835
|
* @param {string} [options.parentField='pid'] 当前数据的父级字段名称
|
|
2834
2836
|
* @param {string} [options.childrenField='children'] 子级字段名称
|
|
@@ -2854,24 +2856,27 @@
|
|
|
2854
2856
|
* // [{"id":"1","name":"首页","code":"trade","pid":null},{"id":"2","name":"交易管理","code":"trade","pid":null,"childs":[{"id":"3","name":"交易查询","code":"trade-1","pid":"2","childs":[{"id":"4","name":"交易查询-查询操作","code":"trade-1-1","pid":"3"}]}]},{"id":"5","name":"权限管理","code":"authorization","pid":null,"childs":[{"id":"6","name":"角色管理","code":"authorization-1","pid":"5"},{"id":"7","name":"用户管理","code":"authorization-2","pid":"5"}]}]
|
|
2855
2857
|
*
|
|
2856
2858
|
*/
|
|
2857
|
-
function listToTree(list) {
|
|
2858
|
-
var
|
|
2859
|
-
|
|
2860
|
-
keyField =
|
|
2861
|
-
|
|
2862
|
-
parentField =
|
|
2863
|
-
|
|
2864
|
-
childrenField =
|
|
2865
|
-
|
|
2866
|
-
emptyChildrenValue =
|
|
2867
|
-
|
|
2868
|
-
nodeAssign =
|
|
2859
|
+
function listToTree(list, options) {
|
|
2860
|
+
var _ref2 = options || {},
|
|
2861
|
+
_ref2$keyField = _ref2.keyField,
|
|
2862
|
+
keyField = _ref2$keyField === void 0 ? 'id' : _ref2$keyField,
|
|
2863
|
+
_ref2$parentField = _ref2.parentField,
|
|
2864
|
+
parentField = _ref2$parentField === void 0 ? 'pid' : _ref2$parentField,
|
|
2865
|
+
_ref2$childrenField = _ref2.childrenField,
|
|
2866
|
+
childrenField = _ref2$childrenField === void 0 ? 'children' : _ref2$childrenField,
|
|
2867
|
+
_ref2$emptyChildrenVa = _ref2.emptyChildrenValue,
|
|
2868
|
+
emptyChildrenValue = _ref2$emptyChildrenVa === void 0 ? 'none' : _ref2$emptyChildrenVa,
|
|
2869
|
+
_ref2$nodeAssign = _ref2.nodeAssign,
|
|
2870
|
+
nodeAssign = _ref2$nodeAssign === void 0 ? 'spread' : _ref2$nodeAssign;
|
|
2869
2871
|
|
|
2870
2872
|
/** @type {R[]} */
|
|
2871
2873
|
var tree = [];
|
|
2872
2874
|
|
|
2873
2875
|
/** @type {Object.<string, T[]>} */
|
|
2874
2876
|
var record = {};
|
|
2877
|
+
if (!Array.isArray(list)) {
|
|
2878
|
+
return tree;
|
|
2879
|
+
}
|
|
2875
2880
|
list.forEach(function (item) {
|
|
2876
2881
|
if (isObject(item)) {
|
|
2877
2882
|
var newItem = nodeAssign === 'spread' ? _objectSpread2({}, item) : item;
|
|
@@ -2928,6 +2933,9 @@
|
|
|
2928
2933
|
function treeToList(tree, childrenField) {
|
|
2929
2934
|
/** @type {R[]} */
|
|
2930
2935
|
var list = [];
|
|
2936
|
+
if (!Array.isArray(tree)) {
|
|
2937
|
+
return list;
|
|
2938
|
+
}
|
|
2931
2939
|
|
|
2932
2940
|
/**
|
|
2933
2941
|
* 递归遍历
|
|
@@ -2939,7 +2947,7 @@
|
|
|
2939
2947
|
var newItem = _objectSpread2({}, item);
|
|
2940
2948
|
// @ts-ignore
|
|
2941
2949
|
list.push(newItem);
|
|
2942
|
-
if (newItem
|
|
2950
|
+
if (newItem[childrenField]) {
|
|
2943
2951
|
if (Array.isArray(newItem[childrenField]) && newItem[childrenField].length > 0) {
|
|
2944
2952
|
recusion(newItem[childrenField]);
|
|
2945
2953
|
}
|
|
@@ -2984,12 +2992,11 @@
|
|
|
2984
2992
|
function filterTree(tree, predicate) {
|
|
2985
2993
|
var childrenField = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'children';
|
|
2986
2994
|
var nodeAssign = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 'spread';
|
|
2987
|
-
if (!Array.isArray(tree)) {
|
|
2988
|
-
return tree;
|
|
2989
|
-
}
|
|
2990
|
-
|
|
2991
2995
|
/** @type {T[]} */
|
|
2992
2996
|
var result = [];
|
|
2997
|
+
if (!Array.isArray(tree)) {
|
|
2998
|
+
return result;
|
|
2999
|
+
}
|
|
2993
3000
|
tree.forEach(function (item) {
|
|
2994
3001
|
var newItem = item;
|
|
2995
3002
|
if (isObject(item)) {
|
|
@@ -3292,14 +3299,12 @@
|
|
|
3292
3299
|
|
|
3293
3300
|
/**
|
|
3294
3301
|
* @private
|
|
3295
|
-
* @param {number}
|
|
3296
|
-
* @param {string}
|
|
3302
|
+
* @param {number} len 长度
|
|
3303
|
+
* @param {string} optionalChars 允许的字符,默认为数字和大小写字母
|
|
3297
3304
|
* @param {string} [prefix=''] 前缀部分,不计入长度
|
|
3298
3305
|
* @returns {string}
|
|
3299
3306
|
*/
|
|
3300
|
-
function internalRandomString() {
|
|
3301
|
-
var len = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
|
|
3302
|
-
var optionalChars = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultChars;
|
|
3307
|
+
function internalRandomString(len, optionalChars) {
|
|
3303
3308
|
var prefix = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '';
|
|
3304
3309
|
while (len-- > 0) {
|
|
3305
3310
|
var r = optionalChars[Math.floor(Math.random() * optionalChars.length)];
|
|
@@ -3396,6 +3401,9 @@
|
|
|
3396
3401
|
|
|
3397
3402
|
/** @type {T|undefined} */
|
|
3398
3403
|
var node;
|
|
3404
|
+
if (!Array.isArray(tree)) {
|
|
3405
|
+
return node;
|
|
3406
|
+
}
|
|
3399
3407
|
var _iterator = _createForOfIteratorHelper(tree),
|
|
3400
3408
|
_step;
|
|
3401
3409
|
try {
|
|
@@ -3464,6 +3472,9 @@
|
|
|
3464
3472
|
|
|
3465
3473
|
/** @type {T[]} */
|
|
3466
3474
|
var nodes = [];
|
|
3475
|
+
if (!Array.isArray(tree)) {
|
|
3476
|
+
return nodes;
|
|
3477
|
+
}
|
|
3467
3478
|
var _iterator = _createForOfIteratorHelper(tree),
|
|
3468
3479
|
_step;
|
|
3469
3480
|
try {
|
|
@@ -3503,14 +3514,13 @@
|
|
|
3503
3514
|
* @template {(item: T) => boolean} F
|
|
3504
3515
|
* @param {T[]} tree 树结构数据
|
|
3505
3516
|
* @param {F} predicate 遍历每一项执行的函数,参数是当前遍历到的节点数据,如果返回 Truthy 将返回包含该节点的所有父级节点
|
|
3506
|
-
* @param {string} [childrenField
|
|
3517
|
+
* @param {string} [childrenField] 子级字段名
|
|
3507
3518
|
* @param {T[]} [path=[]] 当前遍历路径
|
|
3508
3519
|
* @returns {T[]}
|
|
3509
3520
|
*/
|
|
3510
|
-
function internalFindTreeSelect(tree, predicate) {
|
|
3511
|
-
var childrenField = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'children';
|
|
3521
|
+
function internalFindTreeSelect(tree, predicate, childrenField) {
|
|
3512
3522
|
var path = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : [];
|
|
3513
|
-
if (!tree) {
|
|
3523
|
+
if (!Array.isArray(tree)) {
|
|
3514
3524
|
return [];
|
|
3515
3525
|
}
|
|
3516
3526
|
var _iterator = _createForOfIteratorHelper(tree),
|