util-helpers 4.22.1 → 4.23.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.
package/README.md CHANGED
@@ -76,6 +76,7 @@ formatBankCard('6228480402564890018', { spaceMark: '-' }); // 6228-4804-0256-489
76
76
  - [replaceChar](https://doly-dev.github.io/util-helpers/module-Processor.html#.replaceChar) - 替换字符,应用场景如:脱敏
77
77
  - [safeDate](https://doly-dev.github.io/util-helpers/module-Processor.html#.safeDate) - 创建一个 Date 实例,同 new Date
78
78
  - [setDataURLPrefix](https://doly-dev.github.io/util-helpers/module-Processor.html#.setDataURLPrefix) - 设置 DataURL 前缀、MIME 类型、base64 标识
79
+ - [transformObjectValue](https://doly-dev.github.io/util-helpers/module-Processor.html#.transformObjectValue) - 创建一个转换值的新对象或数组
79
80
  - 树结构数据操作
80
81
  - [filterTree](https://doly-dev.github.io/util-helpers/module-Processor.html#.filterTree) - 过滤树节点
81
82
  - [findTreeNode](https://doly-dev.github.io/util-helpers/module-Other.html#.findTreeNode) - 查找树结构数据节点
@@ -107,6 +108,8 @@ formatBankCard('6228480402564890018', { spaceMark: '-' }); // 6228-4804-0256-489
107
108
  - [isVehicle](https://doly-dev.github.io/util-helpers/module-Validator.html#.isVehicle) - 车牌号
108
109
  - [isWX](https://doly-dev.github.io/util-helpers/module-Validator.html#.isWX) - 微信号
109
110
  - [validatePassword](https://doly-dev.github.io/util-helpers/module-Validator.html#.validatePassword) - 验证密码
111
+ - Classes
112
+ - [AsyncMemo](https://doly-dev.github.io/util-helpers/AsyncMemo.html) - 异步缓存类
110
113
 
111
114
  ## 精选第三方工具库
112
115
 
@@ -9,12 +9,20 @@
9
9
  var stringObject = 'object';
10
10
  var objectProto = Object.prototype;
11
11
  var objectProtoToString = objectProto.toString;
12
+ var objectProtoHasOwnProperty = objectProto.hasOwnProperty;
12
13
  var objectProtoPropertyIsEnumerable = objectProto.propertyIsEnumerable;
13
14
  var objectGetOwnPropertySymbols = Object.getOwnPropertySymbols;
15
+ var objectGetPrototypeOf = Object.getPrototypeOf;
14
16
  var objectKeys$1 = Object.keys;
17
+ var functionProto = Function.prototype;
18
+ var functionProtoToString = functionProto.toString;
15
19
  var symbolExisted = typeof Symbol !== stringUndefined;
16
20
  var symbolProto = symbolExisted ? Symbol.prototype : nativeUndefined$1;
17
21
  var mathMin = Math.min;
22
+ var mathMax = Math.max;
23
+ var mathRandom = Math.random;
24
+ var mathFloor = Math.floor;
25
+ var mathCeil = Math.ceil;
18
26
  var numberIsFinite = Number.isFinite;
19
27
  var globalThisExisted = typeof globalThis === stringObject && globalThis;
20
28
  var globalExisted = typeof global === stringObject && global;
@@ -26,6 +34,7 @@
26
34
  var symbolTag = '[object Symbol]';
27
35
  var functionTags = ['Function', 'AsyncFunction', 'GeneratorFunction', 'Proxy'].map(function (item) { return '[object ' + item + ']'; });
28
36
  var blobTag = '[object Blob]';
37
+ var objectTag = '[object Object]';
29
38
 
30
39
  function isArray(value) {
31
40
  return Array.isArray(value);
@@ -98,6 +107,38 @@
98
107
  return value != null && isLength(value.length) && !isFunction(value);
99
108
  }
100
109
 
110
+ var MAX_VALUE = 1.7976931348623157e308;
111
+ function toFinite(value) {
112
+ if (!value) {
113
+ return value === 0 ? value : 0;
114
+ }
115
+ value = toNumber(value);
116
+ if (value === Infinity || value === -Infinity) {
117
+ var sign = value < 0 ? -1 : 1;
118
+ return sign * MAX_VALUE;
119
+ }
120
+ return value === value ? value : 0;
121
+ }
122
+
123
+ function randomInt(lower, upper) {
124
+ if (lower === void 0) { lower = 0; }
125
+ if (upper === void 0) { upper = 1; }
126
+ lower = toFinite(lower);
127
+ upper = toFinite(upper);
128
+ var min = mathCeil(mathMin(lower, upper) || 0);
129
+ var max = mathFloor(mathMax(lower, upper) || 0);
130
+ if (min > max) {
131
+ var temp = min;
132
+ min = max;
133
+ max = temp;
134
+ }
135
+ return mathFloor(min + mathRandom() * (max - min + 1));
136
+ }
137
+
138
+ function isObjectLike(value) {
139
+ return value !== null && typeof value === 'object';
140
+ }
141
+
101
142
  function getSymbols(object) {
102
143
  if (!objectGetOwnPropertySymbols || object === null) {
103
144
  return [];
@@ -167,6 +208,19 @@
167
208
  return getTag(value) === blobTag;
168
209
  }
169
210
 
211
+ var objectCtorString = functionProtoToString.call(Object);
212
+ function isPlainObject(value) {
213
+ if (!isObjectLike(value) || getTag(value) !== objectTag) {
214
+ return false;
215
+ }
216
+ var proto = objectGetPrototypeOf(Object(value));
217
+ if (proto === null) {
218
+ return true;
219
+ }
220
+ var Ctor = objectProtoHasOwnProperty.call(proto, 'constructor') && proto.constructor;
221
+ return typeof Ctor === 'function' && Ctor instanceof Ctor && functionProtoToString.call(Ctor) === objectCtorString;
222
+ }
223
+
170
224
  var freeGlobalThis = globalThisExisted && globalThis.Object === Object && globalThis;
171
225
  var freeGlobal = globalExisted && global.Object === Object && global;
172
226
  var freeSelf = selfExisted && self.Object === Object && self;
@@ -373,19 +427,10 @@
373
427
  }
374
428
  }
375
429
 
376
- var regNumber = /[\d]/;
430
+ var regNumber = /\d/;
377
431
  var regLowerCaseLetter = /[a-z]/;
378
432
  var regUpperCaseLetter = /[A-Z]/;
379
433
  var regAllNumberAndLetter = /[\d|a-z]/gi;
380
- function hasNumber(val) {
381
- return regNumber.test(val);
382
- }
383
- function hasLowerCaseLetter(val) {
384
- return regLowerCaseLetter.test(val);
385
- }
386
- function hasUpperCaseLetter(val) {
387
- return regUpperCaseLetter.test(val);
388
- }
389
434
  function hasHex(val) {
390
435
  return val.indexOf('\\x') > -1 || val.indexOf('\\u') > -1;
391
436
  }
@@ -443,9 +488,9 @@
443
488
  valStr = '';
444
489
  }
445
490
  var currentLevel = 0;
446
- var containesNumber = hasNumber(valStr);
447
- var containesLowerCaseLetter = hasLowerCaseLetter(valStr);
448
- var containesUpperCaseLetter = hasUpperCaseLetter(valStr);
491
+ var containesNumber = regNumber.test(valStr);
492
+ var containesLowerCaseLetter = regLowerCaseLetter.test(valStr);
493
+ var containesUpperCaseLetter = regUpperCaseLetter.test(valStr);
449
494
  var containesSpecialCharacter = hasSpecialCharacter(valStr, special);
450
495
  var containesUnallowableCharacter = hasUnallowableCharacter(valStr, special);
451
496
  if (containesNumber) {
@@ -1221,6 +1266,27 @@
1221
1266
  return str;
1222
1267
  }
1223
1268
 
1269
+ var transformObjectValue = function (data, fn, deep) {
1270
+ if (deep === void 0) { deep = true; }
1271
+ if (isPlainObject(data)) {
1272
+ var result_1 = {};
1273
+ forEach$1(data, function (value, key) {
1274
+ var newValue = deep && (isPlainObject(value) || isArray(value)) ? transformObjectValue(value, fn) : fn(value, key);
1275
+ result_1[key] = newValue;
1276
+ });
1277
+ return result_1;
1278
+ }
1279
+ else if (isArray(data)) {
1280
+ var result_2 = [];
1281
+ forEach$1(data, function (value, index) {
1282
+ var newValue = deep && (isPlainObject(value) || isArray(value)) ? transformObjectValue(value, fn) : fn(value, index);
1283
+ result_2.push(newValue);
1284
+ });
1285
+ return result_2;
1286
+ }
1287
+ return data;
1288
+ };
1289
+
1224
1290
  function times() {
1225
1291
  var nums = [];
1226
1292
  for (var _i = 0; _i < arguments.length; _i++) {
@@ -1943,22 +2009,35 @@
1943
2009
  });
1944
2010
  }
1945
2011
 
1946
- var numberChars = '0123456789';
1947
- var letterChars = 'abcdefghijklmnopqrstuvwxyz';
1948
- var defaultChars = numberChars + letterChars + letterChars.toUpperCase();
1949
- function internalRandomString(len, optionalChars, prefix) {
2012
+ var letter = 'abcdefghijklmnopqrstuvwxyz';
2013
+ var chars = {
2014
+ number: '0123456789',
2015
+ lower: letter,
2016
+ upper: letter.toUpperCase()
2017
+ };
2018
+ var allChars = chars.number + chars.lower + chars.upper;
2019
+ function internalRandomString(len, pool, prefix) {
1950
2020
  if (prefix === void 0) { prefix = ''; }
1951
2021
  while (len-- > 0) {
1952
- var r = optionalChars[Math.floor(Math.random() * optionalChars.length)];
1953
- return internalRandomString(len, optionalChars, prefix + r);
2022
+ var r = pool[randomInt(0, pool.length - 1)];
2023
+ return internalRandomString(len, pool, prefix + r);
1954
2024
  }
1955
2025
  return prefix;
1956
2026
  }
1957
- function randomString$1(len, optionalChars) {
2027
+ var randomString$1 = function (len, pool) {
1958
2028
  if (len === void 0) { len = 0; }
1959
- var realChars = typeof optionalChars === 'string' && optionalChars ? optionalChars : defaultChars;
1960
- return internalRandomString(toNumber(len), realChars);
1961
- }
2029
+ var _pool;
2030
+ if (typeof pool !== 'string') {
2031
+ _pool = allChars;
2032
+ }
2033
+ else if (chars[pool]) {
2034
+ _pool = chars[pool];
2035
+ }
2036
+ else {
2037
+ _pool = pool;
2038
+ }
2039
+ return internalRandomString(toNumber(len), _pool);
2040
+ };
1962
2041
 
1963
2042
  function strlen(str) {
1964
2043
  var realStr = toString(str);
@@ -2243,7 +2322,7 @@
2243
2322
  return internalFindTreeSelect(tree, predicate, childrenField);
2244
2323
  }
2245
2324
 
2246
- var VERSION = "4.22.1";
2325
+ var VERSION = "4.23.0";
2247
2326
 
2248
2327
  // 随机字符串
2249
2328
  function randomString() {
@@ -2587,7 +2666,7 @@
2587
2666
  }
2588
2667
  AsyncMemo.prototype.run = function (asyncFn, key, options) {
2589
2668
  var _this = this;
2590
- if (!isString(key)) {
2669
+ if (!key || !isString(key)) {
2591
2670
  return asyncFn();
2592
2671
  }
2593
2672
  var opts = __assign({ persisted: true }, options);
@@ -2614,7 +2693,7 @@
2614
2693
  return AsyncMemo;
2615
2694
  }());
2616
2695
 
2617
- var version = "4.22.1";
2696
+ var version = "4.23.0";
2618
2697
 
2619
2698
  exports.AsyncMemo = AsyncMemo;
2620
2699
  exports.VERSION = VERSION;
@@ -2679,6 +2758,7 @@
2679
2758
  exports.strlen = strlen;
2680
2759
  exports.times = times;
2681
2760
  exports.transformFieldNames = transformFieldNames;
2761
+ exports.transformObjectValue = transformObjectValue;
2682
2762
  exports.treeToList = treeToList;
2683
2763
  exports.validatePassword = validatePassword;
2684
2764
  exports.version = version;